-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_admin.py
More file actions
1825 lines (1629 loc) · 79.6 KB
/
Copy pathtest_admin.py
File metadata and controls
1825 lines (1629 loc) · 79.6 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
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
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# DejaCode is a trademark of nexB Inc.
# SPDX-License-Identifier: AGPL-3.0-only
# See https://github.com/aboutcode-org/dejacode for support or download.
# See https://aboutcode.org for more information about AboutCode FOSS projects.
#
import datetime
from unittest.mock import patch
from django.contrib.admin.options import IS_POPUP_VAR
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.shortcuts import resolve_url
from django.test import TestCase
from django.urls import reverse
from django.utils.html import escape
from dje.copier import copy_object
from dje.filters import DataspaceFilter
from dje.models import Dataspace
from dje.models import History
from dje.tests import create_superuser
from license_library.admin import LicenseAdmin
from license_library.models import License
from license_library.models import LicenseAnnotation
from license_library.models import LicenseAssignedTag
from license_library.models import LicenseCategory
from license_library.models import LicenseChoice
from license_library.models import LicenseProfile
from license_library.models import LicenseProfileAssignedTag
from license_library.models import LicenseStatus
from license_library.models import LicenseStyle
from license_library.models import LicenseTag
from license_library.models import LicenseTagGroup
from license_library.models import LicenseTagGroupAssignedTag
from license_library.models import validate_slug_plus
from organization.models import Owner
class LicenseAdminCopyViewTestCase(TestCase):
def setUp(self):
self.nexb_dataspace = Dataspace.objects.create(name="nexB")
self.other_dataspace = Dataspace.objects.create(name="Other")
self.dataspace_target = Dataspace.objects.create(name="target_org")
self.user = get_user_model().objects.create_superuser(
"nexb_user", "test@test.com", "t3st", self.nexb_dataspace
)
self.other_user = get_user_model().objects.create_superuser(
"other_user", "other@test.com", "t3st", self.other_dataspace
)
self.owner = Owner.objects.create(name="Test Organization", dataspace=self.nexb_dataspace)
self.owner_other = Owner.objects.create(
name="Organization_Other", dataspace=self.other_dataspace
)
self.owner_target = Owner.objects.create(
name="Organization_Target", dataspace=self.dataspace_target
)
self.category1 = LicenseCategory.objects.create(
label="1: Category 1",
text="Some text",
dataspace=self.nexb_dataspace,
)
self.license_style1 = LicenseStyle.objects.create(
name="style1", dataspace=self.nexb_dataspace
)
self.license_profile1 = LicenseProfile.objects.create(
name="1: LicenseProfile1", dataspace=self.nexb_dataspace
)
self.license1 = License.objects.create(
key="license1",
name="License1",
short_name="License1",
category=self.category1,
dataspace=self.nexb_dataspace,
owner=self.owner,
license_style=self.license_style1,
license_profile=self.license_profile1,
full_text="abcdefghigklmnopqrstuvwxyz1234567890",
)
self.license2 = License.objects.create(
key="license2",
name="License2",
short_name="License2",
category=self.category1,
dataspace=self.nexb_dataspace,
owner=self.owner,
full_text="abcdef",
)
self.license3 = License.objects.create(
key="license3",
name="License3",
short_name="License3",
category=self.category1,
dataspace=self.nexb_dataspace,
owner=self.owner,
)
self.license_tag1 = LicenseTag.objects.create(
label="Tag 1", text="Text for tag1", dataspace=self.nexb_dataspace
)
self.license_tag2 = LicenseTag.objects.create(
label="Tag 2", text="Text for tag2", dataspace=self.nexb_dataspace
)
self.license_assigned_tag1 = LicenseAssignedTag.objects.create(
license=self.license1,
license_tag=self.license_tag1,
value=True,
dataspace=self.nexb_dataspace,
)
self.tag_group1 = LicenseTagGroup.objects.create(
name="Group1", dataspace=self.nexb_dataspace
)
self.tag_group_assigned_tag1 = LicenseTagGroupAssignedTag.objects.create(
license_tag_group=self.tag_group1,
license_tag=self.license_tag1,
dataspace=self.nexb_dataspace,
)
LicenseProfileAssignedTag.objects.create(
license_profile=self.license_profile1,
license_tag=self.license_tag1,
value=True,
dataspace=self.nexb_dataspace,
)
LicenseProfileAssignedTag.objects.create(
license_profile=self.license_profile1,
license_tag=self.license_tag2,
value=True,
dataspace=self.nexb_dataspace,
)
def test_license_copy_proper(self):
original_count = License.objects.count()
# Copy the Category
category1_copy = self.category1
category1_copy.id = None # Force an insert
category1_copy.dataspace = self.dataspace_target
category1_copy.save()
# Copy the LicenseProfile
license_profile1_copy = self.license_profile1
license_profile1_copy.id = None # Force an insert
license_profile1_copy.dataspace = self.dataspace_target
license_profile1_copy.save()
# Copy the LicenseStyle
license_style1_copy = self.license_style1
license_style1_copy.id = None # Force an insert
license_style1_copy.dataspace = self.dataspace_target
license_style1_copy.save()
self.client.login(username="nexb_user", password="t3st")
url = reverse("admin:license_library_license_copy")
data = {
"ct": str(ContentType.objects.get_for_model(License).pk),
"copy_candidates": str(self.license1.id),
"source": self.license1.dataspace.id,
"targets": [self.dataspace_target.id],
"form-TOTAL_FORMS": 1,
"form-INITIAL_FORMS": 1,
"form-0-ct": ContentType.objects.get_for_model(LicenseAssignedTag).pk,
}
self.client.post(url, data)
self.assertEqual(original_count + 1, License.objects.count())
new_license = License.objects.get(name=self.license1.name, dataspace=self.dataspace_target)
self.assertTrue(self.license1.category.label, new_license.category.label)
self.assertTrue(self.license1.owner.name, new_license.owner.name)
self.assertTrue(self.license1.license_profile.name, new_license.license_profile.name)
self.assertTrue(self.license1.license_style.name, new_license.license_style.name)
history = History.objects.get_for_object(new_license).get()
self.assertEqual(
'Copy object from "nexB" dataspace to "target_org" dataspace.',
history.get_change_message(),
)
def test_license_copy_with_m2m(self):
license_temp = License.objects.create(
key="license_temp",
name="License_temp",
short_name="License_temp",
category=self.category1,
dataspace=self.nexb_dataspace,
owner=self.owner,
license_style=self.license_style1,
license_profile=self.license_profile1,
full_text="abcdefghigklmnopqrstuvwxyz1234567890",
)
license_tag1 = LicenseTag.objects.create(
label="Tag temp", text="Text for tag1", dataspace=self.nexb_dataspace
)
LicenseAssignedTag.objects.create(
license=license_temp,
license_tag=license_tag1,
value=True,
dataspace=self.nexb_dataspace,
)
license_temp.save()
self.client.login(username="nexb_user", password="t3st")
url = reverse("admin:license_library_license_copy")
data = {
"ct": str(ContentType.objects.get_for_model(License).pk),
"copy_candidates": str(license_temp.id),
"source": license_temp.dataspace.id,
"targets": [self.dataspace_target.id],
"form-TOTAL_FORMS": 1,
"form-INITIAL_FORMS": 1,
"form-0-ct": ContentType.objects.get_for_model(LicenseAssignedTag).pk,
}
self.client.post(url, data)
self.assertEqual(
1,
len(
LicenseTag.objects.filter(
label="Tag temp", text="Text for tag1", dataspace=self.dataspace_target
)
),
)
new_tag = LicenseTag.objects.get(
label="Tag temp", text="Text for tag1", dataspace=self.dataspace_target
)
self.assertEqual(
1,
len(
LicenseAssignedTag.objects.filter(
license_tag=new_tag, dataspace=self.dataspace_target
)
),
)
history = History.objects.get_for_object(new_tag).get()
self.assertEqual(
'Copy object from "nexB" dataspace to "target_org" dataspace.',
history.get_change_message(),
)
def test_license_copy_to_multiple_targets(self):
self.client.login(username="nexb_user", password="t3st")
url = reverse("admin:license_library_license_copy")
data = {
"ct": str(ContentType.objects.get_for_model(License).pk),
"copy_candidates": str(self.license1.id),
"source": self.license1.dataspace.id,
"targets": [self.dataspace_target.id, self.other_dataspace.id],
"form-TOTAL_FORMS": 1,
"form-INITIAL_FORMS": 1,
"form-0-ct": ContentType.objects.get_for_model(LicenseAssignedTag).pk,
}
response = self.client.post(url, data)
self.assertContains(response, "<h2>The following Licenses have been copied</h2>")
license_in_target = License.objects.get(
uuid=self.license1.uuid, dataspace=self.dataspace_target
)
license_in_other = License.objects.get(
uuid=self.license1.uuid, dataspace=self.other_dataspace
)
self.assertContains(
response, "{} ({})".format(license_in_other, license_in_other.dataspace)
)
self.assertContains(
response, "{} ({})".format(license_in_target, license_in_target.dataspace)
)
history = History.objects.get_for_object(license_in_target).get()
self.assertEqual(
'Copy object from "nexB" dataspace to "target_org" dataspace.',
history.get_change_message(),
)
history = History.objects.get_for_object(license_in_other).get()
self.assertEqual(
'Copy object from "nexB" dataspace to "Other" dataspace.', history.get_change_message()
)
def test_license_copy_update_to_multiple_targets(self):
self.client.login(username="nexb_user", password="t3st")
url = reverse("admin:license_library_license_copy")
target_license = copy_object(self.license1, self.dataspace_target, self.user)
data = {
"ct": str(ContentType.objects.get_for_model(License).pk),
"copy_candidates": ",".join([str(self.license1.id), str(self.license2.id)]),
"select_for_update": str(target_license.id),
"source": self.license1.dataspace.id,
"targets": [self.dataspace_target.id, self.other_dataspace.id],
"form-TOTAL_FORMS": 1,
"form-INITIAL_FORMS": 1,
"form-0-ct": ContentType.objects.get_for_model(LicenseAssignedTag).pk,
}
response = self.client.post(url, data)
self.assertContains(response, "<h2>The following Licenses have been copied</h2>")
self.assertContains(response, "<h2>The following Licenses have been updated.</h2>")
self.assertEqual(3, len(response.context["copied"]))
self.assertEqual(1, len(response.context["updated"]))
self.assertEqual(0, len(response.context["errors"]))
def test_license_copy_with_issue_on_related_organization(self):
# Creating a new Owner in the target using the unique name
# of self.owner but a different uuid, for future integrity error
Owner.objects.create(name=self.license1.owner.name, dataspace=self.dataspace_target)
# We now copy the License in the target
self.client.login(username="nexb_user", password="t3st")
url = reverse("admin:license_library_license_copy")
data = {
"ct": str(ContentType.objects.get_for_model(License).pk),
"copy_candidates": str(self.license1.id),
"source": self.license1.dataspace.id,
"targets": [self.dataspace_target.id],
"form-TOTAL_FORMS": 1,
"form-INITIAL_FORMS": 1,
"form-0-ct": ContentType.objects.get_for_model(LicenseAssignedTag).pk,
}
response = self.client.post(url, data)
# ... License copy fail as the Owner cannot be match nor copy.
self.assertContains(response, "<h2>Errors for the following Licenses.</h2>", html=True)
def test_license_copy_update_into_self_dataspace(self):
# Here we are trying to copy an object in its current Dataspace
# This will raise an error on purpose, to prevent from doing it.
license = self.license1
dataspace = self.license1.dataspace
# Using the high_level copy function, the object is matched, and as
# we don't force update, nothing happen, None is returned
self.assertIsNone(copy_object(license, dataspace, self.user))
# Now with update=True, the AssertionError is raised
with self.assertRaises(AssertionError):
copy_object(license, dataspace, self.user, update=True)
from dje.copier import copy_to
from dje.copier import update_to
# Also error if using the low level copy method
with self.assertRaises(AssertionError):
copy_to(license, dataspace, self.user)
# Also error if using the low level update method
with self.assertRaises(AssertionError):
update_to(license, license, self.user)
def test_license_copy_with_tag_uuid_reconciliation(self):
# Create a copy of the license_tag1 in the target with a different
# name.
tag_in_target = LicenseTag.objects.create(
label="STAMP",
text=self.license_tag1.text,
uuid=self.license_tag1.uuid,
dataspace=self.other_dataspace,
)
# Also let make sure a tag with the original label already exist
# under another uuid.
LicenseTag.objects.create(label=self.license_tag1.label, dataspace=self.other_dataspace)
self.client.login(username="nexb_user", password="t3st")
url = reverse("admin:license_library_license_copy")
data = {
"ct": str(ContentType.objects.get_for_model(License).pk),
"copy_candidates": str(self.license1.id),
"source": self.license1.dataspace.id,
"targets": [self.other_dataspace.id],
"form-TOTAL_FORMS": 1,
"form-INITIAL_FORMS": 1,
"form-0-ct": ContentType.objects.get_for_model(LicenseAssignedTag).pk,
}
self.client.post(url, data)
copied_license = License.objects.get(key=self.license1.key, dataspace=self.other_dataspace)
copied_assigned_tag = LicenseAssignedTag.objects.get(
license=copied_license, license_tag=tag_in_target
)
self.assertEqual(self.license_assigned_tag1.value, copied_assigned_tag.value)
def test_license_copy_with_annotation(self):
license_temp = License.objects.create(
key="license_temp",
name="License_temp",
short_name="License_temp",
category=self.category1,
dataspace=self.nexb_dataspace,
owner=self.owner,
license_style=self.license_style1,
license_profile=self.license_profile1,
full_text="abcdefghigklmnopqrstuvwxyz1234567890",
)
license_tag1 = LicenseTag.objects.create(
label="Tag temp", text="Text for tag1", dataspace=self.nexb_dataspace
)
licenseassignedtag = LicenseAssignedTag.objects.create(
license=license_temp,
license_tag=license_tag1,
value=True,
dataspace=self.nexb_dataspace,
)
LicenseAnnotation.objects.create(
license=license_temp,
assigned_tag=licenseassignedtag,
text="nexb copyright",
range_start_offset=3,
range_end_offset=5,
dataspace=self.nexb_dataspace,
)
self.client.login(username="nexb_user", password="t3st")
url = reverse("admin:license_library_license_copy")
data = {
"ct": str(ContentType.objects.get_for_model(License).pk),
"copy_candidates": str(license_temp.id),
"source": license_temp.dataspace.id,
"targets": [self.dataspace_target.id],
"form-TOTAL_FORMS": 1,
"form-INITIAL_FORMS": 1,
"form-0-ct": ContentType.objects.get_for_model(LicenseAssignedTag).pk,
}
self.client.post(url, data)
copied_license = License.objects.get(
uuid=license_temp.uuid, dataspace=self.dataspace_target
)
# Making sure the License was properly copied
self.assertTrue(copied_license)
# Now looking at the copied annotation
self.assertEqual(2, len(LicenseAnnotation.objects.all()))
new_annotation = LicenseAnnotation.objects.get(dataspace=self.dataspace_target)
self.assertEqual("nexb copyright", new_annotation.text)
self.assertEqual(5, new_annotation.range_end_offset)
self.assertEqual(3, new_annotation.range_start_offset)
history = History.objects.get_for_object(new_annotation).get()
self.assertEqual(
'Copy object from "nexB" dataspace to "target_org" dataspace.',
history.get_change_message(),
)
def test_license_copy_update_annotation(self):
license_temp = License.objects.create(
key="license_temp",
name="License_temp",
short_name="License_temp",
category=self.category1,
dataspace=self.nexb_dataspace,
owner=self.owner,
license_style=self.license_style1,
license_profile=self.license_profile1,
full_text="abcdefghigklmnopqrstuvwxyz1234567890",
)
license_tag1 = LicenseTag.objects.create(
label="Tag temp", text="Text for tag1", dataspace=self.nexb_dataspace
)
licenseassignedtag = LicenseAssignedTag.objects.create(
license=license_temp,
license_tag=license_tag1,
value=True,
dataspace=self.nexb_dataspace,
)
LicenseAnnotation.objects.create(
license=license_temp,
assigned_tag=licenseassignedtag,
text="nexb copyright",
range_start_offset=3,
range_end_offset=5,
dataspace=self.nexb_dataspace,
)
# Copy the license into the target, 1 annotation attached
self.client.login(username="nexb_user", password="t3st")
url = reverse("admin:license_library_license_copy")
data = {
"ct": str(ContentType.objects.get_for_model(License).pk),
"copy_candidates": str(license_temp.id),
"source": license_temp.dataspace.id,
"targets": [self.dataspace_target.id],
"form-TOTAL_FORMS": 1,
"form-INITIAL_FORMS": 1,
"form-0-ct": ContentType.objects.get_for_model(LicenseAssignedTag).pk,
}
self.client.post(url, data)
copied_license = License.objects.get(
uuid=license_temp.uuid, dataspace=self.dataspace_target
)
# Making sure the License was properly copied
self.assertTrue(copied_license)
# and that the annotation was copied along
self.assertEqual(1, copied_license.annotations.count())
# Attach a new annotation on the reference License
license_annotation = LicenseAnnotation.objects.create(
license=license_temp,
assigned_tag=licenseassignedtag,
text="nexb license",
range_start_offset=7,
range_end_offset=9,
dataspace=self.nexb_dataspace,
)
license_annotation.save()
# Run the same copy with update activated
data["select_for_update"] = str(copied_license.id)
self.client.post(url, data)
self.assertEqual(4, len(LicenseAnnotation.objects.all()))
new_annotation = LicenseAnnotation.objects.get(
dataspace=self.dataspace_target, uuid=license_annotation.uuid
)
self.assertEqual(license_annotation.text, new_annotation.text)
self.assertEqual(license_annotation.range_end_offset, new_annotation.range_end_offset)
self.assertEqual(license_annotation.range_start_offset, new_annotation.range_start_offset)
def test_license_update_with_m2m(self):
license_temp = License.objects.create(
key="license_temp",
name="License_temp",
short_name="License_temp",
category=self.category1,
dataspace=self.nexb_dataspace,
owner=self.owner,
license_style=self.license_style1,
license_profile=self.license_profile1,
full_text="abcdefghigklmnopqrstuvwxyz1234567890",
)
license_tag1 = LicenseTag.objects.create(
label="Tag temp", text="Text for tag1", dataspace=self.nexb_dataspace
)
LicenseAssignedTag.objects.create(
license=license_temp,
license_tag=license_tag1,
value=True,
dataspace=self.nexb_dataspace,
)
copied_license = copy_object(license_temp, self.dataspace_target, self.user)
self.client.login(username="nexb_user", password="t3st")
url = reverse("admin:license_library_license_copy")
data = {
"ct": str(ContentType.objects.get_for_model(License).pk),
"select_for_update": str(copied_license.id),
"source": license_temp.dataspace.id,
"targets": [self.dataspace_target.id],
"form-TOTAL_FORMS": 1,
"form-INITIAL_FORMS": 1,
"form-0-ct": ContentType.objects.get_for_model(LicenseAssignedTag).pk,
}
response = self.client.post(url, data)
self.assertContains(response, "<h2>The following Licenses have been updated.</h2>")
self.assertEqual(
1,
len(
LicenseTag.objects.filter(
label="Tag temp", text="Text for tag1", dataspace=self.dataspace_target
)
),
)
new_tag = LicenseTag.objects.get(
label="Tag temp", text="Text for tag1", dataspace=self.dataspace_target
)
self.assertEqual(
1,
len(
LicenseAssignedTag.objects.filter(
license_tag=new_tag, dataspace=self.dataspace_target
)
),
)
def test_attribute_error_change_view(self):
self.client.login(username="nexb_user", password="t3st")
URLS = [
"admin:license_library_license_changelist",
"admin:license_library_licensetaggroup_changelist",
"admin:license_library_licenseprofile_changelist",
# No issue with the LicenseCategory but testing it too for consistency
"admin:license_library_licensecategory_changelist",
]
# Testing with several kind of ids: non-existent, chars, special chars
for id in ["99999", "aaaa", "!$%%5E@"]:
for url in URLS:
response = self.client.get(url + id + "/")
self.assertEqual(404, response.status_code)
def test_object_copy_view_wrong_request(self):
self.client.login(username="nexb_user", password="t3st")
url = reverse("admin:license_library_license_copy")
response = self.client.get(url)
# Requesting the view without giving the required parameters
self.assertContains(response, "The requested page could not be found.", status_code=404)
def test_object_copy_view_maximum_ids_limit(self):
# Testing the limitation of Object we can copy at one time
# Maximum is currently 100
self.client.login(username="nexb_user", password="t3st")
url = reverse("admin:license_library_license_copy")
data = {
"ct": str(ContentType.objects.get_for_model(License).pk),
"ids": ",".join([str(x) for x in range(101)]),
}
response = self.client.get(url, data)
self.assertRedirects(response, reverse("admin:license_library_license_changelist"))
def test_object_copy_view_non_existing_id(self):
self.client.login(username="nexb_user", password="t3st")
url = reverse("admin:license_library_license_copy")
# Using a ContentType that does not exist
data = {"ids": "300000"}
response = self.client.get(url, data)
# Redirecting the user to the list view
self.assertRedirects(response, reverse("admin:license_library_license_changelist"))
def test_object_copy_view_as_reference_target_form(self):
self.assertTrue(self.user.dataspace.is_reference)
self.client.login(username="nexb_user", password="t3st")
url = reverse("admin:license_library_license_copy")
# Proper request as member of the Reference Dataspace
# The target_dataspace is not in the param at that stage
# the response should be the "ToDataspace" form page
response = self.client.get(url, {"ids": str(self.license1.id)})
self.assertTemplateUsed(response, "admin/object_copy_dataspace_form.html")
self.assertContains(response, "<h1>Choose the target Dataspace(s).</h1>")
expected = (
f'<label for="id_target_0"><input type="checkbox" name="target"'
f' value="{self.other_dataspace.id}" id="id_target_0" />'
f" {self.other_dataspace.name}</label>"
)
self.assertContains(response, expected, html=True)
expected = (
f'<label for="id_target_1"><input type="checkbox" name="target"'
f' value="{self.dataspace_target.id}" id="id_target_1" />'
f" {self.dataspace_target.name}</label>"
)
self.assertContains(response, expected, html=True)
# Making sure my Dataspace is not a choice of the Form
self.assertNotContains(response, f'name="target" value="{self.nexb_dataspace.id}"')
def test_object_copy_update_view_as_reference_pre_copy(self):
self.assertTrue(self.user.dataspace.is_reference)
self.client.login(username="nexb_user", password="t3st")
url = reverse("admin:license_library_license_copy")
# Copying license2 so we have 1 object candidate for copy and one for update
copy_object(self.license2, self.dataspace_target, self.user)
# This time, the target is provided in the GET data
data = {
"ids": "{}, {}".format(self.license1.id, self.license2.id),
"target": str(self.dataspace_target.id),
}
response = self.client.get(url, data)
# The request is correct, presenting the confirmation page to the user
self.assertContains(response, "<h2>The following Licenses will be copied.</h2>")
expected = "<li>{} from {} to {}</li>".format(
self.license1.get_admin_link(), self.license1.dataspace, self.dataspace_target
)
self.assertContains(response, expected, html=True)
# Checking the exclude fields options
exclude1 = (
'<input id="id_exclude_copy_0" name="exclude_copy" type="checkbox"'
' value="admin_notes" />'
)
# The usage_policy is always checked by default for exclusion
exclude2 = (
'<input type="checkbox" name="exclude_copy" value="usage_policy"'
' id="id_exclude_copy_26" checked />'
)
self.assertContains(response, exclude1, html=True)
self.assertContains(response, exclude2, html=True)
self.assertContains(
response,
'<input id="id_form-0-exclude_copy_0" name="form-0-exclude_copy" type="checkbox"'
' value="value" />',
html=True,
)
self.assertContains(
response,
'<input id="id_form-0-exclude_update_0" name="form-0-exclude_update" type="checkbox"'
' value="value" />',
html=True,
)
# Let's make sure we do not have the following fields
self.assertNotContains(response, "uuid")
self.assertNotContains(response, "created_date")
self.assertNotContains(response, "created_by")
self.assertNotContains(response, "last_modified_date")
self.assertNotContains(response, "last_modified_by")
self.assertNotContains(response, "request_count")
def test_object_copy_with_fields_exclude(self):
self.assertTrue(self.license1.category)
self.assertTrue(self.license1.license_style)
self.assertTrue(self.license1.license_profile)
self.assertTrue(self.license1.full_text)
excluded_fields = ["category", "license_style", "license_profile", "full_text"]
exclude = {self.license1.__class__: excluded_fields}
copied_object = copy_object(
self.license1, self.dataspace_target, self.user, exclude=exclude
)
self.assertFalse(copied_object.category)
self.assertFalse(copied_object.license_style)
self.assertFalse(copied_object.license_profile)
self.assertFalse(copied_object.full_text)
def test_object_update_with_fields_exclude(self):
self.license1.curation_level = 0 # Just in case
self.license1.save()
copied_object = copy_object(self.license1, self.dataspace_target, self.user)
copied_object.full_text = "New full_text"
copied_object.short_name = "New short_name"
copied_object.is_active = True
copied_object.save()
# Refresh the self.license1 instance
self.license1 = License.objects.get(uuid=self.license1.uuid, dataspace=self.nexb_dataspace)
self.license1.curation_level = 99
self.license1.save()
excluded_fields = ["full_text", "short_name", "is_active"]
exclude = {self.license1.__class__: excluded_fields}
# Force update including fields exclude
copied_object = copy_object(
self.license1, self.dataspace_target, self.user, update=True, exclude=exclude
)
# Making sure the exclude fields were properly ignored
self.assertEqual("New full_text", copied_object.full_text)
self.assertEqual("New short_name", copied_object.short_name)
self.assertTrue(copied_object.is_active)
# And the others properly updated
self.assertEqual(99, copied_object.curation_level)
def test_object_copy_view_pre_copy_including_update(self):
self.client.login(username="nexb_user", password="t3st")
url = reverse("admin:license_library_licensecategory_copy")
copied_object = copy_object(self.category1, self.dataspace_target, self.user)
self.assertEqual(self.category1.uuid, copied_object.uuid)
self.assertNotEqual(self.category1.dataspace, copied_object.dataspace)
# Copied category will be matched and be a candidate for an update
data = {
"ids": str(self.category1.id),
"target": str(self.dataspace_target.id),
}
response = self.client.get(url, data)
# The request is correct, presenting the confirmation page to the user
# Offering the option the update the match
self.assertContains(response, "Select the ones that you would like to apply data changes.")
self.assertContains(
response,
'<input type="checkbox" name="select_for_update" value="{}">'.format(copied_object.id),
)
self.assertContains(response, "Make the Copy and Update")
def test_object_copy_view_as_non_reference(self):
self.assertFalse(self.other_user.dataspace.is_reference)
self.client.login(username="other_user", password="t3st")
url = reverse("admin:license_library_licensecategory_copy")
# Using a Object that is not from the Reference Dataspace
category = LicenseCategory.objects.create(
label="4: Category 5", text="Some text", dataspace=self.other_dataspace
)
self.assertFalse(category.dataspace.is_reference)
data = {"ids": str(category.id)}
response = self.client.get(url, data)
# As you cannot copy from a non Reference Dataspace you are redirected
self.assertRedirects(response, reverse("admin:license_library_licensecategory_changelist"))
# Now, Using a Category from the Reference Dataspace
self.assertTrue(self.category1.dataspace.is_reference)
data["ids"] = str(self.category1.id)
response = self.client.get(url, data)
# The request is correct, presenting the confirmation page to the user
self.assertContains(response, "<h2>The following License categories will be copied.</h2>")
expected = "<li><strong>{}</strong> from {} to {}</li>".format(
self.category1, self.nexb_dataspace, self.other_user.dataspace
)
self.assertContains(response, expected, html=True)
def test_object_copy_view_as_non_reference_source_target_validity(self):
self.assertFalse(self.other_user.dataspace.is_reference)
self.client.login(username="other_user", password="t3st")
url = reverse("admin:license_library_licensecategory_copy")
# Using a Object that is not from the Reference Dataspace
category = LicenseCategory.objects.create(
label="4: Category 5", text="Some text", dataspace=self.other_dataspace
)
self.assertFalse(category.dataspace.is_reference)
data = {
"ct": str(ContentType.objects.get_for_model(LicenseCategory).pk),
"copy_candidates": str(category.id),
"source": "",
"form-TOTAL_FORMS": 1,
"form-INITIAL_FORMS": 1,
"form-0-ct": ContentType.objects.get_for_model(LicenseAssignedTag).pk,
}
# Source and target are not valid IDs, User is redirected
response = self.client.post(url, data)
self.assertContains(response, "The requested page could not be found.", status_code=404)
# Same thing using a name that do not exist in the DB
data["source"] = "SOME NAME"
response = self.client.post(url, data)
self.assertContains(response, "The requested page could not be found.", status_code=404)
# Now using proper source and target
data["source"] = category.dataspace
data["target"] = self.dataspace_target
response = self.client.post(url, data)
# Fail and redirect because if the User Dataspace is not
# the Reference:
# - Source must be the Reference Dataspace
# - Target must be the User Dataspace
self.assertFalse(category.dataspace.is_reference)
self.assertContains(response, "The requested page could not be found.", status_code=404)
# Making sure the copy did not happen
self.assertFalse(
LicenseCategory.objects.filter(label=category.label, dataspace=self.dataspace_target)
)
def test_object_copy_view_results(self):
# Testing copy, update and error
self.client.login(username="nexb_user", password="t3st")
url = reverse("admin:license_library_license_copy")
# Creating a license in the target with same uuid for match/update
license_for_update = License.objects.create(
uuid=self.license2.uuid,
key=self.license2.key,
name="Some Name",
short_name="Some Name",
owner=self.owner_target,
dataspace=self.dataspace_target,
)
# Creating 2 license with different uuid but same name, in both
# Dataspaces to generate an IntegrityError on the copy
license_for_error1 = License.objects.create(
key="any-key",
name="Apache 3.0",
short_name="Apache 3.0",
owner=self.owner,
dataspace=self.nexb_dataspace,
)
License.objects.create(
key="other-key",
owner=self.owner_target,
name=license_for_error1.name,
short_name=license_for_error1.name,
dataspace=self.dataspace_target,
)
data = {
"ct": str(ContentType.objects.get_for_model(License).pk),
"copy_candidates": ",".join([str(self.license1.id), str(license_for_error1.id)]),
"select_for_update": str(license_for_update.id),
"source": self.license1.dataspace.id,
"targets": [self.dataspace_target.id],
"form-TOTAL_FORMS": 1,
"form-INITIAL_FORMS": 1,
"form-0-ct": ContentType.objects.get_for_model(LicenseAssignedTag).pk,
}
response = self.client.post(url, data)
# Getting the added license during the copy
new_license = License.objects.get(key=self.license1.key, dataspace=self.dataspace_target)
# Making sure the correct template is returned
self.assertTemplateUsed(response, "admin/object_copy_results.html")
# Copy results
self.assertContains(response, "<h2>The following Licenses have been copied</h2>")
self.assertContains(response, new_license.get_admin_url())
# Update results
self.assertContains(response, "<h2>The following Licenses have been updated.</h2>")
self.assertContains(response, license_for_update.get_admin_url())
# Errors results
self.assertContains(response, "<h2>Errors for the following Licenses.</h2>")
self.assertContains(response, license_for_error1.get_admin_url())
self.assertContains(response, "duplicate key value violates unique constraint")
def test_license_copy_and_update_configuration_get_view(self):
# Testing the copy configuration view regarding m2m and one2m fields
self.client.login(username="nexb_user", password="t3st")
url = reverse("admin:license_library_license_copy")
data = {"ids": str(self.license1.id), "target": self.dataspace_target.pk}
response = self.client.get(url, data)
self.assertContains(response, "<strong>License assigned tag</strong>")
expected = (
'<input type="checkbox" name="exclude_copy" value="admin_notes"'
' id="id_exclude_copy_0" />'
)
self.assertContains(response, expected, html=True)
self.assertContains(response, "<strong>License annotation</strong>")
expected = (
'<input type="checkbox" name="form-1-exclude_copy" value="assigned_tag"'
' id="id_form-1-exclude_copy_0" />'
)
self.assertContains(response, expected, html=True)
expected = (
'<input type="checkbox" name="form-1-exclude_copy" value="quote"'
' id="id_form-1-exclude_copy_1" />'
)
self.assertContains(response, expected, html=True)
expected = (
'<input type="checkbox" name="form-1-exclude_copy" value="text"'
' id="id_form-1-exclude_copy_2" />'
)
self.assertContains(response, expected, html=True)
# Copy the object to simulation the update configuration view
copied_object = copy_object(self.license1, self.dataspace_target, self.user)
self.assertTrue(copied_object)
response = self.client.get(url, data)
self.assertContains(response, "<strong>License assigned tag</strong>")
expected = (
'<input type="checkbox" name="form-0-exclude_update" value="value"'
' id="id_form-0-exclude_update_0" />'
)
self.assertContains(response, expected, html=True)
self.assertContains(response, "<strong>License annotation</strong>")
expected = (
'<input type="checkbox" name="form-1-exclude_update" value="assigned_tag"'
' id="id_form-1-exclude_update_0" />'
)
self.assertContains(response, expected, html=True)
expected = (
'<input type="checkbox" name="form-1-exclude_update" value="quote"'
' id="id_form-1-exclude_update_1" />'
)
self.assertContains(response, expected, html=True)
expected = (
'<input type="checkbox" name="form-1-exclude_update" value="text"'
' id="id_form-1-exclude_update_2" />'
)
self.assertContains(response, expected, html=True)
def test_license_copy_configuration_post_view(self):
# Testing the copy configuration view regarding m2m and one2m fields
self.client.login(username="nexb_user", password="t3st")
url = reverse("admin:license_library_license_copy")
annotation1 = LicenseAnnotation.objects.create(
license=self.license1,
assigned_tag=self.license_assigned_tag1,
text="nexb copyright",
range_start_offset=3,
range_end_offset=5,
quote="quote",
dataspace=self.nexb_dataspace,
)
# Checking the pre-requisites
self.assertEqual(1, self.license1.tags.count())
self.assertTrue(self.license_assigned_tag1.value)
self.assertTrue(self.license1.full_text)
data = {
"ct": str(ContentType.objects.get_for_model(License).pk),
"copy_candidates": str(self.license1.id),
"source": self.license1.dataspace.id,
"targets": [self.dataspace_target.id],
"exclude_copy": "full_text",
"form-TOTAL_FORMS": 2,
"form-INITIAL_FORMS": 2,
"form-0-ct": ContentType.objects.get_for_model(LicenseAssignedTag).pk,
"form-0-exclude_copy": "value",
"form-1-ct": ContentType.objects.get_for_model(LicenseAnnotation).pk,
"form-1-exclude_copy": ["assigned_tag", "quote", "text"],
}
response = self.client.post(url, data)
self.assertContains(response, "<h2>The following Licenses have been copied</h2>")
# Getting the objects created in the target dataspace
copied_license = License.objects.get(
uuid=self.license1.uuid, dataspace=self.dataspace_target
)
copied_assigned_tag = LicenseAssignedTag.objects.get(
uuid=self.license_assigned_tag1.uuid, dataspace=self.dataspace_target
)