15
15
class ProjectTests(TestCase):
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
19
Make sure .all_objects is the default manager.
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.
22
25
self.assertEquals(Project.all_objects, Project._default_manager)
24
27
def test_is_private_for_public_project(self):
29
Check that Project.is_private returns False for public projects.
25
31
project = factory.make_project(is_private=False)
26
32
self.assertEqual(False, project.is_private)
28
34
def test_is_private_for_private_project(self):
36
Check that Project.is_private returns True for private projects.
29
38
private_project = factory.make_project(is_private=True)
30
39
self.assertEqual(True, private_project.is_private)
32
41
def test_is_visible_to_anyone_if_public(self):
43
Check that public projects are visible to anyone.
33
45
project = factory.make_project(is_private=False)
34
46
self.assertTrue(project.is_visible_to(AnonymousUser()))
36
48
def test_is_not_visible_to_anyone_if_private(self):
50
Check that private projects are not visible to anonymous users.
37
52
project = factory.make_project(is_private=True)
38
53
self.assertFalse(project.is_visible_to(AnonymousUser()))
40
55
def test_is_visible_to_owner_if_private(self):
57
Check that private projects are visible to their owners.
41
59
project = factory.make_project(is_private=True)
42
60
self.assertTrue(project.is_visible_to(project.owner))
44
62
def test_is_visible_to_member_of_access_group_if_private(self):
64
Check that private projects are visible to members of their access
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))
50
def test_default_model_manager_only_returns_public_projects(self):
72
def test_dot_objects_model_manager_only_returns_public_projects(self):
74
Check that the .objects model manager return only public projects.
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()))
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.
82
Check that Project.all_objects includes private objects.
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.
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()))
64
93
def test_accessible_by_user_with_anonymous_user(self):
95
Check that accessible_by_user() can be given an anonymous user as
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)
106
147
raise NotImplementedError()
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.
151
Make sure .all_objects is the default manager.
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.
113
157
self.assertEquals(self.model.all_objects, self.model._default_manager)
115
159
def test_is_private_for_public_object(self):
161
Check that .is_private returns False for public objects.
116
163
public_object = self.factoryMethod(
117
164
factory.make_project(is_private=False))
118
165
self.assertFalse(public_object.is_private)
120
167
def test_is_private_for_private_object(self):
169
Check that .is_private returns True for private objects.
121
171
private_object = self.factoryMethod(
122
172
factory.make_project(is_private=True))
123
173
self.assertTrue(private_object.is_private)
125
175
def test_is_visible_to_anyone_if_public(self):
177
Check that public objects are visible to anyone.
126
179
public_object = self.factoryMethod(
127
180
factory.make_project(is_private=False))
128
181
self.assertTrue(public_object.is_visible_to(AnonymousUser()))
130
183
def test_is_not_visible_to_anyone_if_private(self):
185
Check that private objects are not visible to anonymous users.
131
187
private_object = self.factoryMethod(
132
188
factory.make_project(is_private=True))
133
189
self.assertFalse(private_object.is_visible_to(AnonymousUser()))
135
191
def test_is_visible_to_owner_if_private(self):
193
Check that private objects are visible to their project's owners.
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
142
201
self.assertTrue(private_object.is_visible_to(owner))
144
203
def test_is_visible_to_member_of_access_group_if_private(self):
205
Check that private objects are visible to members of their project's
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))
151
def test_default_model_manager_only_returns_public_objects(self):
214
def test_dot_objects_model_manager_only_returns_public_objects(self):
216
Check that the .objects model manager return only public objects.
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()))
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.
226
Check that .all_objects includes private objects.
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.
163
233
public_object = self.factoryMethod(
164
234
project=factory.make_project(is_private=False))
165
235
private_object = self.factoryMethod(