| 1 |
from http import HTTPStatus |
| 2 |
|
| 3 |
from django.contrib.auth.models import User |
| 4 |
from django.test import TestCase |
| 5 |
|
| 6 |
from allauth.socialaccount.models import SocialAccount |
| 7 |
from allauth.socialaccount.providers.strava.provider import StravaProvider |
| 8 |
from tests.apps.socialaccount.base import OAuth2TestsMixin |
| 9 |
from tests.mocking import MockedResponse |
| 10 |
|
| 11 |
|
| 12 |
class StravaTests(OAuth2TestsMixin, TestCase): |
| 13 |
provider_id = StravaProvider.id |
| 14 |
|
| 15 |
def get_mocked_response(self): |
| 16 |
return MockedResponse( |
| 17 |
HTTPStatus.OK, |
| 18 |
"""{ |
| 19 |
"id": 32641234, |
| 20 |
"username": null, |
| 21 |
"resource_state": 2, |
| 22 |
"firstname": "georges", |
| 23 |
"lastname": "camembert", |
| 24 |
"city": "London", |
| 25 |
"state": "England", |
| 26 |
"country": "United Kingdom", |
| 27 |
"sex": "M", |
| 28 |
"premium": false, |
| 29 |
"summit": false, |
| 30 |
"created_at": "2017-07-12T12:42:52Z", |
| 31 |
"updated_at": "2017-10-21T11:01:23Z", |
| 32 |
"badge_type_id": 0, |
| 33 |
"profile_medium": "avatar/athlete/medium.png", |
| 34 |
"profile": "avatar/athlete/large.png", |
| 35 |
"friend": null, |
| 36 |
"follower": null, |
| 37 |
"email": "[email protected]" |
| 38 |
}""", |
| 39 |
) # noqa |
| 40 |
|
| 41 |
def get_expected_to_str(self): |
| 42 |
return "[email protected]" |
| 43 |
|
| 44 |
def get_mocked_response_avatar_invalid_id(self): |
| 45 |
"""Profile including realistic avatar URL |
| 46 |
user ID set to 0 to test edge case where id would be missing""" |
| 47 |
return MockedResponse( |
| 48 |
HTTPStatus.OK, |
| 49 |
"""{ |
| 50 |
"id": 0, |
| 51 |
"username": null, |
| 52 |
"resource_state": 2, |
| 53 |
"firstname": "georges", |
| 54 |
"lastname": "camembert", |
| 55 |
"city": "London", |
| 56 |
"state": "England", |
| 57 |
"country": "United Kingdom", |
| 58 |
"sex": "M", |
| 59 |
"premium": false, |
| 60 |
"summit": false, |
| 61 |
"created_at": "2017-07-12T12:42:52Z", |
| 62 |
"updated_at": "2017-10-21T11:01:23Z", |
| 63 |
"badge_type_id": 0, |
| 64 |
"profile_medium": "https://cloudfront.net/1/medium.jpg", |
| 65 |
"profile": "https://cloudfront.net/1/large.jpg", |
| 66 |
"friend": null, |
| 67 |
"follower": null, |
| 68 |
"email": "[email protected]" |
| 69 |
}""", |
| 70 |
) # noqa |
| 71 |
|
| 72 |
def test_valid_avatar(self): |
| 73 |
"""test response with Avatar URL""" |
| 74 |
self.login(self.get_mocked_response_avatar_invalid_id()) |
| 75 |
user = User.objects.get(email="[email protected]") |
| 76 |
soc_acc = SocialAccount.objects.filter( |
| 77 |
user=user, provider=self.provider.id |
| 78 |
).get() |
| 79 |
provider_account = soc_acc.get_provider_account() |
| 80 |
self.assertEqual( |
| 81 |
provider_account.get_avatar_url(), |
| 82 |
"https://cloudfront.net/1/large.jpg", |
| 83 |
) |
| 84 |
self.assertIsNone(provider_account.get_profile_url()) |
| 85 |
|
| 86 |
def get_login_response_json(self, with_refresh_token=True): |
| 87 |
rt = "" |
| 88 |
if with_refresh_token: |
| 89 |
rt = ',"refresh_token": "testrf"' |
| 90 |
return ( |
| 91 |
"""{ |
| 92 |
"uid":"weibo", |
| 93 |
"access_token":"testac", |
| 94 |
"livemode": false, |
| 95 |
"token_type": "bearer", |
| 96 |
"strava_publishable_key": "pk_test_someteskey", |
| 97 |
"strava_user_id": "acct_sometestid", |
| 98 |
"scope": "read_write" |
| 99 |
%s }""" |
| 100 |
% rt |
| 101 |
) |