| 1 |
from http import HTTPStatus |
| 2 |
|
| 3 |
from django.test import TestCase |
| 4 |
|
| 5 |
from allauth.socialaccount.providers.dataporten.provider import DataportenProvider |
| 6 |
from tests.apps.socialaccount.base import OAuth2TestsMixin |
| 7 |
from tests.mocking import MockedResponse |
| 8 |
|
| 9 |
|
| 10 |
class DataportenTest(OAuth2TestsMixin, TestCase): |
| 11 |
provider_id = DataportenProvider.id |
| 12 |
|
| 13 |
def setUp(self): |
| 14 |
super().setUp() |
| 15 |
self.mock_data = { |
| 16 |
"userid": "76a7a061-3c55-430d-8ee0-6f82ec42501f", |
| 17 |
"userid_sec": ["feide:[email protected]"], |
| 18 |
"name": "Andreas \u00c5kre Solberg", |
| 19 |
"email": "[email protected]", |
| 20 |
"profilephoto": "p:a3019954-902f-45a3-b4ee-bca7b48ab507", |
| 21 |
"groups": [{}], |
| 22 |
} |
| 23 |
|
| 24 |
def get_login_response_json(self, with_refresh_token=True): |
| 25 |
rt = "" |
| 26 |
if with_refresh_token: |
| 27 |
rt = ',"refresh_token": "testrf"' |
| 28 |
return ( |
| 29 |
"""{ |
| 30 |
"access_token":"testac", |
| 31 |
"expires_in":3600, |
| 32 |
"scope": "userid profile groups" |
| 33 |
%s |
| 34 |
}""" |
| 35 |
% rt |
| 36 |
) |
| 37 |
|
| 38 |
def get_mocked_response(self): |
| 39 |
return MockedResponse( |
| 40 |
status_code=HTTPStatus.OK, |
| 41 |
content="""{ |
| 42 |
"user": { |
| 43 |
"userid": "76a7a061-3c55-430d-8ee0-6f82ec42501f", |
| 44 |
"userid_sec": ["feide:[email protected]"], |
| 45 |
"name": "Andreas \u00c5kre Solberg", |
| 46 |
"email": "[email protected]", |
| 47 |
"profilephoto": "p:a3019954-902f-45a3-b4ee-bca7b48ab507" |
| 48 |
}, |
| 49 |
"audience": "app123id" |
| 50 |
}""", |
| 51 |
headers={"content-type": "application/json"}, |
| 52 |
) |
| 53 |
|
| 54 |
def get_expected_to_str(self): |
| 55 |
return "[email protected]" |
| 56 |
|
| 57 |
def test_extract_uid(self): |
| 58 |
uid = self.provider.extract_uid(self.mock_data) |
| 59 |
self.assertEqual(uid, self.mock_data["userid"]) |
| 60 |
|
| 61 |
def test_extract_extra_data(self): |
| 62 |
# All the processing is done in the complete_login view, and thus |
| 63 |
# the data should be returned unaltered |
| 64 |
extra_data = self.provider.extract_extra_data(self.mock_data) |
| 65 |
self.assertEqual(extra_data, self.mock_data) |
| 66 |
|
| 67 |
def test_extract_common_fields(self): |
| 68 |
# The main task of this function is to parse the data in order to |
| 69 |
# find the Feide username, and if not, use the email |
| 70 |
common_fields = self.provider.extract_common_fields(self.mock_data) |
| 71 |
self.assertEqual(common_fields["username"], "andreas") |
| 72 |
|
| 73 |
# Test correct behaviour when Feide username is unavailable |
| 74 |
new_mock_data = dict(self.mock_data) |
| 75 |
new_mock_data["userid_sec"] = [] |
| 76 |
new_common_fields = self.provider.extract_common_fields(new_mock_data) |
| 77 |
self.assertEqual(new_common_fields["username"], "andreas.solberg") |