-
Notifications
You must be signed in to change notification settings - Fork 6
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
Bugfix: Ensure self_group name matches email during authentication #78
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ab9a5a2
add logging to repo lambda
breedenc 866f7eb
logging update
breedenc 93bd76e
Merge branch 'main' into breedenc/repo-lambda-logging
breedenc 6831160
check name of user's self group during auth and correct if needed
breedenc 7fea9a2
fix logic error, update tests and add new tests to cover self_group t…
breedenc df5f858
small change to MockGroup unit test class, clarify comment
breedenc 983c375
add clarifying comment about deleted users
breedenc 8ea4d98
Merge branch 'main' into breedenc/self-group-bugfix
breedenc afff623
add comment to unit tests
breedenc 9660398
remove unit test dependency on hardcoded value
breedenc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import authorizer.handlers | ||
import copy | ||
import unittest | ||
|
||
from unittest.mock import patch | ||
|
||
import authorizer.handlers | ||
|
||
EMAIL_DOMAIN_ALIASES = [ | ||
{ | ||
"new_domain": "company1.com", | ||
|
@@ -30,28 +30,51 @@ | |
"email": "[email protected]", | ||
"deleted": False, | ||
"last_login": "2023-01-01 00:00:00.000000+00:00", | ||
"self_group": {"name": "[email protected]"}, | ||
}, | ||
{ | ||
"id": 2, | ||
"email": "[email protected]", | ||
"deleted": False, | ||
"last_login": "2023-01-01 00:00:00.000000+00:00", | ||
"self_group": {"name": "[email protected]"}, | ||
}, | ||
{ | ||
"id": 3, | ||
"email": "[email protected]", | ||
"deleted": True, | ||
"last_login": "2023-01-01 00:00:00.000000+00:00", | ||
"self_group": {"name": "[email protected]"}, | ||
}, | ||
{ | ||
"id": 4, | ||
"email": "[email protected]", | ||
"deleted": False, | ||
"last_login": "2023-01-01 00:00:00.000000+00:00", | ||
"self_group": {"name": "[email protected]"}, | ||
}, | ||
{ | ||
"id": 5, | ||
"email": "[email protected]", | ||
"deleted": False, | ||
"last_login": "2023-01-01 00:00:00.000000+00:00", | ||
"self_group": {"name": "[email protected]"}, | ||
}, | ||
] | ||
|
||
|
||
class MockGroup(object): | ||
def __init__(self, **kwargs): | ||
self.name = kwargs.get("name") or "" | ||
|
||
def save(self): | ||
pass | ||
|
||
@classmethod | ||
def create_self_group(cls, user): | ||
user.self_group = MockGroup(name=user.email) | ||
|
||
|
||
class MockUser(object): | ||
users = [] | ||
|
||
|
@@ -62,6 +85,9 @@ def __init__(self, **kwargs): | |
self.email = kwargs.get("email") | ||
self.deleted = kwargs.get("deleted") or False | ||
self.last_login = kwargs.get("last_login") or "" | ||
self_group = kwargs.get("self_group") or None | ||
if self_group: | ||
self.self_group = MockGroup(name=self_group.get("name")) | ||
|
||
def save(self): | ||
for user in MockUser.users: | ||
|
@@ -93,8 +119,8 @@ def get(email: str, **kwargs): | |
|
||
|
||
@patch("authorizer.handlers.EMAIL_DOMAIN_ALIASES", EMAIL_DOMAIN_ALIASES) | ||
@patch("authorizer.handlers.Group.create_self_group", lambda *x, **y: None) | ||
@patch("authorizer.handlers.User", MockUser) | ||
@patch("authorizer.handlers.Group", MockGroup) | ||
class TestGetUser(unittest.TestCase): | ||
def test_get_existing_user(self): | ||
""" | ||
|
@@ -103,7 +129,11 @@ def test_get_existing_user(self): | |
MockUser.users = copy.deepcopy(USERS) | ||
email = "[email protected]" | ||
user = _get_update_or_create_user(email=email) | ||
self.assertTrue(user.__dict__.get("id") == 1 and user.__dict__.get("email") == email) | ||
self.assertTrue( | ||
user.__dict__.get("id") == 1 | ||
and user.__dict__.get("email") == email | ||
and user.__dict__.get("self_group").name == email | ||
) | ||
|
||
def test_get_deleted_user(self): | ||
""" | ||
|
@@ -120,48 +150,83 @@ def test_get_nonexistent_user(self): | |
""" | ||
MockUser.users = copy.deepcopy(USERS) | ||
email = "[email protected]" | ||
expected_userid = MockUser.users[-1].get("id") + 1 | ||
user = _get_update_or_create_user(email=email) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed this to a derived value which still tests that a new user was created, but does not require the expected ID value to be hard-coded into the unit test and updated in the case that unrelated tests are added. |
||
self.assertTrue(user.__dict__.get("id") == 5 and user.__dict__.get("email") == email) | ||
self.assertTrue( | ||
user.__dict__.get("id") == expected_userid | ||
and user.__dict__.get("email") == email | ||
and user.__dict__.get("self_group").name == email | ||
) | ||
|
||
def test_get_user_with_new_email(self): | ||
""" | ||
User logs in with email "[email protected]" and has an existing account with email "[email protected]" | ||
Existing account is found, and email is updated to the new email "[email protected]" | ||
Existing account is found, and email and self group name are updated to the new email "[email protected]" | ||
""" | ||
MockUser.users = copy.deepcopy(USERS) | ||
email = "[email protected]" | ||
user = _get_update_or_create_user(email=email) | ||
self.assertTrue(user.__dict__.get("id") == 1 and user.__dict__.get("email") == email) | ||
self.assertTrue( | ||
user.__dict__.get("id") == 1 | ||
and user.__dict__.get("email") == email | ||
and user.__dict__.get("self_group").name == email | ||
) | ||
|
||
def test_get_user_with_new_email_with_transformation(self): | ||
""" | ||
User logs in with email "[email protected]" and has an existing account with email "[email protected]" | ||
Existing account is found, and email is updated to the new email "[email protected]" | ||
Existing account is found, and email and self group name are updated to the new email "[email protected]" | ||
""" | ||
MockUser.users = copy.deepcopy(USERS) | ||
email = "[email protected]" | ||
user = _get_update_or_create_user(email=email) | ||
self.assertTrue(user.__dict__.get("id") == 2 and user.__dict__.get("email") == email) | ||
self.assertTrue( | ||
user.__dict__.get("id") == 2 | ||
and user.__dict__.get("email") == email | ||
and user.__dict__.get("self_group").name == email | ||
) | ||
|
||
def test_get_user_with_new_email_with_transformation2(self): | ||
""" | ||
User logs in with email "[email protected]" and has an existing account with email "[email protected]" | ||
Existing account is found, and email is updated to the new email "[email protected]" | ||
Existing account is found, and email and self group name are updated to the new email "[email protected]" | ||
""" | ||
MockUser.users = copy.deepcopy(USERS) | ||
email = "[email protected]" | ||
user = _get_update_or_create_user(email=email) | ||
self.assertTrue(user.__dict__.get("id") == 4 and user.__dict__.get("email") == email) | ||
self.assertTrue( | ||
user.__dict__.get("id") == 4 | ||
and user.__dict__.get("email") == email | ||
and user.__dict__.get("self_group").name == email | ||
) | ||
|
||
def test_get_user_with_new_email_with_transformation3(self): | ||
""" | ||
User logs in with email "[email protected]" and has an existing account with email "[email protected]" | ||
Existing account is found, and email is updated to the new email "[email protected]" | ||
Existing account is found, and email and self group name are updated to the new email "[email protected]" | ||
""" | ||
MockUser.users = copy.deepcopy(USERS) | ||
email = "[email protected]" | ||
user = _get_update_or_create_user(email=email) | ||
self.assertTrue(user.__dict__.get("id") == 1 and user.__dict__.get("email") == email) | ||
self.assertTrue( | ||
user.__dict__.get("id") == 1 | ||
and user.__dict__.get("email") == email | ||
and user.__dict__.get("self_group").name == email | ||
) | ||
|
||
def test_get_user_with_email_and_self_group_mismatch(self): | ||
""" | ||
User logs in with email "[email protected]" and has an existing account with email "[email protected]", but self-group name does not match | ||
Existing account is found, and self group name is updated to the new email "[email protected]" | ||
""" | ||
MockUser.users = copy.deepcopy(USERS) | ||
email = "[email protected]" | ||
user = _get_update_or_create_user(email=email) | ||
self.assertTrue( | ||
user.__dict__.get("id") == 5 | ||
and user.__dict__.get("email") == email | ||
and user.__dict__.get("self_group").name == email | ||
) | ||
|
||
def test_get_user_with_new_email_and_deleted_old_user(self): | ||
""" | ||
|
@@ -170,5 +235,10 @@ def test_get_user_with_new_email_and_deleted_old_user(self): | |
""" | ||
MockUser.users = copy.deepcopy(USERS) | ||
email = "[email protected]" | ||
expected_userid = MockUser.users[-1].get("id") + 1 | ||
user = _get_update_or_create_user(email=email) | ||
self.assertTrue(user.__dict__.get("id") == 5 and user.__dict__.get("email") == email) | ||
self.assertTrue( | ||
user.__dict__.get("id") == expected_userid | ||
and user.__dict__.get("email") == email | ||
and user.__dict__.get("self_group").name == email | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Has
black
been run on the changed files? (this change may well be becauseblack
was run, but I figured I'd ask)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, either
black
orisort
would have made this change, i did not make it manually