Debian Codegraph @

tests/apps/socialaccount/providers/twitch/tests.py

raw
1 from http import HTTPStatus
2
3 from django.test import TestCase
4 from django.test.client import RequestFactory
5 from django.urls import reverse
6
7 from allauth.socialaccount.models import SocialToken
8 from allauth.socialaccount.providers.oauth2.client import OAuth2Error
9 from allauth.socialaccount.providers.twitch.provider import TwitchProvider
10 from allauth.socialaccount.providers.twitch.views import TwitchOAuth2Adapter
11 from tests.apps.socialaccount.base import OAuth2TestsMixin
12 from tests.mocking import MockedResponse, mocked_response
13
14
15 class TwitchTests(OAuth2TestsMixin, TestCase):
16 provider_id = TwitchProvider.id
17
18 def get_mocked_response(self):
19 return MockedResponse(
20 HTTPStatus.OK,
21 """
22 {
23 "data": [{
24 "id": "44322889",
25 "login": "dallas",
26 "display_name": "dallas",
27 "type": "staff",
28 "broadcaster_type": "",
29 "description": "Just a gamer playing games and chatting. :)",
30 "profile_image_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/dallas-profile_image-1a2c906ee2c35f12-300x300.png",
31 "offline_image_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/dallas-channel_offline_image-1a2c906ee2c35f12-1920x1080.png",
32 "view_count": 191836881,
33 "email": "[email protected]"
34 }]
35 }
36 """,
37 ) # noqa
38
39 def get_expected_to_str(self):
40 return "dallas"
41
42 def test_response_over_400_raises_OAuth2Error(self):
43 resp_mock = MockedResponse(HTTPStatus.BAD_REQUEST, '{"error": "Invalid token"}')
44 expected_error = "Twitch API Error: Invalid token ()"
45
46 self.check_for_error(resp_mock, expected_error)
47
48 def test_empty_or_missing_data_key_raises_OAuth2Error(self):
49 resp_mock = MockedResponse(HTTPStatus.OK, '{"data": []}')
50 expected_error = "Invalid data from Twitch API: {'data': []}"
51
52 self.check_for_error(resp_mock, expected_error)
53
54 resp_mock = MockedResponse(HTTPStatus.OK, '{"missing_data": "key"}')
55 expected_error = "Invalid data from Twitch API: {'missing_data': 'key'}"
56
57 self.check_for_error(resp_mock, expected_error)
58
59 def test_missing_twitch_id_raises_OAuth2Error(self):
60 resp_mock = MockedResponse(
61 HTTPStatus.OK, '{"data": [{"login": "fake_twitch"}]}'
62 )
63 expected_error = "Invalid data from Twitch API: {'login': 'fake_twitch'}"
64
65 self.check_for_error(resp_mock, expected_error)
66
67 def check_for_error(self, resp_mock, expected_error):
68 with self.assertRaises(OAuth2Error) as error_ctx:
69 self._run_just_complete_login(resp_mock)
70
71 self.assertEqual(str(error_ctx.exception).replace("u", ""), expected_error)
72
73 def _run_just_complete_login(self, resp_mock):
74 """
75 Helper function for checking that Error cases are
76 handled correctly. Running only `complete_login` means
77 we can check that the specific errors are raised before
78 they are caught and rendered to generic error HTML
79 """
80 request = RequestFactory().get(
81 reverse(f"{self.provider.id}_login"),
82 {"process": "login"},
83 )
84 adapter = TwitchOAuth2Adapter(request)
85 app = adapter.get_provider().app
86 token = SocialToken(token="this-is-my-fake-token")
87
88 with mocked_response(resp_mock):
89 adapter = TwitchOAuth2Adapter(request)
90 adapter.complete_login(request, app, token)