Skip to content

Commit

Permalink
Added support for changing a password. (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-Ellenberger authored Aug 20, 2024
1 parent 1fcb8e1 commit 6b2662e
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 9 deletions.
8 changes: 4 additions & 4 deletions autograder/api/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def send_api_request(endpoint, server = None, verbose = False, data = {}, files
"""

if ((server is None) or (server == '')):
raise autograder.api.error.APIError("No server provided.")
raise autograder.api.error.APIError(None, "No server provided.")

server = server.rstrip('/')
endpoint = endpoint.lstrip('/')
Expand All @@ -51,8 +51,8 @@ def send_api_request(endpoint, server = None, verbose = False, data = {}, files
for path in files:
filename = os.path.basename(path)
if (filename in post_files):
raise autograder.api.error.APIError("Cannot submit duplicate filenames ('%s')." % (
filename))
raise autograder.api.error.APIError(None, "Cannot submit duplicate filenames ('%s')."
% (filename))

post_files[filename] = open(path, 'rb')

Expand All @@ -71,7 +71,7 @@ def send_api_request(endpoint, server = None, verbose = False, data = {}, files
try:
response = raw_response.json()
except Exception as ex:
raise autograder.api.error.APIError("Autograder response does not contain valid JSON."
raise autograder.api.error.APIError(None, "Autograder response does not contain valid JSON."
+ " Contact a server admin with the following. Response:\n---\n%s\n---" % (
raw_response.text)) from ex

Expand Down
10 changes: 5 additions & 5 deletions autograder/api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ def __init__(self, key, description,
hash = False):
self.key = str(key)
if ((key is None) or (self.key == '')):
raise autograder.api.error.APIError("APIParam cannot have an empty key.")
raise autograder.api.error.APIError(None, "APIParam cannot have an empty key.")

self.description = str(description)
if ((description is None) or (self.description == '')):
raise autograder.api.error.APIError("APIParam cannot have an empty description.")
raise autograder.api.error.APIError(None, "APIParam cannot have an empty description.")

self.config_key = config_key
if (self.config_key is None):
Expand Down Expand Up @@ -68,7 +68,7 @@ def _parse_api_config(config, params, additional_required_keys, additional_optio
for param in params:
if (param.config_key not in config):
if (param.required):
raise autograder.api.error.APIError(
raise autograder.api.error.APIError(None,
f"Required parameter '{param.config_key}' not set.")

continue
Expand All @@ -81,7 +81,7 @@ def _parse_api_config(config, params, additional_required_keys, additional_optio

for key in additional_required_keys:
if (key not in config):
raise autograder.api.error.APIError(f"Required parameter '{key}' not set.")
raise autograder.api.error.APIError(None, f"Required parameter '{key}' not set.")

extra[key] = config[key]

Expand Down Expand Up @@ -218,7 +218,7 @@ def get_argument_parser(

PARAM_NEW_PASS = APIParam('new-pass',
'The new password to set for the user that is the target of this request.',
required = False, hash = True)
required = True, hash = True)

PARAM_SKIP_EMAILS = APIParam('skip-emails',
'Skip sending any emails. Be aware that this may result in inaccessible information.',
Expand Down
Empty file.
21 changes: 21 additions & 0 deletions autograder/api/users/password/change.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import autograder.api.common
import autograder.api.config

API_ENDPOINT = 'users/password/change'
API_PARAMS = [
autograder.api.config.PARAM_USER_EMAIL,
autograder.api.config.PARAM_USER_PASS,
autograder.api.config.PARAM_NEW_PASS,
]

DESCRIPTION = 'Change your password to the one provided.'

def send(arguments, **kwargs):
return autograder.api.common.handle_api_request(arguments, API_PARAMS, API_ENDPOINT, **kwargs)

def _get_parser():
parser = autograder.api.config.get_argument_parser(
description = DESCRIPTION,
params = API_PARAMS)

return parser
Empty file.
14 changes: 14 additions & 0 deletions autograder/cli/users/password/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
The `autograder.cli.users.password` package contains tools to manage user passwords.
"""

import sys

import autograder.util.cli

def main():
autograder.util.cli.auto_list()
return 0

if (__name__ == '__main__'):
sys.exit(main())
26 changes: 26 additions & 0 deletions autograder/cli/users/password/change.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import sys

import autograder.api.users.password.change

def run(arguments):
result = autograder.api.users.password.change.send(arguments, exit_on_error = True)

if (result['duplicate']):
print("Your new password must be different from your previous password.")
return 0

if (result['success']):
print("You have successfully changed your password.")
return 0

return 1

def main():
return run(_get_parser().parse_args())

def _get_parser():
parser = autograder.api.users.password.change._get_parser()
return parser

if (__name__ == '__main__'):
sys.exit(main())
11 changes: 11 additions & 0 deletions tests/api/data/test_password_change_duplicate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"endpoint": "users/pass/change",
"module": "autograder.api.users.password.change",
"arguments": {
"new-pass": "admin"
},
"output": {
"success": true,
"duplicate": true
}
}
13 changes: 13 additions & 0 deletions tests/api/data/test_password_change_success.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"endpoint": "users/pass/change",
"module": "autograder.api.users.password.change",
"arguments": {
"user": "[email protected]",
"pass": "student",
"new-pass": "spooky"
},
"output": {
"success": true,
"duplicate": false
}
}

0 comments on commit 6b2662e

Please sign in to comment.