test_forms.py 6.88 KB
Newer Older
1
from django.contrib.auth.models import Group, Permission, User
2
from django.contrib.contenttypes.models import ContentType
3
4
from django.core.management import call_command
from django.forms.models import modelform_factory
5
6
from django.test import TestCase, override_settings

7
8
from perfieldperms.forms import PFPModelForm, PFPPermFilterForm, PFPRoleFilterForm
from perfieldperms.models import PFPFilterType, PFPRoleType
9
from perfieldperms.utils import get_non_pfp_perms, get_unpermitted_fields
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from . import helpers
from .models import Pizza


@override_settings(
        AUTHENTICATION_BACKENDS=['perfieldperms.backends.PFPBackend'],
        PFP_MODELS=[('tests', 'pizza')],
        PFP_IGNORE_PERMS={}
        )
class PFPModelFormTest(TestCase):

    @classmethod
    def setUpTestData(cls):
        call_command('pfp-makeperms')

    def setUp(self):
26
        pass
27
28
29
30
31
32

    def test_configure_form(self):
        """Test setting user/disable_attributes of form."""
        # Set attributes on class
        PizzaForm = modelform_factory(Pizza, form=PFPModelForm, fields='__all__')
        PizzaForm.disable_fields = False
33
        PizzaForm.disabled_fields = ['name']
34
        form = PizzaForm()
35
        self.assertEqual(['name'], form.disabled_fields)
36
37
38
39
        self.assertFalse(form.disable_fields)
        # Defaults get set
        PizzaForm = modelform_factory(Pizza, form=PFPModelForm, fields='__all__')
        form = PizzaForm()
40
        self.assertFalse(form.disabled_fields)
41
42
        self.assertTrue(form.disable_fields)
        # Set via args
43
44
        form = PizzaForm(disabled_fields=['name'], disable_fields=False)
        self.assertEqual(['name'], form.disabled_fields)
45
        self.assertFalse(form.disable_fields)
46
        # Set via passing in user merged with other config methods
47
48
49
50
        user1 = User.objects.create_user(username='user1', password='sekret')
        pfp = helpers.get_perm('tests', 'pizza', 'add_pizza__name')
        user1.user_permissions.add(pfp)
        form = PizzaForm(disabled_fields=['name'], user=user1)
51
        self.assertSetEqual({'name', 'price', 'toppings', 'bases'}, set(form.disabled_fields))
52
        self.assertTrue(form.disable_fields)
53

54
    def test_modify_fields(self):
55
56
        """Form fields are modified when appropriate."""
        PizzaForm = modelform_factory(Pizza, form=PFPModelForm, fields='__all__')
57
        user1 = User.objects.create_user(username='user1', password='supersekret')
58
59
60
        # Test disable fields
        pfp1 = helpers.get_perm('tests', 'pizza', 'add_pizza__name')
        pfp2 = helpers.get_perm('tests', 'pizza', 'add_pizza__price')
61
        user1.user_permissions.add(pfp1, pfp2)
62
        disabled_fields = get_unpermitted_fields(Pizza, user1)
63
        form = PizzaForm(disabled_fields=disabled_fields)
64
65
66
67
68
        self.assertFalse(form.fields['name'].disabled)
        self.assertFalse(form.fields['price'].disabled)
        self.assertTrue(form.fields['toppings'].disabled)
        self.assertTrue(form.fields['bases'].disabled)
        # Test delete fields
69
        form = PizzaForm(disabled_fields=disabled_fields, disable_fields=False)
70
71
72
73
        self.assertIn('name', form.fields)
        self.assertIn('price', form.fields)
        self.assertNotIn('toppings', form.fields)
        self.assertNotIn('bases', form.fields)
74
75
76
77
        # Form without all fields.
        PizzaForm = modelform_factory(Pizza, form=PFPModelForm, exclude=['toppings'])
        form = PizzaForm(disabled_fields=disabled_fields)
        self.assertNotIn('toppings', form.fields)
78
79
        self.assertFalse(form.fields['name'].disabled)
        self.assertFalse(form.fields['price'].disabled)
80
        self.assertTrue(form.fields['bases'].disabled)
81
82
83
84
85
86
87


class PFPRoleFilterFormTest(TestCase):

    def test_user_model(self):
        """Sets current user model if nothing else configured."""
        user_ctype = ContentType.objects.get_for_model(User)
88
        form = PFPRoleFilterForm()
89
        self.assertSetEqual(
90
                {user_ctype},
91
92
93
94
95
96
97
98
99
100
101
102
                set(form.fields['role_type'].queryset),
                )

    def test_added_roles(self):
        """Additional configured role types are added to queryset."""
        roles = []
        ctype = ContentType.objects.get_for_model(User)
        roles.append(ctype.id)
        PFPRoleType.objects.create(role_ctype=ctype)
        ctype = ContentType.objects.get_for_model(Group)
        roles.append(ctype.id)
        PFPRoleType.objects.create(role_ctype=ctype)
103
        form = PFPRoleFilterForm()
104
105
106
107
108
109
110
111
112
113
114
115
116
        self.assertSetEqual(
                set(ContentType.objects.filter(id__in=roles)),
                set(form.fields['role_type'].queryset),
                )
        self.assertEqual(
                len(ContentType.objects.filter(id__in=roles)),
                len(form.fields['role_type'].queryset),
                )


class PFPPermFilterFormTest(TestCase):
    def test_all_models(self):
        """All models are returned if no restrictions configured."""
117
        form = PFPPermFilterForm()
118
119
120
121
122
123
124
125
126
        self.assertSetEqual(
                set(ContentType.objects.all()),
                set(form.fields['model'].queryset)
                )

    def test_filtered_models(self):
        """Only filtered models returned if any are configured."""
        user_ctype = ContentType.objects.get_for_model(User)
        PFPFilterType.objects.create(filter_ctype=user_ctype)
127
        form = PFPPermFilterForm()
128
129
130
131
132
133
134
135
136
        self.assertEqual(
                set(ContentType.objects.filter(id=user_ctype.id)),
                set(form.fields['model'].queryset)
                )

    def test_initial_set(self):
        """Correct permission queryset initialised."""
        user_ctype = ContentType.objects.get_for_model(User)
        initial = {'model': user_ctype}
137
        form = PFPPermFilterForm(initial=initial)
138
139
140
141
142
143
144
145
146
147
148
149
150
        self.assertSetEqual(
                set(get_non_pfp_perms(user_ctype)),
                set(form.fields['permissions'].queryset)
                )

    def test_initial_set_show_pfps_false(self):
        """
        PFPs are NOT in the permissions queryset if not in selected filter
        settings.
        """
        user_ctype = ContentType.objects.get_for_model(User)
        helpers.create_pfp('auth', 'user', 'add_user', 'email')
        initial = {'model': user_ctype, 'show_pfps': False}
151
        form = PFPPermFilterForm(initial=initial)
152
153
154
155
156
157
158
159
160
161
        self.assertSetEqual(
                set(get_non_pfp_perms(user_ctype)),
                set(form.fields['permissions'].queryset)
                )

    def test_initial_set_with_pfps(self):
        """PFPs are in the permissions queryset if selected in the filter."""
        user_ctype = ContentType.objects.get_for_model(User)
        helpers.create_pfp('auth', 'user', 'add_user', 'email')
        initial = {'model': user_ctype, 'show_pfps': True}
162
        form = PFPPermFilterForm(initial=initial)
163
164
165
166
        self.assertSetEqual(
                set(Permission.objects.filter(content_type=user_ctype)),
                set(form.fields['permissions'].queryset)
                )