Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for auth module #459

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions abcclassroom/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# github.py for methods that use the API.

import requests

import os.path as op
from ruamel.yaml import YAML

Expand All @@ -16,8 +15,8 @@

def get_github_auth():
"""
Check to see if there is an existing github authentication
and load the authentication.
Check for a YAML file with
github authentication and load the authentication information.

Returns
-------
Expand All @@ -27,7 +26,9 @@ def get_github_auth():
"""
yaml = YAML()
try:
with open(op.expanduser("~/.abc-classroom.tokens.yml")) as f:
with open(
op.join(op.expanduser("~"), ".abc-classroom.tokens.yml")
) as f:
config = yaml.load(f)
return config["github"]

Expand All @@ -45,30 +46,42 @@ def set_github_auth(auth_info):
auth_info : dictionary
The token and id authentication information from github stored in a
dictionary object.

Returns
-------
Creates a file called .abc-classroom.tokens.yml in the users home dir.
"""
yaml = YAML()
config = {}

# If conf file already exists, open it and get config file
if get_github_auth():
with open(op.expanduser("~/.abc-classroom.tokens.yml")) as f:
with open(
op.join(op.expanduser("~"), ".abc-classroom.tokens.yml")
) as f:
config = yaml.load(f)

config["github"] = auth_info

with open(op.expanduser("~/.abc-classroom.tokens.yml"), "w") as f:
with open(
op.join(op.expanduser("~"), ".abc-classroom.tokens.yml"), "w"
) as f:
yaml.dump(config, f)


def get_access_token():
"""Get a GitHub access token for the API
"""Get GitHub API access token stored in a yaml file in users home
directory.

# TODO does it actually generate the new token?
First tries to read from local token file. If token does not exist,
or is not valid, generates a new token using the OAuth Device Flow.
https://docs.github.com/en/free-pro-team@latest/developers/apps/
identifying-and-authorizing-users-for-github-apps#device-flow

Returns an access token (string).
"""
# first, we see if we have a saved token
# First, check for saved token in the users home directory
auth_info = get_github_auth()
if auth_info:
try:
Expand Down
Loading