| 1 |
from http import HTTPStatus |
| 2 |
|
| 3 |
from django.test import TestCase |
| 4 |
from django.test.utils import override_settings |
| 5 |
|
| 6 |
from allauth.socialaccount.models import SocialAccount |
| 7 |
from allauth.socialaccount.providers.bitbucket_oauth2.provider import ( |
| 8 |
BitbucketOAuth2Provider, |
| 9 |
) |
| 10 |
from tests.apps.socialaccount.base import OAuth2TestsMixin |
| 11 |
from tests.mocking import MockedResponse |
| 12 |
|
| 13 |
|
| 14 |
@override_settings(SOCIALACCOUNT_QUERY_EMAIL=True, SOCIALACCOUNT_STORE_TOKENS=True) |
| 15 |
class BitbucketOAuth2Tests(OAuth2TestsMixin, TestCase): |
| 16 |
provider_id = BitbucketOAuth2Provider.id |
| 17 |
|
| 18 |
response_data = """ |
| 19 |
{ |
| 20 |
"created_on": "2011-12-20T16:34:07.132459+00:00", |
| 21 |
"display_name": "tutorials account", |
| 22 |
"links": { |
| 23 |
"avatar": { |
| 24 |
"href": |
| 25 |
"https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2013/Nov/25/tutorials-avatar-1563784409-6_avatar.png" |
| 26 |
}, |
| 27 |
"followers": { |
| 28 |
"href": |
| 29 |
"https://api.bitbucket.org/2.0/users/tutorials/followers" |
| 30 |
}, |
| 31 |
"following": { |
| 32 |
"href": |
| 33 |
"https://api.bitbucket.org/2.0/users/tutorials/following" |
| 34 |
}, |
| 35 |
"html": { |
| 36 |
"href": "https://bitbucket.org/tutorials" |
| 37 |
}, |
| 38 |
"repositories": { |
| 39 |
"href": |
| 40 |
"https://api.bitbucket.org/2.0/repositories/tutorials" |
| 41 |
}, |
| 42 |
"self": { |
| 43 |
"href": "https://api.bitbucket.org/2.0/users/tutorials" |
| 44 |
} |
| 45 |
}, |
| 46 |
"location": "Santa Monica, CA", |
| 47 |
"type": "user", |
| 48 |
"username": "tutorials", |
| 49 |
"uuid": "{c788b2da-b7a2-404c-9e26-d3f077557007}", |
| 50 |
"website": "https://tutorials.bitbucket.org/" |
| 51 |
} |
| 52 |
""" # noqa |
| 53 |
|
| 54 |
email_response_data = """ |
| 55 |
{ |
| 56 |
"page": 1, |
| 57 |
"pagelen": 10, |
| 58 |
"size": 1, |
| 59 |
"values": [ |
| 60 |
{ |
| 61 |
"email": "[email protected]", |
| 62 |
"is_confirmed": true, |
| 63 |
"is_primary": true, |
| 64 |
"links": { |
| 65 |
"self": { |
| 66 |
"href": |
| 67 |
"https://api.bitbucket.org/2.0/user/emails/[email protected]" |
| 68 |
} |
| 69 |
}, |
| 70 |
"type": "email" |
| 71 |
}, |
| 72 |
{ |
| 73 |
"email": "[email protected]", |
| 74 |
"is_confirmed": true, |
| 75 |
"is_primary": true, |
| 76 |
"links": { |
| 77 |
"self": { |
| 78 |
"href": |
| 79 |
"https://api.bitbucket.org/2.0/user/emails/[email protected]" |
| 80 |
} |
| 81 |
}, |
| 82 |
"type": "email" |
| 83 |
} |
| 84 |
] |
| 85 |
} |
| 86 |
""" # noqa |
| 87 |
|
| 88 |
def get_mocked_response(self): |
| 89 |
return [ |
| 90 |
MockedResponse(HTTPStatus.OK, self.response_data), |
| 91 |
MockedResponse(HTTPStatus.OK, self.email_response_data), |
| 92 |
MockedResponse(HTTPStatus.OK, self.response_data), |
| 93 |
MockedResponse(HTTPStatus.OK, self.email_response_data), |
| 94 |
] |
| 95 |
|
| 96 |
def get_expected_to_str(self): |
| 97 |
return "tutorials" |
| 98 |
|
| 99 |
def test_provider_account(self): |
| 100 |
self.login(self.get_mocked_response()) |
| 101 |
socialaccount = SocialAccount.objects.get(uid="tutorials") |
| 102 |
self.assertEqual(socialaccount.user.username, "tutorials") |
| 103 |
self.assertEqual(socialaccount.user.email, "[email protected]") |
| 104 |
account = socialaccount.get_provider_account() |
| 105 |
self.assertEqual(account.to_str(), "tutorials") |
| 106 |
self.assertEqual(account.get_profile_url(), "https://bitbucket.org/tutorials") |
| 107 |
self.assertEqual( |
| 108 |
account.get_avatar_url(), |
| 109 |
"https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2013/Nov/25/tutorials-avatar-1563784409-6_avatar.png", # noqa |
| 110 |
) |