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):
|
class BabyBuddyUserForm(forms.ModelForm):
|
||||||
is_readonly = forms.BooleanField(
|
is_read_only = forms.BooleanField(
|
||||||
required=False,
|
required=False,
|
||||||
label=_("Read only"),
|
label=_("Read only"),
|
||||||
help_text=_("Restricts user to viewing data only."),
|
help_text=_("Restricts user to viewing data only."),
|
||||||
|
@ -23,7 +23,7 @@ class BabyBuddyUserForm(forms.ModelForm):
|
||||||
"last_name",
|
"last_name",
|
||||||
"email",
|
"email",
|
||||||
"is_staff",
|
"is_staff",
|
||||||
"is_readonly",
|
"is_read_only",
|
||||||
"is_active",
|
"is_active",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -31,21 +31,21 @@ class BabyBuddyUserForm(forms.ModelForm):
|
||||||
user = kwargs["instance"]
|
user = kwargs["instance"]
|
||||||
if user:
|
if user:
|
||||||
kwargs["initial"].update(
|
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)
|
super(BabyBuddyUserForm, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
def save(self, commit=True):
|
def save(self, commit=True):
|
||||||
user = super(BabyBuddyUserForm, self).save(commit=False)
|
user = super(BabyBuddyUserForm, self).save(commit=False)
|
||||||
is_readonly = self.cleaned_data["is_readonly"]
|
is_read_only = self.cleaned_data["is_read_only"]
|
||||||
if is_readonly:
|
if is_read_only:
|
||||||
user.is_superuser = False
|
user.is_superuser = False
|
||||||
else:
|
else:
|
||||||
user.is_superuser = True
|
user.is_superuser = True
|
||||||
if commit:
|
if commit:
|
||||||
user.save()
|
user.save()
|
||||||
readonly_group = Group.objects.get(name="read_only")
|
readonly_group = Group.objects.get(name="read_only")
|
||||||
if is_readonly:
|
if is_read_only:
|
||||||
user.groups.add(readonly_group.id)
|
user.groups.add(readonly_group.id)
|
||||||
else:
|
else:
|
||||||
user.groups.remove(readonly_group.id)
|
user.groups.remove(readonly_group.id)
|
||||||
|
|
|
@ -31,8 +31,8 @@
|
||||||
<td>{{ object.first_name }}</td>
|
<td>{{ object.first_name }}</td>
|
||||||
<td>{{ object.last_name }}</td>
|
<td>{{ object.last_name }}</td>
|
||||||
<td>{{ object.email }}</td>
|
<td>{{ object.email }}</td>
|
||||||
{% user_is_read_only object as is_readonly %}
|
{% user_is_read_only object as is_read_only %}
|
||||||
<td>{{ is_readonly|bool_icon }}</td>
|
<td>{{ is_read_only|bool_icon }}</td>
|
||||||
<td>{{ object.is_staff|bool_icon }}</td>
|
<td>{{ object.is_staff|bool_icon }}</td>
|
||||||
<td>{{ object.is_active|bool_icon }}</td>
|
<td>{{ object.is_active|bool_icon }}</td>
|
||||||
<td class="text-center">
|
<td class="text-center">
|
||||||
|
|
|
@ -30,32 +30,40 @@ class CommandsTestCase(TransactionTestCase):
|
||||||
def test_createuser(self):
|
def test_createuser(self):
|
||||||
call_command(
|
call_command(
|
||||||
"createuser",
|
"createuser",
|
||||||
username="test",
|
username="regularuser",
|
||||||
email="test@test.test",
|
email="regularuser@test.test",
|
||||||
password="test",
|
password="test",
|
||||||
verbosity=0,
|
verbosity=0,
|
||||||
)
|
)
|
||||||
self.assertIsInstance(
|
user = get_user_model().objects.get(username="regularuser")
|
||||||
get_user_model().objects.get(username="test"), get_user_model()
|
self.assertIsInstance(user, get_user_model())
|
||||||
)
|
self.assertTrue(user.is_superuser)
|
||||||
self.assertFalse(
|
self.assertFalse(user.is_staff)
|
||||||
get_user_model().objects.filter(
|
self.assertEqual(user.email, "regularuser@test.test")
|
||||||
username="test", is_staff=True, is_superuser=True
|
|
||||||
)
|
|
||||||
)
|
|
||||||
call_command(
|
call_command(
|
||||||
"createuser",
|
"createuser",
|
||||||
"--is-staff",
|
"--is-staff",
|
||||||
username="testadmin",
|
username="staffuser",
|
||||||
email="testadmin@testadmin.testadmin",
|
email="staffuser@test.test",
|
||||||
password="test",
|
password="test",
|
||||||
verbosity=0,
|
verbosity=0,
|
||||||
)
|
)
|
||||||
self.assertIsInstance(
|
user = get_user_model().objects.get(username="staffuser")
|
||||||
get_user_model().objects.get(username="testadmin"), get_user_model()
|
self.assertIsInstance(user, get_user_model())
|
||||||
)
|
self.assertTrue(user.is_superuser)
|
||||||
self.assertTrue(
|
self.assertTrue(user.is_staff)
|
||||||
get_user_model().objects.filter(
|
|
||||||
username="testadmin", is_staff=True, is_superuser=True
|
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
|
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 = {
|
cls.settings_template = {
|
||||||
"first_name": "User",
|
"first_name": "User",
|
||||||
"last_name": "Name",
|
"last_name": "Name",
|
||||||
|
@ -74,15 +85,7 @@ class FormsTestCase(TestCase):
|
||||||
self.user.is_staff = True
|
self.user.is_staff = True
|
||||||
self.user.save()
|
self.user.save()
|
||||||
self.c.login(**self.credentials)
|
self.c.login(**self.credentials)
|
||||||
|
params = self.user_template.copy()
|
||||||
params = {
|
|
||||||
"username": "username",
|
|
||||||
"first_name": "User",
|
|
||||||
"last_name": "Name",
|
|
||||||
"email": "user@user.user",
|
|
||||||
"password1": "d47o8dD&#hu3ulu3",
|
|
||||||
"password2": "d47o8dD&#hu3ulu3",
|
|
||||||
}
|
|
||||||
|
|
||||||
page = self.c.post("/users/add/", params)
|
page = self.c.post("/users/add/", params)
|
||||||
self.assertEqual(page.status_code, 302)
|
self.assertEqual(page.status_code, 302)
|
||||||
|
@ -101,6 +104,53 @@ class FormsTestCase(TestCase):
|
||||||
get_user_model().objects.filter(username="username"), []
|
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):
|
def test_user_settings(self):
|
||||||
self.c.login(**self.credentials)
|
self.c.login(**self.credentials)
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- 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.test import TestCase
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
|
@ -17,3 +19,13 @@ class TemplateTagsTestCase(TestCase):
|
||||||
first_name="Test", last_name="Child 2", birth_date=timezone.localdate()
|
first_name="Test", last_name="Child 2", birth_date=timezone.localdate()
|
||||||
)
|
)
|
||||||
self.assertEqual(babybuddy_tags.get_child_count(), 2)
|
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