| 1 |
import json |
| 2 |
from http import HTTPStatus |
| 3 |
|
| 4 |
from django.test import TestCase, override_settings |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from allauth.account.models import EmailAddress |
| 9 |
from allauth.socialaccount.models import SocialAccount |
| 10 |
from allauth.socialaccount.providers.amazon_cognito.provider import ( |
| 11 |
AmazonCognitoProvider, |
| 12 |
) |
| 13 |
from allauth.socialaccount.providers.amazon_cognito.utils import ( |
| 14 |
convert_to_python_bool_if_value_is_json_string_bool, |
| 15 |
) |
| 16 |
from allauth.socialaccount.providers.amazon_cognito.views import ( |
| 17 |
AmazonCognitoOAuth2Adapter, |
| 18 |
) |
| 19 |
from tests.apps.socialaccount.base import OAuth2TestsMixin |
| 20 |
from tests.mocking import MockedResponse |
| 21 |
|
| 22 |
|
| 23 |
def _get_mocked_claims(): |
| 24 |
return { |
| 25 |
"sub": "4993b410-8a1b-4c36-b843-a9c1a697e6b7", |
| 26 |
"given_name": "John", |
| 27 |
"family_name": "Doe", |
| 28 |
"email": "[email protected]", |
| 29 |
"username": "johndoe", |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
@override_settings( |
| 34 |
SOCIALACCOUNT_PROVIDERS={ |
| 35 |
"amazon_cognito": {"DOMAIN": "https://domain.auth.us-east-1.amazoncognito.com"} |
| 36 |
} |
| 37 |
) |
| 38 |
class AmazonCognitoTestCase(OAuth2TestsMixin, TestCase): |
| 39 |
provider_id = AmazonCognitoProvider.id |
| 40 |
|
| 41 |
def get_mocked_response(self): |
| 42 |
mocked_payload = json.dumps(_get_mocked_claims()) |
| 43 |
return MockedResponse(status_code=HTTPStatus.OK, content=mocked_payload) |
| 44 |
|
| 45 |
def get_expected_to_str(self): |
| 46 |
return "johndoe" |
| 47 |
|
| 48 |
@override_settings(SOCIALACCOUNT_PROVIDERS={"amazon_cognito": {}}) |
| 49 |
def test_oauth2_adapter_raises_if_domain_settings_is_missing( |
| 50 |
self, |
| 51 |
): |
| 52 |
mocked_response = self.get_mocked_response() |
| 53 |
|
| 54 |
with self.assertRaises( |
| 55 |
ValueError, |
| 56 |
msg=AmazonCognitoOAuth2Adapter.DOMAIN_KEY_MISSING_ERROR, |
| 57 |
): |
| 58 |
self.login(mocked_response) |
| 59 |
|
| 60 |
def test_saves_email_as_verified_if_email_is_verified_in_cognito( |
| 61 |
self, |
| 62 |
): |
| 63 |
mocked_claims = _get_mocked_claims() |
| 64 |
mocked_claims["email_verified"] = True |
| 65 |
mocked_payload = json.dumps(mocked_claims) |
| 66 |
mocked_response = MockedResponse( |
| 67 |
status_code=HTTPStatus.OK, content=mocked_payload |
| 68 |
) |
| 69 |
|
| 70 |
self.login(mocked_response) |
| 71 |
|
| 72 |
user_id = SocialAccount.objects.get(uid=mocked_claims["sub"]).user_id |
| 73 |
email_address = EmailAddress.objects.get(user_id=user_id) |
| 74 |
|
| 75 |
self.assertEqual(email_address.email, mocked_claims["email"]) |
| 76 |
self.assertTrue(email_address.verified) |
| 77 |
|
| 78 |
def test_provider_slug_replaces_underscores_with_hyphens(self): |
| 79 |
self.assertTrue("_" not in self.provider.get_slug()) |
| 80 |
|
| 81 |
|
| 82 |
@pytest.mark.parametrize( |
| 83 |
"input,output", |
| 84 |
[ |
| 85 |
(True, True), |
| 86 |
("true", True), |
| 87 |
("false", False), |
| 88 |
(False, False), |
| 89 |
], |
| 90 |
) |
| 91 |
def test_convert_bool(input, output): |
| 92 |
assert convert_to_python_bool_if_value_is_json_string_bool(input) == output |