-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtest_admin.py
More file actions
3076 lines (2665 loc) · 137 KB
/
test_admin.py
File metadata and controls
3076 lines (2665 loc) · 137 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
import json
import urllib
import uuid
from unittest import mock
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.core.exceptions import NON_FIELD_ERRORS
from django.test import TestCase
from django.test import TransactionTestCase
from django.test.utils import override_settings
from django.urls import NoReverseMatch
from django.urls import reverse
from guardian.shortcuts import assign_perm
from component_catalog.admin import ComponentAdmin
from component_catalog.models import AcceptableLinkage
from component_catalog.models import Component
from component_catalog.models import ComponentAssignedPackage
from component_catalog.models import ComponentKeyword
from component_catalog.models import ComponentStatus
from component_catalog.models import ComponentType
from component_catalog.models import Package
from component_catalog.models import PackageAssignedLicense
from component_catalog.models import Subcomponent
from dje.copier import copy_object
from dje.filters import DataspaceFilter
from dje.models import Dataspace
from dje.models import ExternalReference
from dje.models import ExternalSource
from dje.models import History
from dje.tests import add_perm
from dje.tests import create_admin
from dje.tests import create_superuser
from license_library.models import License
from license_library.models import LicenseAssignedTag
from license_library.models import LicenseTag
from organization.models import Owner
from policy.models import AssociatedPolicy
from policy.models import UsagePolicy
from product_portfolio.models import Product
from product_portfolio.models import ProductComponent
from product_portfolio.models import ProductPackage
class ComponentAdminViewsTestCase(TestCase):
def setUp(self):
self.dataspace1 = Dataspace.objects.create(name="Dataspace")
self.alternate_dataspace = Dataspace.objects.create(name="Alternate")
self.user = get_user_model().objects.create_superuser(
"test", "test@test.com", "secret", self.dataspace1
)
self.admin_user = create_admin("admin_user", self.dataspace1)
self.alternate_super_user = create_superuser("alternate_user", self.alternate_dataspace)
self.type1 = ComponentType.objects.create(
label="Type1", notes="notes", dataspace=self.dataspace1
)
self.status1 = ComponentStatus.objects.create(
label="Status1", default_on_addition=False, dataspace=self.dataspace1
)
self.owner1 = Owner.objects.create(
name="owner1",
dataspace=self.dataspace1,
)
self.license1 = License.objects.create(
key="license1",
name="License1",
short_name="License1",
is_active=True,
owner=self.owner1,
dataspace=self.dataspace1,
)
self.license2 = License.objects.create(
key="license2",
name="License2",
short_name="License2",
is_active=True,
owner=self.owner1,
dataspace=self.dataspace1,
)
self.component1 = Component.objects.create(
name="Component1",
version="0.1",
type=self.type1,
owner=self.owner1,
homepage_url="http://localhost.com",
dataspace=self.dataspace1,
)
self.component2 = Component.objects.create(
name="Component2",
version="0.2",
type=self.type1,
owner=self.owner1,
homepage_url="http://localhost.com",
dataspace=self.dataspace1,
)
self.component3 = Component.objects.create(
name="Component3",
version="r1",
type=self.type1,
owner=self.owner1,
dataspace=self.dataspace1,
)
self.component4 = Component.objects.create(
name="Component4",
version="r3",
type=self.type1,
owner=self.owner1,
dataspace=self.dataspace1,
)
self.component5 = Component.objects.create(
name="Component5",
version="r4",
type=self.type1,
owner=self.owner1,
dataspace=self.dataspace1,
)
self.component6 = Component.objects.create(
name="Component6",
version="r5",
type=self.type1,
owner=self.owner1,
dataspace=self.dataspace1,
)
self.sub_component1 = Subcomponent.objects.create(
parent=self.component6,
child=self.component5,
dataspace=self.component6.dataspace,
notes="I have a parent 6, and child 5",
)
self.sub_component2 = Subcomponent.objects.create(
parent=self.component5,
child=self.component4,
dataspace=self.component5.dataspace,
notes="I have a parent 5, and child 4",
)
self.sub_component3 = Subcomponent.objects.create(
parent=self.component4,
child=self.component3,
dataspace=self.component4.dataspace,
notes="I have a parent 4, and child 3",
)
self.sub_component4 = Subcomponent.objects.create(
parent=self.component3,
child=self.component2,
dataspace=self.component3.dataspace,
notes="I have a parent 3, and child 2",
)
self.sub_component5 = Subcomponent.objects.create(
parent=self.component2,
child=self.component1,
dataspace=self.component2.dataspace,
notes="I have a parent 2, and child 1",
)
self.dataspace_target = Dataspace.objects.create(name="target_org")
self.product1 = Product.objects.create(
name="MyProduct", version="1.0", dataspace=self.dataspace1
)
def test_component_admin_views(self):
self.client.login(username="test", password="secret")
# List view
url = reverse("admin:component_catalog_component_changelist")
response = self.client.get(url)
self.assertContains(response, "Component1</a>")
# View on site link
self.assertContains(response, '<div class="grp-text"><span>View</span></div>')
self.assertContains(
response,
'<a href="{}" target="_blank">View</a>'.format(self.component1.get_absolute_url()),
)
# Details view
url = self.component1.get_admin_url()
response = self.client.get(url)
# Check if the Inlines are present
self.assertContains(response, '<h2 class="grp-collapse-handler">Child Components</h2>')
self.assertContains(
response,
'<a href="{}" class="grp-state-focus" target="_blank">View</a>'.format(
self.component1.get_absolute_url()
),
)
def test_component_admin_form_clean(self):
self.client.login(username="test", password="secret")
url = self.component1.get_admin_url()
# Using the name, organization and version from component2, to make
# sure POSTing this is raising the proper error and message.
data = {
"name": self.component2.name,
"version": self.component2.version,
"type": self.type1.id,
"curation_level": 0,
"related_children-INITIAL_FORMS": 0,
"related_children-TOTAL_FORMS": 0,
"componentassignedpackage_set-TOTAL_FORMS": 0,
"componentassignedpackage_set-INITIAL_FORMS": 0,
"dje-externalreference-content_type-object_id-TOTAL_FORMS": 0,
"dje-externalreference-content_type-object_id-INITIAL_FORMS": 0,
}
response = self.client.post(url, data)
self.assertContains(response, '<p class="errornote">Please correct the error below.</p>')
self.assertContains(
response, "Component with this Dataspace, Name and Version already exists."
)
expected = {
NON_FIELD_ERRORS: ["Component with this Dataspace, Name and Version already exists."]
}
self.assertEqual(expected, response.context_data["adminform"].form.errors)
# Removing one of the required field and POST again
del data["name"]
response = self.client.post(url, data)
self.assertContains(response, '<p class="errornote">Please correct the error below.</p>')
self.assertContains(response, '<ul class="errorlist"><li>This field is required.</li>')
@override_settings(REFERENCE_DATASPACE="Dataspace")
def test_component_admin_form_clean_validate_against_reference_data(self):
self.client.login(username=self.alternate_super_user, password="secret")
url = reverse("admin:component_catalog_component_add")
data = {
"name": self.component1.name,
"version": self.component1.version,
"curation_level": 0,
"related_children-INITIAL_FORMS": 0,
"related_children-TOTAL_FORMS": 0,
"componentassignedpackage_set-TOTAL_FORMS": 0,
"componentassignedpackage_set-INITIAL_FORMS": 0,
"dje-externalreference-content_type-object_id-TOTAL_FORMS": 0,
"dje-externalreference-content_type-object_id-INITIAL_FORMS": 0,
}
response = self.client.post(url, data)
absolute_link = self.component1.get_absolute_link(target="_blank")
copy_link = self.component1.get_html_link(
self.component1.get_copy_url(), value="Copy to my Dataspace", target="_blank"
)
error = (
f"The application object that you are creating already exists as "
f"{absolute_link} in the reference dataspace. {copy_link}"
)
expected = {
"version": [error],
"name": [error],
}
self.assertEqual(expected, response.context_data["adminform"].form.errors)
@override_settings(REFERENCE_DATASPACE="Dataspace")
def test_component_admin_changelist_reference_data_link(self):
url = reverse("admin:component_catalog_component_changelist")
reference_params = "?{}={}".format(DataspaceFilter.parameter_name, self.dataspace1.id)
reference_link = '<a href="{}" class="reference-data-link">View Reference Data</a>'.format(
reference_params
)
my_dataspace_link = '<a href="?" class="reference-data-link">View My Data</a>'
self.client.login(username=self.alternate_super_user, password="secret")
response = self.client.get(url)
self.assertContains(response, reference_link)
self.assertNotContains(response, my_dataspace_link)
self.assertFalse(getattr(response.context_data["cl"], "my_dataspace_link", None))
self.assertEqual(
reference_params, getattr(response.context_data["cl"], "reference_params", None)
)
# Not displayed in popup mode
response = self.client.get("{}?{}=1".format(url, IS_POPUP_VAR))
self.assertNotContains(response, reference_link)
self.assertNotContains(response, my_dataspace_link)
self.assertFalse(getattr(response.context_data["cl"], "my_dataspace_link", None))
self.assertFalse(getattr(response.context_data["cl"], "reference_params", None))
response = self.client.get("{}{}".format(url, reference_params))
self.assertNotContains(response, reference_link)
self.assertContains(response, my_dataspace_link)
self.assertTrue(getattr(response.context_data["cl"], "my_dataspace_link", None))
self.assertFalse(getattr(response.context_data["cl"], "reference_params", None))
with override_settings(REFERENCE_DATASPACE=""):
response = self.client.get(url)
self.assertNotContains(response, reference_link)
self.assertNotContains(response, my_dataspace_link)
self.client.login(username=self.user.username, password="secret")
response = self.client.get(url)
self.assertNotContains(response, reference_link)
self.assertNotContains(response, my_dataspace_link)
url = reverse("admin:component_catalog_subcomponent_changelist")
self.client.login(username=self.alternate_super_user, password="secret")
response = self.client.get("{}{}".format(url, reference_params))
self.assertNotContains(response, "copy_to")
def test_subcomponent_inline_admin_form_edit_clean(self):
# This is an EDITION case
self.client.login(username="test", password="secret")
url = self.component1.get_admin_url()
dataspace = Dataspace.objects.create(name="Other")
other_org = Owner.objects.create(name="Other Org", dataspace=dataspace)
other_type = ComponentType.objects.create(
label="Other Type", notes="notes", dataspace=dataspace
)
other_component = Component.objects.create(
name="Component1", version="0.1", type=other_type, owner=other_org, dataspace=dataspace
)
data = {
"related_children-INITIAL_FORMS": 0,
"related_children-TOTAL_FORMS": 1,
"related_children-0-notes": "Notes",
"related_children-0-parent": self.component1.id,
"related_children-0-child": other_component.id,
"componentassignedpackage_set-TOTAL_FORMS": 0,
"componentassignedpackage_set-INITIAL_FORMS": 0,
"dje-externalreference-content_type-object_id-TOTAL_FORMS": 0,
"dje-externalreference-content_type-object_id-INITIAL_FORMS": 0,
}
response = self.client.post(url, data)
error = "Select a valid choice. That choice is not one of the available choices."
self.assertContains(response, error)
other_component.name = "New Component Name"
other_component.dataspace = self.component1.dataspace
other_component.owner = self.component1.owner
other_component.type = self.component1.type
other_component.save()
response = self.client.post(url, data)
self.assertNotContains(response, error)
def test_component_available_actions(self):
delete_input = '<option value="delete_selected">Delete selected components</option>'
compare_input = '<option value="compare_with">Compare the selected object</option>'
copy_input = '<option value="copy_to">Copy the selected objects</option>'
add_to_product_input = (
'<option value="add_to_product">Add the selected components to a product</option>'
)
set_policy = '<option value="set_policy">Set usage policy from licenses</option>'
self.client.login(username="test", password="secret")
url = reverse("admin:component_catalog_component_changelist")
# Looking at my own Dataspace data (default)
response = self.client.get(url)
self.assertContains(response, delete_input)
self.assertNotContains(response, compare_input)
self.assertNotContains(response, copy_input)
self.assertContains(response, add_to_product_input)
self.assertContains(response, set_policy)
# Looking at data from another Dataspace
data = {DataspaceFilter.parameter_name: self.alternate_dataspace.id}
response = self.client.get(url, data)
self.assertNotContains(response, delete_input)
self.assertContains(response, compare_input)
self.assertContains(response, copy_input)
self.assertNotContains(response, add_to_product_input)
self.assertNotContains(response, set_policy)
def test_edit_component_assigned_package_replacement(self):
self.client.login(username="test", password="secret")
url = self.component1.get_admin_url()
# Making sure we have unicode char in the name
package1 = Package.objects.create(
filename="\u02a0package1.zip", dataspace=self.component1.dataspace
)
assigned_package1 = ComponentAssignedPackage.objects.create(
component=self.component1, package=package1, dataspace=self.component1.dataspace
)
params = {
"name": self.component1.name,
"curation_level": self.component1.curation_level,
"related_children-INITIAL_FORMS": 0,
"related_children-TOTAL_FORMS": 0,
"dje-externalreference-content_type-object_id-TOTAL_FORMS": "0",
"dje-externalreference-content_type-object_id-INITIAL_FORMS": "0",
"componentassignedpackage_set-TOTAL_FORMS": 1,
"componentassignedpackage_set-INITIAL_FORMS": 1,
"componentassignedpackage_set-0-id": assigned_package1.id,
"componentassignedpackage_set-0-component": self.component1.id,
"componentassignedpackage_set-0-package": package1.id,
}
response = self.client.post(url, params, follow=True)
self.assertRedirects(response, reverse("admin:component_catalog_component_changelist"))
component = Component.objects.get(pk=self.component1.pk)
# Save went through, nothing changed so far.
self.assertEqual(package1, component.componentassignedpackage_set.get().package)
# Replacing the existing file by a another one
package2 = Package.objects.create(
filename="\u02a0package2.zip", dataspace=self.component1.dataspace
)
params["componentassignedpackage_set-0-package"] = package2.id
self.client.post(url, params)
component.refresh_from_db()
self.assertEqual(package2, component.componentassignedpackage_set.get().package)
def test_component_admin_form_inline_formsets_data_tampered(self):
self.client.login(username="test", password="secret")
url = self.component1.get_admin_url()
package1 = Package.objects.create(filename="p1.zip", dataspace=self.component1.dataspace)
assigned_package1 = ComponentAssignedPackage.objects.create(
component=self.component1, package=package1, dataspace=self.component1.dataspace
)
response = self.client.get(url)
self.assertContains(response, "componentassignedpackage_set-0")
self.assertContains(
response,
'<input type="hidden" name="componentassignedpackage_set-TOTAL_FORMS" value="1"'
' id="id_componentassignedpackage_set-TOTAL_FORMS" />',
html=True,
)
self.assertContains(
response,
'<input type="hidden" name="componentassignedpackage_set-INITIAL_FORMS" value="1"'
' id="id_componentassignedpackage_set-INITIAL_FORMS" />',
html=True,
)
# Calling the changeform view, componentassignedpackage_set-0 are properly set
data = {
"name": self.component1.name,
"curation_level": 0,
"related_children-INITIAL_FORMS": 0,
"related_children-TOTAL_FORMS": 0,
"dje-externalreference-content_type-object_id-TOTAL_FORMS": "0",
"dje-externalreference-content_type-object_id-INITIAL_FORMS": "0",
"componentassignedpackage_set-TOTAL_FORMS": 1,
"componentassignedpackage_set-INITIAL_FORMS": 1,
"componentassignedpackage_set-0-id": assigned_package1.id,
"componentassignedpackage_set-0-component": self.component1.id,
"componentassignedpackage_set-0-package": package1.id,
}
# Deletes the assigned_package1 relation, so the componentassignedpackage_set-0 are
# not valid anymore.
assigned_package1.delete()
response = self.client.post(url, data, follow=True)
self.assertRedirects(response, url)
self.assertNotContains(response, "componentassignedpackage_set-0")
self.assertContains(
response,
'<input type="hidden" name="componentassignedpackage_set-TOTAL_FORMS" value="0"'
' id="id_componentassignedpackage_set-TOTAL_FORMS" />',
html=True,
)
self.assertContains(
response,
'<input type="hidden" name="componentassignedpackage_set-INITIAL_FORMS" value="0"'
' id="id_componentassignedpackage_set-INITIAL_FORMS" />',
html=True,
)
self.assertContains(response, "Form data outdated or inconsistent.")
self.assertContains(response, "The form data has been refreshed.")
messages = list(response.context["messages"])
expected = "Form data outdated or inconsistent. The form data has been refreshed."
self.assertEqual(expected, str(messages[0]))
def test_activity_log_activated(self):
self.client.login(username="test", password="secret")
response = self.client.get(reverse("admin:component_catalog_component_changelist"))
self.assertContains(response, "activity_log_link")
def test_component_admin_changelist_list_display_as_popup(self):
self.client.login(username="test", password="secret")
url = reverse("admin:component_catalog_component_changelist")
expect1 = '<input type="checkbox" id="action-toggle"'
expect2 = "get_hierarchy_link"
expect3 = "<span>View</span>"
expect4 = '<td class="action-checkbox">'
expect5 = 'title="Hierarchy"'
expect6 = "view_on_site"
response = self.client.get(url)
self.assertContains(response, expect1)
self.assertContains(response, expect2)
self.assertContains(response, expect3)
self.assertContains(response, expect4)
self.assertContains(response, expect5)
self.assertContains(response, expect6)
response = self.client.get(url + "?{}=1".format(IS_POPUP_VAR))
self.assertNotContains(response, expect1)
self.assertNotContains(response, expect2)
self.assertNotContains(response, expect3)
self.assertNotContains(response, expect4)
self.assertNotContains(response, expect5)
self.assertNotContains(response, expect6)
def test_component_unicity_in_form_validation_on_addition(self):
# Let's take an existing Component an try to save a new one using the
# unique_together value from the existing one.
self.client.login(username="test", password="secret")
url = reverse("admin:component_catalog_component_add")
data = {
"name": self.component1.name,
"owner": self.component1.owner.id,
"version": self.component1.version,
"curation_level": 0,
"related_children-INITIAL_FORMS": 0,
"related_children-TOTAL_FORMS": 0,
"componentassignedpackage_set-TOTAL_FORMS": 0,
"componentassignedpackage_set-INITIAL_FORMS": 0,
"dje-externalreference-content_type-object_id-TOTAL_FORMS": 0,
"dje-externalreference-content_type-object_id-INITIAL_FORMS": 0,
}
response = self.client.post(url, data)
# Error a the Field level in the Form
self.assertContains(response, "Please correct the error below.")
# Global Formset error
self.assertContains(
response, "Component with this Dataspace, Name and Version already exists."
)
expected = {
NON_FIELD_ERRORS: ["Component with this Dataspace, Name and Version already exists."]
}
self.assertEqual(expected, response.context_data["adminform"].form.errors)
def test_component_admin_add_is_active_true_by_default(self):
self.client.login(username="test", password="secret")
url = reverse("admin:component_catalog_component_add")
data = {
"name": "new component",
"curation_level": 0,
"related_children-INITIAL_FORMS": 0,
"related_children-TOTAL_FORMS": 0,
"componentassignedpackage_set-TOTAL_FORMS": 0,
"componentassignedpackage_set-INITIAL_FORMS": 0,
"dje-externalreference-content_type-object_id-TOTAL_FORMS": 0,
"dje-externalreference-content_type-object_id-INITIAL_FORMS": 0,
}
self.client.post(url, data)
component = Component.objects.get(name=data["name"])
self.assertTrue(component.is_active)
def test_component_adminform_validate_case_insensitive_uniqueness(self):
self.client.login(username="test", password="secret")
url = reverse("admin:component_catalog_component_add")
data = {
"name": self.component1.name.upper(),
"curation_level": 0,
"related_children-INITIAL_FORMS": 0,
"related_children-TOTAL_FORMS": 0,
"componentassignedpackage_set-TOTAL_FORMS": 0,
"componentassignedpackage_set-INITIAL_FORMS": 0,
"dje-externalreference-content_type-object_id-TOTAL_FORMS": 0,
"dje-externalreference-content_type-object_id-INITIAL_FORMS": 0,
}
response = self.client.post(url, data)
expected = {
"name": [
"The application object that you are creating already exists as "
'"Component1". Note that a different case in the object name is not '
"sufficient to make it unique."
],
}
self.assertEqual(expected, response.context_data["adminform"].form.errors)
url = self.component2.get_admin_url()
response = self.client.post(url, data)
self.assertEqual(302, response.status_code)
data["name"] = self.component1.name
response = self.client.post(url, data)
self.assertEqual(302, response.status_code)
url = self.component1.get_admin_url()
data["version"] = self.component1.version
response = self.client.post(url, data)
self.assertEqual(302, response.status_code)
def test_component_adminform_validate_version(self):
self.client.login(username="test", password="secret")
url = reverse("admin:component_catalog_component_add")
data = {
"name": "c:o:m:p",
"version": "1:0",
"curation_level": 0,
"related_children-INITIAL_FORMS": 0,
"related_children-TOTAL_FORMS": 0,
"componentassignedpackage_set-TOTAL_FORMS": 0,
"componentassignedpackage_set-INITIAL_FORMS": 0,
"dje-externalreference-content_type-object_id-TOTAL_FORMS": 0,
"dje-externalreference-content_type-object_id-INITIAL_FORMS": 0,
}
response = self.client.post(url, data)
expected = {
"version": [
"Enter a valid value consisting of spaces, periods, letters, numbers, "
"or !#\"',&()+_-."
]
}
self.assertEqual(expected, response.context_data["adminform"].form.errors)
def test_component_curation_level_validation(self):
self.client.login(username="test", password="secret")
url = reverse("admin:component_catalog_component_add")
data = {
"name": "some name",
"curation_level": 101, # Using a value greater than the max
"related_children-INITIAL_FORMS": 0,
"related_children-TOTAL_FORMS": 0,
"componentassignedpackage_set-TOTAL_FORMS": 0,
"componentassignedpackage_set-INITIAL_FORMS": 0,
"dje-externalreference-content_type-object_id-TOTAL_FORMS": 0,
"dje-externalreference-content_type-object_id-INITIAL_FORMS": 0,
}
response = self.client.post(url, data)
self.assertContains(response, "Ensure this value is less than or equal to 100.")
data["curation_level"] = 99
response = self.client.post(url, data)
self.assertRedirects(response, reverse("admin:component_catalog_component_changelist"))
def test_component_save_as_proper(self):
# Setting some value for further validation
self.component1.configuration_status = self.status1
self.component1.save()
self.client.login(username="test", password="secret")
url = self.component1.get_admin_url()
response = self.client.get(url)
# Save as button is present on the Edit page
save_as_input_html = (
'<input type="submit" value="Save as new" class="grp-button" name="_saveasnew" />'
)
self.assertContains(response, save_as_input_html, html=True)
# We need to set a different Component.name to avoid raising validation
# errors
new_name = "THIS IS A NEW NAME"
data = {
"name": new_name,
"owner": self.component1.owner.id,
"type": self.component1.type.id,
"curation_level": 0,
"configuration_status": self.component1.configuration_status.id,
"_saveasnew": "Save as new",
"related_children-INITIAL_FORMS": 0,
"related_children-TOTAL_FORMS": 0,
"componentassignedpackage_set-TOTAL_FORMS": 0,
"componentassignedpackage_set-INITIAL_FORMS": 0,
"dje-externalreference-content_type-object_id-TOTAL_FORMS": 0,
"dje-externalreference-content_type-object_id-INITIAL_FORMS": 0,
}
response = self.client.post(url, data)
# A new Component was created
new_component = Component.objects.get(name=new_name)
self.assertRedirects(response, new_component.get_admin_url())
# Making sure the values of the Original Component were preserved
self.assertEqual(self.component1.owner, new_component.owner)
self.assertEqual(self.component1.type, new_component.type)
self.assertEqual(self.component1.is_active, new_component.is_active)
self.assertEqual(self.component1.configuration_status, new_component.configuration_status)
def test_component_save_as_with_failing_validation(self):
self.client.login(username="test", password="secret")
url = self.component1.get_admin_url()
response = self.client.get(url)
self.assertContains(response, 'name="_saveasnew"')
# Since request.GET is empty
self.assertNotContains(response, 'value="Save and go to next"')
response = self.client.get(url + "?_changelist_filters=o%3D3.4")
self.assertContains(response, 'name="_saveasnew"')
self.assertContains(response, 'value="Save and go to next"')
data = {
"name": self.component1.name,
"owner": self.component1.owner.id,
"version": self.component1.version,
"type": self.component1.type.id,
"curation_level": 0,
"_saveasnew": "Save as new",
"related_children-INITIAL_FORMS": 0,
"related_children-TOTAL_FORMS": 0,
"componentassignedpackage_set-TOTAL_FORMS": 0,
"componentassignedpackage_set-INITIAL_FORMS": 0,
"dje-externalreference-content_type-object_id-TOTAL_FORMS": 0,
"dje-externalreference-content_type-object_id-INITIAL_FORMS": 0,
}
# Save as new with no data modification, raise a form validation error
response = self.client.post(url, data)
context_data = response.context_data
self.assertContains(
response, "Component with this Dataspace, Name and Version already exists."
)
expected = {
NON_FIELD_ERRORS: ["Component with this Dataspace, Name and Version already exists."]
}
self.assertEqual(expected, context_data["adminform"].form.errors)
# We are now on a form that works like an ADDITION form
# Save as button is the only button present
self.assertContains(response, 'name="_saveasnew"')
self.assertNotContains(response, 'value="Save and go to next"')
self.assertEqual(context_data["save_as"], True)
self.assertEqual(context_data["add"], False)
self.assertEqual(context_data["change"], True)
def test_component_save_as_with_external_reference(self):
self.client.login(username="test", password="secret")
url = self.component1.get_admin_url()
ext_source1 = ExternalSource.objects.create(
label="GitHub",
dataspace=self.dataspace1,
)
er = ExternalReference.objects.create_for_content_object(
content_object=self.component1, external_source=ext_source1, external_id="external_id"
)
# We need to set a different Component.name to avoid raising validation errors
new_name = "THIS IS A NEW NAME"
data = {
"name": new_name,
"curation_level": 0,
"_saveasnew": "Save as new",
"related_children-INITIAL_FORMS": 0,
"related_children-TOTAL_FORMS": 0,
"componentassignedpackage_set-TOTAL_FORMS": 0,
"componentassignedpackage_set-INITIAL_FORMS": 0,
"dje-externalreference-content_type-object_id-TOTAL_FORMS": 1,
"dje-externalreference-content_type-object_id-INITIAL_FORMS": 1,
"dje-externalreference-content_type-object_id-0-id": er.id,
"dje-externalreference-content_type-object_id-0-external_source": er.external_source.id,
"dje-externalreference-content_type-object_id-0-external_id": er.external_id,
}
response = self.client.post(url, data)
# A new Component was created
new_component = Component.objects.get(name=new_name)
self.assertRedirects(response, new_component.get_admin_url())
# Making sure the values of the Original Component were preserved
self.assertEqual(self.component1.owner, self.owner1)
er1 = ExternalReference.objects.get_for_content_object(self.component1).get()
er2 = ExternalReference.objects.get_for_content_object(new_component).get()
# Making sure a new ExternalReference was created
self.assertNotEqual(er1.id, er2.id)
def test_component_edit_save_as_delete_inline(self):
self.client.login(username="test", password="secret")
url = self.component6.get_admin_url()
extra_subcomponent = Subcomponent.objects.create(
parent=self.component6, child=self.component1, dataspace=self.component6.dataspace
)
self.assertEqual(2, self.component6.related_children.count())
new_name = "THIS IS A NEW NAME"
data = {
"name": new_name,
"curation_level": 0,
"_saveasnew": "Save+as+new",
"related_children-0-id": self.sub_component1.pk,
"related_children-0-DELETE": "1",
"related_children-1-id": extra_subcomponent.pk,
"related_children-1-child": extra_subcomponent.child.pk,
"related_children-1-parent": extra_subcomponent.parent.pk,
"related_children-INITIAL_FORMS": "2",
"related_children-TOTAL_FORMS": "2",
"componentassignedpackage_set-TOTAL_FORMS": 0,
"componentassignedpackage_set-INITIAL_FORMS": 0,
"dje-externalreference-content_type-object_id-TOTAL_FORMS": 0,
"dje-externalreference-content_type-object_id-INITIAL_FORMS": 0,
}
response = self.client.post(url, data)
self.assertEqual(response.status_code, 302)
# Started with 2 children, one was deleted.
self.assertEqual(Component.objects.latest("id").related_children.count(), 1)
@override_settings(REFERENCE_DATASPACE="Dataspace")
def test_component_save_as_deactivated_for_dataspace(self):
# The 'Save as' button is not available when editing an object from
# another Dataspace
dataspace = Dataspace.objects.create(name="Other")
other_org = Owner.objects.create(name="Other Org", dataspace=dataspace)
other_type = ComponentType.objects.create(
label="Other Type", notes="notes", dataspace=dataspace
)
other_component = Component.objects.create(
name="Component1", version="0.1", type=other_type, owner=other_org, dataspace=dataspace
)
self.client.login(username="test", password="secret")
url = other_component.get_admin_url()
response = self.client.get(url)
# 'Save as' is not exposed in this case.
self.assertNotContains(response, 'name="_saveasnew"')
self.assertContains(response, "_addanother")
def test_component_save_as_with_inlines(self):
self.client.login(username="test", password="secret")
package1 = Package.objects.create(filename="p1.zip", dataspace=self.dataspace1)
assigned_package = ComponentAssignedPackage.objects.create(
component=self.component1, package=package1, dataspace=self.dataspace1
)
package_count = Package.objects.count()
assigned_package_count = ComponentAssignedPackage.objects.count()
self.component1.license_expression = self.license1.key
self.component1.save()
url = self.component1.get_admin_url()
response = self.client.get(url) # Make a GET request to get the csrf token
# We need to set a different Component.name to avoid raising validation errors
new_name = "THIS IS A NEW NAME"
data = {
"csrfmiddlewaretoken": str(response.context["csrf_token"]),
"name": new_name,
"version": self.component1.version,
"curation_level": 0,
"license_expression": self.component1.license_expression,
"_saveasnew": "Save as new",
"related_children-INITIAL_FORMS": 0,
"related_children-TOTAL_FORMS": 0,
"componentassignedpackage_set-TOTAL_FORMS": 1,
"componentassignedpackage_set-INITIAL_FORMS": 1,
"componentassignedpackage_set-0-package": assigned_package.package.id,
"componentassignedpackage_set-0-component": assigned_package.component.id,
"componentassignedpackage_set-0-id": assigned_package.id,
"dje-externalreference-content_type-object_id-TOTAL_FORMS": 0,
"dje-externalreference-content_type-object_id-INITIAL_FORMS": 0,
}
response = self.client.post(url, data)
self.assertEqual(302, response.status_code)
new_component = Component.objects.get(name=new_name, dataspace=self.dataspace1)
self.assertRedirects(response, new_component.get_admin_url())
self.assertEqual(1, new_component.packages.count())
self.assertEqual(package_count, Package.objects.count())
self.assertEqual(assigned_package_count + 1, ComponentAssignedPackage.objects.count())
# The assigned license was copied too
self.assertEqual(1, new_component.licenses.count())
self.assertEqual(self.component1.license_expression, new_component.license_expression)
def test_add_to_product_action_proper_component(self):
self.client.login(username=self.user.username, password="secret")
self.component1.license_expression = self.license1.key
self.component1.save()
ids = ",".join([str(self.component1.pk), str(self.component2.pk), str(self.component3.pk)])
add_to_product_url = reverse("admin:component_catalog_component_add_to_product")
# Simulate displaying the page
response = self.client.get("{}?ids={}".format(add_to_product_url, ids))
self.assertEqual(200, response.status_code)
self.assertTrue("form" in response.context)
response = self.client.post(
add_to_product_url, {"product": self.product1.pk, "ids": ids}, follow=True
)
self.assertRedirects(response, reverse("admin:component_catalog_component_changelist"))
self.assertEqual(3, self.product1.productcomponents.count())
# pc.license_expression is taken from the Component
pc = ProductComponent.objects.get(product=self.product1, component=self.component1)
self.assertEqual(self.license1.key, pc.license_expression)
self.assertEqual(self.user, pc.created_by)
self.assertEqual(self.user, pc.last_modified_by)
self.assertEqual(32, len(str(pc.created_date)))
self.assertEqual(32, len(str(pc.last_modified_date)))
self.assertFalse(History.objects.get_for_object(pc).exists())
self.assertEqual(self.user, pc.created_by)
self.assertTrue(pc.created_date)
expected = "3 component(s) added to MyProduct."
self.assertEqual(expected, list(response.context["messages"])[0].message)
response = self.client.post(
add_to_product_url, {"product": self.product1.pk, "ids": ids}, follow=True
)
expected = "0 component(s) added to MyProduct. 3 component(s) were already assigned."
self.assertEqual(expected, list(response.context["messages"])[0].message)
self.product1.refresh_from_db()
history_entries = History.objects.get_for_object(self.product1)
expected_messages = sorted(
[
'Added component "Component3 r1"',
'Added component "Component2 0.2"',
'Added component "Component1 0.1"',
]
)
self.assertEqual(
expected_messages, sorted([entry.change_message for entry in history_entries])
)
self.assertEqual(self.user, self.product1.last_modified_by)
def test_add_to_product_action_proper_package(self):
self.client.login(username=self.user.username, password="secret")
package1 = Package.objects.create(
filename="p1.zip", dataspace=self.dataspace1, license_expression=self.license1.key
)
ids = str(package1.pk)
add_to_product_url = reverse("admin:component_catalog_package_add_to_product")
# Simulate displaying the page
response = self.client.get("{}?ids={}".format(add_to_product_url, ids))
self.assertEqual(200, response.status_code)
self.assertTrue("form" in response.context)
response = self.client.post(
add_to_product_url, {"product": self.product1.pk, "ids": ids}, follow=True
)
self.assertRedirects(response, reverse("admin:component_catalog_package_changelist"))
self.assertEqual(1, self.product1.productpackages.count())
# pc.license_expression is taken from the Package
pp = ProductPackage.objects.get(product=self.product1, package=package1)
self.assertEqual(self.license1.key, pp.license_expression)
self.assertEqual(self.user, pp.created_by)
self.assertEqual(self.user, pp.last_modified_by)
self.assertEqual(32, len(str(pp.created_date)))
self.assertEqual(32, len(str(pp.last_modified_date)))
self.assertFalse(History.objects.get_for_object(pp).exists())
self.assertEqual(self.user, pp.created_by)
self.assertTrue(pp.created_date)
expected = "1 package(s) added to MyProduct."
self.assertEqual(expected, list(response.context["messages"])[0].message)
response = self.client.post(
add_to_product_url, {"product": self.product1.pk, "ids": ids}, follow=True
)
expected = "0 package(s) added to MyProduct. 1 package(s) were already assigned."
self.assertEqual(expected, list(response.context["messages"])[0].message)
self.product1.refresh_from_db()
history_entries = History.objects.get_for_object(self.product1)
expected_messages = ['Added package "p1.zip"']
self.assertEqual(expected_messages, [entry.change_message for entry in history_entries])
self.assertEqual(self.user, self.product1.last_modified_by)
def test_add_to_product_action_permission(self):
self.client.login(username=self.user.username, password="secret")
ids = ",".join([str(self.component1.pk), str(self.component2.pk), str(self.component3.pk)])
add_to_product_url = reverse("admin:component_catalog_component_add_to_product")
component_changelist_url = reverse("admin:component_catalog_component_changelist")
self.user.is_superuser = False
self.user.save()
self.assertFalse(self.user.has_perm("product_portfolio.add_productcomponent"))
response = self.client.get("{}?ids={}".format(add_to_product_url, ids))
self.assertEqual(404, response.status_code)
self.user = add_perm(self.user, "change_component")
self.assertNotContains(self.client.get(component_changelist_url), "add_to_product")
self.user = add_perm(self.user, "add_productcomponent")
self.assertTrue(self.user.has_perm("product_portfolio.add_productcomponent"))
response = self.client.get("{}?ids={}".format(add_to_product_url, ids))
self.assertEqual(200, response.status_code)
self.assertContains(self.client.get(component_changelist_url), "add_to_product")