| 1 |
import json |
| 2 |
from http import HTTPStatus |
| 3 |
|
| 4 |
from django.test import TestCase |
| 5 |
from django.test.client import RequestFactory |
| 6 |
from django.test.utils import override_settings |
| 7 |
|
| 8 |
from allauth.socialaccount.providers.authentiq.provider import AuthentiqProvider |
| 9 |
from allauth.socialaccount.providers.authentiq.views import AuthentiqOAuth2Adapter |
| 10 |
from tests.apps.socialaccount.base import OAuth2TestsMixin |
| 11 |
from tests.mocking import MockedResponse |
| 12 |
|
| 13 |
|
| 14 |
class AuthentiqTests(OAuth2TestsMixin, TestCase): |
| 15 |
provider_id = AuthentiqProvider.id |
| 16 |
|
| 17 |
def get_mocked_response(self): |
| 18 |
return MockedResponse( |
| 19 |
HTTPStatus.OK, |
| 20 |
json.dumps( |
| 21 |
{ |
| 22 |
"sub": "ZLARGMFT1M", |
| 23 |
"email": "[email protected]", |
| 24 |
"email_verified": True, |
| 25 |
"given_name": "Jane", |
| 26 |
"family_name": "Doe", |
| 27 |
} |
| 28 |
), |
| 29 |
) |
| 30 |
|
| 31 |
def get_expected_to_str(self): |
| 32 |
return "[email protected]" |
| 33 |
|
| 34 |
@override_settings( |
| 35 |
SOCIALACCOUNT_QUERY_EMAIL=False, |
| 36 |
) |
| 37 |
def test_default_scopes_no_email(self): |
| 38 |
scopes = self.provider.get_default_scope() |
| 39 |
self.assertIn("aq:name", scopes) |
| 40 |
self.assertNotIn("email", scopes) |
| 41 |
|
| 42 |
@override_settings( |
| 43 |
SOCIALACCOUNT_QUERY_EMAIL=True, |
| 44 |
) |
| 45 |
def test_default_scopes_email(self): |
| 46 |
scopes = self.provider.get_default_scope() |
| 47 |
self.assertIn("aq:name", scopes) |
| 48 |
self.assertIn("email", scopes) |
| 49 |
|
| 50 |
def test_scopes(self): |
| 51 |
request = RequestFactory().get(AuthentiqOAuth2Adapter.authorize_url) |
| 52 |
scopes = self.provider.get_scope_from_request(request) |
| 53 |
self.assertIn("openid", scopes) |
| 54 |
self.assertIn("aq:name", scopes) |
| 55 |
|
| 56 |
def test_dynamic_scopes(self): |
| 57 |
request = RequestFactory().get( |
| 58 |
AuthentiqOAuth2Adapter.authorize_url, dict(scope="foo") |
| 59 |
) |
| 60 |
scopes = self.provider.get_scope_from_request(request) |
| 61 |
self.assertIn("openid", scopes) |
| 62 |
self.assertIn("aq:name", scopes) |
| 63 |
self.assertIn("foo", scopes) |
| 64 |
|
| 65 |
@override_settings( |
| 66 |
SOCIALACCOUNT_QUERY_EMAIL=True, |
| 67 |
SOCIALACCOUNT_EMAIL_REQUIRED=True, |
| 68 |
SOCIALACCOUNT_EMAIL_VERIFICATION=True, |
| 69 |
) |
| 70 |
def test_scopes_required_verified_email(self): |
| 71 |
request = RequestFactory().get(AuthentiqOAuth2Adapter.authorize_url) |
| 72 |
scopes = self.provider.get_scope_from_request(request) |
| 73 |
self.assertIn("email~rs", scopes) |
| 74 |
self.assertNotIn("email", scopes) |
| 75 |
|
| 76 |
@override_settings( |
| 77 |
SOCIALACCOUNT_QUERY_EMAIL=True, |
| 78 |
SOCIALACCOUNT_EMAIL_REQUIRED=False, |
| 79 |
SOCIALACCOUNT_EMAIL_VERIFICATION=True, |
| 80 |
) |
| 81 |
def test_scopes_optional_verified_email(self): |
| 82 |
request = RequestFactory().get(AuthentiqOAuth2Adapter.authorize_url) |
| 83 |
scopes = self.provider.get_scope_from_request(request) |
| 84 |
self.assertIn("email~s", scopes) |
| 85 |
self.assertNotIn("email", scopes) |
| 86 |
|
| 87 |
@override_settings( |
| 88 |
SOCIALACCOUNT_QUERY_EMAIL=True, |
| 89 |
SOCIALACCOUNT_EMAIL_REQUIRED=True, |
| 90 |
SOCIALACCOUNT_EMAIL_VERIFICATION=False, |
| 91 |
) |
| 92 |
def test_scopes_required_email(self): |
| 93 |
request = RequestFactory().get(AuthentiqOAuth2Adapter.authorize_url) |
| 94 |
scopes = self.provider.get_scope_from_request(request) |
| 95 |
self.assertIn("email~r", scopes) |
| 96 |
self.assertNotIn("email", scopes) |
| 97 |
|
| 98 |
@override_settings( |
| 99 |
SOCIALACCOUNT_QUERY_EMAIL=True, |
| 100 |
SOCIALACCOUNT_EMAIL_REQUIRED=False, |
| 101 |
SOCIALACCOUNT_EMAIL_VERIFICATION=False, |
| 102 |
) |
| 103 |
def test_scopes_optional_email(self): |
| 104 |
request = RequestFactory().get(AuthentiqOAuth2Adapter.authorize_url) |
| 105 |
scopes = self.provider.get_scope_from_request(request) |
| 106 |
self.assertIn("email", scopes) |