Skip to content

Commit

Permalink
Test against django-modelcluster
Browse files Browse the repository at this point in the history
  • Loading branch information
gasman committed Feb 28, 2022
1 parent b72c726 commit 32dc317
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 5 deletions.
4 changes: 3 additions & 1 deletion setup.py
Expand Up @@ -19,7 +19,9 @@
install_requires=[
],
extras_require={
'testing': [],
'testing': [
'django-modelcluster',
],
},
classifiers=[
'Development Status :: 4 - Beta',
Expand Down
27 changes: 25 additions & 2 deletions tests/forms.py
@@ -1,7 +1,8 @@
from django import forms
from permissionedforms import PermissionedForm, PermissionedModelForm
from modelcluster.forms import ClusterForm, ClusterFormMetaclass, ClusterFormOptions
from permissionedforms import PermissionedForm, PermissionedFormMetaclass, PermissionedFormOptionsMixin, PermissionedModelForm

from .models import Country
from .models import Country, Page


class PersonForm(PermissionedForm):
Expand All @@ -21,3 +22,25 @@ class Meta:
field_permissions = {
'description': 'tests.change_country_description'
}


class PermissionedClusterFormOptions(PermissionedFormOptionsMixin, ClusterFormOptions):
pass


class PermissionedClusterFormMetaclass(PermissionedFormMetaclass, ClusterFormMetaclass):
options_class = PermissionedClusterFormOptions


class PermissionedClusterForm(PermissionedForm, ClusterForm, metaclass=PermissionedClusterFormMetaclass):
pass


class PageForm(PermissionedClusterForm):
class Meta:
model = Page
fields = ['title', 'body']
formsets = ['tags']
field_permissions = {
'title': 'tests.change_page_title'
}
17 changes: 17 additions & 0 deletions tests/models.py
@@ -1,4 +1,6 @@
from django.db import models
from modelcluster.fields import ParentalKey
from modelcluster.models import ClusterableModel


class Country(models.Model):
Expand All @@ -9,3 +11,18 @@ class Meta:
permissions = [
("change_country_description", "Can change country descriptions")
]


class Page(ClusterableModel):
title = models.CharField(max_length=255)
body = models.TextField(blank=True)

class Meta:
permissions = [
("change_page_title", "Can change page titles")
]


class PageTag(models.Model):
page = ParentalKey(Page, related_name='tags')
tag = models.CharField(max_length=255)
71 changes: 69 additions & 2 deletions tests/tests.py
@@ -1,8 +1,8 @@
from django.contrib.auth.models import Permission, User
from django.test import TestCase

from .forms import CountryForm, PersonForm
from .models import Country
from .forms import CountryForm, PageForm, PersonForm
from .models import Country, Page


class PermissionedFormTest(TestCase):
Expand Down Expand Up @@ -156,3 +156,70 @@ def test_bound_form_for_normal_user_without_permission(self):
form.save()
self.assertEqual(self.country.name, 'Sweden')
self.assertEqual(self.country.description, 'A lovely country with a blue and yellow flag')


class PermissionedClusterFormTest(TestCase):
def setUp(self):
self.page = Page.objects.create(title="Sheep", body="And did those sheep in ancient time")
self.page_data = {
'title': 'Teeth',
'body': 'And did those teeth in ancient time',
'tags-TOTAL_FORMS': '1',
'tags-INITIAL_FORMS': '0',
'tags-MIN_NUM_FORMS': '0',
'tags-MAX_NUM_FORMS': '1000',
'tags-0-tag': 'Dentistry',
'tags-0-id': '',
}

def test_unbound_form_without_user(self):
form = PageForm(instance=self.page)
self.assertIn('title', form.fields)
self.assertIn('body', form.fields)
self.assertIn('tags', form.formsets)

def test_bound_form_without_user(self):
form = PageForm(self.page_data, instance=self.page)
self.assertIn('title', form.fields)
self.assertIn('body', form.fields)
self.assertTrue(form.is_valid())
form.save()
self.assertEqual(self.page.title, 'Teeth')
self.assertEqual(self.page.body, 'And did those teeth in ancient time')
self.assertEqual(self.page.tags.first().tag, 'Dentistry')

def test_unbound_form_for_superuser(self):
superuser = User.objects.create_superuser('admin', 'admin@example.com', 'password')
form = PageForm(instance=self.page, for_user=superuser)
self.assertIn('title', form.fields)
self.assertIn('body', form.fields)
self.assertIn('tags', form.formsets)

def test_bound_form_for_superuser(self):
superuser = User.objects.create_superuser('admin', 'admin@example.com', 'password')
form = PageForm(self.page_data, instance=self.page, for_user=superuser)
self.assertIn('title', form.fields)
self.assertIn('body', form.fields)
self.assertTrue(form.is_valid())
form.save()
self.assertEqual(self.page.title, 'Teeth')
self.assertEqual(self.page.body, 'And did those teeth in ancient time')
self.assertEqual(self.page.tags.first().tag, 'Dentistry')

def test_unbound_form_for_normal_user_without_permission(self):
bob = User.objects.create_user('bob', 'bob@example.com', 'password')
form = PageForm(instance=self.page, for_user=bob)
self.assertNotIn('title', form.fields)
self.assertIn('body', form.fields)
self.assertIn('tags', form.formsets)

def test_bound_form_for_normal_user_without_permission(self):
bob = User.objects.create_user('bob', 'bob@example.com', 'password')
form = PageForm(self.page_data, instance=self.page, for_user=bob)
self.assertNotIn('title', form.fields)
self.assertIn('body', form.fields)
self.assertTrue(form.is_valid())
form.save()
self.assertEqual(self.page.title, 'Sheep')
self.assertEqual(self.page.body, 'And did those teeth in ancient time')
self.assertEqual(self.page.tags.first().tag, 'Dentistry')

0 comments on commit 32dc317

Please sign in to comment.