From a40b3c6f93170311dabf109500daf00220578e60 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Wed, 8 Feb 2023 19:33:22 -0800 Subject: [PATCH] Add user types tests --- babybuddy/forms.py | 12 ++-- babybuddy/templates/babybuddy/user_list.html | 4 +- babybuddy/tests/tests_commands.py | 46 +++++++------ babybuddy/tests/tests_forms.py | 68 +++++++++++++++++--- babybuddy/tests/tests_templatetags.py | 12 ++++ 5 files changed, 106 insertions(+), 36 deletions(-) diff --git a/babybuddy/forms.py b/babybuddy/forms.py index 14d4495b..ceec15ae 100644 --- a/babybuddy/forms.py +++ b/babybuddy/forms.py @@ -9,7 +9,7 @@ from .models import Settings class BabyBuddyUserForm(forms.ModelForm): - is_readonly = forms.BooleanField( + is_read_only = forms.BooleanField( required=False, label=_("Read only"), help_text=_("Restricts user to viewing data only."), @@ -23,7 +23,7 @@ class BabyBuddyUserForm(forms.ModelForm): "last_name", "email", "is_staff", - "is_readonly", + "is_read_only", "is_active", ] @@ -31,21 +31,21 @@ class BabyBuddyUserForm(forms.ModelForm): user = kwargs["instance"] if user: kwargs["initial"].update( - {"is_readonly": user.groups.filter(name="read_only").exists()} + {"is_read_only": user.groups.filter(name="read_only").exists()} ) super(BabyBuddyUserForm, self).__init__(*args, **kwargs) def save(self, commit=True): user = super(BabyBuddyUserForm, self).save(commit=False) - is_readonly = self.cleaned_data["is_readonly"] - if is_readonly: + is_read_only = self.cleaned_data["is_read_only"] + if is_read_only: user.is_superuser = False else: user.is_superuser = True if commit: user.save() readonly_group = Group.objects.get(name="read_only") - if is_readonly: + if is_read_only: user.groups.add(readonly_group.id) else: user.groups.remove(readonly_group.id) diff --git a/babybuddy/templates/babybuddy/user_list.html b/babybuddy/templates/babybuddy/user_list.html index 076fed0f..39e56abc 100644 --- a/babybuddy/templates/babybuddy/user_list.html +++ b/babybuddy/templates/babybuddy/user_list.html @@ -31,8 +31,8 @@ {{ object.first_name }} {{ object.last_name }} {{ object.email }} - {% user_is_read_only object as is_readonly %} - {{ is_readonly|bool_icon }} + {% user_is_read_only object as is_read_only %} + {{ is_read_only|bool_icon }} {{ object.is_staff|bool_icon }} {{ object.is_active|bool_icon }} diff --git a/babybuddy/tests/tests_commands.py b/babybuddy/tests/tests_commands.py index fac66af6..fec1ec5e 100644 --- a/babybuddy/tests/tests_commands.py +++ b/babybuddy/tests/tests_commands.py @@ -30,32 +30,40 @@ class CommandsTestCase(TransactionTestCase): def test_createuser(self): call_command( "createuser", - username="test", - email="test@test.test", + username="regularuser", + email="regularuser@test.test", password="test", verbosity=0, ) - self.assertIsInstance( - get_user_model().objects.get(username="test"), get_user_model() - ) - self.assertFalse( - get_user_model().objects.filter( - username="test", is_staff=True, is_superuser=True - ) - ) + user = get_user_model().objects.get(username="regularuser") + self.assertIsInstance(user, get_user_model()) + self.assertTrue(user.is_superuser) + self.assertFalse(user.is_staff) + self.assertEqual(user.email, "regularuser@test.test") + call_command( "createuser", "--is-staff", - username="testadmin", - email="testadmin@testadmin.testadmin", + username="staffuser", + email="staffuser@test.test", password="test", verbosity=0, ) - self.assertIsInstance( - get_user_model().objects.get(username="testadmin"), get_user_model() - ) - self.assertTrue( - get_user_model().objects.filter( - username="testadmin", is_staff=True, is_superuser=True - ) + user = get_user_model().objects.get(username="staffuser") + self.assertIsInstance(user, get_user_model()) + self.assertTrue(user.is_superuser) + self.assertTrue(user.is_staff) + + call_command( + "createuser", + "--read-only", + username="readonlyuser", + email="readonlyuser@test.test", + password="test", + verbosity=0, ) + user = get_user_model().objects.get(username="readonlyuser") + self.assertIsInstance(user, get_user_model()) + self.assertFalse(user.is_superuser) + self.assertFalse(user.is_staff) + self.assertTrue(user.groups.filter(name="read_only").exists()) diff --git a/babybuddy/tests/tests_forms.py b/babybuddy/tests/tests_forms.py index e87e8c65..91aeeb19 100644 --- a/babybuddy/tests/tests_forms.py +++ b/babybuddy/tests/tests_forms.py @@ -28,6 +28,17 @@ class FormsTestCase(TestCase): is_superuser=True, **cls.credentials ) + cls.user_template = { + "username": "username", + "first_name": "User", + "last_name": "Name", + "email": "user@user.user", + "is_staff": False, + "is_read_only": False, + "password1": "d47o8dD&#hu3ulu3", + "password2": "d47o8dD&#hu3ulu3", + } + cls.settings_template = { "first_name": "User", "last_name": "Name", @@ -74,15 +85,7 @@ class FormsTestCase(TestCase): self.user.is_staff = True self.user.save() self.c.login(**self.credentials) - - params = { - "username": "username", - "first_name": "User", - "last_name": "Name", - "email": "user@user.user", - "password1": "d47o8dD&#hu3ulu3", - "password2": "d47o8dD&#hu3ulu3", - } + params = self.user_template.copy() page = self.c.post("/users/add/", params) self.assertEqual(page.status_code, 302) @@ -101,6 +104,53 @@ class FormsTestCase(TestCase): get_user_model().objects.filter(username="username"), [] ) + def test_add_regular_user(self): + self.user.is_staff = True + self.user.save() + self.c.login(**self.credentials) + + params = self.user_template.copy() + + page = self.c.post("/users/add/", params) + self.assertEqual(page.status_code, 302) + user = get_user_model().objects.get(username="username") + self.assertIsInstance(user, get_user_model()) + self.assertTrue(user.is_superuser) + self.assertFalse(user.is_staff) + self.assertFalse(user.groups.filter(name="read_only").exists()) + + def test_add_staff_user(self): + self.user.is_staff = True + self.user.save() + self.c.login(**self.credentials) + + params = self.user_template.copy() + params["is_staff"] = True + + page = self.c.post("/users/add/", params) + self.assertEqual(page.status_code, 302) + user = get_user_model().objects.get(username="username") + self.assertIsInstance(user, get_user_model()) + self.assertTrue(user.is_superuser) + self.assertTrue(user.is_staff) + self.assertFalse(user.groups.filter(name="read_only").exists()) + + def test_add_read_only_user(self): + self.user.is_staff = True + self.user.save() + self.c.login(**self.credentials) + + params = self.user_template.copy() + params["is_read_only"] = True + + page = self.c.post("/users/add/", params) + self.assertEqual(page.status_code, 302) + user = get_user_model().objects.get(username="username") + self.assertIsInstance(user, get_user_model()) + self.assertFalse(user.is_superuser) + self.assertFalse(user.is_staff) + self.assertTrue(user.groups.filter(name="read_only").exists()) + def test_user_settings(self): self.c.login(**self.credentials) diff --git a/babybuddy/tests/tests_templatetags.py b/babybuddy/tests/tests_templatetags.py index 46a84fe4..cfb6eb4e 100644 --- a/babybuddy/tests/tests_templatetags.py +++ b/babybuddy/tests/tests_templatetags.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +from django.contrib.auth import get_user_model +from django.contrib.auth.models import Group from django.test import TestCase from django.utils import timezone @@ -17,3 +19,13 @@ class TemplateTagsTestCase(TestCase): first_name="Test", last_name="Child 2", birth_date=timezone.localdate() ) self.assertEqual(babybuddy_tags.get_child_count(), 2) + + def user_is_read_only(self): + user = get_user_model().objects.create_user( + username="readonly", password="readonly", is_superuser=False, is_staf=False + ) + self.assertFalse(babybuddy_tags.user_is_read_only(user)) + + group = Group.objects.get(name="read_only") + user.groups.add(group) + self.assertTrue(babybuddy_tags.user_is_read_only(user))