test_blogpost_auth.py
14.3 KB
1
2
3
4
5
6
7
8
9
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
35
36
37
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2015 SciFabric LTD.
#
# PyBossa is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBossa. If not, see <http://www.gnu.org/licenses/>.
from default import Test, assert_not_raises
from pybossa.auth import ensure_authorized_to
from nose.tools import assert_raises
from werkzeug.exceptions import Forbidden, Unauthorized
from mock import patch
from test_authorization import mock_current_user
from factories import ProjectFactory, BlogpostFactory, UserFactory
from pybossa.model.blogpost import Blogpost
class TestBlogpostAuthorization(Test):
mock_anonymous = mock_current_user()
mock_authenticated = mock_current_user(anonymous=False, admin=False, id=2)
mock_admin = mock_current_user(anonymous=False, admin=True, id=1)
@patch('pybossa.auth.current_user', new=mock_anonymous)
def test_anonymous_user_create_given_blogpost(self):
"""Test anonymous users cannot create a given blogpost"""
project = ProjectFactory.create()
blogpost = BlogpostFactory.build(project=project, owner=None)
assert_raises(Unauthorized, ensure_authorized_to, 'create', blogpost)
@patch('pybossa.auth.current_user', new=mock_anonymous)
def test_anonymous_user_create_blogposts_for_given_project(self):
"""Test anonymous users cannot create blogposts for a given project"""
project = ProjectFactory.create()
assert_raises(Unauthorized, ensure_authorized_to, 'create', Blogpost, project_id=project.id)
@patch('pybossa.auth.current_user', new=mock_anonymous)
def test_anonymous_user_create_blogposts(self):
"""Test anonymous users cannot create any blogposts"""
assert_raises(Unauthorized, ensure_authorized_to, 'create', Blogpost)
@patch('pybossa.auth.current_user', new=mock_admin)
def test_non_owner_authenticated_user_create_given_blogpost(self):
"""Test authenticated user cannot create a given blogpost if is not the
project owner, even if is admin"""
admin = UserFactory.create()
project = ProjectFactory.create()
blogpost = BlogpostFactory.build(project=project, owner=admin)
assert self.mock_admin.id != project.owner_id
assert_raises(Forbidden, ensure_authorized_to, 'create', blogpost)
@patch('pybossa.auth.current_user', new=mock_admin)
def test_non_owner_authenticated_user_create_blogpost_for_given_project(self):
"""Test authenticated user cannot create blogposts for a given project
if is not the project owner, even if is admin"""
owner = UserFactory.create_batch(2)[1]
project = ProjectFactory.create(owner=owner)
assert self.mock_admin.id != project.owner.id
assert_raises(Forbidden, ensure_authorized_to, 'create', Blogpost, project_id=project.id)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_owner_create_given_blogpost(self):
"""Test authenticated user can create a given blogpost if is project owner"""
owner = UserFactory.create_batch(2)[1]
project = ProjectFactory.create(owner=owner)
blogpost = BlogpostFactory.build(project=project, owner=owner)
assert self.mock_authenticated.id == project.owner_id
assert_not_raises(Exception, ensure_authorized_to, 'create', blogpost)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_owner_create_blogpost_for_given_project(self):
"""Test authenticated user can create blogposts for a given project
if is project owner"""
owner = UserFactory.create_batch(2)[1]
project = ProjectFactory.create(owner=owner)
assert self.mock_authenticated.id == project.owner.id
assert_not_raises(Exception, ensure_authorized_to, 'create', Blogpost, project_id=project.id)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_owner_create_blogpost_as_other_user(self):
"""Test authenticated user cannot create blogpost if is project owner but
sets another person as the author of the blogpost"""
another_user = UserFactory.create()
project = ProjectFactory.create()
blogpost = BlogpostFactory.build(project_id=project.id,
owner=another_user)
assert self.mock_authenticated.id == project.owner_id
assert_raises(Forbidden, ensure_authorized_to, 'create', blogpost)
@patch('pybossa.auth.current_user', new=mock_anonymous)
def test_anonymous_user_read_given_blogpost(self):
"""Test anonymous users can read a given blogpost"""
project = ProjectFactory.create(published=True)
blogpost = BlogpostFactory.create(project=project)
assert_not_raises(Exception, ensure_authorized_to, 'read', blogpost)
@patch('pybossa.auth.current_user', new=mock_anonymous)
def test_anonymous_user_read_blogposts_for_given_project(self):
"""Test anonymous users can read blogposts of a given project"""
project = ProjectFactory.create(published=True)
assert_not_raises(Exception, ensure_authorized_to, 'read', Blogpost, project_id=project.id)
@patch('pybossa.auth.current_user', new=mock_anonymous)
def test_anonymous_user_read_given_blogpost_draft_project(self):
"""Test anonymous users cannot read a given blogpost of a draft project"""
project = ProjectFactory.create(published=False)
blogpost = BlogpostFactory.create(project=project)
assert_raises(Unauthorized, ensure_authorized_to, 'read', blogpost)
@patch('pybossa.auth.current_user', new=mock_anonymous)
def test_anonymous_user_read_blogposts_for_given_draft_project(self):
"""Test anonymous users cannot read blogposts of a given project if is a draft"""
project = ProjectFactory.create(published=False)
assert_raises(Unauthorized, ensure_authorized_to, 'read', Blogpost, project_id=project.id)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_non_owner_authenticated_user_read_given_blogpost(self):
"""Test authenticated user can read a given blogpost if is not the project owner"""
project = ProjectFactory.create(published=True)
user = UserFactory.create()
blogpost = BlogpostFactory.create(project=project)
assert self.mock_authenticated.id != project.owner.id
assert_not_raises(Exception, ensure_authorized_to, 'read', blogpost)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_non_owner_authenticated_user_read_blogposts_for_given_project(self):
"""Test authenticated user can read blogposts of a given project if
is not the project owner"""
project = ProjectFactory.create(published=True)
user = UserFactory.create()
assert self.mock_authenticated.id != project.owner.id
assert_not_raises(Exception, ensure_authorized_to, 'read', Blogpost, project_id=project.id)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_non_owner_authenticated_user_read_given_blogpost_draft_project(self):
"""Test authenticated user cannot read a given blogpost of a draft project
if is not the project owner"""
project = ProjectFactory.create(published=False)
user = UserFactory.create()
blogpost = BlogpostFactory.create(project=project)
assert self.mock_authenticated.id != project.owner.id
assert_raises(Forbidden, ensure_authorized_to, 'read', blogpost)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_non_owner_authenticated_user_read_blogposts_for_given_draft_project(self):
"""Test authenticated user cannot read blogposts of a given project if is
a draft and is not the project owner"""
project = ProjectFactory.create(published=False)
user = UserFactory.create()
assert self.mock_authenticated.id != project.owner.id
assert_raises(Forbidden, ensure_authorized_to, 'read', Blogpost, project_id=project.id)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_owner_read_given_blogpost(self):
"""Test authenticated user can read a given blogpost if is the project owner"""
owner = UserFactory.create_batch(2)[1]
project = ProjectFactory.create(owner=owner, published=True)
blogpost = BlogpostFactory.create(project=project)
assert self.mock_authenticated.id == project.owner.id
assert_not_raises(Exception, ensure_authorized_to, 'read', blogpost)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_owner_read_blogposts_for_given_project(self):
"""Test authenticated user can read blogposts of a given project if is the
project owner"""
owner = UserFactory.create_batch(2)[1]
project = ProjectFactory.create(owner=owner, published=True)
assert self.mock_authenticated.id == project.owner.id
assert_not_raises(Exception, ensure_authorized_to, 'read', Blogpost, project_id=project.id)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_owner_read_given_blogpost_draft_project(self):
"""Test authenticated user can read a given blogpost of a draft project if
is the project owner"""
owner = UserFactory.create_batch(2)[1]
project = ProjectFactory.create(owner=owner, published=False)
blogpost = BlogpostFactory.create(project=project)
assert self.mock_authenticated.id == project.owner.id
assert_not_raises(Exception, ensure_authorized_to, 'read', blogpost)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_owner_read_blogposts_for_given_draft_project(self):
"""Test authenticated user can read blogposts of a given draft project if
is the project owner"""
owner = UserFactory.create_batch(2)[1]
project = ProjectFactory.create(owner=owner, published=False)
assert self.mock_authenticated.id == project.owner.id
assert_not_raises(Exception, ensure_authorized_to, 'read', Blogpost, project_id=project.id)
@patch('pybossa.auth.current_user', new=mock_admin)
def test_admin_read_given_blogpost_draft_project(self):
"""Test admin can read a given blogpost of a draft project"""
admin = UserFactory.create()
project = ProjectFactory.create(published=False)
blogpost = BlogpostFactory.create(project=project)
assert self.mock_admin.id != project.owner.id
assert_not_raises(Exception, ensure_authorized_to, 'read', blogpost)
@patch('pybossa.auth.current_user', new=mock_admin)
def test_admin_read_blogposts_for_given_draft_project(self):
"""Test admin can read blogposts of a given draft project"""
admin = UserFactory.create()
project = ProjectFactory.create(published=False)
assert self.mock_admin.id != project.owner.id
assert_not_raises(Exception, ensure_authorized_to, 'read', Blogpost, project_id=project.id)
@patch('pybossa.auth.current_user', new=mock_anonymous)
def test_anonymous_user_update_blogpost(self):
"""Test anonymous users cannot update blogposts"""
blogpost = BlogpostFactory.create()
assert_raises(Unauthorized, ensure_authorized_to, 'update', blogpost)
@patch('pybossa.auth.current_user', new=mock_admin)
def test_non_owner_authenticated_user_update_blogpost(self):
"""Test authenticated user cannot update a blogpost if is not the post
owner, even if is admin"""
admin = UserFactory.create()
project = ProjectFactory.create()
blogpost = BlogpostFactory.create(project=project)
assert self.mock_admin.id != blogpost.owner.id
assert_raises(Forbidden, ensure_authorized_to, 'update', blogpost)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_owner_update_blogpost(self):
"""Test authenticated user can update blogpost if is the post owner"""
owner = UserFactory.create_batch(2)[1]
project = ProjectFactory.create()
blogpost = BlogpostFactory.create(project=project, owner=owner)
assert self.mock_authenticated.id == blogpost.owner.id
assert_not_raises(Exception, ensure_authorized_to, 'update', blogpost)
@patch('pybossa.auth.current_user', new=mock_anonymous)
def test_anonymous_user_delete_blogpost(self):
"""Test anonymous users cannot delete blogposts"""
blogpost = BlogpostFactory.create()
assert_raises(Unauthorized, ensure_authorized_to, 'delete', blogpost)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_non_owner_authenticated_user_delete_blogpost(self):
"""Test authenticated user cannot delete a blogpost if is not the post
owner and is not admin"""
blogpost = BlogpostFactory.create()
assert self.mock_authenticated.id != blogpost.owner.id
assert not self.mock_authenticated.admin
assert_raises(Forbidden, ensure_authorized_to, 'delete', blogpost)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_owner_delete_blogpost(self):
"""Test authenticated user can delete a blogpost if is the post owner"""
owner = UserFactory.create_batch(2)[1]
project = ProjectFactory.create()
blogpost = BlogpostFactory.create(project=project, owner=owner)
assert self.mock_authenticated.id == blogpost.owner.id
assert_not_raises(Exception, ensure_authorized_to, 'delete', blogpost)
@patch('pybossa.auth.current_user', new=mock_admin)
def test_admin_authenticated_user_delete_blogpost(self):
"""Test authenticated user can delete any blogpost if is admin"""
admin = UserFactory.create()
blogpost = BlogpostFactory.create()
assert self.mock_admin.id != blogpost.owner.id
assert_not_raises(Exception, ensure_authorized_to, 'delete', blogpost)