@@ -13,6 +13,7 @@ const testCollection = require('./data/collection.json');
13
13
const gitlabTestPipelines = require ( './data/pipelinesFromGitlab.json' ) ;
14
14
const testJob = require ( './data/job.json' ) ;
15
15
const testJobs = require ( './data/jobs.json' ) ;
16
+ const testStages = require ( './data/stages.json' ) ;
16
17
const testTriggers = require ( './data/triggers.json' ) ;
17
18
const testBuild = require ( './data/buildWithSteps.json' ) ;
18
19
const testBuilds = require ( './data/builds.json' ) . slice ( 0 , 2 ) ;
@@ -137,6 +138,22 @@ const getSecretsMocks = secrets => {
137
138
return decorateJobMock ( secrets ) ;
138
139
} ;
139
140
141
+ const decorateStageMock = stage => {
142
+ const mock = hoek . clone ( stage ) ;
143
+
144
+ mock . toJson = sinon . stub ( ) . returns ( stage ) ;
145
+
146
+ return mock ;
147
+ } ;
148
+
149
+ const getStagesMocks = stages => {
150
+ if ( Array . isArray ( stages ) ) {
151
+ return stages . map ( decorateStageMock ) ;
152
+ }
153
+
154
+ return decorateStageMock ( stages ) ;
155
+ } ;
156
+
140
157
const getUserMock = user => {
141
158
const mock = hoek . clone ( user ) ;
142
159
@@ -170,6 +187,7 @@ describe('pipeline plugin test', () => {
170
187
let tokenFactoryMock ;
171
188
let bannerFactoryMock ;
172
189
let jobFactoryMock ;
190
+ let stageFactoryMock ;
173
191
let triggerFactoryMock ;
174
192
let secretFactoryMock ;
175
193
let bannerMock ;
@@ -223,6 +241,9 @@ describe('pipeline plugin test', () => {
223
241
create : sinon . stub ( ) . resolves ( null ) ,
224
242
list : sinon . stub ( ) . resolves ( null )
225
243
} ;
244
+ stageFactoryMock = {
245
+ list : sinon . stub ( )
246
+ } ;
226
247
tokenFactoryMock = {
227
248
get : sinon . stub ( ) ,
228
249
create : sinon . stub ( )
@@ -261,6 +282,7 @@ describe('pipeline plugin test', () => {
261
282
server . app = {
262
283
eventFactory : eventFactoryMock ,
263
284
jobFactory : jobFactoryMock ,
285
+ stageFactory : stageFactoryMock ,
264
286
triggerFactory : triggerFactoryMock ,
265
287
pipelineFactory : pipelineFactoryMock ,
266
288
userFactory : userFactoryMock ,
@@ -915,6 +937,76 @@ describe('pipeline plugin test', () => {
915
937
} ) ;
916
938
} ) ;
917
939
940
+ describe ( 'GET /pipelines/{id}/stages' , ( ) => {
941
+ const id = 123 ;
942
+ let options ;
943
+ let pipelineMock ;
944
+ let stagesMocks ;
945
+
946
+ beforeEach ( ( ) => {
947
+ options = {
948
+ method : 'GET' ,
949
+ url : `/pipelines/${ id } /stages`
950
+ } ;
951
+ pipelineMock = getPipelineMocks ( testPipeline ) ;
952
+ stagesMocks = getStagesMocks ( testStages ) ;
953
+ stageFactoryMock . list . resolves ( stagesMocks ) ;
954
+ pipelineFactoryMock . get . resolves ( pipelineMock ) ;
955
+ } ) ;
956
+
957
+ it ( 'returns 200 for getting stages' , ( ) =>
958
+ server . inject ( options ) . then ( reply => {
959
+ assert . equal ( reply . statusCode , 200 ) ;
960
+ assert . calledWith ( stageFactoryMock . list , {
961
+ params : {
962
+ pipelineId : id
963
+ }
964
+ } ) ;
965
+ assert . deepEqual ( reply . result , testStages ) ;
966
+ } ) ) ;
967
+
968
+ it ( 'returns 200 for getting stages with state passed in as query param' , ( ) => {
969
+ options . url = `/pipelines/${ id } /stages?state=ARCHIVED` ;
970
+
971
+ return server . inject ( options ) . then ( reply => {
972
+ assert . equal ( reply . statusCode , 200 ) ;
973
+ assert . calledWith ( stageFactoryMock . list , {
974
+ params : {
975
+ pipelineId : id ,
976
+ state : 'ARCHIVED'
977
+ }
978
+ } ) ;
979
+ assert . deepEqual ( reply . result , testStages ) ;
980
+ } ) ;
981
+ } ) ;
982
+
983
+ it ( 'returns 400 for passing in string as pipeline id' , ( ) => {
984
+ const stringId = 'test' ;
985
+
986
+ options . url = `/pipelines/${ stringId } /stages` ;
987
+
988
+ return server . inject ( options ) . then ( reply => {
989
+ assert . equal ( reply . statusCode , 400 ) ;
990
+ } ) ;
991
+ } ) ;
992
+
993
+ it ( 'returns 404 for updating a pipeline that does not exist' , ( ) => {
994
+ pipelineFactoryMock . get . resolves ( null ) ;
995
+
996
+ return server . inject ( options ) . then ( reply => {
997
+ assert . equal ( reply . statusCode , 404 ) ;
998
+ } ) ;
999
+ } ) ;
1000
+
1001
+ it ( 'returns 500 when the datastore returns an error' , ( ) => {
1002
+ pipelineFactoryMock . get . rejects ( new Error ( 'icantdothatdave' ) ) ;
1003
+
1004
+ return server . inject ( options ) . then ( reply => {
1005
+ assert . equal ( reply . statusCode , 500 ) ;
1006
+ } ) ;
1007
+ } ) ;
1008
+ } ) ;
1009
+
918
1010
describe ( 'GET /pipelines/{id}/triggers' , ( ) => {
919
1011
const id = 123 ;
920
1012
let options ;
0 commit comments