| 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.battlenet.provider import BattleNetProvider |
| 8 |
from allauth.socialaccount.providers.battlenet.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 BattleNetTests(OAuth2TestsMixin, TestCase): |
| 15 |
provider_id = BattleNetProvider.id |
| 16 |
_uid = 123456789 |
| 17 |
_battletag = "LuckyDragon#1953" |
| 18 |
|
| 19 |
def get_mocked_response(self): |
| 20 |
data = {"battletag": self._battletag, "id": self._uid} |
| 21 |
return MockedResponse(HTTPStatus.OK, json.dumps(data)) |
| 22 |
|
| 23 |
def get_expected_to_str(self): |
| 24 |
return self._battletag |
| 25 |
|
| 26 |
def test_valid_response_no_battletag(self): |
| 27 |
data = {"id": 12345} |
| 28 |
response = MockedResponse(HTTPStatus.OK, json.dumps(data)) |
| 29 |
self.assertEqual(_check_errors(response), data) |
| 30 |
|
| 31 |
def test_invalid_data(self): |
| 32 |
response = MockedResponse(HTTPStatus.OK, json.dumps({})) |
| 33 |
with self.assertRaises(OAuth2Error): |
| 34 |
# No id, raises |
| 35 |
_check_errors(response) |
| 36 |
|
| 37 |
def test_profile_invalid_response(self): |
| 38 |
data = { |
| 39 |
"code": HTTPStatus.FORBIDDEN, |
| 40 |
"type": "Forbidden", |
| 41 |
"detail": "Account Inactive", |
| 42 |
} |
| 43 |
response = MockedResponse(HTTPStatus.UNAUTHORIZED, json.dumps(data)) |
| 44 |
|
| 45 |
with self.assertRaises(OAuth2Error): |
| 46 |
# no id, 4xx code, raises |
| 47 |
_check_errors(response) |
| 48 |
|
| 49 |
def test_error_response(self): |
| 50 |
body = json.dumps({"error": "invalid_token"}) |
| 51 |
response = MockedResponse(HTTPStatus.BAD_REQUEST, body) |
| 52 |
|
| 53 |
with self.assertRaises(OAuth2Error): |
| 54 |
# no id, 4xx code, raises |
| 55 |
_check_errors(response) |
| 56 |
|
| 57 |
def test_service_not_found(self): |
| 58 |
response = MockedResponse(596, "<h1>596 Service Not Found</h1>") |
| 59 |
with self.assertRaises(OAuth2Error): |
| 60 |
# bad json, 5xx code, raises |
| 61 |
_check_errors(response) |
| 62 |
|
| 63 |
def test_invalid_response(self): |
| 64 |
response = MockedResponse(HTTPStatus.OK, "invalid json data") |
| 65 |
with self.assertRaises(OAuth2Error): |
| 66 |
# bad json, raises |
| 67 |
_check_errors(response) |
| 68 |
|
| 69 |
def test_extra_data(self): |
| 70 |
self.login(self.get_mocked_response()) |
| 71 |
account = SocialAccount.objects.get(uid=str(self._uid)) |
| 72 |
self.assertEqual(account.extra_data["battletag"], self._battletag) |
| 73 |
self.assertEqual(account.extra_data["id"], self._uid) |
| 74 |
self.assertEqual(account.extra_data["region"], "us") |