| 1 |
import json |
| 2 |
from http import HTTPStatus |
| 3 |
|
| 4 |
from django.test import TestCase |
| 5 |
|
| 6 |
from allauth.socialaccount.models import SocialAccount |
| 7 |
from allauth.socialaccount.providers.gitlab.provider import GitLabProvider |
| 8 |
from allauth.socialaccount.providers.gitlab.views import _check_errors |
| 9 |
from allauth.socialaccount.providers.oauth2.client import OAuth2Error |
| 10 |
from tests.apps.socialaccount.base import OAuth2TestsMixin |
| 11 |
from tests.mocking import MockedResponse |
| 12 |
|
| 13 |
|
| 14 |
class GitLabTests(OAuth2TestsMixin, TestCase): |
| 15 |
provider_id = GitLabProvider.id |
| 16 |
_uid = 2 |
| 17 |
|
| 18 |
def get_mocked_response(self): |
| 19 |
return MockedResponse( |
| 20 |
HTTPStatus.OK, |
| 21 |
""" |
| 22 |
{ |
| 23 |
"avatar_url": "https://secure.gravatar.com/avatar/123", |
| 24 |
"bio": null, |
| 25 |
"can_create_group": true, |
| 26 |
"can_create_project": true, |
| 27 |
"color_scheme_id": 5, |
| 28 |
"confirmed_at": "2015-03-02T16:53:58.370Z", |
| 29 |
"created_at": "2015-03-02T16:53:58.885Z", |
| 30 |
"current_sign_in_at": "2018-06-12T18:44:49.985Z", |
| 31 |
"email": "[email protected]", |
| 32 |
"external": false, |
| 33 |
"id": 2, |
| 34 |
"identities": [], |
| 35 |
"last_activity_on": "2018-06-11", |
| 36 |
"last_sign_in_at": "2018-05-31T14:59:44.527Z", |
| 37 |
"linkedin": "", |
| 38 |
"location": null, |
| 39 |
"name": "Mr Bob", |
| 40 |
"organization": null, |
| 41 |
"projects_limit": 10, |
| 42 |
"shared_runners_minutes_limit": 2000, |
| 43 |
"skype": "", |
| 44 |
"state": "active", |
| 45 |
"theme_id": 6, |
| 46 |
"twitter": "mrbob", |
| 47 |
"two_factor_enabled": true, |
| 48 |
"username": "mr.bob", |
| 49 |
"web_url": "https://gitlab.example.com/u/mr.bob", |
| 50 |
"website_url": "" |
| 51 |
} |
| 52 |
""", |
| 53 |
) |
| 54 |
|
| 55 |
def get_expected_to_str(self): |
| 56 |
return "mr.bob" |
| 57 |
|
| 58 |
def test_valid_response(self): |
| 59 |
data = {"id": 12345} |
| 60 |
response = MockedResponse(HTTPStatus.OK, json.dumps(data)) |
| 61 |
self.assertEqual(_check_errors(response), data) |
| 62 |
|
| 63 |
def test_invalid_data(self): |
| 64 |
response = MockedResponse(HTTPStatus.OK, json.dumps({})) |
| 65 |
with self.assertRaises(OAuth2Error): |
| 66 |
# No id, raises |
| 67 |
_check_errors(response) |
| 68 |
|
| 69 |
def test_account_invalid_response(self): |
| 70 |
body = ( |
| 71 |
"403 Forbidden - You (@domain.com) must accept the Terms of " |
| 72 |
"Service in order to perform this action. Please access GitLab " |
| 73 |
"from a web browser to accept these terms." |
| 74 |
) |
| 75 |
response = MockedResponse(HTTPStatus.FORBIDDEN, body) |
| 76 |
|
| 77 |
# GitLab allow users to login with their API and provides |
| 78 |
# an error requiring the user to accept the Terms of Service. |
| 79 |
# see: https://gitlab.com/gitlab-org/gitlab-foss/-/issues/45849 |
| 80 |
with self.assertRaises(OAuth2Error): |
| 81 |
# no id, 4xx code, raises |
| 82 |
_check_errors(response) |
| 83 |
|
| 84 |
def test_error_response(self): |
| 85 |
body = "403 Forbidden" |
| 86 |
response = MockedResponse(HTTPStatus.FORBIDDEN, body) |
| 87 |
|
| 88 |
with self.assertRaises(OAuth2Error): |
| 89 |
# no id, 4xx code, raises |
| 90 |
_check_errors(response) |
| 91 |
|
| 92 |
def test_invalid_response(self): |
| 93 |
response = MockedResponse(HTTPStatus.OK, json.dumps({})) |
| 94 |
with self.assertRaises(OAuth2Error): |
| 95 |
# No id, raises |
| 96 |
_check_errors(response) |
| 97 |
|
| 98 |
def test_bad_response(self): |
| 99 |
response = MockedResponse(HTTPStatus.BAD_REQUEST, json.dumps({})) |
| 100 |
with self.assertRaises(OAuth2Error): |
| 101 |
# bad json, raises |
| 102 |
_check_errors(response) |
| 103 |
|
| 104 |
def test_extra_data(self): |
| 105 |
self.login(self.get_mocked_response()) |
| 106 |
account = SocialAccount.objects.get(uid=str(self._uid)) |
| 107 |
self.assertEqual(account.extra_data["id"], self._uid) |