| 1 |
from http import HTTPStatus |
| 2 |
|
| 3 |
from django.contrib.auth import get_user_model |
| 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.account import app_settings as account_settings |
| 10 |
from allauth.account.models import EmailAddress |
| 11 |
from allauth.socialaccount.adapter import get_adapter |
| 12 |
from allauth.socialaccount.models import SocialAccount |
| 13 |
from allauth.socialaccount.providers.facebook.provider import FacebookProvider |
| 14 |
from tests.apps.socialaccount.base import OAuth2TestsMixin |
| 15 |
from tests.mocking import MockedResponse, mocked_response |
| 16 |
|
| 17 |
|
| 18 |
@override_settings( |
| 19 |
SOCIALACCOUNT_AUTO_SIGNUP=True, |
| 20 |
ACCOUNT_SIGNUP_FORM_CLASS=None, |
| 21 |
LOGIN_REDIRECT_URL="/accounts/profile/", |
| 22 |
ACCOUNT_EMAIL_VERIFICATION=account_settings.EmailVerificationMethod.NONE, |
| 23 |
SOCIALACCOUNT_PROVIDERS={"facebook": {"AUTH_PARAMS": {}, "VERIFIED_EMAIL": False}}, |
| 24 |
) |
| 25 |
class FacebookTests(OAuth2TestsMixin, TestCase): |
| 26 |
provider_id = FacebookProvider.id |
| 27 |
|
| 28 |
facebook_data = """ |
| 29 |
{ |
| 30 |
"id": "630595557", |
| 31 |
"name": "Raymond Penners", |
| 32 |
"first_name": "Raymond", |
| 33 |
"last_name": "Penners", |
| 34 |
"email": "[email protected]", |
| 35 |
"link": "https://www.facebook.com/raymond.penners", |
| 36 |
"username": "raymond.penners", |
| 37 |
"birthday": "07/17/1973", |
| 38 |
"work": [ |
| 39 |
{ |
| 40 |
"employer": { |
| 41 |
"id": "204953799537777", |
| 42 |
"name": "IntenCT" |
| 43 |
} |
| 44 |
} |
| 45 |
], |
| 46 |
"timezone": 1, |
| 47 |
"locale": "nl_NL", |
| 48 |
"verified": true, |
| 49 |
"updated_time": "2012-11-30T20:40:33+0000" |
| 50 |
}""" |
| 51 |
|
| 52 |
def get_mocked_response(self, data=None): |
| 53 |
if data is None: |
| 54 |
data = self.facebook_data |
| 55 |
return MockedResponse(HTTPStatus.OK, data) |
| 56 |
|
| 57 |
def get_expected_to_str(self): |
| 58 |
return "raymond.penners" |
| 59 |
|
| 60 |
def test_username_conflict(self): |
| 61 |
User = get_user_model() |
| 62 |
User.objects.create(username="raymond.penners") |
| 63 |
self.login(self.get_mocked_response()) |
| 64 |
socialaccount = SocialAccount.objects.get(uid="630595557") |
| 65 |
self.assertEqual(socialaccount.user.username, "raymond") |
| 66 |
|
| 67 |
def test_username_based_on_provider(self): |
| 68 |
self.login(self.get_mocked_response()) |
| 69 |
socialaccount = SocialAccount.objects.get(uid="630595557") |
| 70 |
self.assertEqual(socialaccount.user.username, "raymond.penners") |
| 71 |
|
| 72 |
def test_username_based_on_provider_with_simple_name(self): |
| 73 |
data = '{"id": "1234567", "name": "Harvey McGillicuddy"}' |
| 74 |
self.login(self.get_mocked_response(data=data)) |
| 75 |
socialaccount = SocialAccount.objects.get(uid="1234567") |
| 76 |
self.assertEqual(socialaccount.user.username, "harvey") |
| 77 |
|
| 78 |
@override_settings( |
| 79 |
SOCIALACCOUNT_PROVIDERS={ |
| 80 |
"facebook": { |
| 81 |
"METHOD": "js_sdk", |
| 82 |
} |
| 83 |
}, |
| 84 |
) |
| 85 |
def test_media_js(self): |
| 86 |
request = RequestFactory().get(reverse("account_login")) |
| 87 |
request.session = {} |
| 88 |
script = self.provider.media_js(request) |
| 89 |
self.assertTrue('"appId": "app123id"' in script) |
| 90 |
|
| 91 |
def test_token_auth(self): |
| 92 |
with mocked_response( |
| 93 |
{"access_token": "app_token"}, |
| 94 |
{ |
| 95 |
"data": { |
| 96 |
"app_id": "app123id", |
| 97 |
"is_valid": True, |
| 98 |
} |
| 99 |
}, |
| 100 |
self.get_mocked_response(), |
| 101 |
): |
| 102 |
login = self.provider.verify_token(None, {"access_token": "dummy"}) |
| 103 |
assert login.user.email == "[email protected]" |
| 104 |
assert login.token.token == "dummy" |
| 105 |
|
| 106 |
def test_login_by_token(self): |
| 107 |
resp = self.client.get(reverse("account_login")) |
| 108 |
with mocked_response( |
| 109 |
{"access_token": "app_token"}, |
| 110 |
{ |
| 111 |
"data": { |
| 112 |
"app_id": "app123id", |
| 113 |
"is_valid": True, |
| 114 |
} |
| 115 |
}, |
| 116 |
self.get_mocked_response(), |
| 117 |
): |
| 118 |
resp = self.client.post( |
| 119 |
reverse("facebook_login_by_token"), |
| 120 |
data={"access_token": "dummy"}, |
| 121 |
) |
| 122 |
self.assertRedirects( |
| 123 |
resp, "/accounts/profile/", fetch_redirect_response=False |
| 124 |
) |
| 125 |
|
| 126 |
@override_settings( |
| 127 |
SOCIALACCOUNT_PROVIDERS={ |
| 128 |
"facebook": { |
| 129 |
"METHOD": "js_sdk", |
| 130 |
"AUTH_PARAMS": {"auth_type": "reauthenticate"}, |
| 131 |
"VERIFIED_EMAIL": False, |
| 132 |
} |
| 133 |
} |
| 134 |
) |
| 135 |
def test_login_by_token_reauthenticate(self): |
| 136 |
resp = self.client.get(reverse("account_login")) |
| 137 |
nonce = resp.context["fb_data"]["loginOptions"]["auth_nonce"] |
| 138 |
with mocked_response( |
| 139 |
{"access_token": "app_token"}, |
| 140 |
{ |
| 141 |
"data": { |
| 142 |
"app_id": "app123id", |
| 143 |
"is_valid": True, |
| 144 |
} |
| 145 |
}, |
| 146 |
{"auth_nonce": nonce}, |
| 147 |
self.get_mocked_response(), |
| 148 |
): |
| 149 |
resp = self.client.post( |
| 150 |
reverse("facebook_login_by_token"), |
| 151 |
data={"access_token": "dummy"}, |
| 152 |
) |
| 153 |
self.assertRedirects( |
| 154 |
resp, "/accounts/profile/", fetch_redirect_response=False |
| 155 |
) |
| 156 |
|
| 157 |
@override_settings(SOCIALACCOUNT_PROVIDERS={"facebook": {"VERIFIED_EMAIL": True}}) |
| 158 |
def test_login_verified(self): |
| 159 |
emailaddress = self._login_verified() |
| 160 |
self.assertTrue(emailaddress.verified) |
| 161 |
|
| 162 |
def test_login_unverified(self): |
| 163 |
emailaddress = self._login_verified() |
| 164 |
self.assertFalse(emailaddress.verified) |
| 165 |
|
| 166 |
def _login_verified(self): |
| 167 |
self.login(self.get_mocked_response()) |
| 168 |
return EmailAddress.objects.get(email="[email protected]") |
| 169 |
|
| 170 |
|
| 171 |
def test_limited_token(rf, db, settings, jwt_decode_bypass): |
| 172 |
settings.SOCIALACCOUNT_PROVIDERS = { |
| 173 |
"facebook": { |
| 174 |
"AUTH_PARAMS": {}, |
| 175 |
"VERIFIED_EMAIL": False, |
| 176 |
"APPS": [{"client_id": "123"}], |
| 177 |
} |
| 178 |
} |
| 179 |
request = rf.get("/") |
| 180 |
adapter = get_adapter(request) |
| 181 |
provider = adapter.get_provider(request, FacebookProvider.id) |
| 182 |
token = {"id_token": "X"} |
| 183 |
with jwt_decode_bypass( |
| 184 |
{ |
| 185 |
"sub": "f123", |
| 186 |
"email": "[email protected]", |
| 187 |
"given_name": "John", |
| 188 |
"family_name": "Doe", |
| 189 |
} |
| 190 |
): |
| 191 |
login = provider.verify_token(request, token) |
| 192 |
assert login.account.uid == "f123" |
| 193 |
assert login.email_addresses[0].email == "[email protected]" |