| 1 |
from http import HTTPStatus |
| 2 |
from urllib.parse import parse_qs, urlparse |
| 3 |
|
| 4 |
from django.test import TestCase |
| 5 |
from django.urls import reverse |
| 6 |
from django.utils.http import urlencode |
| 7 |
|
| 8 |
from allauth.socialaccount.providers.notion.provider import NotionProvider |
| 9 |
from tests.apps.socialaccount.base import OAuth2TestsMixin |
| 10 |
from tests.mocking import MockedResponse, mocked_response |
| 11 |
|
| 12 |
|
| 13 |
class NotionTests(OAuth2TestsMixin, TestCase): |
| 14 |
provider_id = NotionProvider.id |
| 15 |
pkce_enabled_default = False # Notion does not support PKCE. |
| 16 |
|
| 17 |
def get_mocked_response(self): |
| 18 |
return MockedResponse( |
| 19 |
HTTPStatus.OK, |
| 20 |
""" |
| 21 |
{ |
| 22 |
"workspace_id": "workspace-abc", |
| 23 |
"workspace_name": "My Workspace", |
| 24 |
"owner": { |
| 25 |
"user": { |
| 26 |
"id": "test123", |
| 27 |
"name": "John Doe", |
| 28 |
"avatar_url": "", |
| 29 |
"person": {"email": "[email protected]"}, |
| 30 |
} |
| 31 |
}, |
| 32 |
} |
| 33 |
""", |
| 34 |
) # noqa |
| 35 |
|
| 36 |
def get_expected_to_str(self): |
| 37 |
return "John Doe (My Workspace)" |
| 38 |
|
| 39 |
def get_login_response_json(self, with_refresh_token=False): |
| 40 |
""" |
| 41 |
Docs here: |
| 42 |
https://developers.notion.com/docs/authorization#step-4-notion-responds-with-an-access_token-and-additional-information |
| 43 |
""" |
| 44 |
return """ |
| 45 |
{ |
| 46 |
"access_token": "testac", |
| 47 |
"bot_id": "bot-abc", |
| 48 |
"duplicated_template_id": "template-abc", |
| 49 |
"owner": { |
| 50 |
"workspace_id": "workspace-abc", |
| 51 |
"user": { |
| 52 |
"id": "test123", |
| 53 |
"name": "John Doe", |
| 54 |
"avatar_url": "", |
| 55 |
"person": { |
| 56 |
"email": "[email protected]" |
| 57 |
} |
| 58 |
} |
| 59 |
}, |
| 60 |
"workspace_icon": "https://example.com/icon.png", |
| 61 |
"workspace_id": "workspace-abc", |
| 62 |
"workspace_name": "My Workspace" |
| 63 |
} |
| 64 |
""" |
| 65 |
|
| 66 |
def login(self, resp_mock=None, process="login", with_refresh_token=True): |
| 67 |
resp = self.client.post( |
| 68 |
f"{reverse(f'{self.provider.id}_login')}?{urlencode(dict(process=process))}" |
| 69 |
) |
| 70 |
|
| 71 |
p = urlparse(resp["location"]) |
| 72 |
q = parse_qs(p.query) |
| 73 |
|
| 74 |
complete_url = reverse(f"{self.provider.id}_callback") |
| 75 |
response_json = self.get_login_response_json( |
| 76 |
with_refresh_token=with_refresh_token |
| 77 |
) |
| 78 |
|
| 79 |
if isinstance(resp_mock, list): |
| 80 |
resp_mocks = resp_mock |
| 81 |
elif resp_mock is None: |
| 82 |
resp_mocks = [] |
| 83 |
else: |
| 84 |
resp_mocks = [resp_mock] |
| 85 |
|
| 86 |
with mocked_response( |
| 87 |
MockedResponse( |
| 88 |
HTTPStatus.OK, response_json, {"content-type": "application/json"} |
| 89 |
), |
| 90 |
*resp_mocks, |
| 91 |
): |
| 92 |
resp = self.client.get(complete_url, self.get_complete_parameters(q)) |
| 93 |
|
| 94 |
return resp |