moviewyrm/bookwyrm/tests/views/landing/test_register.py

370 lines
14 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" test for app action functionality """
2021-01-12 17:09:05 +00:00
from unittest.mock import patch
from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import PermissionDenied
from django.http.response import Http404
from django.template.response import TemplateResponse
from django.test import TestCase
2021-01-12 17:53:04 +00:00
from django.test.client import RequestFactory
2021-01-12 17:09:05 +00:00
2021-09-07 22:03:15 +00:00
from bookwyrm import models, views
2021-01-12 17:09:05 +00:00
from bookwyrm.settings import DOMAIN
from bookwyrm.tests.validate_html import validate_html
2021-01-12 17:09:05 +00:00
# pylint: disable=too-many-public-methods
2021-08-03 17:25:53 +00:00
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
2021-09-06 21:50:33 +00:00
@patch("bookwyrm.activitystreams.populate_stream_task.delay")
2021-12-10 03:14:10 +00:00
@patch("bookwyrm.lists_stream.populate_lists_task.delay")
2021-09-07 22:03:15 +00:00
class RegisterViews(TestCase):
2021-04-26 16:15:42 +00:00
"""login and password management"""
2021-03-08 16:49:10 +00:00
2021-01-12 17:53:04 +00:00
def setUp(self):
2021-04-26 16:15:42 +00:00
"""we need basic test data and mocks"""
2021-01-12 17:53:04 +00:00
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
2021-12-10 03:14:10 +00:00
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
2021-08-03 17:25:53 +00:00
self.local_user = models.User.objects.create_user(
2021-08-06 18:23:38 +00:00
"mouse@your.domain.here",
2021-08-03 17:25:53 +00:00
"mouse@mouse.com",
"password",
local=True,
localname="mouse",
)
2021-01-12 17:53:04 +00:00
self.anonymous_user = AnonymousUser
self.anonymous_user.is_authenticated = False
2021-08-02 23:05:40 +00:00
2021-08-07 01:13:51 +00:00
self.settings = models.SiteSettings.objects.create(
id=1, require_confirm_email=False
2021-08-07 01:13:51 +00:00
)
2021-01-12 17:53:04 +00:00
2021-10-16 17:46:05 +00:00
def test_get_redirect(self, *_):
"""there's no dedicated registration page"""
view = views.Register.as_view()
request = self.factory.get("register/")
response = view(request)
self.assertEqual(response.status_code, 302)
2021-09-06 22:09:04 +00:00
def test_register(self, *_):
2021-04-26 16:15:42 +00:00
"""create a user"""
2021-01-12 17:53:04 +00:00
view = views.Register.as_view()
self.assertEqual(models.User.objects.count(), 1)
2021-01-12 17:09:05 +00:00
request = self.factory.post(
2021-03-08 16:49:10 +00:00
"register/",
2021-01-12 17:09:05 +00:00
{
2021-03-08 16:49:10 +00:00
"localname": "nutria-user.user_nutria",
"password": "mouseword",
"email": "aa@bb.cccc",
},
)
2021-10-16 19:00:06 +00:00
with patch("bookwyrm.views.landing.register.login"):
2021-08-02 23:05:40 +00:00
response = view(request)
2021-01-12 17:53:04 +00:00
self.assertEqual(models.User.objects.count(), 2)
2021-01-12 17:09:05 +00:00
self.assertEqual(response.status_code, 302)
nutria = models.User.objects.last()
self.assertEqual(nutria.username, f"nutria-user.user_nutria@{DOMAIN}")
2021-03-08 16:49:10 +00:00
self.assertEqual(nutria.localname, "nutria-user.user_nutria")
2021-01-12 17:09:05 +00:00
self.assertEqual(nutria.local, True)
2021-08-07 15:01:14 +00:00
@patch("bookwyrm.emailing.send_email.delay")
def test_register_email_confirm(self, *_):
2021-08-07 14:28:24 +00:00
"""create a user"""
self.settings.require_confirm_email = True
self.settings.save()
view = views.Register.as_view()
self.assertEqual(models.User.objects.count(), 1)
request = self.factory.post(
"register/",
{
"localname": "nutria",
"password": "mouseword",
"email": "aa@bb.cccc",
},
)
2021-10-16 19:00:06 +00:00
with patch("bookwyrm.views.landing.register.login"):
2021-08-07 14:28:24 +00:00
response = view(request)
self.assertEqual(response.status_code, 302)
nutria = models.User.objects.get(localname="nutria")
self.assertEqual(nutria.username, f"nutria@{DOMAIN}")
2021-08-07 14:28:24 +00:00
self.assertEqual(nutria.local, True)
self.assertFalse(nutria.is_active)
self.assertEqual(nutria.deactivation_reason, "pending")
self.assertIsNotNone(nutria.confirmation_code)
2021-09-06 22:09:04 +00:00
def test_register_trailing_space(self, *_):
2021-04-26 16:15:42 +00:00
"""django handles this so weirdly"""
2021-01-12 17:53:04 +00:00
view = views.Register.as_view()
2021-01-12 17:09:05 +00:00
request = self.factory.post(
2021-03-08 16:49:10 +00:00
"register/",
{"localname": "nutria ", "password": "mouseword", "email": "aa@bb.ccc"},
)
2021-10-16 19:00:06 +00:00
with patch("bookwyrm.views.landing.register.login"):
2021-08-02 23:05:40 +00:00
response = view(request)
2021-01-12 17:53:04 +00:00
self.assertEqual(models.User.objects.count(), 2)
2021-01-12 17:09:05 +00:00
self.assertEqual(response.status_code, 302)
nutria = models.User.objects.last()
self.assertEqual(nutria.username, f"nutria@{DOMAIN}")
2021-03-08 16:49:10 +00:00
self.assertEqual(nutria.localname, "nutria")
2021-01-12 17:09:05 +00:00
self.assertEqual(nutria.local, True)
2021-09-06 22:09:04 +00:00
def test_register_invalid_email(self, *_):
2021-04-26 16:15:42 +00:00
"""gotta have an email"""
2021-01-12 17:53:04 +00:00
view = views.Register.as_view()
self.assertEqual(models.User.objects.count(), 1)
2021-01-12 17:09:05 +00:00
request = self.factory.post(
2021-03-08 16:49:10 +00:00
"register/", {"localname": "nutria", "password": "mouseword", "email": "aa"}
)
2021-01-12 17:53:04 +00:00
response = view(request)
self.assertEqual(models.User.objects.count(), 1)
2021-10-16 17:46:05 +00:00
validate_html(response.render())
2021-01-12 17:09:05 +00:00
2021-10-16 18:15:57 +00:00
def test_register_error_and_invite(self, *_):
"""redirect to the invite page"""
view = views.Register.as_view()
self.settings.allow_registration = False
self.settings.save()
models.SiteInvite.objects.create(
code="testcode", user=self.local_user, use_limit=1
)
self.assertEqual(models.SiteInvite.objects.get().times_used, 0)
request = self.factory.post(
"register/",
{
"localname": "nutria",
"password": "mouseword",
"email": "",
"invite_code": "testcode",
},
)
2021-10-16 19:00:06 +00:00
with patch("bookwyrm.views.landing.register.login"):
2021-10-16 18:15:57 +00:00
response = view(request)
response = view(request)
validate_html(response.render())
2021-10-16 17:56:22 +00:00
def test_register_username_in_use(self, *_):
"""that username is taken"""
view = views.Register.as_view()
self.assertEqual(models.User.objects.count(), 1)
request = self.factory.post(
"register/",
{"localname": "mouse", "password": "mouseword", "email": "aa@bb.ccc"},
)
response = view(request)
self.assertEqual(models.User.objects.count(), 1)
validate_html(response.render())
2021-09-06 22:09:04 +00:00
def test_register_invalid_username(self, *_):
2021-04-26 16:15:42 +00:00
"""gotta have an email"""
2021-01-12 17:53:04 +00:00
view = views.Register.as_view()
self.assertEqual(models.User.objects.count(), 1)
2021-01-12 17:09:05 +00:00
request = self.factory.post(
2021-03-08 16:49:10 +00:00
"register/",
{"localname": "nut@ria", "password": "mouseword", "email": "aa@bb.ccc"},
)
2021-01-12 17:53:04 +00:00
response = view(request)
self.assertEqual(models.User.objects.count(), 1)
2021-10-16 17:46:05 +00:00
validate_html(response.render())
2021-01-12 17:09:05 +00:00
request = self.factory.post(
2021-03-08 16:49:10 +00:00
"register/",
{"localname": "nutr ia", "password": "mouseword", "email": "aa@bb.ccc"},
)
2021-01-12 17:53:04 +00:00
response = view(request)
self.assertEqual(models.User.objects.count(), 1)
2021-10-16 17:46:05 +00:00
validate_html(response.render())
2021-01-12 17:09:05 +00:00
request = self.factory.post(
2021-03-08 16:49:10 +00:00
"register/",
{"localname": "nut@ria", "password": "mouseword", "email": "aa@bb.ccc"},
)
2021-01-12 17:53:04 +00:00
response = view(request)
self.assertEqual(models.User.objects.count(), 1)
2021-10-16 17:46:05 +00:00
validate_html(response.render())
2021-01-12 17:09:05 +00:00
2021-09-06 22:09:04 +00:00
def test_register_closed_instance(self, *_):
2021-04-26 16:15:42 +00:00
"""you can't just register"""
2021-01-12 17:53:04 +00:00
view = views.Register.as_view()
2021-01-12 17:09:05 +00:00
self.settings.allow_registration = False
self.settings.save()
request = self.factory.post(
2021-03-08 16:49:10 +00:00
"register/",
{"localname": "nutria ", "password": "mouseword", "email": "aa@bb.ccc"},
)
2021-01-12 17:09:05 +00:00
with self.assertRaises(PermissionDenied):
2021-01-12 17:53:04 +00:00
view(request)
2021-01-12 17:09:05 +00:00
2021-09-08 23:14:01 +00:00
def test_register_blocked_domain(self, *_):
"""you can't register with a blocked domain"""
view = views.Register.as_view()
models.EmailBlocklist.objects.create(domain="gmail.com")
# one that fails
request = self.factory.post(
"register/",
{"localname": "nutria ", "password": "mouseword", "email": "aa@gmail.com"},
)
result = view(request)
self.assertEqual(result.status_code, 302)
self.assertFalse(models.User.objects.filter(email="aa@gmail.com").exists())
# one that succeeds
request = self.factory.post(
"register/",
{"localname": "nutria ", "password": "mouseword", "email": "aa@bleep.com"},
)
2021-10-16 19:00:06 +00:00
with patch("bookwyrm.views.landing.register.login"):
2021-09-08 23:14:01 +00:00
result = view(request)
self.assertEqual(result.status_code, 302)
self.assertTrue(models.User.objects.filter(email="aa@bleep.com").exists())
2021-09-06 22:09:04 +00:00
def test_register_invite(self, *_):
2021-04-26 16:15:42 +00:00
"""you can't just register"""
2021-01-12 17:53:04 +00:00
view = views.Register.as_view()
2021-01-12 17:09:05 +00:00
self.settings.allow_registration = False
self.settings.save()
models.SiteInvite.objects.create(
2021-03-08 16:49:10 +00:00
code="testcode", user=self.local_user, use_limit=1
)
2021-01-12 17:09:05 +00:00
self.assertEqual(models.SiteInvite.objects.get().times_used, 0)
request = self.factory.post(
2021-03-08 16:49:10 +00:00
"register/",
2021-01-12 17:09:05 +00:00
{
2021-03-08 16:49:10 +00:00
"localname": "nutria",
"password": "mouseword",
"email": "aa@bb.ccc",
"invite_code": "testcode",
},
)
2021-10-16 19:00:06 +00:00
with patch("bookwyrm.views.landing.register.login"):
2021-08-02 23:05:40 +00:00
response = view(request)
2021-01-12 17:53:04 +00:00
self.assertEqual(models.User.objects.count(), 2)
2021-01-12 17:09:05 +00:00
self.assertEqual(response.status_code, 302)
self.assertEqual(models.SiteInvite.objects.get().times_used, 1)
# invite already used to max capacity
request = self.factory.post(
2021-03-08 16:49:10 +00:00
"register/",
2021-01-12 17:09:05 +00:00
{
2021-03-08 16:49:10 +00:00
"localname": "nutria2",
"password": "mouseword",
"email": "aa@bb.ccc",
"invite_code": "testcode",
},
)
2021-01-12 17:09:05 +00:00
with self.assertRaises(PermissionDenied):
2021-01-12 17:53:04 +00:00
response = view(request)
self.assertEqual(models.User.objects.count(), 2)
2021-01-12 17:09:05 +00:00
# bad invite code
request = self.factory.post(
2021-03-08 16:49:10 +00:00
"register/",
2021-01-12 17:09:05 +00:00
{
2021-03-08 16:49:10 +00:00
"localname": "nutria3",
"password": "mouseword",
"email": "aa@bb.ccc",
"invite_code": "dkfkdjgdfkjgkdfj",
},
)
2021-01-12 17:09:05 +00:00
with self.assertRaises(Http404):
2021-01-12 17:53:04 +00:00
response = view(request)
self.assertEqual(models.User.objects.count(), 2)
2021-08-07 14:28:24 +00:00
2021-09-06 22:09:04 +00:00
def test_confirm_email_code_get(self, *_):
2021-08-07 14:28:24 +00:00
"""there are so many views, this just makes sure it LOADS"""
self.settings.require_confirm_email = True
self.settings.save()
self.local_user.is_active = False
self.local_user.deactivation_reason = "pending"
self.local_user.confirmation_code = "12345"
self.local_user.save(
broadcast=False,
update_fields=["is_active", "deactivation_reason", "confirmation_code"],
)
view = views.ConfirmEmailCode.as_view()
request = self.factory.get("")
request.user = self.anonymous_user
result = view(request, "12345")
self.assertEqual(result.url, "/login/confirmed")
self.assertEqual(result.status_code, 302)
self.local_user.refresh_from_db()
self.assertTrue(self.local_user.is_active)
self.assertIsNone(self.local_user.deactivation_reason)
request.user = self.local_user
result = view(request, "12345")
self.assertEqual(result.url, "/")
self.assertEqual(result.status_code, 302)
2021-09-06 22:09:04 +00:00
def test_confirm_email_code_get_invalid_code(self, *_):
2021-08-07 14:28:24 +00:00
"""there are so many views, this just makes sure it LOADS"""
self.settings.require_confirm_email = True
self.settings.save()
self.local_user.is_active = False
self.local_user.deactivation_reason = "pending"
self.local_user.confirmation_code = "12345"
self.local_user.save(
broadcast=False,
update_fields=["is_active", "deactivation_reason", "confirmation_code"],
)
view = views.ConfirmEmailCode.as_view()
request = self.factory.get("")
request.user = self.anonymous_user
result = view(request, "abcde")
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
2021-08-07 14:28:24 +00:00
self.assertEqual(result.status_code, 200)
self.assertFalse(self.local_user.is_active)
self.assertEqual(self.local_user.deactivation_reason, "pending")
2021-09-06 22:09:04 +00:00
def test_confirm_email_get(self, *_):
2021-08-07 14:28:24 +00:00
"""there are so many views, this just makes sure it LOADS"""
self.settings.require_confirm_email = True
self.settings.save()
login = views.ConfirmEmail.as_view()
request = self.factory.get("")
request.user = self.anonymous_user
result = login(request)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
2021-08-07 14:28:24 +00:00
self.assertEqual(result.status_code, 200)
request.user = self.local_user
result = login(request)
self.assertEqual(result.url, "/")
self.assertEqual(result.status_code, 302)
2021-10-16 18:15:57 +00:00
def test_confirm_email_post(self, *_):
2021-10-16 18:23:45 +00:00
"""send the email"""
2021-10-16 18:15:57 +00:00
self.settings.require_confirm_email = True
self.settings.save()
view = views.ConfirmEmail.as_view()
models.SiteInvite.objects.create(
code="testcode", user=self.local_user, use_limit=1
)
request = self.factory.post("", {"code": "testcode"})
request.user = self.anonymous_user
result = view(request)
validate_html(result.render())
def test_resend_link(self, *_):
2021-10-16 18:23:45 +00:00
"""try again"""
2021-10-16 18:15:57 +00:00
request = self.factory.post("", {"email": "mouse@mouse.com"})
request.user = self.anonymous_user
with patch("bookwyrm.emailing.send_email.delay") as mock:
views.resend_link(request)
self.assertEqual(mock.call_count, 1)