2017-11-09 12:24:32 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2022-07-30 21:22:34 +00:00
|
|
|
import re
|
2020-05-17 10:11:08 +00:00
|
|
|
import time
|
|
|
|
|
2022-07-30 21:22:34 +00:00
|
|
|
from django.test import TestCase, override_settings, tag
|
2017-11-09 12:24:32 +00:00
|
|
|
from django.test import Client as HttpClient
|
|
|
|
from django.contrib.auth.models import User
|
2022-07-30 21:22:34 +00:00
|
|
|
from django.core import mail
|
2017-11-09 12:24:32 +00:00
|
|
|
from django.core.management import call_command
|
|
|
|
|
2022-05-28 02:56:38 +00:00
|
|
|
from faker import Faker
|
2017-11-09 12:24:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ViewsTestCase(TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
super(ViewsTestCase, cls).setUpClass()
|
2022-05-28 02:56:38 +00:00
|
|
|
fake = Faker()
|
2022-02-10 00:00:30 +00:00
|
|
|
call_command("migrate", verbosity=0)
|
2017-11-09 12:24:32 +00:00
|
|
|
|
|
|
|
cls.c = HttpClient()
|
|
|
|
|
|
|
|
fake_user = fake.simple_profile()
|
|
|
|
cls.credentials = {
|
2022-02-10 00:00:30 +00:00
|
|
|
"username": fake_user["username"],
|
|
|
|
"password": fake.password(),
|
2017-11-09 12:24:32 +00:00
|
|
|
}
|
2022-07-30 21:22:34 +00:00
|
|
|
cls.user = User.objects.create_user(
|
|
|
|
is_superuser=True, email="admin@admin.admin", **cls.credentials
|
|
|
|
)
|
2017-11-09 12:24:32 +00:00
|
|
|
|
|
|
|
cls.c.login(**cls.credentials)
|
|
|
|
|
|
|
|
def test_root_router(self):
|
2022-02-10 00:00:30 +00:00
|
|
|
page = self.c.get("/")
|
|
|
|
self.assertEqual(page.url, "/dashboard/")
|
2017-11-13 00:43:40 +00:00
|
|
|
|
2020-06-28 12:31:34 +00:00
|
|
|
@override_settings(ROLLING_SESSION_REFRESH=1)
|
2020-05-17 10:11:08 +00:00
|
|
|
def test_rolling_sessions(self):
|
2022-02-10 00:00:30 +00:00
|
|
|
self.c.get("/")
|
|
|
|
session1 = str(self.c.cookies["sessionid"])
|
2020-06-28 12:31:34 +00:00
|
|
|
# Sleep longer than ROLLING_SESSION_REFRESH.
|
2020-05-17 10:11:08 +00:00
|
|
|
time.sleep(2)
|
2022-02-10 00:00:30 +00:00
|
|
|
self.c.get("/")
|
|
|
|
session2 = str(self.c.cookies["sessionid"])
|
|
|
|
self.c.get("/")
|
|
|
|
session3 = str(self.c.cookies["sessionid"])
|
2020-05-17 10:11:08 +00:00
|
|
|
self.assertNotEqual(session1, session2)
|
|
|
|
self.assertEqual(session2, session3)
|
|
|
|
|
2017-11-13 00:43:40 +00:00
|
|
|
def test_user_settings(self):
|
2022-02-10 00:00:30 +00:00
|
|
|
page = self.c.get("/user/settings/")
|
2017-11-13 00:43:40 +00:00
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
|
2022-10-17 07:45:26 +00:00
|
|
|
def test_add_device_page(self):
|
|
|
|
page = self.c.get("/user/add-device/")
|
2022-08-30 20:49:54 +00:00
|
|
|
self.assertRegex(
|
|
|
|
page.content.decode(),
|
|
|
|
r""".*<div [^>]* data-qr-code-content="[^"]+"[^>]*>.*""",
|
|
|
|
)
|
|
|
|
|
2017-12-11 22:18:22 +00:00
|
|
|
def test_user_views(self):
|
2017-12-13 18:49:10 +00:00
|
|
|
# Staff setting is required to access user management.
|
2022-02-10 00:00:30 +00:00
|
|
|
page = self.c.get("/users/")
|
2018-08-04 20:00:51 +00:00
|
|
|
self.assertEqual(page.status_code, 403)
|
2017-12-13 18:49:10 +00:00
|
|
|
self.user.is_staff = True
|
|
|
|
self.user.save()
|
|
|
|
|
2022-02-10 00:00:30 +00:00
|
|
|
page = self.c.get("/users/")
|
2017-12-11 22:18:22 +00:00
|
|
|
self.assertEqual(page.status_code, 200)
|
2022-02-10 00:00:30 +00:00
|
|
|
page = self.c.get("/users/add/")
|
2017-12-11 22:18:22 +00:00
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
|
|
|
|
entry = User.objects.first()
|
2022-02-10 00:00:30 +00:00
|
|
|
page = self.c.get("/users/{}/edit/".format(entry.id))
|
2017-12-11 22:18:22 +00:00
|
|
|
self.assertEqual(page.status_code, 200)
|
2022-02-10 00:00:30 +00:00
|
|
|
page = self.c.get("/users/{}/delete/".format(entry.id))
|
2017-12-11 22:18:22 +00:00
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
|
2017-11-13 00:43:40 +00:00
|
|
|
def test_welcome(self):
|
2022-02-10 00:00:30 +00:00
|
|
|
page = self.c.get("/welcome/")
|
2017-11-13 00:43:40 +00:00
|
|
|
self.assertEqual(page.status_code, 200)
|
2021-12-18 22:38:08 +00:00
|
|
|
|
|
|
|
def test_logout_get_fails(self):
|
2022-02-10 00:00:30 +00:00
|
|
|
page = self.c.get("/logout/")
|
2021-12-18 22:38:08 +00:00
|
|
|
self.assertEqual(page.status_code, 405)
|
2022-07-30 21:22:34 +00:00
|
|
|
|
|
|
|
@tag("isolate")
|
|
|
|
def test_password_reset(self):
|
|
|
|
"""
|
|
|
|
Testing this class primarily ensures Baby Buddy's custom templates are correctly
|
|
|
|
configured for Django's password reset flow.
|
|
|
|
"""
|
|
|
|
self.c.logout()
|
|
|
|
|
|
|
|
page = self.c.get("/reset/")
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
|
|
|
|
page = self.c.post("/reset/", data={"email": self.user.email}, follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
|
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
|
|
|
|
|
|
|
path = re.search(
|
|
|
|
"http://testserver(?P<path>[^\\s]+)", mail.outbox[0].body
|
|
|
|
).group("path")
|
|
|
|
page = self.c.get(path, follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
|
|
|
|
new_password = "xZZVN6z4TvhFg6S"
|
|
|
|
data = {
|
|
|
|
"new_password1": new_password,
|
|
|
|
"new_password2": new_password,
|
|
|
|
}
|
|
|
|
page = self.c.post(page.request["PATH_INFO"], data=data, follow=True)
|
|
|
|
self.assertEqual(page.status_code, 200)
|