-
Notifications
You must be signed in to change notification settings - Fork 12
/
test_dandiset.py
1043 lines (908 loc) · 34.9 KB
/
test_dandiset.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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import datetime
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from guardian.shortcuts import assign_perm
import pytest
from dandiapi.api.asset_paths import add_asset_paths, add_version_asset_paths
from dandiapi.api.models import Dandiset, Version
from .fuzzy import DANDISET_ID_RE, DANDISET_SCHEMA_ID_RE, TIMESTAMP_RE, UTC_ISO_TIMESTAMP_RE
@pytest.mark.django_db()
def test_dandiset_identifier(dandiset):
assert int(dandiset.identifier) == dandiset.id
def test_dandiset_identifer_missing(dandiset_factory):
dandiset = dandiset_factory.build()
# This should have a sane fallback
assert dandiset.identifier == ''
@pytest.mark.django_db()
def test_dandiset_published_count(
dandiset_factory, draft_version_factory, published_version_factory
):
# empty dandiset
dandiset_factory()
# dandiset with draft version
draft_version_factory(dandiset=dandiset_factory())
# dandiset with published version
published_version_factory(dandiset=dandiset_factory())
assert Dandiset.published_count() == 1
@pytest.mark.parametrize(
('embargo_status', 'user_status', 'visible'),
[
('OPEN', 'owner', True),
('OPEN', 'anonymous', True),
('OPEN', 'not-owner', True),
('EMBARGOED', 'owner', True),
('EMBARGOED', 'anonymous', False),
('EMBARGOED', 'not-owner', False),
('UNEMBARGOING', 'owner', True),
('UNEMBARGOING', 'anonymous', False),
('UNEMBARGOING', 'not-owner', False),
],
)
@pytest.mark.django_db()
def test_dandiset_manager_visible_to(
dandiset_factory, user_factory, embargo_status, user_status, visible
):
dandiset = dandiset_factory(embargo_status=embargo_status)
user = AnonymousUser if user_status == 'anonymous' else user_factory()
if user_status == 'owner':
assign_perm('owner', user, dandiset)
assert list(Dandiset.objects.visible_to(user)) == ([dandiset] if visible else [])
@pytest.mark.django_db()
def test_dandiset_rest_list(api_client, user, dandiset):
# Test un-authenticated request
assert api_client.get('/api/dandisets/', {'draft': 'true', 'empty': 'true'}).json() == {
'count': 1,
'next': None,
'previous': None,
'results': [
{
'identifier': dandiset.identifier,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
'draft_version': None,
'most_recent_published_version': None,
'contact_person': '',
'embargo_status': 'OPEN',
}
],
}
# Test request for embargoed dandisets while still un-authenticated
r = api_client.get('/api/dandisets/', {'draft': 'true', 'empty': 'true', 'embargoed': 'true'})
assert r.status_code == 401
# Test request for embargoed dandisets after authenticated
api_client.force_authenticate(user=user)
r = api_client.get('/api/dandisets/', {'draft': 'true', 'empty': 'true', 'embargoed': 'true'})
assert r.status_code == 200
@pytest.mark.parametrize(
('params', 'results'),
[
('', ['empty', 'draft', 'published', 'erased']),
('?draft=false', ['published', 'erased']),
('?empty=false', ['draft', 'published']),
('?draft=true&empty=true', ['empty', 'draft', 'published', 'erased']),
('?empty=true&draft=true', ['empty', 'draft', 'published', 'erased']),
('?draft=false&empty=false', ['published']),
],
ids=[
'nothing',
'empty-only',
'draft-only',
'draft-empty',
'empty-draft',
'draft-false-empty-false',
],
)
@pytest.mark.django_db()
def test_dandiset_versions(
api_client,
dandiset_factory,
draft_version_factory,
published_version_factory,
asset_factory,
params,
results,
):
# Create some dandisets of different kinds.
#
# Dandiset with empty draft
empty_dandiset = dandiset_factory()
draft_version_factory(dandiset=empty_dandiset)
# Dandiset with populated draft
draft_dandiset = dandiset_factory()
draft_version = draft_version_factory(dandiset=draft_dandiset)
draft_version.assets.add(asset_factory())
add_version_asset_paths(draft_version)
# Dandiset with published version
published_dandiset = dandiset_factory()
draft_version = draft_version_factory(dandiset=published_dandiset)
draft_version.assets.add(asset_factory())
add_version_asset_paths(draft_version)
published_version = published_version_factory(dandiset=published_dandiset)
published_version.assets.add(asset_factory())
add_version_asset_paths(published_version)
# Dandiset with published version and empty draft
erased_dandiset = dandiset_factory()
draft_version_factory(dandiset=erased_dandiset)
published_version = published_version_factory(dandiset=erased_dandiset)
published_version.assets.add(asset_factory())
add_version_asset_paths(published_version)
def expected_serialization(dandiset: Dandiset):
draft_version = dandiset.draft_version
published_version = dandiset.most_recent_published_version
contact_person = (published_version or draft_version).metadata['contributor'][0]['name']
return {
'identifier': dandiset.identifier,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
'contact_person': contact_person,
'embargo_status': 'OPEN',
'draft_version': {
'version': draft_version.version,
'name': draft_version.name,
'asset_count': draft_version.asset_count,
'size': draft_version.size,
'status': 'Pending',
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
}
if draft_version is not None
else None,
'most_recent_published_version': {
'version': published_version.version,
'name': published_version.name,
'asset_count': published_version.asset_count,
'size': published_version.size,
'status': 'Pending',
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
}
if published_version is not None
else None,
}
possible_results = {
'empty': expected_serialization(empty_dandiset),
'draft': expected_serialization(draft_dandiset),
'published': expected_serialization(published_dandiset),
'erased': expected_serialization(erased_dandiset),
}
expected_results = [possible_results[result] for result in results]
assert api_client.get(f'/api/dandisets/{params}').json() == {
'count': len(results),
'next': None,
'previous': None,
'results': expected_results,
}
@pytest.mark.django_db()
def test_dandiset_rest_list_for_user(api_client, user, dandiset_factory):
dandiset = dandiset_factory()
# Create an extra dandiset that should not be included in the response
dandiset_factory()
api_client.force_authenticate(user=user)
assign_perm('owner', user, dandiset)
assert api_client.get('/api/dandisets/?user=me&draft=true&empty=true').data == {
'count': 1,
'next': None,
'previous': None,
'results': [
{
'identifier': dandiset.identifier,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
'draft_version': None,
'most_recent_published_version': None,
'contact_person': '',
'embargo_status': 'OPEN',
}
],
}
@pytest.mark.django_db()
def test_dandiset_rest_retrieve(api_client, dandiset):
assert api_client.get(f'/api/dandisets/{dandiset.identifier}/').data == {
'identifier': dandiset.identifier,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
'draft_version': None,
'most_recent_published_version': None,
'contact_person': '',
'embargo_status': 'OPEN',
}
@pytest.mark.parametrize(
('embargo_status'),
[choice[0] for choice in Dandiset.EmbargoStatus.choices],
ids=[choice[1] for choice in Dandiset.EmbargoStatus.choices],
)
@pytest.mark.django_db()
def test_dandiset_rest_embargo_access(
api_client, user_factory, dandiset_factory, embargo_status: str
):
owner = user_factory()
unauthorized_user = user_factory()
dandiset = dandiset_factory(embargo_status=embargo_status)
assign_perm('owner', owner, dandiset)
# This is what authorized users should get from the retrieve endpoint
expected_dandiset_serialization = {
'identifier': dandiset.identifier,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
'draft_version': None,
'most_recent_published_version': None,
'contact_person': '',
'embargo_status': embargo_status,
}
# This is what unauthorized users should get from the retrieve endpoint
expected_error_message = {'detail': 'Not found.'}
# This is what authorized users should get from the list endpoint
expected_visible_pagination = {
'count': 1,
'next': None,
'previous': None,
'results': [expected_dandiset_serialization],
}
# This is what unauthorized users should get from the list endpoint
expected_invisible_pagination = {
'count': 0,
'next': None,
'previous': None,
'results': [],
}
# Anonymous users should only be able to access OPEN dandisets
assert (
api_client.get(f'/api/dandisets/{dandiset.identifier}/').json()
== expected_dandiset_serialization
if embargo_status == 'OPEN'
else expected_error_message
)
assert (
api_client.get('/api/dandisets/').json() == expected_visible_pagination
if embargo_status == 'OPEN'
else expected_invisible_pagination
)
# An unauthorized user should only be able to access OPEN dandisets
api_client.force_authenticate(user=unauthorized_user)
assert (
api_client.get(f'/api/dandisets/{dandiset.identifier}/').json()
== expected_dandiset_serialization
if embargo_status == 'OPEN'
else expected_error_message
)
assert (
api_client.get('/api/dandisets/').json() == expected_visible_pagination
if embargo_status == 'OPEN'
else expected_invisible_pagination
)
# The owner should always be able to access the dandiset
api_client.force_authenticate(user=owner)
assert (
api_client.get(f'/api/dandisets/{dandiset.identifier}/').json()
== expected_dandiset_serialization
)
assert (
api_client.get('/api/dandisets/', {'embargoed': 'true'}).json()
== expected_visible_pagination
)
@pytest.mark.django_db()
def test_dandiset_rest_create(api_client, user):
user.first_name = 'John'
user.last_name = 'Doe'
user.save()
api_client.force_authenticate(user=user)
name = 'Test Dandiset'
metadata = {'foo': 'bar'}
response = api_client.post(
'/api/dandisets/', {'name': name, 'metadata': metadata}, format='json'
)
assert response.data == {
'identifier': DANDISET_ID_RE,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
'contact_person': 'Doe, John',
'embargo_status': 'OPEN',
'draft_version': {
'version': 'draft',
'name': name,
'asset_count': 0,
'size': 0,
'dandiset': {
'identifier': DANDISET_ID_RE,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
'contact_person': 'Doe, John',
'embargo_status': 'OPEN',
},
'status': 'Pending',
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
},
'most_recent_published_version': None,
}
dandiset_id = int(response.data['identifier'])
# Creating a Dandiset has side affects.
# Verify that the user is the only owner.
dandiset = Dandiset.objects.get(id=dandiset_id)
assert list(dandiset.owners.all()) == [user]
# Verify that a draft Version and VersionMetadata were also created.
assert dandiset.versions.count() == 1
assert dandiset.most_recent_published_version is None
assert dandiset.draft_version.version == 'draft'
assert dandiset.draft_version.name == name
assert dandiset.draft_version.status == Version.Status.PENDING
# Verify that computed metadata was injected
year = datetime.datetime.now(datetime.UTC).year
url = f'{settings.DANDI_WEB_APP_URL}/dandiset/{dandiset.identifier}/draft'
assert dandiset.draft_version.metadata == {
**metadata,
'manifestLocation': [
f'{settings.DANDI_API_URL}/api/dandisets/{dandiset.identifier}/versions/draft/assets/'
],
'name': name,
'identifier': DANDISET_SCHEMA_ID_RE,
'id': f'DANDI:{dandiset.identifier}/draft',
'version': 'draft',
'url': url,
'dateCreated': UTC_ISO_TIMESTAMP_RE,
'citation': (
f'{user.last_name}, {user.first_name} ({year}) {name} '
f'(Version draft) [Data set]. DANDI archive. {url}'
),
'@context': f'https://raw.githubusercontent.com/dandi/schema/master/releases/{settings.DANDI_SCHEMA_VERSION}/context.json',
'schemaVersion': settings.DANDI_SCHEMA_VERSION,
'schemaKey': 'Dandiset',
'access': [{'schemaKey': 'AccessRequirements', 'status': 'dandi:OpenAccess'}],
'repository': settings.DANDI_WEB_APP_URL,
'contributor': [
{
'name': 'Doe, John',
'email': user.email,
'roleName': ['dcite:ContactPerson'],
'schemaKey': 'Person',
'affiliation': [],
'includeInCitation': True,
}
],
'assetsSummary': {
'schemaKey': 'AssetsSummary',
'numberOfBytes': 0,
'numberOfFiles': 0,
},
}
@pytest.mark.django_db()
def test_dandiset_rest_create_with_identifier(api_client, admin_user):
admin_user.first_name = 'John'
admin_user.last_name = 'Doe'
admin_user.save()
api_client.force_authenticate(user=admin_user)
name = 'Test Dandiset'
identifier = '123456'
metadata = {'foo': 'bar', 'identifier': f'DANDI:{identifier}'}
response = api_client.post(
'/api/dandisets/',
{'name': name, 'metadata': metadata},
format='json',
)
assert response.data == {
'identifier': identifier,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
'most_recent_published_version': None,
'draft_version': {
'version': 'draft',
'name': name,
'asset_count': 0,
'size': 0,
'dandiset': {
'identifier': identifier,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
'contact_person': 'Doe, John',
'embargo_status': 'OPEN',
},
'status': 'Pending',
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
},
'contact_person': 'Doe, John',
'embargo_status': 'OPEN',
}
# Creating a Dandiset has side affects.
# Verify that the user is the only owner.
dandiset = Dandiset.objects.get(id=identifier)
assert list(dandiset.owners.all()) == [admin_user]
# Verify that a draft Version and VersionMetadata were also created.
assert dandiset.versions.count() == 1
assert dandiset.most_recent_published_version is None
assert dandiset.draft_version.version == 'draft'
assert dandiset.draft_version.name == name
assert dandiset.draft_version.status == Version.Status.PENDING
# Verify that computed metadata was injected
year = datetime.datetime.now(datetime.UTC).year
url = f'{settings.DANDI_WEB_APP_URL}/dandiset/{dandiset.identifier}/draft'
assert dandiset.draft_version.metadata == {
**metadata,
'manifestLocation': [
f'{settings.DANDI_API_URL}/api/dandisets/{dandiset.identifier}/versions/draft/assets/'
],
'name': name,
'identifier': f'DANDI:{identifier}',
'id': f'DANDI:{dandiset.identifier}/draft',
'version': 'draft',
'url': url,
'dateCreated': UTC_ISO_TIMESTAMP_RE,
'citation': (
f'{admin_user.last_name}, {admin_user.first_name} ({year}) {name} '
f'(Version draft) [Data set]. DANDI archive. {url}'
),
'@context': f'https://raw.githubusercontent.com/dandi/schema/master/releases/{settings.DANDI_SCHEMA_VERSION}/context.json',
'schemaVersion': settings.DANDI_SCHEMA_VERSION,
'schemaKey': 'Dandiset',
'access': [{'schemaKey': 'AccessRequirements', 'status': 'dandi:OpenAccess'}],
'repository': settings.DANDI_WEB_APP_URL,
'contributor': [
{
'name': 'Doe, John',
'email': admin_user.email,
'roleName': ['dcite:ContactPerson'],
'schemaKey': 'Person',
'affiliation': [],
'includeInCitation': True,
}
],
'assetsSummary': {
'schemaKey': 'AssetsSummary',
'numberOfBytes': 0,
'numberOfFiles': 0,
},
}
@pytest.mark.django_db()
def test_dandiset_rest_create_with_contributor(api_client, admin_user):
admin_user.first_name = 'John'
admin_user.last_name = 'Doe'
admin_user.save()
api_client.force_authenticate(user=admin_user)
name = 'Test Dandiset'
identifier = '123456'
metadata = {
'foo': 'bar',
'identifier': f'DANDI:{identifier}',
# This contributor is different from the admin_user
'contributor': [
{
'name': 'Jane Doe',
'email': 'jane.doe@kitware.com',
'roleName': ['dcite:ContactPerson'],
'schemaKey': 'Person',
'affiliation': [],
'includeInCitation': True,
}
],
}
response = api_client.post(
'/api/dandisets/',
{'name': name, 'metadata': metadata},
format='json',
)
assert response.data == {
'identifier': identifier,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
'most_recent_published_version': None,
'draft_version': {
'version': 'draft',
'name': name,
'asset_count': 0,
'size': 0,
'dandiset': {
'identifier': identifier,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
'contact_person': 'Jane Doe',
'embargo_status': 'OPEN',
},
'status': 'Pending',
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
},
'contact_person': 'Jane Doe',
'embargo_status': 'OPEN',
}
# Creating a Dandiset has side affects.
# Verify that the user is the only owner.
dandiset = Dandiset.objects.get(id=identifier)
assert list(dandiset.owners.all()) == [admin_user]
# Verify that a draft Version and VersionMetadata were also created.
assert dandiset.versions.count() == 1
assert dandiset.most_recent_published_version is None
assert dandiset.draft_version.version == 'draft'
assert dandiset.draft_version.name == name
assert dandiset.draft_version.status == Version.Status.PENDING
# Verify that computed metadata was injected
year = datetime.datetime.now(datetime.UTC).year
url = f'{settings.DANDI_WEB_APP_URL}/dandiset/{dandiset.identifier}/draft'
assert dandiset.draft_version.metadata == {
**metadata,
'manifestLocation': [
f'{settings.DANDI_API_URL}/api/dandisets/{dandiset.identifier}/versions/draft/assets/'
],
'name': name,
'identifier': f'DANDI:{identifier}',
'id': f'DANDI:{dandiset.identifier}/draft',
'version': 'draft',
'url': url,
'dateCreated': UTC_ISO_TIMESTAMP_RE,
'citation': (
f'Jane Doe ({year}) {name} ' f'(Version draft) [Data set]. DANDI archive. {url}'
),
'@context': f'https://raw.githubusercontent.com/dandi/schema/master/releases/{settings.DANDI_SCHEMA_VERSION}/context.json',
'schemaVersion': settings.DANDI_SCHEMA_VERSION,
'schemaKey': 'Dandiset',
'access': [{'schemaKey': 'AccessRequirements', 'status': 'dandi:OpenAccess'}],
'repository': settings.DANDI_WEB_APP_URL,
'contributor': [
{
'name': 'Jane Doe',
'email': 'jane.doe@kitware.com',
'roleName': ['dcite:ContactPerson'],
'schemaKey': 'Person',
'affiliation': [],
'includeInCitation': True,
}
],
'assetsSummary': {
'schemaKey': 'AssetsSummary',
'numberOfBytes': 0,
'numberOfFiles': 0,
},
}
@pytest.mark.django_db()
def test_dandiset_rest_create_embargoed(api_client, user):
user.first_name = 'John'
user.last_name = 'Doe'
user.save()
api_client.force_authenticate(user=user)
name = 'Test Dandiset'
metadata = {'foo': 'bar'}
response = api_client.post(
'/api/dandisets/?embargo=true', {'name': name, 'metadata': metadata}, format='json'
)
assert response.data == {
'identifier': DANDISET_ID_RE,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
'contact_person': 'Doe, John',
'embargo_status': 'EMBARGOED',
'draft_version': {
'version': 'draft',
'name': name,
'asset_count': 0,
'size': 0,
'dandiset': {
'identifier': DANDISET_ID_RE,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
'contact_person': 'Doe, John',
'embargo_status': 'EMBARGOED',
},
'status': 'Pending',
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
},
'most_recent_published_version': None,
}
dandiset_id = int(response.data['identifier'])
# Creating a Dandiset has side affects.
# Verify that the user is the only owner.
dandiset = Dandiset.objects.get(id=dandiset_id)
assert list(dandiset.owners.all()) == [user]
# Verify that a draft Version and VersionMetadata were also created.
assert dandiset.versions.count() == 1
assert dandiset.most_recent_published_version is None
assert dandiset.draft_version.version == 'draft'
assert dandiset.draft_version.name == name
assert dandiset.draft_version.status == Version.Status.PENDING
# Verify that computed metadata was injected
year = datetime.datetime.now(datetime.UTC).year
url = f'{settings.DANDI_WEB_APP_URL}/dandiset/{dandiset.identifier}/draft'
assert dandiset.draft_version.metadata == {
**metadata,
'manifestLocation': [
f'{settings.DANDI_API_URL}/api/dandisets/{dandiset.identifier}/versions/draft/assets/'
],
'name': name,
'identifier': DANDISET_SCHEMA_ID_RE,
'id': f'DANDI:{dandiset.identifier}/draft',
'version': 'draft',
'url': url,
'dateCreated': UTC_ISO_TIMESTAMP_RE,
'citation': (
f'{user.last_name}, {user.first_name} ({year}) {name} '
f'(Version draft) [Data set]. DANDI archive. {url}'
),
'@context': f'https://raw.githubusercontent.com/dandi/schema/master/releases/{settings.DANDI_SCHEMA_VERSION}/context.json',
'schemaVersion': settings.DANDI_SCHEMA_VERSION,
'schemaKey': 'Dandiset',
'access': [{'schemaKey': 'AccessRequirements', 'status': 'dandi:EmbargoedAccess'}],
'repository': settings.DANDI_WEB_APP_URL,
'contributor': [
{
'name': 'Doe, John',
'email': user.email,
'roleName': ['dcite:ContactPerson'],
'schemaKey': 'Person',
'affiliation': [],
'includeInCitation': True,
}
],
'assetsSummary': {
'schemaKey': 'AssetsSummary',
'numberOfBytes': 0,
'numberOfFiles': 0,
},
}
@pytest.mark.django_db()
def test_dandiset_rest_create_with_duplicate_identifier(api_client, admin_user, dandiset):
api_client.force_authenticate(user=admin_user)
name = 'Test Dandiset'
identifier = dandiset.identifier
metadata = {'foo': 'bar', 'identifier': f'DANDI:{identifier}'}
response = api_client.post(
'/api/dandisets/',
{'name': name, 'metadata': metadata},
format='json',
)
assert response.status_code == 400
assert response.data == f'Dandiset {identifier} already exists'
@pytest.mark.django_db()
def test_dandiset_rest_create_with_invalid_identifier(api_client, admin_user):
api_client.force_authenticate(user=admin_user)
name = 'Test Dandiset'
identifier = 'abc123'
metadata = {'foo': 'bar', 'identifier': identifier}
response = api_client.post(
'/api/dandisets/',
{'name': name, 'metadata': metadata},
format='json',
)
assert response.status_code == 400
assert response.data == f'Invalid Identifier {identifier}'
@pytest.mark.parametrize(
('embargo_status'),
[choice[0] for choice in Dandiset.EmbargoStatus.choices],
ids=[choice[1] for choice in Dandiset.EmbargoStatus.choices],
)
@pytest.mark.django_db()
def test_dandiset_rest_delete(api_client, draft_version_factory, user, embargo_status):
draft_version = draft_version_factory(dandiset__embargo_status=embargo_status)
api_client.force_authenticate(user=user)
assign_perm('owner', user, draft_version.dandiset)
response = api_client.delete(f'/api/dandisets/{draft_version.dandiset.identifier}/')
assert response.status_code == 204
assert not Dandiset.objects.all()
@pytest.mark.django_db()
def test_dandiset_rest_delete_with_zarrs(
api_client,
draft_version,
user,
zarr_archive_factory,
draft_asset_factory,
):
api_client.force_authenticate(user=user)
assign_perm('owner', user, draft_version.dandiset)
zarr = zarr_archive_factory(dandiset=draft_version.dandiset)
asset = draft_asset_factory(blob=None, embargoed_blob=None, zarr=zarr)
# Add paths
add_asset_paths(asset=asset, version=draft_version)
# Delete
response = api_client.delete(f'/api/dandisets/{draft_version.dandiset.identifier}/')
assert response.status_code == 204
assert not Dandiset.objects.all()
@pytest.mark.django_db()
def test_dandiset_rest_delete_not_an_owner(api_client, draft_version, user):
api_client.force_authenticate(user=user)
response = api_client.delete(f'/api/dandisets/{draft_version.dandiset.identifier}/')
assert response.status_code == 403
assert draft_version.dandiset in Dandiset.objects.all()
@pytest.mark.django_db()
def test_dandiset_rest_delete_published(api_client, published_version, user):
api_client.force_authenticate(user=user)
assign_perm('owner', user, published_version.dandiset)
response = api_client.delete(f'/api/dandisets/{published_version.dandiset.identifier}/')
assert response.status_code == 403
assert response.data == 'Cannot delete dandisets with published versions.'
assert published_version.dandiset in Dandiset.objects.all()
@pytest.mark.django_db()
def test_dandiset_rest_delete_published_admin(api_client, published_version, admin_user):
api_client.force_authenticate(user=admin_user)
response = api_client.delete(f'/api/dandisets/{published_version.dandiset.identifier}/')
assert response.status_code == 403
assert response.data == 'Cannot delete dandisets with published versions.'
assert published_version.dandiset in Dandiset.objects.all()
@pytest.mark.django_db()
def test_dandiset_rest_get_owners(api_client, dandiset, social_account):
assign_perm('owner', social_account.user, dandiset)
resp = api_client.get(f'/api/dandisets/{dandiset.identifier}/users/')
assert resp.status_code == 200
assert resp.data == [
{
'username': social_account.extra_data['login'],
'name': social_account.extra_data['name'],
}
]
@pytest.mark.django_db()
def test_dandiset_rest_get_owners_no_social_account(api_client, dandiset, user):
assign_perm('owner', user, dandiset)
resp = api_client.get(f'/api/dandisets/{dandiset.identifier}/users/')
assert resp.status_code == 200
assert resp.data == [{'username': user.username, 'name': f'{user.first_name} {user.last_name}'}]
@pytest.mark.django_db()
def test_dandiset_rest_change_owner(
api_client,
draft_version,
user_factory,
social_account_factory,
mailoutbox,
):
dandiset = draft_version.dandiset
user1 = user_factory()
user2 = user_factory()
social_account2 = social_account_factory(user=user2)
assign_perm('owner', user1, dandiset)
api_client.force_authenticate(user=user1)
resp = api_client.put(
f'/api/dandisets/{dandiset.identifier}/users/',
[{'username': social_account2.extra_data['login']}],
format='json',
)
assert resp.status_code == 200
assert resp.data == [
{
'username': social_account2.extra_data['login'],
'name': social_account2.extra_data['name'],
}
]
assert list(dandiset.owners) == [user2]
assert len(mailoutbox) == 2
assert mailoutbox[0].subject == f'Removed from Dandiset "{dandiset.draft_version.name}"'
assert mailoutbox[0].to == [user1.email]
assert mailoutbox[1].subject == f'Added to Dandiset "{dandiset.draft_version.name}"'
assert mailoutbox[1].to == [user2.email]
@pytest.mark.django_db()
def test_dandiset_rest_add_owner(
api_client,
draft_version,
user_factory,
social_account_factory,
mailoutbox,
):
dandiset = draft_version.dandiset
user1 = user_factory()
user2 = user_factory()
social_account1 = social_account_factory(user=user1)
social_account2 = social_account_factory(user=user2)
assign_perm('owner', user1, dandiset)
api_client.force_authenticate(user=user1)
resp = api_client.put(
f'/api/dandisets/{dandiset.identifier}/users/',
[
{'username': social_account1.extra_data['login']},
{'username': social_account2.extra_data['login']},
],
format='json',
)
assert resp.status_code == 200
assert resp.data == [
{
'username': social_account1.extra_data['login'],
'name': social_account1.extra_data['name'],
},
{
'username': social_account2.extra_data['login'],
'name': social_account2.extra_data['name'],
},
]
assert list(dandiset.owners) == [user1, user2]
assert len(mailoutbox) == 1
assert mailoutbox[0].subject == f'Added to Dandiset "{dandiset.draft_version.name}"'
assert mailoutbox[0].to == [user2.email]
@pytest.mark.django_db()
def test_dandiset_rest_remove_owner(
api_client,
draft_version,
user_factory,
social_account_factory,
mailoutbox,
):
dandiset = draft_version.dandiset
user1 = user_factory()
user2 = user_factory()
social_account1 = social_account_factory(user=user1)
assign_perm('owner', user1, dandiset)
assign_perm('owner', user2, dandiset)
api_client.force_authenticate(user=user1)
resp = api_client.put(
f'/api/dandisets/{dandiset.identifier}/users/',
[{'username': social_account1.extra_data['login']}],
format='json',
)
assert resp.status_code == 200
assert resp.data == [
{
'username': social_account1.extra_data['login'],
'name': social_account1.extra_data['name'],
}
]
assert list(dandiset.owners) == [user1]
assert len(mailoutbox) == 1
assert mailoutbox[0].subject == f'Removed from Dandiset "{dandiset.draft_version.name}"'
assert mailoutbox[0].to == [user2.email]
@pytest.mark.django_db()
def test_dandiset_rest_not_an_owner(api_client, dandiset, user):
api_client.force_authenticate(user=user)
resp = api_client.put(
f'/api/dandisets/{dandiset.identifier}/users/',
[{'username': user.username}],
format='json',
)
assert resp.status_code == 403
@pytest.mark.django_db()
def test_dandiset_rest_delete_all_owners_fails(api_client, dandiset, user):
assign_perm('owner', user, dandiset)
api_client.force_authenticate(user=user)
resp = api_client.put(
f'/api/dandisets/{dandiset.identifier}/users/',
[],
format='json',
)
assert resp.status_code == 400
assert resp.data == ['Cannot remove all draft owners']
@pytest.mark.django_db()
def test_dandiset_rest_add_owner_does_not_exist(api_client, dandiset, user):
assign_perm('owner', user, dandiset)
api_client.force_authenticate(user=user)
fake_name = user.username + 'butnotreally'
resp = api_client.put(
f'/api/dandisets/{dandiset.identifier}/users/',
[{'username': fake_name}],
format='json',
)
assert resp.status_code == 400
assert resp.data == [f'User {fake_name} not found']
@pytest.mark.django_db()
def test_dandiset_rest_add_malformed(api_client, dandiset, user):
assign_perm('owner', user, dandiset)
api_client.force_authenticate(user=user)
resp = api_client.put(
f'/api/dandisets/{dandiset.identifier}/users/',
[{'email': user.email}],
format='json',
)