mirror of https://github.com/snachodog/mybuddy.git
Add user types tests
This commit is contained in:
parent
ae06cbd47e
commit
a40b3c6f93
|
@ -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)
|
||||
|
|
|
@ -31,8 +31,8 @@
|
|||
<td>{{ object.first_name }}</td>
|
||||
<td>{{ object.last_name }}</td>
|
||||
<td>{{ object.email }}</td>
|
||||
{% user_is_read_only object as is_readonly %}
|
||||
<td>{{ is_readonly|bool_icon }}</td>
|
||||
{% user_is_read_only object as is_read_only %}
|
||||
<td>{{ is_read_only|bool_icon }}</td>
|
||||
<td>{{ object.is_staff|bool_icon }}</td>
|
||||
<td>{{ object.is_active|bool_icon }}</td>
|
||||
<td class="text-center">
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in New Issue