@@ -191,3 +191,151 @@ func Test_stackRepository_Update(t *testing.T) {
191
191
assert .Equal (t , "stack-name" , actualVars ["id" ])
192
192
assert .IsType (t , structs.StackInput {}, actualVars ["input" ])
193
193
}
194
+
195
+ func Test_stackRepository_Update_WithAWSIntegration (t * testing.T ) {
196
+ originalClient := spaceliftclient .DefaultClient
197
+ defer func () { spaceliftclient .DefaultClient = originalClient }()
198
+ fakeClient := mocks .NewClient (t )
199
+ spaceliftclient .DefaultClient = func (_ context.Context , _ client.Client , _ string ) (spaceliftclient.Client , error ) {
200
+ return fakeClient , nil
201
+ }
202
+
203
+ fakeStackId := "stack-id"
204
+ var actualVars map [string ]any
205
+ fakeClient .EXPECT ().
206
+ Mutate (mock .Anything , mock .AnythingOfType ("*repository.stackUpdateMutation" ), mock .Anything ).
207
+ Run (func (_ context.Context , mutation any , vars map [string ]interface {}, _ ... graphql.RequestOption ) {
208
+ actualVars = vars
209
+ updateMutation := mutation .(* stackUpdateMutation )
210
+ updateMutation .StackUpdate .ID = fakeStackId
211
+ updateMutation .StackUpdate .AttachedAWSIntegrations = []stackUpdateMutationAWSIntegration {
212
+ {
213
+ ID : "attachment-id" ,
214
+ IntegrationID : "another-integration-id" ,
215
+ Read : true ,
216
+ Write : true ,
217
+ },
218
+ }
219
+ }).Return (nil )
220
+ fakeClient .EXPECT ().URL ("/stack/%s" , fakeStackId ).Return ("" )
221
+
222
+ var detachVars map [string ]any
223
+ fakeClient .EXPECT ().Mutate (mock .Anything , mock .AnythingOfType ("*repository.awsIntegrationDetachMutation" ), mock .Anything ).
224
+ Run (func (_ context.Context , _ any , vars map [string ]any , _ ... graphql.RequestOption ) {
225
+ detachVars = vars
226
+ }).
227
+ Return (nil )
228
+ var attachVars map [string ]any
229
+ fakeClient .EXPECT ().Mutate (mock .Anything , mock .AnythingOfType ("*repository.awsIntegrationAttachMutation" ), mock .Anything ).
230
+ Run (func (_ context.Context , _ any , vars map [string ]any , _ ... graphql.RequestOption ) {
231
+ attachVars = vars
232
+ }).
233
+ Return (nil )
234
+
235
+ repo := NewStackRepository (nil )
236
+
237
+ fakeStack := & v1beta1.Stack {
238
+ ObjectMeta : v1.ObjectMeta {
239
+ Name : "stack-name" ,
240
+ },
241
+ Spec : v1beta1.StackSpec {
242
+ SpaceId : utils .AddressOf ("space-id" ),
243
+ AWSIntegration : & v1beta1.AWSIntegration {
244
+ Id : "integration-id" ,
245
+ Read : true ,
246
+ Write : true ,
247
+ },
248
+ },
249
+ Status : v1beta1.StackStatus {
250
+ Id : fakeStackId ,
251
+ },
252
+ }
253
+ _ , err := repo .Update (context .Background (), fakeStack )
254
+ require .NoError (t , err )
255
+ assert .Equal (t , "stack-name" , actualVars ["id" ])
256
+ assert .IsType (t , structs.StackInput {}, actualVars ["input" ])
257
+ assert .Equal (t , map [string ]any {
258
+ "id" : graphql .ID ("attachment-id" ),
259
+ }, detachVars )
260
+ assert .Equal (t , map [string ]any {
261
+ "id" : "integration-id" ,
262
+ "stack" : fakeStackId ,
263
+ "read" : graphql .Boolean (true ),
264
+ "write" : graphql .Boolean (true ),
265
+ }, attachVars )
266
+ }
267
+
268
+ func Test_stackRepository_Update_WithAWSIntegration_UpdateExistingIntegration (t * testing.T ) {
269
+ originalClient := spaceliftclient .DefaultClient
270
+ defer func () { spaceliftclient .DefaultClient = originalClient }()
271
+ fakeClient := mocks .NewClient (t )
272
+ spaceliftclient .DefaultClient = func (_ context.Context , _ client.Client , _ string ) (spaceliftclient.Client , error ) {
273
+ return fakeClient , nil
274
+ }
275
+
276
+ fakeStackId := "stack-id"
277
+ var actualVars map [string ]any
278
+ fakeClient .EXPECT ().
279
+ Mutate (mock .Anything , mock .AnythingOfType ("*repository.stackUpdateMutation" ), mock .Anything ).
280
+ Run (func (_ context.Context , mutation any , vars map [string ]interface {}, _ ... graphql.RequestOption ) {
281
+ actualVars = vars
282
+ updateMutation := mutation .(* stackUpdateMutation )
283
+ updateMutation .StackUpdate .ID = fakeStackId
284
+ updateMutation .StackUpdate .AttachedAWSIntegrations = []stackUpdateMutationAWSIntegration {
285
+ {
286
+ ID : "attachment-id" ,
287
+ IntegrationID : "integration-id" ,
288
+ Read : true ,
289
+ Write : true ,
290
+ },
291
+ }
292
+ }).Return (nil )
293
+ fakeClient .EXPECT ().URL ("/stack/%s" , fakeStackId ).Return ("" )
294
+
295
+ var detachVars map [string ]any
296
+ fakeClient .EXPECT ().Mutate (mock .Anything , mock .AnythingOfType ("*repository.awsIntegrationDetachMutation" ), mock .Anything ).
297
+ Run (func (_ context.Context , _ any , vars map [string ]any , _ ... graphql.RequestOption ) {
298
+ detachVars = vars
299
+ }).
300
+ Return (nil )
301
+ var attachVars map [string ]any
302
+ fakeClient .EXPECT ().Mutate (mock .Anything , mock .AnythingOfType ("*repository.awsIntegrationAttachMutation" ), mock .Anything ).
303
+ Run (func (_ context.Context , _ any , vars map [string ]any , _ ... graphql.RequestOption ) {
304
+ attachVars = vars
305
+ }).
306
+ Return (nil )
307
+
308
+ repo := NewStackRepository (nil )
309
+
310
+ fakeStack := & v1beta1.Stack {
311
+ ObjectMeta : v1.ObjectMeta {
312
+ Name : "stack-name" ,
313
+ },
314
+ Spec : v1beta1.StackSpec {
315
+ SpaceId : utils .AddressOf ("space-id" ),
316
+ // Because Write has been changed from true to false
317
+ // The integration should be detached and reattached
318
+ AWSIntegration : & v1beta1.AWSIntegration {
319
+ Id : "integration-id" ,
320
+ Read : true ,
321
+ Write : false ,
322
+ },
323
+ },
324
+ Status : v1beta1.StackStatus {
325
+ Id : fakeStackId ,
326
+ },
327
+ }
328
+ _ , err := repo .Update (context .Background (), fakeStack )
329
+ require .NoError (t , err )
330
+ assert .Equal (t , "stack-name" , actualVars ["id" ])
331
+ assert .IsType (t , structs.StackInput {}, actualVars ["input" ])
332
+ assert .Equal (t , map [string ]any {
333
+ "id" : graphql .ID ("attachment-id" ),
334
+ }, detachVars )
335
+ assert .Equal (t , map [string ]any {
336
+ "id" : "integration-id" ,
337
+ "stack" : fakeStackId ,
338
+ "read" : graphql .Boolean (true ),
339
+ "write" : graphql .Boolean (false ),
340
+ }, attachVars )
341
+ }
0 commit comments