2021-03-08 16:49:10 +00:00
|
|
|
""" test for app action functionality """
|
2021-10-16 17:14:53 +00:00
|
|
|
from datetime import timedelta
|
2021-01-26 17:56:01 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
from django.contrib.auth.models import AnonymousUser
|
2021-10-16 17:14:53 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2021-01-26 17:56:01 +00:00
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.client import RequestFactory
|
2021-10-16 17:14:53 +00:00
|
|
|
from django.utils import timezone
|
2021-01-26 17:56:01 +00:00
|
|
|
|
|
|
|
from bookwyrm import models, views
|
2021-10-16 17:09:25 +00:00
|
|
|
from bookwyrm.tests.validate_html import validate_html
|
2021-01-26 17:56:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PasswordViews(TestCase):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""view user and edit profile"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-01-26 17:56:01 +00:00
|
|
|
def setUp(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""we need basic test data and mocks"""
|
2021-01-26 17:56:01 +00:00
|
|
|
self.factory = RequestFactory()
|
2021-09-06 21:48:45 +00:00
|
|
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
|
|
|
"bookwyrm.activitystreams.populate_stream_task.delay"
|
|
|
|
):
|
2021-08-03 17:25:53 +00:00
|
|
|
self.local_user = models.User.objects.create_user(
|
|
|
|
"mouse@local.com",
|
|
|
|
"mouse@mouse.com",
|
|
|
|
"password",
|
|
|
|
local=True,
|
|
|
|
localname="mouse",
|
|
|
|
)
|
2021-01-26 17:56:01 +00:00
|
|
|
self.anonymous_user = AnonymousUser
|
|
|
|
self.anonymous_user.is_authenticated = False
|
2021-08-02 23:05:40 +00:00
|
|
|
models.SiteSettings.objects.create(id=1)
|
2021-01-26 17:56:01 +00:00
|
|
|
|
|
|
|
def test_password_reset_request(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-01-26 17:56:01 +00:00
|
|
|
view = views.PasswordResetRequest.as_view()
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("")
|
2021-01-26 17:56:01 +00:00
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
result = view(request)
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-10-16 17:09:25 +00:00
|
|
|
validate_html(result.render())
|
2021-01-26 17:56:01 +00:00
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
def test_password_reset_request_post(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""send 'em an email"""
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.post("", {"email": "aa@bb.ccc"})
|
2021-09-27 17:26:25 +00:00
|
|
|
request.user = self.anonymous_user
|
2021-01-26 17:56:01 +00:00
|
|
|
view = views.PasswordResetRequest.as_view()
|
|
|
|
resp = view(request)
|
2021-03-21 19:33:19 +00:00
|
|
|
self.assertEqual(resp.status_code, 200)
|
2021-10-16 19:03:42 +00:00
|
|
|
validate_html(resp.render())
|
2021-01-26 17:56:01 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.post("", {"email": "mouse@mouse.com"})
|
2021-09-27 19:38:49 +00:00
|
|
|
request.user = self.anonymous_user
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.emailing.send_email.delay"):
|
2021-01-26 17:56:01 +00:00
|
|
|
resp = view(request)
|
2021-10-16 19:03:42 +00:00
|
|
|
validate_html(resp.render())
|
2021-01-26 17:56:01 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(models.PasswordReset.objects.get().user, self.local_user)
|
2021-01-26 17:56:01 +00:00
|
|
|
|
|
|
|
def test_password_reset(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-01-26 17:56:01 +00:00
|
|
|
view = views.PasswordReset.as_view()
|
|
|
|
code = models.PasswordReset.objects.create(user=self.local_user)
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("")
|
2021-01-26 17:56:01 +00:00
|
|
|
request.user = self.anonymous_user
|
|
|
|
result = view(request, code.code)
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-10-16 17:09:25 +00:00
|
|
|
validate_html(result.render())
|
2021-01-26 17:56:01 +00:00
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2021-10-16 17:14:53 +00:00
|
|
|
def test_password_reset_nonexistant_code(self):
|
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
|
|
|
view = views.PasswordReset.as_view()
|
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.anonymous_user
|
|
|
|
with self.assertRaises(PermissionDenied):
|
|
|
|
view(request, "beep")
|
|
|
|
|
|
|
|
def test_password_reset_invalid_code(self):
|
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
|
|
|
view = views.PasswordReset.as_view()
|
|
|
|
code = models.PasswordReset.objects.create(
|
2021-10-16 17:38:45 +00:00
|
|
|
user=self.local_user, expiry=timezone.now() - timedelta(days=2)
|
2021-10-16 17:14:53 +00:00
|
|
|
)
|
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.anonymous_user
|
|
|
|
with self.assertRaises(PermissionDenied):
|
|
|
|
view(request, code.code)
|
|
|
|
|
|
|
|
def test_password_reset_logged_in(self):
|
|
|
|
"""redirect logged in users"""
|
|
|
|
view = views.PasswordReset.as_view()
|
|
|
|
code = models.PasswordReset.objects.create(user=self.local_user)
|
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.local_user
|
|
|
|
result = view(request, code.code)
|
|
|
|
self.assertEqual(result.status_code, 302)
|
|
|
|
|
2021-01-26 17:56:01 +00:00
|
|
|
def test_password_reset_post(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""reset from code"""
|
2021-01-26 17:56:01 +00:00
|
|
|
view = views.PasswordReset.as_view()
|
|
|
|
code = models.PasswordReset.objects.create(user=self.local_user)
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.post("", {"password": "hi", "confirm-password": "hi"})
|
2021-10-16 19:00:06 +00:00
|
|
|
with patch("bookwyrm.views.landing.password.login"):
|
2021-01-26 17:56:01 +00:00
|
|
|
resp = view(request, code.code)
|
|
|
|
self.assertEqual(resp.status_code, 302)
|
|
|
|
self.assertFalse(models.PasswordReset.objects.exists())
|
|
|
|
|
|
|
|
def test_password_reset_wrong_code(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""reset from code"""
|
2021-01-26 17:56:01 +00:00
|
|
|
view = views.PasswordReset.as_view()
|
|
|
|
models.PasswordReset.objects.create(user=self.local_user)
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.post("", {"password": "hi", "confirm-password": "hi"})
|
|
|
|
resp = view(request, "jhgdkfjgdf")
|
2021-10-16 19:03:42 +00:00
|
|
|
validate_html(resp.render())
|
2021-01-26 17:56:01 +00:00
|
|
|
self.assertTrue(models.PasswordReset.objects.exists())
|
|
|
|
|
|
|
|
def test_password_reset_mismatch(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""reset from code"""
|
2021-01-26 17:56:01 +00:00
|
|
|
view = views.PasswordReset.as_view()
|
|
|
|
code = models.PasswordReset.objects.create(user=self.local_user)
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.post("", {"password": "hi", "confirm-password": "hihi"})
|
2021-01-26 17:56:01 +00:00
|
|
|
resp = view(request, code.code)
|
2021-10-16 19:03:42 +00:00
|
|
|
validate_html(resp.render())
|
2021-01-26 17:56:01 +00:00
|
|
|
self.assertTrue(models.PasswordReset.objects.exists())
|