@@ -12,6 +12,7 @@ const testtemplateversions = require('./data/templateVersions.json');
12
12
const testTemplateVersionsMetrics = require ( './data/templateVersionsMetrics.json' ) ;
13
13
const testTemplateWithNamespace = require ( './data/templateWithNamespace.json' ) ;
14
14
const testpipeline = require ( './data/pipeline.json' ) ;
15
+ const testPipelineUsage = require ( './data/pipelineUsage.json' ) ;
15
16
const TEMPLATE_INVALID = require ( './data/template-validator.missing-version.json' ) ;
16
17
const TEMPLATE_VALID = require ( './data/template-validator.input.json' ) ;
17
18
const TEMPLATE_VALID_NEW_VERSION = require ( './data/template-create.input.json' ) ;
@@ -69,7 +70,8 @@ describe('template plugin test', () => {
69
70
list : sinon . stub ( ) ,
70
71
listWithMetrics : sinon . stub ( ) ,
71
72
getTemplate : sinon . stub ( ) ,
72
- get : sinon . stub ( )
73
+ get : sinon . stub ( ) ,
74
+ getPipelineUsage : sinon . stub ( )
73
75
} ;
74
76
templateTagFactoryMock = {
75
77
create : sinon . stub ( ) ,
@@ -432,6 +434,55 @@ describe('template plugin test', () => {
432
434
} ) ;
433
435
} ) ;
434
436
437
+ describe ( 'GET /templates/name/versionOrTag/usage/pipelines' , ( ) => {
438
+ let options ;
439
+
440
+ beforeEach ( ( ) => {
441
+ options = {
442
+ method : 'GET' ,
443
+ url : '/templates/screwdriver%2Fbuild/1.7.3/usage/pipelines'
444
+ } ;
445
+ } ) ;
446
+
447
+ it ( 'returns 200 and all pipelines that use the template version' , ( ) => {
448
+ templateFactoryMock . getPipelineUsage . resolves ( testPipelineUsage ) ;
449
+
450
+ return server . inject ( options ) . then ( reply => {
451
+ assert . deepEqual ( reply . result , testPipelineUsage ) ;
452
+ assert . equal ( reply . statusCode , 200 ) ;
453
+ assert . calledWith ( templateFactoryMock . getPipelineUsage , `screwdriver/build@1.7.3` ) ;
454
+ } ) ;
455
+ } ) ;
456
+
457
+ it ( 'returns 200 even if no pipelines are using the template version' , ( ) => {
458
+ templateFactoryMock . getPipelineUsage . resolves ( [ ] ) ;
459
+
460
+ return server . inject ( options ) . then ( reply => {
461
+ assert . deepEqual ( reply . result , [ ] ) ;
462
+ assert . equal ( reply . statusCode , 200 ) ;
463
+ assert . calledWith ( templateFactoryMock . getPipelineUsage , `screwdriver/build@1.7.3` ) ;
464
+ } ) ;
465
+ } ) ;
466
+
467
+ it ( 'returns 404 when the template does not exist' , ( ) => {
468
+ templateFactoryMock . getPipelineUsage . returns ( Promise . reject ( new Error ( 'Template does not exist' ) ) ) ;
469
+ const error = {
470
+ statusCode : 404 ,
471
+ error : 'Not Found' ,
472
+ message : 'Template screwdriver/build@1.7.3 does not exist'
473
+ } ;
474
+
475
+ return server . inject ( options ) . then ( reply => {
476
+ const payload = JSON . parse ( reply . payload ) ;
477
+
478
+ Object . keys ( error ) . forEach ( k => {
479
+ assert . equal ( payload [ k ] , error [ k ] ) ;
480
+ } ) ;
481
+ assert . calledWith ( templateFactoryMock . getPipelineUsage , `screwdriver/build@1.7.3` ) ;
482
+ } ) ;
483
+ } ) ;
484
+ } ) ;
485
+
435
486
describe ( 'GET /templates/name/metrics' , ( ) => {
436
487
let options ;
437
488
@@ -1241,7 +1292,10 @@ describe('template plugin test', () => {
1241
1292
1242
1293
templateFactoryMock . get . resolves ( testTemplateV1 ) ;
1243
1294
templateFactoryMock . get
1244
- . withArgs ( { name : `${ templateNameSpace } /${ templateName } ` , version : templateVersion1 } )
1295
+ . withArgs ( {
1296
+ name : `${ templateNameSpace } /${ templateName } ` ,
1297
+ version : templateVersion1
1298
+ } )
1245
1299
. resolves ( testTemplateV1 ) ;
1246
1300
templateTagFactoryMock . list . resolves ( [ testTemplateTag ] ) ;
1247
1301
} ) ;
@@ -1274,7 +1328,10 @@ describe('template plugin test', () => {
1274
1328
return server . inject ( options ) . then ( reply => {
1275
1329
assert . equal ( reply . statusCode , error . statusCode ) ;
1276
1330
assert . deepEqual ( reply . result , error ) ;
1277
- assert . calledWith ( templateFactoryMock . get , { name : 'test-namespace/test-template' , version : '1.0.0' } ) ;
1331
+ assert . calledWith ( templateFactoryMock . get , {
1332
+ name : 'test-namespace/test-template' ,
1333
+ version : '1.0.0'
1334
+ } ) ;
1278
1335
} ) ;
1279
1336
} ) ;
1280
1337
0 commit comments