| 1 |
from http import HTTPStatus |
| 2 |
|
| 3 |
from django.test import RequestFactory, TestCase |
| 4 |
from django.utils.http import base36_to_int, int_to_base36 |
| 5 |
from django.views import csrf |
| 6 |
|
| 7 |
from allauth import app_settings, utils |
| 8 |
|
| 9 |
|
| 10 |
class BasicTests(TestCase): |
| 11 |
def setUp(self): |
| 12 |
self.factory = RequestFactory() |
| 13 |
|
| 14 |
def test_generate_unique_username(self): |
| 15 |
examples = [ |
| 16 |
("[email protected]", "a.b-c"), |
| 17 |
("Üsêrnamê", "username"), |
| 18 |
("User Name", "user_name"), |
| 19 |
("", "user"), |
| 20 |
] |
| 21 |
for input, username in examples: |
| 22 |
self.assertEqual(utils.generate_unique_username([input]), username) |
| 23 |
|
| 24 |
def test_build_absolute_uri(self): |
| 25 |
request = None |
| 26 |
if not app_settings.SITES_ENABLED: |
| 27 |
request = self.factory.get("/") |
| 28 |
request.META["SERVER_NAME"] = "example.com" |
| 29 |
self.assertEqual( |
| 30 |
utils.build_absolute_uri(request, "/foo"), "http://example.com/foo" |
| 31 |
) |
| 32 |
self.assertEqual( |
| 33 |
utils.build_absolute_uri(request, "/foo", protocol="ftp"), |
| 34 |
"ftp://example.com/foo", |
| 35 |
) |
| 36 |
self.assertEqual( |
| 37 |
utils.build_absolute_uri(request, "http://foo.com/bar"), |
| 38 |
"http://foo.com/bar", |
| 39 |
) |
| 40 |
|
| 41 |
def test_int_to_base36(self): |
| 42 |
n = 55798679658823689999 |
| 43 |
b36 = "brxk553wvxbf3" |
| 44 |
assert int_to_base36(n) == b36 |
| 45 |
assert base36_to_int(b36) == n |
| 46 |
|
| 47 |
def test_templatetag_with_csrf_failure(self): |
| 48 |
# Generate a fictitious GET request |
| 49 |
if not app_settings.SOCIALACCOUNT_ENABLED: |
| 50 |
return |
| 51 |
|
| 52 |
from allauth.socialaccount.models import SocialApp |
| 53 |
|
| 54 |
app = SocialApp.objects.create(provider="google") |
| 55 |
if app_settings.SITES_ENABLED: |
| 56 |
from django.contrib.sites.models import Site |
| 57 |
|
| 58 |
app.sites.add(Site.objects.get_current()) |
| 59 |
|
| 60 |
request = self.factory.get("/tests/test_403_csrf.html") |
| 61 |
# Simulate a CSRF failure by calling the View directly |
| 62 |
# This template is using the `provider_login_url` templatetag |
| 63 |
response = csrf.csrf_failure(request, template_name="test_403_csrf.html") |
| 64 |
# Ensure that CSRF failures with this template |
| 65 |
# tag succeed with the expected 403 response |
| 66 |
self.assertEqual(response.status_code, HTTPStatus.FORBIDDEN) |