| 1 |
""" |
| 2 |
Run just this suite: |
| 3 |
python manage.py test allauth.socialaccount.providers.trainingpeaks.tests.TrainingPeaksTests |
| 4 |
""" |
| 5 |
|
| 6 |
from collections import namedtuple |
| 7 |
from http import HTTPStatus |
| 8 |
|
| 9 |
from django.test import TestCase |
| 10 |
from django.test.utils import override_settings |
| 11 |
|
| 12 |
from allauth.socialaccount.providers.trainingpeaks.provider import TrainingPeaksProvider |
| 13 |
from allauth.socialaccount.providers.trainingpeaks.views import ( |
| 14 |
TrainingPeaksOAuth2Adapter, |
| 15 |
) |
| 16 |
from tests.apps.socialaccount.base import OAuth2TestsMixin |
| 17 |
from tests.mocking import MockedResponse |
| 18 |
|
| 19 |
|
| 20 |
class TrainingPeaksTests(OAuth2TestsMixin, TestCase): |
| 21 |
provider_id = TrainingPeaksProvider.id |
| 22 |
|
| 23 |
def get_mocked_response(self): |
| 24 |
return MockedResponse( |
| 25 |
HTTPStatus.OK, |
| 26 |
"""{ |
| 27 |
"Id": 123456, |
| 28 |
"FirstName": "John", |
| 29 |
"LastName": "Doe", |
| 30 |
"Email": "[email protected]", |
| 31 |
"DateOfBirth": "1986-02-01T00:00:00", |
| 32 |
"CoachedBy": 987654, |
| 33 |
"Weight": 87.5223617553711 |
| 34 |
}""", |
| 35 |
) # noqa |
| 36 |
|
| 37 |
def get_expected_to_str(self): |
| 38 |
return "[email protected]" |
| 39 |
|
| 40 |
def get_login_response_json(self, with_refresh_token=True): |
| 41 |
rtoken = "" |
| 42 |
if with_refresh_token: |
| 43 |
rtoken = ',"refresh_token": "testrf"' |
| 44 |
return ( |
| 45 |
"""{ |
| 46 |
"access_token" : "testac", |
| 47 |
"token_type" : "bearer", |
| 48 |
"expires_in" : 600, |
| 49 |
"scope": "scopes granted" |
| 50 |
%s }""" |
| 51 |
% rtoken |
| 52 |
) |
| 53 |
|
| 54 |
def test_default_use_sandbox_uri(self): |
| 55 |
adapter = TrainingPeaksOAuth2Adapter(None) |
| 56 |
self.assertTrue(".sandbox." in adapter.authorize_url) |
| 57 |
self.assertTrue(".sandbox." in adapter.access_token_url) |
| 58 |
self.assertTrue(".sandbox." in adapter.profile_url) |
| 59 |
|
| 60 |
@override_settings( |
| 61 |
SOCIALACCOUNT_PROVIDERS={"trainingpeaks": {"USE_PRODUCTION": True}} |
| 62 |
) |
| 63 |
def test_use_production_uri(self): |
| 64 |
adapter = TrainingPeaksOAuth2Adapter(None) |
| 65 |
self.assertFalse(".sandbox." in adapter.authorize_url) |
| 66 |
self.assertFalse(".sandbox." in adapter.access_token_url) |
| 67 |
self.assertFalse(".sandbox." in adapter.profile_url) |
| 68 |
|
| 69 |
def test_scope_from_default(self): |
| 70 |
Request = namedtuple("request", ["GET"]) |
| 71 |
mock_request = Request(GET={}) |
| 72 |
scope = self.provider.get_scope_from_request(mock_request) |
| 73 |
self.assertTrue("athlete:profile" in scope) |
| 74 |
|
| 75 |
@override_settings( |
| 76 |
SOCIALACCOUNT_PROVIDERS={ |
| 77 |
"trainingpeaks": {"SCOPE": ["athlete:profile", "workouts", "workouts:wod"]} |
| 78 |
} |
| 79 |
) |
| 80 |
def test_scope_from_settings(self): |
| 81 |
Request = namedtuple("request", ["GET"]) |
| 82 |
mock_request = Request(GET={}) |
| 83 |
scope = self.provider.get_scope_from_request(mock_request) |
| 84 |
for item in ("athlete:profile", "workouts", "workouts:wod"): |
| 85 |
self.assertTrue(item in scope) |