test_taskrun_auth.py
12.9 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
# -*- 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, AnonymousTaskRunFactory,
TaskFactory, TaskRunFactory, UserFactory)
class TestTaskrunAuthorization(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_first_taskrun(self):
"""Test anonymous user can create a taskrun for a given task if he
hasn't already done it"""
task = TaskFactory.create()
taskrun = AnonymousTaskRunFactory.build(task=task)
assert_not_raises(Exception, ensure_authorized_to, 'create', taskrun)
@patch('pybossa.auth.current_user', new=mock_anonymous)
def test_anonymous_user_create_repeated_taskrun(self):
"""Test anonymous user cannot create a taskrun for a task to which
he has previously posted a taskrun"""
task = TaskFactory.create()
taskrun1 = AnonymousTaskRunFactory.create(task=task)
taskrun2 = AnonymousTaskRunFactory.build(task=task)
assert_raises(Forbidden, ensure_authorized_to, 'create', taskrun2)
@patch('pybossa.auth.current_user', new=mock_anonymous)
def test_anonymous_user_create_taskrun(self):
"""Test anonymous user can create a taskrun for a task even though
he has posted taskruns for different tasks in the same project"""
project = ProjectFactory.create()
tasks = TaskFactory.create_batch(2, project=project)
taskrun1 = AnonymousTaskRunFactory.create(task=tasks[0])
taskrun2 = AnonymousTaskRunFactory.build(task_id=tasks[1].id,
project_id=project.id)
assert_not_raises(Exception, ensure_authorized_to, 'create', taskrun2)
@patch('pybossa.auth.current_user', new=mock_anonymous)
def test_anonymous_user_create_taskrun_non_allow_anonymous_contrib(self):
"""Test anonymous user cannot create a taskrun for a project that does
not allow for anonymous contributors"""
project = ProjectFactory.create(allow_anonymous_contributors=False)
task = TaskFactory.create(project=project)
taskrun = AnonymousTaskRunFactory.build(task=task)
assert_raises(Unauthorized, ensure_authorized_to, 'create', taskrun)
@patch('pybossa.auth.current_user', new=mock_anonymous)
def test_anonymous_user_cannot_create_taskrun_for_draft_project(self):
"""Test anonymous users cannot create a taskrun for a project that
is a draft"""
project = ProjectFactory.create(published=False)
task = TaskFactory.create(project=project)
taskrun = AnonymousTaskRunFactory.build(task_id=task.id,
project_id=project.id)
assert_raises(Forbidden, ensure_authorized_to, 'create', taskrun)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_authenticated_user_create_first_taskrun(self):
"""Test authenticated user can create a taskrun for a given task if he
hasn't already done it"""
task = TaskFactory.create()
taskrun = TaskRunFactory.build(task_id=task.id,
project_id=task.project_id,
user_id=self.mock_authenticated.id)
assert self.mock_authenticated.id == taskrun.user_id, taskrun
assert_not_raises(Exception, ensure_authorized_to, 'create', taskrun)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_authenticated_user_create_repeated_taskrun(self):
"""Test authenticated user cannot create a taskrun for a task to which
he has previously posted a taskrun"""
task = TaskFactory.create()
taskrun1 = TaskRunFactory.create(task=task)
taskrun2 = TaskRunFactory.build(task=task, user=taskrun1.user)
assert self.mock_authenticated.id == taskrun1.user.id
assert_raises(Forbidden, ensure_authorized_to, 'create', taskrun2)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_authenticated_user_create_taskrun(self):
"""Test authenticated user can create a taskrun for a task even though
he has posted taskruns for different tasks in the same project"""
user = UserFactory.create_batch(2)[1]
project = ProjectFactory.create()
tasks = TaskFactory.create_batch(2, project=project)
taskrun1 = TaskRunFactory.create(task=tasks[0], user=user)
taskrun2 = TaskRunFactory.build(task_id=tasks[1].id, user_id=user.id,
project_id=tasks[1].project_id)
assert self.mock_authenticated.id == taskrun2.user_id
assert_not_raises(Exception, ensure_authorized_to, 'create', taskrun2)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_authenticated_user_create_taskrun_non_allow_anonymous_contrib(self):
"""Test authenticated user can create a taskrun for a project that does
not allow for anonymous contributors"""
project = ProjectFactory.create(allow_anonymous_contributors=False)
task = TaskFactory.create(project=project)
taskrun = TaskRunFactory.build(task_id=task.id, project_id=project.id)
assert_not_raises(Exception, ensure_authorized_to, 'create', taskrun)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_authenticated_user_cannot_create_taskrun_for_draft_project(self):
"""Test authenticated users cannot create a taskrun for a project that
is a draft"""
project = ProjectFactory.create(published=False)
task = TaskFactory.create(project=project)
taskrun = TaskRunFactory.build(task_id=task.id, project_id=project.id)
assert_raises(Forbidden, ensure_authorized_to, 'create', taskrun)
@patch('pybossa.auth.current_user', new=mock_anonymous)
def test_anonymous_user_read(self):
"""Test anonymous user can read any taskrun"""
anonymous_taskrun = AnonymousTaskRunFactory.create()
user_taskrun = TaskRunFactory.create()
assert_not_raises(Exception,
ensure_authorized_to, 'read', anonymous_taskrun)
assert_not_raises(Exception, ensure_authorized_to, 'read', user_taskrun)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_authenticated_user_read(self):
"""Test authenticated user can read any taskrun"""
own_taskrun = TaskRunFactory.create()
anonymous_taskrun = AnonymousTaskRunFactory.create()
other_users_taskrun = TaskRunFactory.create()
assert self.mock_authenticated.id == own_taskrun.user.id
assert self.mock_authenticated.id != other_users_taskrun.user.id
assert_not_raises(Exception,
ensure_authorized_to, 'read', anonymous_taskrun)
assert_not_raises(Exception,
ensure_authorized_to, 'read', other_users_taskrun)
assert_not_raises(Exception, ensure_authorized_to, 'read', own_taskrun)
@patch('pybossa.auth.current_user', new=mock_anonymous)
def test_anonymous_user_update_anoymous_taskrun(self):
"""Test anonymous users cannot update an anonymously posted taskrun"""
anonymous_taskrun = AnonymousTaskRunFactory.create()
assert_raises(Unauthorized,
ensure_authorized_to, 'update', anonymous_taskrun)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_authenticated_user_update_anonymous_taskrun(self):
"""Test authenticated users cannot update an anonymously posted taskrun"""
anonymous_taskrun = AnonymousTaskRunFactory.create()
assert_raises(Forbidden,
ensure_authorized_to, 'update', anonymous_taskrun)
@patch('pybossa.auth.current_user', new=mock_admin)
def test_admin_update_anonymous_taskrun(self):
"""Test admins cannot update anonymously posted taskruns"""
anonymous_taskrun = AnonymousTaskRunFactory.create()
assert_raises(Forbidden,
ensure_authorized_to, 'update', anonymous_taskrun)
@patch('pybossa.auth.current_user', new=mock_anonymous)
def test_anonymous_user_update_user_taskrun(self):
"""Test anonymous user cannot update taskruns posted by authenticated users"""
user_taskrun = TaskRunFactory.create()
assert_raises(Unauthorized,
ensure_authorized_to, 'update', user_taskrun)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_authenticated_user_update_other_users_taskrun(self):
"""Test authenticated user cannot update any user taskrun"""
own_taskrun = TaskRunFactory.create()
other_users_taskrun = TaskRunFactory.create()
assert self.mock_authenticated.id == own_taskrun.user.id
assert self.mock_authenticated.id != other_users_taskrun.user.id
assert_raises(Forbidden, ensure_authorized_to, 'update', own_taskrun)
assert_raises(Forbidden,
ensure_authorized_to, 'update', other_users_taskrun)
@patch('pybossa.auth.current_user', new=mock_admin)
def test_admin_update_user_taskrun(self):
"""Test admins cannot update taskruns posted by authenticated users"""
user_taskrun = TaskRunFactory.create()
assert self.mock_admin.id != user_taskrun.user.id
assert_raises(Forbidden, ensure_authorized_to, 'update', user_taskrun)
@patch('pybossa.auth.current_user', new=mock_anonymous)
def test_anonymous_user_delete_anonymous_taskrun(self):
"""Test anonymous users cannot delete an anonymously posted taskrun"""
anonymous_taskrun = AnonymousTaskRunFactory.create()
assert_raises(Unauthorized,
ensure_authorized_to, 'delete', anonymous_taskrun)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_authenticated_user_delete_anonymous_taskrun(self):
"""Test authenticated users cannot delete an anonymously posted taskrun"""
anonymous_taskrun = AnonymousTaskRunFactory.create()
assert_raises(Forbidden,
ensure_authorized_to, 'delete', anonymous_taskrun)
@patch('pybossa.auth.current_user', new=mock_admin)
def test_admin_delete_anonymous_taskrun(self):
"""Test admins can delete anonymously posted taskruns"""
anonymous_taskrun = AnonymousTaskRunFactory.create()
assert_not_raises(Exception,
ensure_authorized_to, 'delete', anonymous_taskrun)
@patch('pybossa.auth.current_user', new=mock_anonymous)
def test_anonymous_user_delete_user_taskrun(self):
"""Test anonymous user cannot delete taskruns posted by authenticated users"""
user_taskrun = TaskRunFactory.create()
assert_raises(Unauthorized,
ensure_authorized_to, 'delete', user_taskrun)
@patch('pybossa.auth.current_user', new=mock_authenticated)
def test_authenticated_user_delete_other_users_taskrun(self):
"""Test authenticated user cannot delete a taskrun if it was created
by another authenticated user, but can delete his own taskruns"""
own_taskrun = TaskRunFactory.create()
other_users_taskrun = TaskRunFactory.create()
assert self.mock_authenticated.id == own_taskrun.user.id
assert self.mock_authenticated.id != other_users_taskrun.user.id
assert_not_raises(Exception,
ensure_authorized_to, 'delete', own_taskrun)
assert_raises(Forbidden,
ensure_authorized_to, 'delete', other_users_taskrun)
@patch('pybossa.auth.current_user', new=mock_admin)
def test_admin_delete_user_taskrun(self):
"""Test admins can delete taskruns posted by authenticated users"""
user_taskrun = TaskRunFactory.create()
assert self.mock_admin.id != user_taskrun.user.id, user_taskrun.user.id
assert_not_raises(Exception,
ensure_authorized_to, 'delete', user_taskrun)