-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to check if multiple fields are unique
- Loading branch information
1 parent
80c2708
commit b9d635f
Showing
4 changed files
with
104 additions
and
21 deletions.
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
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 |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
Tests for the OpenID Client | ||
""" | ||
|
||
import json | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.test import TestCase | ||
from django.test.utils import override_settings | ||
|
@@ -41,7 +43,7 @@ | |
}, | ||
"SPLIT_NAME_CLAIM": True, | ||
"USE_EMAIL_USERNAME": True, | ||
"USER_UNIQUE_FILTER_FIELD": "email", | ||
"USER_UNIQUE_FILTER_FIELDS": ["email", "username"], | ||
"SSO_COOKIE_DATA": "email", | ||
"JWT_ALGORITHM": "HS256", | ||
"JWT_SECRET_KEY": "abc", | ||
|
@@ -85,6 +87,83 @@ def test_returns_data_entry_template_on_missing_username_claim(self): | |
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(response.template_name, "oidc/oidc_user_data_entry.html") | ||
|
||
@override_settings(OPENID_CONNECT_VIEWSET_CONFIG=OPENID_CONNECT_VIEWSET_CONFIG) | ||
def test_recreating_already_existing_user(self): | ||
""" | ||
Trying to create a user that already exists will ask you to chose a different username | ||
""" | ||
view = UserModelOpenIDConnectViewset.as_view({"post": "callback"}) | ||
with patch( | ||
"oidc.viewsets.OpenIDClient.verify_and_decode_id_token" | ||
) as mock_func: | ||
mock_func.return_value = { | ||
"family_name": "Frankline", | ||
"given_name": "Benjamin", | ||
"username": "bfrank", | ||
"email": "[email protected]", | ||
} | ||
|
||
data = {"id_token": "sadsdaio3209lkasdlkas0d.sdojdsiad.iosdadia"} | ||
request = self.factory.post("/", data=data) | ||
response = view(request, auth_server="default") | ||
# Creating the user for the first time will work ok | ||
self.assertEqual(response.status_code, 302) | ||
user = User.objects.get(username="bfrank") | ||
self.assertEqual(user.email, "[email protected]") | ||
|
||
with patch( | ||
"oidc.viewsets.OpenIDClient.verify_and_decode_id_token" | ||
) as mock_func: | ||
mock_func.return_value = { | ||
"family_name": "Frankline", | ||
"given_name": "Benjamin", | ||
"username": "bfrank", | ||
"email": "[email protected]", | ||
} | ||
|
||
data = {"id_token": "sadsdaio3209lkasdlkas0d.sdojdsiad.iosdadia"} | ||
request = self.factory.post("/", data=data) | ||
response = view(request, auth_server="default") | ||
# Creating the user for the second time will not work ok | ||
self.assertEqual(response.status_code, 200) | ||
|
||
response_data = json.loads(response.rendered_content.decode("utf-8")) | ||
self.assertEqual( | ||
"Username field is already in use.", response_data["error"] | ||
) | ||
self.assertEqual(response.template_name, "oidc/oidc_user_data_entry.html") | ||
|
||
# Original user with original email address still exists | ||
user = User.objects.get(username="bfrank") | ||
self.assertEqual(user.email, "[email protected]") | ||
|
||
# Try creating the same user in uppercase | ||
with patch( | ||
"oidc.viewsets.OpenIDClient.verify_and_decode_id_token" | ||
) as mock_func: | ||
mock_func.return_value = { | ||
"family_name": "Frankline", | ||
"given_name": "Benjamin", | ||
"username": "BFRANK", | ||
"email": "[email protected]", | ||
} | ||
|
||
data = {"id_token": "sadsdaio3209lkasdlkas0d.sdojdsiad.iosdadia"} | ||
request = self.factory.post("/", data=data) | ||
response = view(request, auth_server="default") | ||
# Creating the user for the second time will not work ok | ||
self.assertEqual(response.status_code, 200) | ||
|
||
response_data = json.loads(response.rendered_content.decode("utf-8")) | ||
self.assertEqual( | ||
"Username field is already in use.", response_data["error"] | ||
) | ||
self.assertEqual(response.template_name, "oidc/oidc_user_data_entry.html") | ||
|
||
# Original user with original email address still exists | ||
user = User.objects.get(username="bfrank") | ||
self.assertEqual(user.email, "[email protected]") | ||
|
||
@override_settings(OPENID_CONNECT_VIEWSET_CONFIG=OPENID_CONNECT_VIEWSET_CONFIG) | ||
def test_user_created_successfully_when_email_has_a_valid_username(self): | ||
""" | ||
|