Debian Codegraph @

tests/apps/socialaccount/providers/microsoft/tests.py

raw
1 import json
2 from http import HTTPStatus
3
4 from django.test import TestCase
5
6 from allauth.socialaccount.providers.microsoft.provider import MicrosoftGraphProvider
7 from allauth.socialaccount.providers.microsoft.views import _check_errors
8 from allauth.socialaccount.providers.oauth2.client import OAuth2Error
9 from tests.apps.socialaccount.base import OAuth2TestsMixin
10 from tests.mocking import MockedResponse
11
12
13 class MicrosoftGraphTests(OAuth2TestsMixin, TestCase):
14 provider_id = MicrosoftGraphProvider.id
15
16 def get_mocked_response(self):
17 response_data = """
18 {
19 "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
20 "id": "16f5a7b6-5a15-4568-aa5a-31bb117e9967",
21 "businessPhones": [],
22 "displayName": "Anne Weiler",
23 "givenName": "Anne",
24 "jobTitle": "Manufacturing Lead",
25 "mail": "[email protected]",
26 "mobilePhone": "+1 3528700812",
27 "officeLocation": null,
28 "preferredLanguage": "en-US",
29 "surname": "Weiler",
30 "userPrincipalName": "[email protected]",
31 "mailNickname": "annew"
32 }
33 """ # noqa
34 return MockedResponse(HTTPStatus.OK, response_data)
35
36 def get_expected_to_str(self):
37 return "[email protected]"
38
39 def test_invalid_data(self):
40 response = MockedResponse(HTTPStatus.OK, json.dumps({}))
41 with self.assertRaises(OAuth2Error):
42 # No id, raises
43 _check_errors(response)
44
45 def test_profile_invalid_response(self):
46 data = {
47 "error": {
48 "code": "InvalidAuthenticationToken",
49 "message": "Access token validation failure. Invalid audience.",
50 }
51 }
52 response = MockedResponse(HTTPStatus.UNAUTHORIZED, json.dumps(data))
53
54 with self.assertRaises(OAuth2Error):
55 # no id, 4xx code, raises with message
56 _check_errors(response)
57
58 def test_invalid_response(self):
59 response = MockedResponse(HTTPStatus.OK, "invalid json data")
60 with self.assertRaises(OAuth2Error):
61 # bad json, raises
62 _check_errors(response)