test_management.py 7.13 KB
Newer Older
1
2
3
4
5
6
7
8
from django.test import TestCase, override_settings
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.core.management import call_command
from django.utils.six import StringIO

from perfieldperms.models import PerFieldPermission, PFPContentType
from perfieldperms.utils import list_fields_for_ctype
Lincoln Smith's avatar
Lincoln Smith committed
9
from tests import helpers
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34


@override_settings(PFP_IGNORE_PERMS={})
class PFPMakepermsTest(TestCase):

    def test_adds_perms(self):
        """
        Adds appropriate number of permissions for allowed types. Takes
        settings from database.
        """
        ctype = ContentType.objects.get(app_label='auth', model='user')
        PFPContentType.objects.create(content_type=ctype)
        fields = list_fields_for_ctype(ctype)
        num_pfps = len(fields) * 2
        out = StringIO()
        call_command('pfp-makeperms', stdout=out)
        pfps = PerFieldPermission.objects.count()
        self.assertEqual(pfps, num_pfps)

    @override_settings(DEBUG=True)
    def test_adds_perms_output(self):
        """Adds PFPs with correct field values."""
        ctype = ContentType.objects.get(app_label='auth', model='user')
        PFPContentType(content_type=ctype).save()
        out = StringIO()
35
        call_command('pfp-makeperms', stdout=out, verbosity=2)
36
        pfps = PerFieldPermission.objects.all()
37
        self.assertIn('Permissions added: {}'.format(len(pfps)), out.getvalue())
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
        for pfp in pfps:
            with self.subTest(pfp=pfp):
                self.assertIn(
                        'Added new permission {} for field {}.{} - {}'.format(
                                pfp.codename,
                                pfp.content_type.app_label,
                                pfp.content_type.model,
                                pfp.field_name,
                                ),
                        out.getvalue()
                        )

    @override_settings(PFP_MODELS=[('auth', 'user')])
    def test_add_perms_settings(self):
        """Correctly adds PFPs if configured via settings."""
        ctype = ContentType.objects.get(app_label='auth', model='user')
        fields = list_fields_for_ctype(ctype)
        num_pfps = len(fields) * 2
        out = StringIO()
        call_command('pfp-makeperms', stdout=out)
        pfps = PerFieldPermission.objects.count()
        self.assertEqual(pfps, num_pfps)

    @override_settings(PFP_MODELS=[('auth', 'user')])
    def test_add_perms_merge(self):
        """Correctly merges configuration from settings and database."""
        ctype = ContentType.objects.get(app_label='auth', model='group')
        PFPContentType.objects.create(content_type=ctype)
        fields = list_fields_for_ctype(ctype)
        ctype = ContentType.objects.get(app_label='auth', model='user')
        fields += list_fields_for_ctype(ctype)
        num_pfps = len(fields) * 2
        out = StringIO()
        call_command('pfp-makeperms', stdout=out)
        pfps = PerFieldPermission.objects.count()
        self.assertEqual(pfps, num_pfps)

    @override_settings(PFP_IGNORE_DELETE=False)
    def test_add_perms_ignore_delete(self):
        """Adds delete PFPs if configured to via settings."""
        ctype = ContentType.objects.get(app_label='auth', model='user')
        PFPContentType.objects.create(content_type=ctype)
        fields = list_fields_for_ctype(ctype)
        num_pfps = len(fields) * 3
        out = StringIO()
        call_command('pfp-makeperms', stdout=out)
        pfps = PerFieldPermission.objects.count()
        self.assertEqual(pfps, num_pfps)

    @override_settings(PFP_IGNORE_PERMS={
            'auth': {
                'user': ['add_user']
                }
            })
    def test_add_perms_ignore_perms(self):
        """Doesn't add PFPs for model permissions configured to ignore."""
        ctype = ContentType.objects.get(app_label='auth', model='user')
        PFPContentType.objects.create(content_type=ctype)
        fields = list_fields_for_ctype(ctype)
        num_pfps = len(fields)
        out = StringIO()
        call_command('pfp-makeperms', stdout=out)
        pfps = PerFieldPermission.objects.count()
        self.assertEqual(pfps, num_pfps)

    @override_settings(DEBUG=True)
    def test_add_perms_delete_perm(self):
        """Deletes permissions for removed fields."""
106
        helpers.create_pfp('auth', 'user', 'change_user', 'foo')
107
108
109
110
111
        ctype = ContentType.objects.get(app_label='auth', model='user')
        PFPContentType.objects.create(content_type=ctype)
        fields = list_fields_for_ctype(ctype)
        num_pfps = len(fields) * 2
        out = StringIO()
112
        call_command('pfp-makeperms', stdout=out, verbosity=2)
113
114
115
116
        pfps = PerFieldPermission.objects.count()
        self.assertEqual(pfps, num_pfps)
        self.assertIn('Permissions deleted: 1', out.getvalue())
        self.assertIn(
117
                'Deleted permission change_user__foo for removed field auth.user - foo',
118
119
120
121
122
123
                out.getvalue()
                )

    @override_settings(DEBUG=True)
    def test_add_perms_skip_existing(self):
        """Doesn't try to duplicate already existing PFPs."""
124
        helpers.create_pfp('auth', 'user', 'change_user', 'is_staff')
125
126
127
128
129
        ctype = ContentType.objects.get(app_label='auth', model='user')
        PFPContentType.objects.create(content_type=ctype)
        fields = list_fields_for_ctype(ctype)
        num_created = len(fields) * 2 - 1
        out = StringIO()
130
131
        call_command('pfp-makeperms', stdout=out, verbosity=2)
        self.assertIn('Permissions added: {}'.format(num_created), out.getvalue())
132
133
134
135
136
137
138
139
        self.assertIn('Existing permissions skipped: 1', out.getvalue())

    def test_existing_perms_removed(self):
        """Removed previously create permissions if configuration changed."""
        ctype = ContentType.objects.get(app_label='auth', model='user')
        PFPContentType.objects.create(content_type=ctype)
        out = StringIO()
        call_command('pfp-makeperms', stdout=out)
140
        with self.settings(PFP_IGNORE_PERMS={'auth': {'user': ['add_user']}}):
141
            out = StringIO()
142
            call_command('pfp-makeperms', stdout=out, verbosity=2)
143
144
145
146
147

        fields = list_fields_for_ctype(ctype)
        num_pfps = len(fields)
        pfps = PerFieldPermission.objects.count()
        self.assertEqual(pfps, num_pfps)
148
        self.assertIn('Permissions deleted: {}'.format(len(fields)), out.getvalue())
149
150
151

    def test_lots_of_skipping(self):
        """
152
        Skip over multiple sets of PFPs to make sure loop variables are being reset properly.
153
154
155
156
157
158
159
        """
        ctype = ContentType.objects.get(app_label='auth', model='user')
        PFPContentType.objects.create(content_type=ctype)
        call_command('pfp-makeperms')
        Permission.objects.create(content_type=ctype, codename='foo_user')
        fields = list_fields_for_ctype(ctype)
        out = StringIO()
160
        call_command('pfp-makeperms', stdout=out, verbosity=2)
161
162
        pfps = PerFieldPermission.objects.count()
        self.assertEqual(pfps, len(fields) * 3)
163
164
        self.assertIn('Existing permissions skipped: {}'.format(len(fields) * 2), out.getvalue())
        self.assertIn('Permissions added: {}'.format(len(fields)), out.getvalue())