| 1 |
from http import HTTPStatus |
| 2 |
from requests.exceptions import HTTPError |
| 3 |
|
| 4 |
from django.test import TestCase |
| 5 |
from django.test.client import RequestFactory |
| 6 |
from django.test.utils import override_settings |
| 7 |
from django.urls import reverse |
| 8 |
|
| 9 |
from allauth.socialaccount.models import SocialToken |
| 10 |
from allauth.socialaccount.providers.ynab.provider import YNABProvider |
| 11 |
from tests.apps.socialaccount.base import OAuth2TestsMixin |
| 12 |
from tests.mocking import MockedResponse, mocked_response |
| 13 |
|
| 14 |
|
| 15 |
@override_settings( |
| 16 |
SOCIALACCOUNT_AUTO_SIGNUP=True, |
| 17 |
ACCOUNT_SIGNUP_FORM_CLASS=None, |
| 18 |
) |
| 19 |
# ACCOUNT_EMAIL_VERIFICATION=account_settings |
| 20 |
# .EmailVerificationMethod.MANDATORY) |
| 21 |
class YNABTests(OAuth2TestsMixin, TestCase): |
| 22 |
provider_id = YNABProvider.id |
| 23 |
|
| 24 |
def get_mocked_response(self): |
| 25 |
return MockedResponse( |
| 26 |
HTTPStatus.OK, |
| 27 |
""" |
| 28 |
{"data": { |
| 29 |
"user":{ |
| 30 |
"id": "abcd1234xyz5678" |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
""", |
| 35 |
) |
| 36 |
|
| 37 |
def get_expected_to_str(self): |
| 38 |
return "YNAB" |
| 39 |
|
| 40 |
def test_ynab_compelete_login_401(self): |
| 41 |
from allauth.socialaccount.providers.ynab.views import YNABOAuth2Adapter |
| 42 |
|
| 43 |
class LessMockedResponse(MockedResponse): |
| 44 |
def raise_for_status(self): |
| 45 |
if self.status_code != HTTPStatus.OK: |
| 46 |
raise HTTPError(None) |
| 47 |
|
| 48 |
request = RequestFactory().get( |
| 49 |
reverse(f"{self.provider.id}_login"), dict(process="login") |
| 50 |
) |
| 51 |
|
| 52 |
adapter = YNABOAuth2Adapter(request) |
| 53 |
app = adapter.get_provider().app |
| 54 |
token = SocialToken(token="some_token") |
| 55 |
response_with_401 = LessMockedResponse( |
| 56 |
HTTPStatus.UNAUTHORIZED, |
| 57 |
""" |
| 58 |
{"error": { |
| 59 |
"errors": [{ |
| 60 |
"domain": "global", |
| 61 |
"reason": "authError", |
| 62 |
"message": "Invalid Credentials", |
| 63 |
"locationType": "header", |
| 64 |
"location": "Authorization" } ], |
| 65 |
"code": 401, |
| 66 |
"message": "Invalid Credentials" } |
| 67 |
}""", |
| 68 |
) |
| 69 |
with mocked_response(response_with_401): |
| 70 |
with self.assertRaises(HTTPError): |
| 71 |
adapter.complete_login(request, app, token) |