~salgado/offspring/private-projects

« back to all changes in this revision

Viewing changes to lib/offspring/web/queuemanager/tests/test_models.py

  • Committer: Guilherme Salgado
  • Date: 2011-12-01 15:09:51 UTC
  • Revision ID: salgado@canonical.com-20111201150951-i9d2dfw4xjd5gtm7
Lots of docstrings for tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
class ProjectTests(TestCase):
16
16
 
17
17
    def test_default_manager(self):
18
 
        # Make sure .all_objects is the default manager or else the admin UI
19
 
        # will exclude all private objects and we would break model validation
20
 
        # of unique fields if the user reuses a value from an object they're
21
 
        # not allowed to see.
 
18
        """
 
19
        Make sure .all_objects is the default manager.
 
20
        
 
21
        If that's not the case the admin UI will exclude all private objects
 
22
        and we would break model validation of unique fields if the user
 
23
        reuses a value from an object they're not allowed to see.
 
24
        """
22
25
        self.assertEquals(Project.all_objects, Project._default_manager)
23
26
 
24
27
    def test_is_private_for_public_project(self):
 
28
        """
 
29
        Check that Project.is_private returns False for public projects.
 
30
        """
25
31
        project = factory.make_project(is_private=False)
26
32
        self.assertEqual(False, project.is_private)
27
33
 
28
34
    def test_is_private_for_private_project(self):
 
35
        """
 
36
        Check that Project.is_private returns True for private projects.
 
37
        """
29
38
        private_project = factory.make_project(is_private=True)
30
39
        self.assertEqual(True, private_project.is_private)
31
40
 
32
41
    def test_is_visible_to_anyone_if_public(self):
 
42
        """
 
43
        Check that public projects are visible to anyone.
 
44
        """
33
45
        project = factory.make_project(is_private=False)
34
46
        self.assertTrue(project.is_visible_to(AnonymousUser()))
35
47
 
36
48
    def test_is_not_visible_to_anyone_if_private(self):
 
49
        """
 
50
        Check that private projects are not visible to anonymous users.
 
51
        """
37
52
        project = factory.make_project(is_private=True)
38
53
        self.assertFalse(project.is_visible_to(AnonymousUser()))
39
54
 
40
55
    def test_is_visible_to_owner_if_private(self):
 
56
        """
 
57
        Check that private projects are visible to their owners.
 
58
        """
41
59
        project = factory.make_project(is_private=True)
42
60
        self.assertTrue(project.is_visible_to(project.owner))
43
61
 
44
62
    def test_is_visible_to_member_of_access_group_if_private(self):
 
63
        """
 
64
        Check that private projects are visible to members of their access
 
65
        groups.
 
66
        """
45
67
        user = factory.make_user()
46
68
        group = factory.make_access_group([user])
47
69
        project = factory.make_project(is_private=True, access_groups=[group])
48
70
        self.assertTrue(project.is_visible_to(user))
49
71
 
50
 
    def test_default_model_manager_only_returns_public_projects(self):
 
72
    def test_dot_objects_model_manager_only_returns_public_projects(self):
 
73
        """
 
74
        Check that the .objects model manager return only public projects.
 
75
        """
51
76
        project = factory.make_project(is_private=False)
52
77
        project2 = factory.make_project(is_private=True)
53
78
        self.assertEqual([project], list(Project.objects.all()))
54
79
 
55
80
    def test_all_objects_model_manager(self):
56
 
        # Project.all_objects() is a separate model manager which returns
57
 
        # private objects as well. Its .all() method will return public and
58
 
        # private objects regardless of whether or not the current user has
59
 
        # access to them. It is to be used with caution.
 
81
        """
 
82
        Check that Project.all_objects includes private objects.
 
83
 
 
84
        Project.all_objects() is a separate model manager which returns
 
85
        private objects as well. Its .all() method will return public and
 
86
        private objects regardless of whether or not the current user has
 
87
        access to them. It is to be used with caution.
 
88
        """
60
89
        project = factory.make_project(is_private=False)
61
90
        project2 = factory.make_project(is_private=True)
62
91
        self.assertEqual([project, project2], list(Project.all_objects.all()))
63
92
 
64
93
    def test_accessible_by_user_with_anonymous_user(self):
 
94
        """
 
95
        Check that accessible_by_user() can be given an anonymous user as
 
96
        argument.
 
97
        """
65
98
        project = factory.make_project(is_private=True)
66
99
        project2 = factory.make_project(is_private=False)
67
100
        self.assertEqual(True, project.is_private)
70
103
        self.assertEqual([project2], list(objects))
71
104
 
72
105
    def test_accessible_by_user_with_public_project(self):
 
106
        """
 
107
        Check that accessible_by_user() returns public projects regardless of
 
108
        the given user.
 
109
        """
73
110
        user = factory.make_user()
74
111
        project = factory.make_project(is_private=False)
75
112
        self.assertEqual(False, project.is_private)
77
114
        self.assertEqual([project], list(objects))
78
115
 
79
116
    def test_accessible_by_user_with_private_project(self):
 
117
        """
 
118
        Check that accessible_by_user() returns only the private projects the
 
119
        given user is allowed to see.
 
120
        """
80
121
        user = factory.make_user()
81
122
        group = factory.make_access_group([user])
82
123
        project = factory.make_project(is_private=True, access_groups=[group])
106
147
        raise NotImplementedError()
107
148
 
108
149
    def test_default_manager(self):
109
 
        # Make sure .all_objects is the default manager or else the admin UI
110
 
        # will exclude all private objects and we would break model validation
111
 
        # of unique fields if the user reuses a value from an object they're
112
 
        # not allowed to see.
 
150
        """
 
151
        Make sure .all_objects is the default manager.
 
152
        
 
153
        If that's not the case the admin UI will exclude all private objects
 
154
        and we would break model validation of unique fields if the user
 
155
        reuses a value from an object they're not allowed to see.
 
156
        """
113
157
        self.assertEquals(self.model.all_objects, self.model._default_manager)
114
158
 
115
159
    def test_is_private_for_public_object(self):
 
160
        """
 
161
        Check that .is_private returns False for public objects.
 
162
        """
116
163
        public_object = self.factoryMethod(
117
164
            factory.make_project(is_private=False))
118
165
        self.assertFalse(public_object.is_private)
119
166
 
120
167
    def test_is_private_for_private_object(self):
 
168
        """
 
169
        Check that .is_private returns True for private objects.
 
170
        """
121
171
        private_object = self.factoryMethod(
122
172
            factory.make_project(is_private=True))
123
173
        self.assertTrue(private_object.is_private)
124
174
 
125
175
    def test_is_visible_to_anyone_if_public(self):
 
176
        """
 
177
        Check that public objects are visible to anyone.
 
178
        """
126
179
        public_object = self.factoryMethod(
127
180
            factory.make_project(is_private=False))
128
181
        self.assertTrue(public_object.is_visible_to(AnonymousUser()))
129
182
 
130
183
    def test_is_not_visible_to_anyone_if_private(self):
 
184
        """
 
185
        Check that private objects are not visible to anonymous users.
 
186
        """
131
187
        private_object = self.factoryMethod(
132
188
            factory.make_project(is_private=True))
133
189
        self.assertFalse(private_object.is_visible_to(AnonymousUser()))
134
190
 
135
191
    def test_is_visible_to_owner_if_private(self):
 
192
        """
 
193
        Check that private objects are visible to their project's owners.
 
194
        """
136
195
        # The object we're dealing with inherits the access-control rules from
137
196
        # the project it's related to, so the owner that matters is the
138
197
        # project owner.
142
201
        self.assertTrue(private_object.is_visible_to(owner))
143
202
 
144
203
    def test_is_visible_to_member_of_access_group_if_private(self):
 
204
        """
 
205
        Check that private objects are visible to members of their project's
 
206
        access groups.
 
207
        """
145
208
        user = factory.make_user()
146
209
        group = factory.make_access_group([user])
147
210
        private_object = self.factoryMethod(
148
211
            factory.make_project(is_private=True, access_groups=[group]))
149
212
        self.assertTrue(private_object.is_visible_to(user))
150
213
 
151
 
    def test_default_model_manager_only_returns_public_objects(self):
 
214
    def test_dot_objects_model_manager_only_returns_public_objects(self):
 
215
        """
 
216
        Check that the .objects model manager return only public objects.
 
217
        """
152
218
        private_object = self.factoryMethod(
153
219
            factory.make_project(is_private=True))
154
220
        public_object = self.factoryMethod(
156
222
        self.assertEqual([public_object], list(self.model.objects.all()))
157
223
 
158
224
    def test_all_objects_model_manager(self):
159
 
        # self.model.all_objects() is a separate model manager which returns
160
 
        # private objects as well. Its .all() method will return public and
161
 
        # private objects regardless of whether or not the current user has
162
 
        # access to them. It is to be used with caution.
 
225
        """
 
226
        Check that .all_objects includes private objects.
 
227
 
 
228
        self.model.all_objects() is a separate model manager which returns
 
229
        private objects as well. Its .all() method will return public and
 
230
        private objects regardless of whether or not the current user has
 
231
        access to them. It is to be used with caution.
 
232
        """
163
233
        public_object = self.factoryMethod(
164
234
            project=factory.make_project(is_private=False))
165
235
        private_object = self.factoryMethod(
168
238
                         list(self.model.all_objects.all()))
169
239
 
170
240
    def test_accessible_by_user(self):
 
241
        """
 
242
        Check that accessible_by_user() returns only the private objects the
 
243
        user is allowed to see.
 
244
        """
171
245
        user = factory.make_user()
172
246
        group = factory.make_access_group([user])
173
247
        visible_object = self.factoryMethod(
214
288
class ProjectGroupTests(TestCase):
215
289
 
216
290
    def test_get_projects_filters_private_objects(self):
 
291
        """
 
292
        ProjectGroup.get_projects() will return only the private projects the
 
293
        given user is allowed to see.
 
294
        """
217
295
        user = factory.make_user()
218
296
        group = factory.make_project_group()
219
297
        public_project = factory.make_project(