Adds setup views tests

This commit is contained in:
Mouse Reeve 2022-02-17 14:14:31 -08:00
parent 3b0fc9785e
commit 31d362d715
3 changed files with 81 additions and 2 deletions

View file

@ -0,0 +1,79 @@
""" test for app action functionality """
from unittest.mock import patch
from django.core.exceptions import PermissionDenied
from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.client import RequestFactory
from bookwyrm import forms, models, views
from bookwyrm.tests.validate_html import validate_html
class SetupViews(TestCase):
"""activity feed, statuses, dms"""
def setUp(self):
"""we need basic test data and mocks"""
self.factory = RequestFactory()
self.site = models.SiteSettings.objects.create(install_mode=True)
def test_instance_config_permission_denied(self):
"""there are so many views, this just makes sure it LOADS"""
self.site.install_mode = False
self.site.save()
view = views.InstanceConfig.as_view()
request = self.factory.get("")
with self.assertRaises(PermissionDenied):
view(request)
def test_instance_config(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.InstanceConfig.as_view()
request = self.factory.get("")
result = view(request)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_create_admin_get(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.CreateAdmin.as_view()
request = self.factory.get("")
result = view(request)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_create_admin_post(self):
"""there are so many views, this just makes sure it LOADS"""
self.site.name = "hello"
self.site.save()
self.assertFalse(self.site.allow_registration)
self.assertTrue(self.site.require_confirm_email)
self.assertTrue(self.site.install_mode)
view = views.CreateAdmin.as_view()
form = forms.RegisterForm()
form.data["localname"] = "mouse"
form.data["password"] = "mouseword"
form.data["email"] = "aaa@bbb.ccc"
request = self.factory.post("", form.data)
with patch("bookwyrm.views.setup.login") as mock:
view(request)
self.assertTrue(mock.called)
self.site.refresh_from_db()
self.assertFalse(self.site.install_mode)
user = models.User.objects.get()
self.assertTrue(user.is_active)
self.assertTrue(user.is_superuser)
self.assertTrue(user.is_staff)
self.assertTrue(user.shelf_set.exists())

View file

@ -64,7 +64,7 @@ class CreateAdmin(View):
@transaction.atomic
def post(self, request):
"""Create that user"""
site = models.SiteSettings.get()
site = models.SiteSettings.objects.get()
# you can't create an admin user if you're in config mode
if not site.install_mode:
raise PermissionDenied()

2
bw-dev
View file

@ -101,7 +101,7 @@ case "$CMD" in
docker-compose restart celery_worker
;;
pytest)
runweb pytest --no-cov-on-fail "$@"
runweb --no-deps pytest --no-cov-on-fail "$@"
;;
collectstatic)
runweb python manage.py collectstatic --no-input