-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_models.py
executable file
·520 lines (418 loc) · 16.4 KB
/
test_models.py
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import datetime
import json
import pytest
from hashids import Hashids
from django.http import Http404
from django.contrib import auth
from django.forms.models import model_to_dict
from guardian.shortcuts import get_perms
from webapp.apps.users.models import Project, Profile, create_profile_from_user
from webapp.apps.comp.models import Inputs, Simulation, PendingPermission, ANON_BEFORE
from webapp.apps.comp.exceptions import (
ForkObjectException,
VersionMismatchException,
ResourceLimitException,
)
from .utils import (
_submit_inputs,
_submit_sim,
read_outputs,
_shuffled_sims,
Customer,
gen_collabs,
)
User = auth.get_user_model()
@pytest.fixture
def shuffled_sims(profile, get_inputs, meta_param_dict):
return _shuffled_sims(profile, get_inputs, meta_param_dict)
def test_new_sim(db, profile):
project = Project.objects.get(
title="Used-for-testing", owner__user__username="modeler"
)
sim = Simulation.objects.new_sim(profile.user, project)
assert sim
assert sim.inputs
def test_get_next_model_pk(db):
owner = Profile.objects.get(user__username="modeler")
project = Project.objects.get(title="Used-for-testing", owner=owner)
inputs = Inputs(inputs_style="paramtools", project=project)
inputs.save()
naive_next_model_pk = 1
for sim in Simulation.objects.filter(project=project):
if sim.model_pk >= naive_next_model_pk:
naive_next_model_pk = sim.model_pk
sim = Simulation(
inputs=inputs,
project=project,
model_pk=Simulation.objects.next_model_pk(project),
)
sim.save()
assert sim.model_pk == naive_next_model_pk
assert Simulation.objects.next_model_pk(project) == sim.model_pk + 1
def test_parent_sims(db, get_inputs, meta_param_dict, profile):
modeler = User.objects.get(username="modeler").profile
inputs = _submit_inputs("Used-for-testing", get_inputs, meta_param_dict, modeler)
sims = []
for i in range(0, 10):
submit_inputs, submit_sim = _submit_sim(inputs)
sims.append(submit_sim.submit())
sims[-1].is_public = True
sims[-1].save()
inputs = _submit_inputs(
"Used-for-testing",
get_inputs,
meta_param_dict,
profile if i % 3 else modeler, # swap profiles every three sims.
parent_model_pk=sims[-1].model_pk,
)
child_sim = sims[-1]
assert child_sim.parent_sims() == list(reversed(sims[:-1]))
init_sim = sims[0]
assert init_sim.parent_sims() == []
for ix in range(1, 10):
middle_sim = sims[ix]
assert middle_sim.parent_sims() == list(reversed(sims[:ix]))
def test_private_parent_sims(db, shuffled_sims, profile):
modeler = User.objects.get(username="modeler").profile
sims, modeler_sims, tester_sims = shuffled_sims
child_sim = sims[-1]
assert child_sim.parent_sims(user=None) == []
assert child_sim.parent_sims(user=modeler.user) == list(reversed(modeler_sims))
assert child_sim.parent_sims(user=profile.user) == list(reversed(tester_sims))
def test_sim_fork(db, get_inputs, meta_param_dict, profile):
modeler = User.objects.get(username="modeler").profile
inputs = _submit_inputs("Used-for-testing", get_inputs, meta_param_dict, modeler)
sims = []
submit_inputs, submit_sim = _submit_sim(inputs)
sim = submit_sim.submit()
sim.inputs.status = "SUCCESS"
sim.inputs.save()
sim.outputs = "hello world"
sim.status = "SUCCESS"
sim.save()
next_model_pk = Simulation.objects.next_model_pk(sim.project)
newsim = Simulation.objects.fork(sim, profile.user)
assert newsim.owner != sim.owner
assert newsim.inputs.owner == newsim.owner and newsim.inputs.owner != sim.owner
assert newsim.model_pk == next_model_pk
assert float(newsim.run_cost) == 0.0
assert newsim.parent_sim == newsim.inputs.parent_sim == sim
# make sure each sim's owner has read access and that the
# read access for the new sim only applies to the owner
# of the new sim and not the owner of the old sim.
assert newsim.has_admin_access(newsim.owner.user)
assert sim.has_admin_access(sim.owner.user)
newsim.is_public = False
newsim.save()
assert not newsim.has_read_access(sim.owner.user)
def objects_eq(obj1, obj2, fields_to_exclude):
data1 = model_to_dict(obj1)
data2 = model_to_dict(obj2)
for field in data1:
if field in fields_to_exclude:
continue
assert data1[field] == data2[field]
fields_to_exclude = ["id", "owner", "job_id", "parent_sim"]
objects_eq(sim.inputs, newsim.inputs, fields_to_exclude)
fields_to_exclude += ["inputs", "run_cost", "model_pk", "creation_date", "authors"]
objects_eq(sim, newsim, fields_to_exclude)
sim.status = "PENDING"
sim.save()
with pytest.raises(ForkObjectException):
Simulation.objects.fork(sim, profile.user)
sim.inputs.status = "PENDING"
sim.inputs.save()
with pytest.raises(ForkObjectException):
Simulation.objects.fork(sim, profile.user)
def test_outputs_versions(db, get_inputs, meta_param_dict):
modeler = User.objects.get(username="modeler").profile
inputs = _submit_inputs("Used-for-testing", get_inputs, meta_param_dict, modeler)
_, submit_sim = _submit_sim(inputs)
sim = submit_sim.submit()
sim.status = "SUCCESS"
sim.save()
v0_outputs = json.loads(read_outputs("Matchups_v0"))
sim.outputs = v0_outputs
sim.save()
assert sim.outputs_version() == "v0"
assert (
sim.get_absolute_url()
== sim.get_absolute_v0_url()
== f"/{sim.project.owner}/{sim.project.title}/{sim.model_pk}/v0/"
)
v1_outputs = json.loads(read_outputs("Matchups_v1"))
sim.outputs = v1_outputs
sim.save()
assert sim.outputs_version() == "v1"
assert (
sim.get_absolute_url()
== f"/{sim.project.owner}/{sim.project.title}/{sim.model_pk}/"
)
with pytest.raises(VersionMismatchException):
sim.get_absolute_v0_url()
def test_get_owner(db, get_inputs, meta_param_dict):
modeler = User.objects.get(username="modeler").profile
inputs = _submit_inputs("Used-for-testing", get_inputs, meta_param_dict, modeler)
_, submit_sim = _submit_sim(inputs)
sim = submit_sim.submit()
sim.status = "SUCCESS"
sim.save()
# get_owner gives sim owner after ANON_BEFORE
sim.creation_date = ANON_BEFORE + datetime.timedelta(days=2)
sim.save()
assert sim.get_owner() == modeler
# get_owner gives "unsigned" before ANON_BEFORE
sim.creation_date = ANON_BEFORE - datetime.timedelta(days=2)
sim.save()
assert sim.get_owner() == "unsigned"
def test_sim_permissions(db, get_inputs, meta_param_dict, plus_profile):
collab = next(gen_collabs(1))
inputs = _submit_inputs(
"Used-for-testing", get_inputs, meta_param_dict, plus_profile
)
_, submit_sim = _submit_sim(inputs)
sim = submit_sim.submit()
sim.status = "SUCCESS"
sim.save()
# check permissions for owner and random profile
assert get_perms(sim.owner.user, sim) == ["admin_simulation"]
assert sim.role(sim.owner.user) == "admin"
assert get_perms(collab.user, sim) == []
assert sim.role(collab.user) is None
# sim owner has all levels of access
assert (
sim.is_owner(sim.owner.user)
and sim.has_admin_access(sim.owner.user)
and sim.has_write_access(sim.owner.user)
and sim.has_read_access(sim.owner.user)
)
# random user has no access
assert (
not sim.is_owner(collab.user)
and not sim.has_admin_access(collab.user)
and not sim.has_write_access(collab.user)
and not sim.has_read_access(collab.user)
)
# None has no access and does not cause errors
assert (
not sim.is_owner(None)
and not sim.has_admin_access(None)
and not sim.has_write_access(None)
and not sim.has_read_access(None)
)
# test grant/removal of read access.
sim.grant_read_permissions(collab.user)
assert (
get_perms(collab.user, sim) == ["read_simulation"]
and sim.role(collab.user) == "read"
)
assert (
not sim.is_owner(collab.user)
and not sim.has_admin_access(collab.user)
and not sim.has_write_access(collab.user)
and sim.has_read_access(collab.user)
)
sim.remove_permissions(collab.user)
assert get_perms(collab.user, sim) == [] and sim.role(collab.user) is None
assert (
not sim.is_owner(collab.user)
and not sim.has_admin_access(collab.user)
and not sim.has_write_access(collab.user)
and not sim.has_read_access(collab.user)
)
# test grant/remove are idempotent:
for _ in range(3):
sim.grant_read_permissions(collab.user)
assert sim.has_read_access(collab.user)
for _ in range(3):
sim.remove_permissions(collab.user)
assert not sim.has_read_access(collab.user)
# test that only one permission is applied at a time.
sim.grant_read_permissions(collab.user)
assert get_perms(collab.user, sim) == ["read_simulation"]
sim.grant_write_permissions(collab.user)
assert get_perms(collab.user, sim) == ["write_simulation"]
sim.grant_admin_permissions(collab.user)
assert get_perms(collab.user, sim) == ["admin_simulation"]
sim.is_public = True
sim.save()
assert sim.has_read_access(plus_profile.user)
assert sim.has_read_access(collab.user)
assert sim.has_read_access(None) is True
# test role
sim.is_public = False
sim.save()
sim.assign_role("admin", collab.user)
assert sim.has_admin_access(collab.user) and sim.role(collab.user) == "admin"
sim.assign_role("write", collab.user)
assert sim.has_write_access(collab.user) and sim.role(collab.user) == "write"
sim.assign_role("read", collab.user)
assert sim.has_read_access(collab.user) and sim.role(collab.user) == "read"
sim.assign_role(None, collab.user)
assert not sim.has_read_access(collab.user) and sim.role(collab.user) == None
with pytest.raises(ValueError):
sim.assign_role("dne", collab.user)
def test_add_authors(db, get_inputs, meta_param_dict, plus_profile):
inputs = _submit_inputs(
"Used-for-testing", get_inputs, meta_param_dict, plus_profile
)
_, submit_sim = _submit_sim(inputs)
sim = submit_sim.submit()
sim.status = "SUCCESS"
sim.is_public = False
sim.save()
collab = next(gen_collabs(1))
assert not sim.has_read_access(collab.user)
# Create new pending permission object and make sure that read access was
# granted appropriately.
pp, created = PendingPermission.objects.get_or_create(
sim=sim, profile=collab, permission_name="add_author"
)
assert created
assert not pp.is_expired()
assert sim.has_read_access(collab.user)
assert get_perms(collab.user, sim) == ["read_simulation"]
sim = Simulation.objects.get(pk=sim.pk)
assert sim.pending_permissions.filter(id=pp.id).count() == 1
assert sim.authors.all().count() == 1 and sim.authors.get(pk=plus_profile.pk)
pp.add_author()
sim = Simulation.objects.get(pk=sim.pk)
assert sim.authors.all().count() == 2 and sim.authors.get(pk=collab.pk) == collab
assert PendingPermission.objects.filter(id=pp.id).count() == 0
def test_public_sims(db, shuffled_sims, profile):
# modeler = User.objects.get(username="modeler").profile
_, modeler_sims, _ = shuffled_sims
for sim in modeler_sims:
sim.is_public = True
sim.save()
assert set(Simulation.objects.public_sims()) == set(modeler_sims)
modeler_sims[1].creation_date = ANON_BEFORE - datetime.timedelta(days=2)
modeler_sims[1].save()
assert set(Simulation.objects.public_sims()) == set(
[modeler_sims[0]] + modeler_sims[2:]
)
class TestCollaborators:
def test_free_tier(self, db, get_inputs, meta_param_dict, free_profile):
"""
Test private sim can not have any collaborators but
public is unlimited.
"""
inputs = _submit_inputs(
"Used-for-testing", get_inputs, meta_param_dict, free_profile
)
_, submit_sim = _submit_sim(inputs)
sim = submit_sim.submit()
sim.status = "SUCCESS"
sim.is_public = False
sim.save()
collabs = list(gen_collabs(3))
# test cannot add collaborator when sim is private.
with pytest.raises(ResourceLimitException) as excinfo:
sim.assign_role("read", collabs[0].user)
assert excinfo.value.todict() == {
"upgrade_to": "plus",
"resource": "collaborators",
"test_name": "add_collaborator",
"msg": ResourceLimitException.collaborators_msg,
}
assert (
get_perms(collabs[0].user, sim) == [] and sim.role(collabs[1].user) == None
)
# OK to make sim private.
sim.make_private_test()
# test no limit on collaborators when sim is public.
sim.is_public = True
sim.save()
for collab in collabs:
sim.assign_role("read", collab.user)
assert (
get_perms(collab.user, sim) == ["read_simulation"]
and sim.role(collab.user) == "read"
)
with pytest.raises(ResourceLimitException):
sim.make_private_test()
def test_plus_tier(self, db, get_inputs, meta_param_dict, plus_profile):
"""
Test can only add one collaborator to private sim but
public is unlimited.
"""
inputs = _submit_inputs(
"Used-for-testing", get_inputs, meta_param_dict, plus_profile
)
_, submit_sim = _submit_sim(inputs)
sim = submit_sim.submit()
sim.status = "SUCCESS"
sim.is_public = False
sim.save()
collabs = list(gen_collabs(3))
# add first collaborator.
sim.assign_role("read", collabs[0].user)
assert (
get_perms(collabs[0].user, sim) == ["read_simulation"]
and sim.role(collabs[0].user) == "read"
)
# cannot add second collaborator when sim is private.
with pytest.raises(ResourceLimitException) as excinfo:
sim.assign_role("read", collabs[1].user)
assert excinfo.value.todict() == {
"resource": "collaborators",
"test_name": "add_collaborator",
"upgrade_to": "pro",
"msg": ResourceLimitException.collaborators_msg,
}
assert (
get_perms(collabs[1].user, sim) == [] and sim.role(collabs[1].user) == None
)
# make sure first collaborator is not affected.
assert (
get_perms(collabs[0].user, sim) == ["read_simulation"]
and sim.role(collabs[0].user) == "read"
)
# OK to make sim private.
sim.make_private_test()
# test no limit on collaborators when sim is free.
sim.is_public = True
sim.save()
for collab in collabs:
sim.assign_role("read", collab.user)
assert (
get_perms(collab.user, sim) == ["read_simulation"]
and sim.role(collab.user) == "read"
)
# Making sim private is not allowed.
with pytest.raises(ResourceLimitException):
sim.make_private_test()
def test_pro_tier(self, db, get_inputs, meta_param_dict, pro_profile, profile):
"""
Test able to add more than one collaborator with a private
and public sim.
"""
inputs = _submit_inputs(
"Used-for-testing", get_inputs, meta_param_dict, pro_profile
)
_, submit_sim = _submit_sim(inputs)
sim = submit_sim.submit()
sim.status = "SUCCESS"
sim.save()
collabs = list(gen_collabs(3))
for collab in collabs:
sim.assign_role("read", collab.user)
assert (
get_perms(collab.user, sim) == ["read_simulation"]
and sim.role(collab.user) == "read"
)
# OK making sim private.
sim.make_private_test()
sim.is_public = True
sim.save()
for collab in collabs:
sim.assign_role(None, collab.user)
assert sim.role(collab.user) == None
for collab in collabs:
sim.assign_role("read", collab.user)
assert (
get_perms(collab.user, sim) == ["read_simulation"]
and sim.role(collab.user) == "read"
)
# OK making sim private.
sim.make_private_test()