Skip to content

Commit

Permalink
fix auto generated password
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Sep 3, 2024
1 parent aaa281b commit 03219ef
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ name = "pypi"
# 5. Run `pipenv install --dev` in your terminal.

[packages]
codeforlife = {ref = "v0.18.6", git = "https://github.com/ocadotechnology/codeforlife-package-python.git"}
codeforlife = {ref = "portal-frontend-50", git = "https://github.com/ocadotechnology/codeforlife-package-python.git"}
# 🚫 Don't add [packages] below that are inherited from the CFL package.
pyjwt = "==2.6.0" # TODO: upgrade to latest version
# TODO: Needed by RR. Remove when RR has moved to new system.
Expand All @@ -32,7 +32,7 @@ django-sekizai = "==2.0.0"
django-classy-tags = "==2.0.0"

[dev-packages]
codeforlife = {ref = "v0.18.6", git = "https://github.com/ocadotechnology/codeforlife-package-python.git", extras = ["dev"]}
codeforlife = {ref = "portal-frontend-50", git = "https://github.com/ocadotechnology/codeforlife-package-python.git", extras = ["dev"]}
# codeforlife = {file = "../codeforlife-package-python", editable = true, extras = ["dev"]}
# 🚫 Don't add [dev-packages] below that are inherited from the CFL package.

Expand Down
6 changes: 3 additions & 3 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/api/serializers/student.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ def to_representation(self, instance):
representation = super().to_representation(instance)

# Return student's auto-generated password.
# pylint: disable-next=protected-access
password = instance.new_user._password
if password is not None:
password = getattr(instance.new_user, "_password", None)
auto_gen_password = getattr(instance.new_user, "_login_id", None)
if password is not None and auto_gen_password is not None:
representation["user"]["password"] = password
representation["login_id"] = instance.login_id
representation["auto_gen_password"] = auto_gen_password

return representation

Expand Down
2 changes: 1 addition & 1 deletion src/api/serializers/student_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def test_update_many(self):
"new_user": {
"password": user_make_password.return_value
},
"login_id": get_random_login_id.return_value,
"login_id": get_random_login_id.return_value[1],
}
],
)
Expand Down
15 changes: 10 additions & 5 deletions src/api/views/student_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
User,
)
from codeforlife.user.permissions import IsTeacher
from common.helpers.generators import ( # type: ignore[import-untyped]
get_hashed_login_id,
)

from ..serializers import (
CreateStudentSerializer,
Expand Down Expand Up @@ -81,18 +84,20 @@ def assert_passwords(
for student_fields in response_json:
assert isinstance(student_fields, dict)

login_id = student_fields["login_id"]
assert isinstance(login_id, str) and login_id
auto_gen_password = student_fields["auto_gen_password"]
assert isinstance(auto_gen_password, str) and auto_gen_password

student_id = student_fields["id"]
assert isinstance(student_id, int)
student = student_lookup[student_id]

login_id_is_valid = student.login_id == login_id
auto_gen_password_is_valid = (
student.login_id == get_hashed_login_id(auto_gen_password)
)
if are_valid:
assert login_id_is_valid
assert auto_gen_password_is_valid
else:
assert not login_id_is_valid
assert not auto_gen_password_is_valid

student_user_fields = student_fields["user"]
assert isinstance(student_user_fields, dict)
Expand Down

0 comments on commit 03219ef

Please sign in to comment.