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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Packages::Package, type: :model, feature_category: :package_registry do
include SortingHelper
using RSpec::Parameterized::TableSyntax
it_behaves_like 'having unique enum values'
it { is_expected.to be_a Packages::Downloadable }
describe 'relationships' do
it { is_expected.to belong_to(:project) }
it { is_expected.to belong_to(:creator) }
it { is_expected.to have_many(:package_files).dependent(:destroy) }
it { is_expected.to have_many(:dependency_links).inverse_of(:package) }
it { is_expected.to have_many(:tags).inverse_of(:package) }
it { is_expected.to have_many(:build_infos).inverse_of(:package) }
it { is_expected.to have_many(:installable_nuget_package_files).inverse_of(:package) }
it { is_expected.to have_one(:conan_metadatum).inverse_of(:package) }
it { is_expected.to have_one(:maven_metadatum).inverse_of(:package) }
it { is_expected.to have_one(:debian_publication).inverse_of(:package).class_name('Packages::Debian::Publication') }
it { is_expected.to have_one(:debian_distribution).through(:debian_publication).source(:distribution).inverse_of(:packages).class_name('Packages::Debian::ProjectDistribution') }
it { is_expected.to have_one(:nuget_metadatum).inverse_of(:package) }
it { is_expected.to have_one(:rubygems_metadatum).inverse_of(:package) }
it { is_expected.to have_one(:npm_metadatum).inverse_of(:package) }
it { is_expected.to have_one(:rpm_metadatum).inverse_of(:package) }
end
describe '.with_debian_codename' do
let_it_be(:publication) { create(:debian_publication) }
subject { described_class.with_debian_codename(publication.distribution.codename).to_a }
it { is_expected.to contain_exactly(publication.package) }
end
describe '.with_debian_codename_or_suite' do
let_it_be(:distribution1) { create(:debian_project_distribution, :with_suite) }
let_it_be(:distribution2) { create(:debian_project_distribution, :with_suite) }
let_it_be(:package1) { create(:debian_package, published_in: distribution1) }
let_it_be(:package2) { create(:debian_package, published_in: distribution2) }
context 'with a codename' do
subject { described_class.with_debian_codename_or_suite(distribution1.codename).to_a }
it { is_expected.to contain_exactly(package1) }
end
context 'with a suite' do
subject { described_class.with_debian_codename_or_suite(distribution2.suite).to_a }
it { is_expected.to contain_exactly(package2) }
end
end
describe '.with_composer_target' do
let!(:package1) { create(:composer_package, :with_metadatum, sha: '123') }
let!(:package2) { create(:composer_package, :with_metadatum, sha: '123') }
let!(:package3) { create(:composer_package, :with_metadatum, sha: '234') }
subject { described_class.with_composer_target('123').to_a }
it 'selects packages with the specified sha' do
expect(subject).to include(package1)
expect(subject).to include(package2)
expect(subject).not_to include(package3)
end
end
describe '.sort_by_attribute' do
let_it_be(:group) { create(:group, :public) }
let_it_be(:project) { create(:project, :public, namespace: group, name: 'project A') }
let!(:package1) { create(:npm_package, project: project, version: '3.1.0', name: "@#{project.root_namespace.path}/foo1") }
let!(:package2) { create(:nuget_package, project: project, version: '2.0.4') }
let(:package3) { create(:maven_package, project: project, version: '1.1.1', name: 'zzz') }
before do
travel_to(1.day.ago) do
package3
end
end
RSpec.shared_examples 'package sorting by attribute' do |order_by|
subject { described_class.where(id: packages.map(&:id)).sort_by_attribute("#{order_by}_#{sort}").to_a }
context "sorting by #{order_by}" do
context 'ascending order' do
let(:sort) { 'asc' }
it { is_expected.to eq packages }
end
context 'descending order' do
let(:sort) { 'desc' }
it { is_expected.to eq packages.reverse }
end
end
end
it_behaves_like 'package sorting by attribute', 'name' do
let(:packages) { [package1, package2, package3] }
end
it_behaves_like 'package sorting by attribute', 'created_at' do
let(:packages) { [package3, package1, package2] }
end
it_behaves_like 'package sorting by attribute', 'version' do
let(:packages) { [package3, package2, package1] }
end
it_behaves_like 'package sorting by attribute', 'type' do
let(:packages) { [package3, package1, package2] }
end
it_behaves_like 'package sorting by attribute', 'project_path' do
let(:another_project) { create(:project, :public, namespace: group, name: 'project B') }
let!(:package4) { create(:npm_package, project: another_project, version: '3.1.0', name: "@#{project.root_namespace.path}/bar") }
let(:packages) { [package1, package2, package3, package4] }
end
end
describe '.for_projects' do
let_it_be(:package1) { create(:maven_package) }
let_it_be(:package2) { create(:maven_package) }
let_it_be(:package3) { create(:maven_package) }
let(:projects) { ::Project.id_in([package1.project_id, package2.project_id]) }
subject { described_class.for_projects(projects.select(:id)) }
it 'returns package1 and package2' do
expect(projects).not_to receive(:any?)
expect(subject).to match_array([package1, package2])
end
end
describe 'validations' do
subject { build(:package) }
it { is_expected.to validate_presence_of(:project) }
it { is_expected.to validate_uniqueness_of(:name).scoped_to(:project_id, :version, :package_type) }
describe '#name' do
it { is_expected.to allow_value("my/domain/com/my-app").for(:name) }
it { is_expected.to allow_value("my.app-11.07.2018").for(:name) }
it { is_expected.not_to allow_value("my(dom$$$ain)com.my-app").for(:name) }
context 'conan package' do
subject { build_stubbed(:conan_package) }
let(:fifty_one_characters) { 'f_b' * 17 }
it { is_expected.to allow_value('foo+bar').for(:name) }
it { is_expected.to allow_value('foo_bar').for(:name) }
it { is_expected.to allow_value('foo.bar').for(:name) }
it { is_expected.not_to allow_value(fifty_one_characters).for(:name) }
it { is_expected.not_to allow_value('+foobar').for(:name) }
it { is_expected.not_to allow_value('.foobar').for(:name) }
it { is_expected.not_to allow_value('%foo%bar').for(:name) }
end
context 'debian package' do
subject { build(:debian_package) }
it { is_expected.to allow_value('0ad').for(:name) }
it { is_expected.to allow_value('g++').for(:name) }
it { is_expected.not_to allow_value('a_b').for(:name) }
end
context 'debian incoming' do
subject { create(:debian_incoming) }
# Only 'incoming' is accepted
it { is_expected.to allow_value('incoming').for(:name) }
it { is_expected.not_to allow_value('0ad').for(:name) }
it { is_expected.not_to allow_value('g++').for(:name) }
it { is_expected.not_to allow_value('a_b').for(:name) }
end
context 'generic package' do
subject { build_stubbed(:generic_package) }
it { is_expected.to allow_value('123').for(:name) }
it { is_expected.to allow_value('foo').for(:name) }
it { is_expected.to allow_value('foo.bar.baz-2.0-20190901.47283-1').for(:name) }
it { is_expected.not_to allow_value('../../foo').for(:name) }
it { is_expected.not_to allow_value('..\..\foo').for(:name) }
it { is_expected.not_to allow_value('%2f%2e%2e%2f%2essh%2fauthorized_keys').for(:name) }
it { is_expected.not_to allow_value('$foo/bar').for(:name) }
it { is_expected.not_to allow_value('my file name').for(:name) }
it { is_expected.not_to allow_value('!!().for(:name)().for(:name)').for(:name) }
end
context 'helm package' do
subject { build(:helm_package) }
it { is_expected.to allow_value('prometheus').for(:name) }
it { is_expected.to allow_value('rook-ceph').for(:name) }
it { is_expected.not_to allow_value('a+b').for(:name) }
it { is_expected.not_to allow_value('Hé').for(:name) }
end
context 'nuget package' do
subject { build_stubbed(:nuget_package) }
it { is_expected.to allow_value('My.Package').for(:name) }
it { is_expected.to allow_value('My.Package.Mvc').for(:name) }
it { is_expected.to allow_value('MyPackage').for(:name) }
it { is_expected.to allow_value('My.23.Package').for(:name) }
it { is_expected.to allow_value('My23Package').for(:name) }
it { is_expected.to allow_value('runtime.my-test64.runtime.package.Mvc').for(:name) }
it { is_expected.to allow_value('my_package').for(:name) }
it { is_expected.not_to allow_value('My/package').for(:name) }
it { is_expected.not_to allow_value('../../../my_package').for(:name) }
it { is_expected.not_to allow_value('%2e%2e%2fmy_package').for(:name) }
end
context 'npm package' do
subject { build_stubbed(:npm_package) }
it { is_expected.to allow_value("@group-1/package").for(:name) }
it { is_expected.to allow_value("@any-scope/package").for(:name) }
it { is_expected.to allow_value("unscoped-package").for(:name) }
it { is_expected.not_to allow_value("@inv@lid-scope/package").for(:name) }
it { is_expected.not_to allow_value("@scope/../../package").for(:name) }
it { is_expected.not_to allow_value("@scope%2e%2e%fpackage").for(:name) }
it { is_expected.not_to allow_value("@scope/sub/package").for(:name) }
end
context 'terraform module package' do
subject { build_stubbed(:terraform_module_package) }
it { is_expected.to allow_value('my-module/my-system').for(:name) }
it { is_expected.to allow_value('my/module').for(:name) }
it { is_expected.not_to allow_value('my-module').for(:name) }
it { is_expected.not_to allow_value('My-Module').for(:name) }
it { is_expected.not_to allow_value('my_module').for(:name) }
it { is_expected.not_to allow_value('my.module').for(:name) }
it { is_expected.not_to allow_value('../../../my-module').for(:name) }
it { is_expected.not_to allow_value('%2e%2e%2fmy-module').for(:name) }
end
end
describe '#version' do
RSpec.shared_examples 'validating version to be SemVer compliant for' do |factory_name|
context "for #{factory_name}" do
subject { build_stubbed(factory_name) }
it { is_expected.to allow_value('1.2.3').for(:version) }
it { is_expected.to allow_value('1.2.3-beta').for(:version) }
it { is_expected.to allow_value('1.2.3-alpha.3').for(:version) }
it { is_expected.not_to allow_value('1').for(:version) }
it { is_expected.not_to allow_value('1.2').for(:version) }
it { is_expected.not_to allow_value('1./2.3').for(:version) }
it { is_expected.not_to allow_value('../../../../../1.2.3').for(:version) }
it { is_expected.not_to allow_value('%2e%2e%2f1.2.3').for(:version) }
end
end
context 'conan package' do
subject { build_stubbed(:conan_package) }
let(:fifty_one_characters) { '1.2' * 17 }
it { is_expected.to allow_value('1.2').for(:version) }
it { is_expected.to allow_value('1.2.3-beta').for(:version) }
it { is_expected.to allow_value('1.2.3-pre1+build2').for(:version) }
it { is_expected.not_to allow_value('1').for(:version) }
it { is_expected.not_to allow_value(fifty_one_characters).for(:version) }
it { is_expected.not_to allow_value('1./2.3').for(:version) }
it { is_expected.not_to allow_value('.1.2.3').for(:version) }
it { is_expected.not_to allow_value('+1.2.3').for(:version) }
it { is_expected.not_to allow_value('%2e%2e%2f1.2.3').for(:version) }
end
context 'composer package' do
it_behaves_like 'validating version to be SemVer compliant for', :composer_package
it { is_expected.to allow_value('dev-master').for(:version) }
it { is_expected.to allow_value('2.x-dev').for(:version) }
end
context 'debian package' do
subject { build(:debian_package) }
it { is_expected.to allow_value('2:4.9.5+dfsg-5+deb10u1').for(:version) }
it { is_expected.not_to allow_value('1_0').for(:version) }
end
context 'debian incoming' do
subject { create(:debian_incoming) }
it { is_expected.to allow_value(nil).for(:version) }
it { is_expected.not_to allow_value('2:4.9.5+dfsg-5+deb10u1').for(:version) }
it { is_expected.not_to allow_value('1_0').for(:version) }
end
context 'maven package' do
subject { build_stubbed(:maven_package) }
it { is_expected.to allow_value('0').for(:version) }
it { is_expected.to allow_value('1').for(:version) }
it { is_expected.to allow_value('10').for(:version) }
it { is_expected.to allow_value('1.0').for(:version) }
it { is_expected.to allow_value('1.3.350.v20200505-1744').for(:version) }
it { is_expected.to allow_value('1.1-beta-2').for(:version) }
it { is_expected.to allow_value('1.2-SNAPSHOT').for(:version) }
it { is_expected.to allow_value('12.1.2-2-1').for(:version) }
it { is_expected.to allow_value('1.2.3-beta').for(:version) }
it { is_expected.to allow_value('10.2.3-beta').for(:version) }
it { is_expected.to allow_value('2.0.0.v200706041905-7C78EK9E_EkMNfNOd2d8qq').for(:version) }
it { is_expected.to allow_value('1.2-alpha-1-20050205.060708-1').for(:version) }
it { is_expected.to allow_value('703220b4e2cea9592caeb9f3013f6b1e5335c293').for(:version) }
it { is_expected.to allow_value('RELEASE').for(:version) }
it { is_expected.not_to allow_value('..1.2.3').for(:version) }
it { is_expected.not_to allow_value('1.2.3..beta').for(:version) }
it { is_expected.not_to allow_value(' 1.2.3').for(:version) }
it { is_expected.not_to allow_value("1.2.3 \r\t").for(:version) }
it { is_expected.not_to allow_value("\r\t 1.2.3").for(:version) }
it { is_expected.not_to allow_value('1.2.3-4/../../').for(:version) }
it { is_expected.not_to allow_value('1.2.3-4%2e%2e%').for(:version) }
it { is_expected.not_to allow_value('../../../../../1.2.3').for(:version) }
it { is_expected.not_to allow_value('%2e%2e%2f1.2.3').for(:version) }
end
context 'pypi package' do
subject { create(:pypi_package) }
it { is_expected.to allow_value('0.1').for(:version) }
it { is_expected.to allow_value('2.0').for(:version) }
it { is_expected.to allow_value('1.2.0').for(:version) }
it { is_expected.to allow_value('0100!0.0').for(:version) }
it { is_expected.to allow_value('00!1.2').for(:version) }
it { is_expected.to allow_value('1.0a').for(:version) }
it { is_expected.to allow_value('1.0-a').for(:version) }
it { is_expected.to allow_value('1.0.a1').for(:version) }
it { is_expected.to allow_value('1.0a1').for(:version) }
it { is_expected.to allow_value('1.0-a1').for(:version) }
it { is_expected.to allow_value('1.0alpha1').for(:version) }
it { is_expected.to allow_value('1.0b1').for(:version) }
it { is_expected.to allow_value('1.0beta1').for(:version) }
it { is_expected.to allow_value('1.0rc1').for(:version) }
it { is_expected.to allow_value('1.0pre1').for(:version) }
it { is_expected.to allow_value('1.0preview1').for(:version) }
it { is_expected.to allow_value('1.0.dev1').for(:version) }
it { is_expected.to allow_value('1.0.DEV1').for(:version) }
it { is_expected.to allow_value('1.0.post1').for(:version) }
it { is_expected.to allow_value('1.0.rev1').for(:version) }
it { is_expected.to allow_value('1.0.r1').for(:version) }
it { is_expected.to allow_value('1.0c2').for(:version) }
it { is_expected.to allow_value('2012.15').for(:version) }
it { is_expected.to allow_value('1.0+5').for(:version) }
it { is_expected.to allow_value('1.0+abc.5').for(:version) }
it { is_expected.to allow_value('1!1.1').for(:version) }
it { is_expected.to allow_value('1.0c3').for(:version) }
it { is_expected.to allow_value('1.0rc2').for(:version) }
it { is_expected.to allow_value('1.0c1').for(:version) }
it { is_expected.to allow_value('1.0b2-346').for(:version) }
it { is_expected.to allow_value('1.0b2.post345').for(:version) }
it { is_expected.to allow_value('1.0b2.post345.dev456').for(:version) }
it { is_expected.to allow_value('1.2.rev33+123456').for(:version) }
it { is_expected.to allow_value('1.1.dev1').for(:version) }
it { is_expected.to allow_value('1.0b1.dev456').for(:version) }
it { is_expected.to allow_value('1.0a12.dev456').for(:version) }
it { is_expected.to allow_value('1.0b2').for(:version) }
it { is_expected.to allow_value('1.0.dev456').for(:version) }
it { is_expected.to allow_value('1.0c1.dev456').for(:version) }
it { is_expected.to allow_value('1.0.post456').for(:version) }
it { is_expected.to allow_value('1.0.post456.dev34').for(:version) }
it { is_expected.to allow_value('1.2+123abc').for(:version) }
it { is_expected.to allow_value('1.2+abc').for(:version) }
it { is_expected.to allow_value('1.2+abc123').for(:version) }
it { is_expected.to allow_value('1.2+abc123def').for(:version) }
it { is_expected.to allow_value('1.2+1234.abc').for(:version) }
it { is_expected.to allow_value('1.2+123456').for(:version) }
it { is_expected.to allow_value('1.2.r32+123456').for(:version) }
it { is_expected.to allow_value('1!1.2.rev33+123456').for(:version) }
it { is_expected.to allow_value('1.0a12').for(:version) }
it { is_expected.to allow_value('1.2.3-45+abcdefgh').for(:version) }
it { is_expected.to allow_value('v1.2.3').for(:version) }
it { is_expected.not_to allow_value('1.2.3-45-abcdefgh').for(:version) }
it { is_expected.not_to allow_value('..1.2.3').for(:version) }
it { is_expected.not_to allow_value(' 1.2.3').for(:version) }
it { is_expected.not_to allow_value("1.2.3 \r\t").for(:version) }
it { is_expected.not_to allow_value("\r\t 1.2.3").for(:version) }
it { is_expected.not_to allow_value('1./2.3').for(:version) }
it { is_expected.not_to allow_value('1.2.3-4/../../').for(:version) }
it { is_expected.not_to allow_value('1.2.3-4%2e%2e%').for(:version) }
it { is_expected.not_to allow_value('../../../../../1.2.3').for(:version) }
it { is_expected.not_to allow_value('%2e%2e%2f1.2.3').for(:version) }
end
context 'generic package' do
subject { build_stubbed(:generic_package) }
it { is_expected.to validate_presence_of(:version) }
it { is_expected.to allow_value('1.2.3').for(:version) }
it { is_expected.to allow_value('1.3.350').for(:version) }
it { is_expected.to allow_value('1.3.350-20201230123456').for(:version) }
it { is_expected.to allow_value('1.2.3-rc1').for(:version) }
it { is_expected.to allow_value('1.2.3g').for(:version) }
it { is_expected.to allow_value('1.2').for(:version) }
it { is_expected.to allow_value('1.2.bananas').for(:version) }
it { is_expected.to allow_value('v1.2.4-build').for(:version) }
it { is_expected.to allow_value('d50d836eb3de6177ce6c7a5482f27f9c2c84b672').for(:version) }
it { is_expected.to allow_value('this_is_a_string_only').for(:version) }
it { is_expected.not_to allow_value('..1.2.3').for(:version) }
it { is_expected.not_to allow_value(' 1.2.3').for(:version) }
it { is_expected.not_to allow_value("1.2.3 \r\t").for(:version) }
it { is_expected.not_to allow_value("\r\t 1.2.3").for(:version) }
it { is_expected.not_to allow_value('1.2.3-4/../../').for(:version) }
it { is_expected.not_to allow_value('1.2.3-4%2e%2e%').for(:version) }
it { is_expected.not_to allow_value('../../../../../1.2.3').for(:version) }
it { is_expected.not_to allow_value('%2e%2e%2f1.2.3').for(:version) }
it { is_expected.not_to allow_value('').for(:version) }
it { is_expected.not_to allow_value(nil).for(:version) }
end
context 'helm package' do
subject { build_stubbed(:helm_package) }
it { is_expected.not_to allow_value(nil).for(:version) }
it { is_expected.not_to allow_value('').for(:version) }
it { is_expected.to allow_value('v1.2.3').for(:version) }
it { is_expected.to allow_value('1.2.3').for(:version) }
it { is_expected.not_to allow_value('v1.2').for(:version) }
end
it_behaves_like 'validating version to be SemVer compliant for', :npm_package
it_behaves_like 'validating version to be SemVer compliant for', :terraform_module_package
context 'nuget package' do
subject { build_stubbed(:nuget_package) }
it { is_expected.to allow_value('1.2').for(:version) }
it { is_expected.to allow_value('1.2.3').for(:version) }
it { is_expected.to allow_value('1.2.3.4').for(:version) }
it { is_expected.to allow_value('1.2.3-beta').for(:version) }
it { is_expected.to allow_value('1.2.3-alpha.3').for(:version) }
it { is_expected.not_to allow_value('1').for(:version) }
it { is_expected.not_to allow_value('1./2.3').for(:version) }
it { is_expected.not_to allow_value('../../../../../1.2.3').for(:version) }
it { is_expected.not_to allow_value('%2e%2e%2f1.2.3').for(:version) }
end
end
describe '#npm_package_already_taken' do
context 'maven package' do
let!(:package) { create(:maven_package) }
it 'will allow a package of the same name' do
new_package = build(:maven_package, name: package.name)
expect(new_package).to be_valid
end
end
context 'npm package' do
let_it_be(:group) { create(:group) }
let_it_be(:project) { create(:project, namespace: group) }
let_it_be(:second_project) { create(:project, namespace: group) }
let(:package) { build(:npm_package, project: project, name: name) }
shared_examples 'validating the first package' do
it 'validates the first package' do
expect(package).to be_valid
end
end
shared_examples 'validating the second package' do
it 'validates the second package' do
package.save!
expect(second_package).to be_valid
end
end
shared_examples 'not validating the second package' do |field_with_error:|
it 'does not validate the second package' do
package.save!
expect(second_package).not_to be_valid
case field_with_error
when :base
expect(second_package.errors.messages[:base]).to eq ['Package already exists']
when :name
expect(second_package.errors.messages[:name]).to eq ['has already been taken']
else
raise ArgumentError, "field #{field_with_error} not expected"
end
end
end
shared_examples 'validating both if the first package is pending destruction' do
before do
package.status = :pending_destruction
end
it_behaves_like 'validating the first package'
it_behaves_like 'validating the second package'
end
context 'following the naming convention' do
let(:name) { "@#{group.path}/test" }
context 'with the second package in the project of the first package' do
let(:second_package) { build(:npm_package, project: project, name: second_package_name, version: second_package_version) }
context 'with no duplicated name' do
let(:second_package_name) { "@#{group.path}/test2" }
let(:second_package_version) { '5.0.0' }
it_behaves_like 'validating the first package'
it_behaves_like 'validating the second package'
end
context 'with duplicated name' do
let(:second_package_name) { package.name }
let(:second_package_version) { '5.0.0' }
it_behaves_like 'validating the first package'
it_behaves_like 'validating the second package'
end
context 'with duplicate name and duplicated version' do
let(:second_package_name) { package.name }
let(:second_package_version) { package.version }
it_behaves_like 'validating the first package'
it_behaves_like 'not validating the second package', field_with_error: :name
it_behaves_like 'validating both if the first package is pending destruction'
end
end
context 'with the second package in a different project than the first package' do
let(:second_package) { build(:npm_package, project: second_project, name: second_package_name, version: second_package_version) }
context 'with no duplicated name' do
let(:second_package_name) { "@#{group.path}/test2" }
let(:second_package_version) { '5.0.0' }
it_behaves_like 'validating the first package'
it_behaves_like 'validating the second package'
end
context 'with duplicated name' do
let(:second_package_name) { package.name }
let(:second_package_version) { '5.0.0' }
it_behaves_like 'validating the first package'
it_behaves_like 'validating the second package'
end
context 'with duplicate name and duplicated version' do
let(:second_package_name) { package.name }
let(:second_package_version) { package.version }
it_behaves_like 'validating the first package'
it_behaves_like 'not validating the second package', field_with_error: :base
it_behaves_like 'validating both if the first package is pending destruction'
end
end
end
context 'not following the naming convention' do
let(:name) { '@foobar/test' }
context 'with the second package in the project of the first package' do
let(:second_package) { build(:npm_package, project: project, name: second_package_name, version: second_package_version) }
context 'with no duplicated name' do
let(:second_package_name) { "@foobar/test2" }
let(:second_package_version) { '5.0.0' }
it_behaves_like 'validating the first package'
it_behaves_like 'validating the second package'
end
context 'with duplicated name' do
let(:second_package_name) { package.name }
let(:second_package_version) { '5.0.0' }
it_behaves_like 'validating the first package'
it_behaves_like 'validating the second package'
end
context 'with duplicate name and duplicated version' do
let(:second_package_name) { package.name }
let(:second_package_version) { package.version }
it_behaves_like 'validating the first package'
it_behaves_like 'not validating the second package', field_with_error: :name
it_behaves_like 'validating both if the first package is pending destruction'
end
end
context 'with the second package in a different project than the first package' do
let(:second_package) { build(:npm_package, project: second_project, name: second_package_name, version: second_package_version) }
context 'with no duplicated name' do
let(:second_package_name) { "@foobar/test2" }
let(:second_package_version) { '5.0.0' }
it_behaves_like 'validating the first package'
it_behaves_like 'validating the second package'
end
context 'with duplicated name' do
let(:second_package_name) { package.name }
let(:second_package_version) { '5.0.0' }
it_behaves_like 'validating the first package'
it_behaves_like 'validating the second package'
end
context 'with duplicate name and duplicated version' do
let(:second_package_name) { package.name }
let(:second_package_version) { package.version }
it_behaves_like 'validating the first package'
it_behaves_like 'validating the second package'
it_behaves_like 'validating both if the first package is pending destruction'
end
end
end
end
end
context "recipe uniqueness for conan packages" do
let_it_be(:package) { create(:conan_package) }
it "will allow a conan package with same project, name, version and package_type" do
new_package = build(:conan_package, project: package.project, name: package.name, version: package.version)
new_package.conan_metadatum.package_channel = 'beta'
expect(new_package).to be_valid
end
it "will not allow a conan package with same recipe (name, version, metadatum.package_channel, metadatum.package_username, and package_type)" do
new_package = build(:conan_package, project: package.project, name: package.name, version: package.version)
expect(new_package).not_to be_valid
expect(new_package.errors.to_a).to include("Package recipe already exists")
end
context 'with pending destruction package' do
let_it_be(:package) { create(:conan_package, :pending_destruction) }
it 'will allow a conan package with same recipe (name, version, metadatum.package_channel, metadatum.package_username, and package_type)' do
new_package = build(:conan_package, project: package.project, name: package.name, version: package.version)
expect(new_package).to be_valid
end
end
end
describe '#valid_composer_global_name' do
let_it_be(:package) { create(:composer_package) }
context 'with different name and different project' do
let(:new_package) { build(:composer_package, name: 'different_name') }
it { expect(new_package).to be_valid }
end
context 'with same name and different project' do
let(:new_package) { build(:composer_package, name: package.name) }
it 'will not validate second package' do
expect(new_package).not_to be_valid
expect(new_package.errors.to_a).to include('Name is already taken by another project')
end
context 'with pending destruction package' do
let_it_be(:package) { create(:composer_package, :pending_destruction) }
it { expect(new_package).to be_valid }
end
end
end
describe "uniqueness for package type debian" do
let!(:package) { create(:debian_package) }
it "will not allow a Debian package with same project, name, version and distribution" do
new_package = build(:debian_package, project: package.project, name: package.name, version: package.version)
new_package.debian_publication.distribution = package.debian_publication.distribution
expect(new_package).not_to be_valid
expect(new_package.errors.to_a).to include('Name has already been taken')
end
it "will not allow a Debian package with same project, name, version, but no distribution" do
new_package = build(:debian_package, project: package.project, name: package.name, version: package.version, published_in: nil)
expect(new_package).not_to be_valid
expect(new_package.errors.to_a).to include('Name has already been taken')
end
context 'with pending_destruction package' do
let!(:package) { create(:debian_package, :pending_destruction) }
it "will allow a Debian package with same project, name, version and distribution" do
new_package = build(:debian_package, project: package.project, name: package.name, version: package.version)
new_package.debian_publication.distribution = package.debian_publication.distribution
expect(new_package).to be_valid
end
end
end
Packages::Package.package_types.keys.without('conan').each do |pt|
context "project id, name, version and package type uniqueness for package type #{pt}" do
let(:package) { create("#{pt}_package") }
it "will not allow a #{pt} package with same project, name, version and package_type" do
new_package = build("#{pt}_package", project: package.project, name: package.name, version: package.version)
expect(new_package).not_to be_valid
expect(new_package.errors.to_a).to include("Name has already been taken")
end
context 'with pending_destruction package' do
let!(:package) { create("#{pt}_package", :pending_destruction) }
it "will allow a #{pt} package with same project, name, version and package_type" do
new_package = build("#{pt}_package", project: package.project, name: package.name, version: package.version)
expect(new_package).to be_valid
end
end
end
end
end
describe '#destroy' do
let(:package) { create(:npm_package) }
let(:package_file) { package.package_files.first }
let(:project_statistics) { package.project.statistics }
subject(:destroy!) { package.destroy! }
it 'updates the project statistics' do
expect(project_statistics).to receive(:increment_counter).with(:packages_size, have_attributes(amount: -package_file.size))
destroy!
end
end
describe '.by_name_and_file_name' do
let!(:package) { create(:npm_package) }
let!(:package_file) { package.package_files.first }
subject { described_class }
it 'finds a package with correct arguiments' do
expect(subject.by_name_and_file_name(package.name, package_file.file_name)).to eq(package)
end
it 'will raise error if not found' do
expect { subject.by_name_and_file_name('foo', 'foo-5.5.5.tgz') }.to raise_error(ActiveRecord::RecordNotFound)
end
end
describe '.debian_incoming_package!' do
let!(:debian_package) { create(:debian_package) }
let!(:debian_processing_incoming) { create(:debian_incoming, :processing) }
subject { described_class.debian_incoming_package! }
context 'when incoming exists' do
let!(:debian_incoming) { create(:debian_incoming) }
it { is_expected.to eq(debian_incoming) }
end
context 'when incoming not found' do
it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
end
end
describe '.with_package_type' do
let!(:package1) { create(:terraform_module_package) }
let!(:package2) { create(:npm_package) }
let(:package_type) { :terraform_module }
subject { described_class.with_package_type(package_type) }
it { is_expected.to eq([package1]) }
end
describe '.without_package_type' do
let!(:package1) { create(:npm_package) }
let!(:package2) { create(:terraform_module_package) }
let(:package_type) { :terraform_module }
subject { described_class.without_package_type(package_type) }
it { is_expected.to eq([package1]) }
end
context 'version scopes' do
let!(:package1) { create(:npm_package, version: '1.0.0') }
let!(:package2) { create(:npm_package, version: '1.0.1') }
let!(:package3) { create(:npm_package, version: '1.0.1') }
describe '.has_version' do
subject { described_class.has_version }
before do
create(:maven_metadatum).package.update!(version: nil)
end
it 'includes only packages with version attribute' do
is_expected.to match_array([package1, package2, package3])
end
end
describe '.with_version' do
subject { described_class.with_version('1.0.1') }
it 'includes only packages with specified version' do
is_expected.to match_array([package2, package3])
end
end
describe '.without_version_like' do
let(:version_pattern) { '%.0.0%' }
subject { described_class.without_version_like(version_pattern) }
it 'includes packages without the version pattern' do
is_expected.to match_array([package2, package3])
end
end
end
context 'conan scopes' do
let!(:package) { create(:conan_package) }
describe '.with_conan_channel' do
subject { described_class.with_conan_channel('stable') }
it 'includes only packages with specified version' do
is_expected.to include(package)
end
end
describe '.with_conan_username' do
subject do
described_class.with_conan_username(
Packages::Conan::Metadatum.package_username_from(full_path: package.project.full_path)
)
end
it 'includes only packages with specified version' do
is_expected.to match_array([package])
end
end
describe '.preload_conan_metadatum' do
subject { described_class.preload_conan_metadatum }
it 'loads conan metadatum' do
expect(subject.first.association(:conan_metadatum)).to be_loaded
end
end
end
describe '.with_npm_scope' do
let_it_be(:package1) { create(:npm_package, name: '@test/foobar') }
let_it_be(:package2) { create(:npm_package, name: '@test2/foobar') }
let_it_be(:package3) { create(:npm_package, name: 'foobar') }
subject { described_class.with_npm_scope('test') }
it { is_expected.to contain_exactly(package1) }
context 'when npm_package_registry_fix_group_path_validation is disabled' do
before do
stub_feature_flags(npm_package_registry_fix_group_path_validation: false)
end
it { is_expected.to contain_exactly(package1) }
end
end
describe '.without_nuget_temporary_name' do
let!(:package1) { create(:nuget_package) }
let!(:package2) { create(:nuget_package, name: Packages::Nuget::TEMPORARY_PACKAGE_NAME) }
subject { described_class.without_nuget_temporary_name }
it 'does not include nuget temporary packages' do
expect(subject).to eq([package1])
end
end
describe '.limit_recent' do
let!(:package1) { create(:nuget_package) }
let!(:package2) { create(:nuget_package) }
let!(:package3) { create(:nuget_package) }
subject { described_class.limit_recent(2) }
it { is_expected.to match_array([package3, package2]) }
end
context 'with several packages' do
let_it_be(:package1) { create(:nuget_package, name: 'FooBar') }
let_it_be(:package2) { create(:nuget_package, name: 'foobar') }
let_it_be(:package3) { create(:npm_package) }
let_it_be(:package4) { create(:npm_package) }
describe '.pluck_names' do
subject { described_class.pluck_names }
it { is_expected.to match_array([package1, package2, package3, package4].map(&:name)) }
end
describe '.pluck_versions' do
subject { described_class.pluck_versions }
it { is_expected.to match_array([package1, package2, package3, package4].map(&:version)) }
end
describe '.with_name_like' do
subject { described_class.with_name_like(name_term) }
context 'with downcase name' do
let(:name_term) { 'foobar' }
it { is_expected.to match_array([package1, package2]) }
end
context 'with prefix wildcard' do
let(:name_term) { '%ar' }
it { is_expected.to match_array([package1, package2]) }
end
context 'with suffix wildcard' do
let(:name_term) { 'foo%' }
it { is_expected.to match_array([package1, package2]) }
end
context 'with surrounding wildcards' do
let(:name_term) { '%ooba%' }
it { is_expected.to match_array([package1, package2]) }
end
end
describe '.search_by_name' do
let(:query) { 'oba' }
subject { described_class.search_by_name(query) }
it { is_expected.to match_array([package1, package2]) }
end
describe '.with_normalized_pypi_name' do
let_it_be(:pypi_package) { create(:pypi_package, name: 'Foo.bAr---BAZ_buz') }
subject { described_class.with_normalized_pypi_name('foo-bar-baz-buz') }
it { is_expected.to match_array([pypi_package]) }
end
describe '.with_case_insensitive_version' do
let_it_be(:nuget_package) { create(:nuget_package, version: '1.0.0-ABC') }
subject { described_class.with_case_insensitive_version('1.0.0-abC') }
it { is_expected.to match_array([nuget_package]) }
end
describe '.with_case_insensitive_name' do
let_it_be(:nuget_package) { create(:nuget_package, name: 'TestPackage') }
subject { described_class.with_case_insensitive_name('testpackage') }
it { is_expected.to match_array([nuget_package]) }
end
describe '.with_nuget_version_or_normalized_version' do
let_it_be(:nuget_package) { create(:nuget_package, :with_metadatum, version: '1.0.7+r3456') }
before do
nuget_package.nuget_metadatum.update_column(:normalized_version, '1.0.7')
end
subject { described_class.with_nuget_version_or_normalized_version(version, with_normalized: with_normalized) }
where(:version, :with_normalized, :expected) do
'1.0.7' | true | [ref(:nuget_package)]
'1.0.7' | false | []
'1.0.7+r3456' | true | [ref(:nuget_package)]
'1.0.7+r3456' | false | [ref(:nuget_package)]
end
with_them do
it { is_expected.to match_array(expected) }
end
end
context 'status scopes' do
let_it_be(:default_package) { create(:maven_package, :default) }
let_it_be(:hidden_package) { create(:maven_package, :hidden) }
let_it_be(:processing_package) { create(:maven_package, :processing) }
let_it_be(:error_package) { create(:maven_package, :error) }
describe '.displayable' do
subject { described_class.displayable }
it 'does not include non-displayable packages', :aggregate_failures do
is_expected.to include(error_package)
is_expected.not_to include(hidden_package)
is_expected.not_to include(processing_package)
end
end
describe '.installable' do
subject { described_class.installable }
it 'does not include non-installable packages', :aggregate_failures do
is_expected.not_to include(error_package)
is_expected.not_to include(processing_package)
end
it 'includes installable packages', :aggregate_failures do
is_expected.to include(default_package)
is_expected.to include(hidden_package)
end
end
describe '.with_status' do
subject { described_class.with_status(:hidden) }
it 'returns packages with specified status' do
is_expected.to match_array([hidden_package])
end
end
end
end
describe '.select_distinct_name' do
let_it_be(:nuget_package) { create(:nuget_package) }
let_it_be(:nuget_packages) { create_list(:nuget_package, 3, name: nuget_package.name, project: nuget_package.project) }
let_it_be(:maven_package) { create(:maven_package) }
let_it_be(:maven_packages) { create_list(:maven_package, 3, name: maven_package.name, project: maven_package.project) }
subject { described_class.select_distinct_name }
it 'returns only distinct names' do
packages = subject
expect(packages.size).to eq(2)
expect(packages.pluck(:name)).to match_array([nuget_package.name, maven_package.name])
end
end
context 'sorting' do
let_it_be(:project) { create(:project, name: 'aaa') }
let_it_be(:project2) { create(:project, name: 'bbb') }
let_it_be(:package1) { create(:package, project: project) }
let_it_be(:package2) { create(:package, project: project2) }
let_it_be(:package3) { create(:package, project: project2) }
let_it_be(:package4) { create(:package, project: project) }
it 'orders packages by their projects name ascending' do
expect(described_class.order_project_name).to eq([package1, package4, package2, package3])
end
it 'orders packages by their projects name descending' do
expect(described_class.order_project_name_desc).to eq([package2, package3, package1, package4])
end
shared_examples 'order_project_path scope' do
it 'orders packages by their projects path asc, then package id asc' do
expect(described_class.order_project_path).to eq([package1, package4, package2, package3])
end
end
shared_examples 'order_project_path_desc scope' do
it 'orders packages by their projects path desc, then package id desc' do
expect(described_class.order_project_path_desc).to eq([package3, package2, package4, package1])
end
end
it_behaves_like 'order_project_path scope'
it_behaves_like 'order_project_path_desc scope'
end
describe '.order_by_package_file' do
let_it_be(:project) { create(:project) }
let_it_be(:package1) { create(:maven_package, project: project) }
let_it_be(:package2) { create(:maven_package, project: project) }
it 'orders packages their associated package_file\'s created_at date', :aggregate_failures do
expect(project.packages.order_by_package_file).to match_array([package1, package1, package1, package2, package2, package2])
create(:package_file, :xml, package: package1)
expect(project.packages.order_by_package_file).to match_array([package1, package1, package1, package2, package2, package2, package1])
end
end
describe '.keyset_pagination_order' do
let(:join_class) { nil }
let(:column_name) { nil }
let(:direction) { nil }
subject { described_class.keyset_pagination_order(join_class: join_class, column_name: column_name, direction: direction) }
it { expect { subject }.to raise_error(NoMethodError) }
context 'with valid params' do
let(:join_class) { Project }
let(:column_name) { :name }
context 'ascending direction' do
let(:direction) { :asc }
it { is_expected.to eq('"projects"."name" ASC NULLS LAST, "packages_packages"."id" ASC') }
end
context 'descending direction' do
let(:direction) { :desc }
it { is_expected.to eq('"projects"."name" DESC NULLS FIRST, "packages_packages"."id" DESC') }
end
end
end
describe '#versions' do
let_it_be(:project) { create(:project) }
let_it_be(:package) { create(:maven_package, project: project) }
let_it_be(:package2) { create(:maven_package, project: project) }
let_it_be(:package3) { create(:maven_package, :error, project: project) }
let_it_be(:package4) { create(:maven_package, project: project, name: 'foo') }
let_it_be(:pending_destruction_package) { create(:maven_package, :pending_destruction, project: project) }
it 'returns other package versions of the same package name belonging to the project' do
expect(package.versions).to contain_exactly(package2, package3)
end
it 'does not return different packages' do
expect(package.versions).not_to include(package4)
end
end
describe '#pipeline' do
let_it_be_with_refind(:package) { create(:maven_package) }
context 'package without pipeline' do
it 'returns nil if there is no pipeline' do
expect(package.pipeline).to be_nil
end
end
context 'package with pipeline' do
let_it_be(:pipeline) { create(:ci_pipeline) }
before do
package.build_infos.create!(pipeline: pipeline)
end
it 'returns the pipeline' do
expect(package.pipeline).to eq(pipeline)
end
end
end
describe '#pipelines' do
let_it_be_with_refind(:package) { create(:maven_package) }
subject { package.pipelines }
context 'package without pipeline' do
it { is_expected.to be_empty }
end
context 'package with pipeline' do
let_it_be(:pipeline) { create(:ci_pipeline) }
let_it_be(:pipeline2) { create(:ci_pipeline) }
before do
package.build_infos.create!(pipeline: pipeline)
package.build_infos.create!(pipeline: pipeline2)
end
it { is_expected.to contain_exactly(pipeline, pipeline2) }
end
end
describe '#tag_names' do
let_it_be(:package) { create(:nuget_package) }
subject { package.tag_names }
it { is_expected.to eq([]) }
context 'with tags' do
let(:tags) { %w(tag1 tag2 tag3) }
before do
tags.each { |t| create(:packages_tag, name: t, package: package) }
end
it { is_expected.to contain_exactly(*tags) }
end
end
describe '#debian_incoming?' do
let(:package) { build(:package) }
subject { package.debian_incoming? }
it { is_expected.to eq(false) }
context 'with debian_incoming' do
let(:package) { create(:debian_incoming) }
it { is_expected.to eq(true) }
end
context 'with debian_package' do
let(:package) { create(:debian_package) }
it { is_expected.to eq(false) }
end
end
describe '#debian_package?' do
let(:package) { build(:package) }
subject { package.debian_package? }
it { is_expected.to eq(false) }
context 'with debian_incoming' do
let(:package) { create(:debian_incoming) }
it { is_expected.to eq(false) }
end
context 'with debian_package' do
let(:package) { create(:debian_package) }
it { is_expected.to eq(true) }
end
end
describe '#infrastructure_package?' do
let(:package) { create(:package) }
subject { package.infrastructure_package? }
it { is_expected.to eq(false) }
context 'with generic package' do
let(:package) { create(:generic_package) }
it { is_expected.to eq(false) }
end
context 'with terraform module package' do
let(:package) { create(:terraform_module_package) }
it { is_expected.to eq(true) }
end
end
describe 'plan_limits' do
Packages::Package.package_types.keys.without('composer').each do |pt|
plan_limit_name = if pt == 'generic'
"#{pt}_packages_max_file_size"
else
"#{pt}_max_file_size"
end
context "File size limits for #{pt}" do
let(:package) { create("#{pt}_package") }
it "plan_limits includes column #{plan_limit_name}" do
expect { package.project.actual_limits.send(plan_limit_name) }
.not_to raise_error
end
end
end
end
describe '#last_build_info' do
let_it_be_with_refind(:package) { create(:npm_package) }
context 'without build_infos' do
it 'returns nil' do
expect(package.last_build_info).to be_nil
end
end
context 'with build_infos' do
let_it_be(:first_build_info) { create(:package_build_info, :with_pipeline, package: package) }
let_it_be(:second_build_info) { create(:package_build_info, :with_pipeline, package: package) }
it 'returns the last build info' do
expect(package.last_build_info).to eq(second_build_info)
end
end
end
describe '#package_settings' do
let_it_be(:group) { create(:group) }
let_it_be(:project) { create(:project, group: group) }
let_it_be(:package) { create(:maven_package, project: project) }
it 'returns the namespace package_settings' do
expect(package.package_settings).to eq(group.package_settings)
end
end
describe '#sync_maven_metadata' do
let_it_be(:user) { create(:user) }
let_it_be(:package) { create(:maven_package) }
subject { package.sync_maven_metadata(user) }
shared_examples 'not enqueuing a sync worker job' do
it 'does not enqueue a sync worker job' do
expect(::Packages::Maven::Metadata::SyncWorker)
.not_to receive(:perform_async)
subject
end
end
it 'enqueues a sync worker job' do
expect(::Packages::Maven::Metadata::SyncWorker)
.to receive(:perform_async).with(user.id, package.project.id, package.name)
subject
end
context 'with no user' do
let(:user) { nil }
it_behaves_like 'not enqueuing a sync worker job'
end
context 'with a versionless maven package' do
let_it_be(:package) { create(:maven_package, version: nil) }
it_behaves_like 'not enqueuing a sync worker job'
end
context 'with a non maven package' do
let_it_be(:package) { create(:npm_package) }
it_behaves_like 'not enqueuing a sync worker job'
end
end
describe '#mark_package_files_for_destruction' do
let_it_be(:package) { create(:npm_package, :pending_destruction) }
subject { package.mark_package_files_for_destruction }
it 'enqueues a sync worker job' do
expect(::Packages::MarkPackageFilesForDestructionWorker)
.to receive(:perform_async).with(package.id)
subject
end
context 'for a package non pending destruction' do
let_it_be(:package) { create(:npm_package) }
it 'does not enqueues a sync worker job' do
expect(::Packages::MarkPackageFilesForDestructionWorker)
.not_to receive(:perform_async).with(package.id)
subject
end
end
end
describe '#create_build_infos!' do
let_it_be(:package) { create(:package) }
let_it_be(:pipeline) { create(:ci_pipeline) }
let(:build) { double(pipeline: pipeline) }
subject { package.create_build_infos!(build) }
context 'with a valid build' do
it 'creates a build info' do
expect { subject }.to change { ::Packages::BuildInfo.count }.by(1)
last_build = ::Packages::BuildInfo.last
expect(last_build.package).to eq(package)
expect(last_build.pipeline).to eq(pipeline)
end
context 'with an already existing build info' do
let_it_be(:build_info) { create(:package_build_info, package: package, pipeline: pipeline) }
it 'does not create a build info' do
expect { subject }.not_to change { ::Packages::BuildInfo.count }
end
end
end
context 'with a nil build' do
let(:build) { nil }
it 'does not create a build info' do
expect { subject }.not_to change { ::Packages::BuildInfo.count }
end
end
context 'with a build without a pipeline' do
let(:build) { double(pipeline: nil) }
it 'does not create a build info' do
expect { subject }.not_to change { ::Packages::BuildInfo.count }
end
end
end
context 'with identical pending destruction package' do
described_class.package_types.keys.each do |package_format|
context "for package format #{package_format}" do
let_it_be(:package_pending_destruction) { create("#{package_format}_package", :pending_destruction) }
let(:new_package) { build("#{package_format}_package", name: package_pending_destruction.name, version: package_pending_destruction.version, project: package_pending_destruction.project) }
it { expect(new_package).to be_valid }
end
end
end
describe '#normalized_pypi_name' do
let_it_be(:package) { create(:pypi_package) }
subject { package.normalized_pypi_name }
where(:package_name, :normalized_name) do
'ASDF' | 'asdf'
'a.B_c-d' | 'a-b-c-d'
'a-------b....c___d' | 'a-b-c-d'
end
with_them do
before do
package.update_column(:name, package_name)
end
it { is_expected.to eq(normalized_name) }
end
end
describe '#normalized_nuget_version' do
let_it_be(:package) { create(:nuget_package, :with_metadatum, version: '1.0') }
let(:normalized_version) { '1.0.0' }
subject { package.normalized_nuget_version }
before do
package.nuget_metadatum.update_column(:normalized_version, normalized_version)
end
it { is_expected.to eq(normalized_version) }
end
describe "#publish_creation_event" do
let_it_be(:project) { create(:project) }
let(:version) { '-' }
let(:package_type) { :generic }
subject { described_class.create!(project: project, name: 'incoming', version: version, package_type: package_type) }
context 'when package is generic' do
it 'publishes an event' do
expect { subject }
.to publish_event(::Packages::PackageCreatedEvent)
.with({
project_id: project.id,
id: kind_of(Numeric),
name: "incoming",
version: "-",
package_type: 'generic'
})
end
end
context 'when package is not generic' do
let(:package_type) { :debian }
let(:version) { 1 }
it 'does not create event' do
expect { subject }.not_to publish_event(::Packages::PackageCreatedEvent)
end
end
end
end
|