@@ -273,3 +273,137 @@ func Test_ServiceAccountsAPI(t *testing.T) {
273
273
}
274
274
275
275
}
276
+
277
+ func DeleteMultipleServiceAccounts (serviceAccounts []string ) (* http.Response , error ) {
278
+ /*
279
+ Helper function to delete multiple service accounts
280
+ URL: http://localhost:9001/api/v1/service-accounts/delete-multi
281
+ HTTP Verb: DELETE
282
+ Data: ["U3RADB7J2ZZHELR0WSBB","ZE8H1HYOA6AVGKFCV6YU"]
283
+ Response: Status Code: 204 No Content
284
+ */
285
+ client := & http.Client {
286
+ Timeout : 3 * time .Second ,
287
+ }
288
+ requestDataJSON , _ := json .Marshal (serviceAccounts )
289
+ requestDataBody := bytes .NewReader (requestDataJSON )
290
+ request , err := http .NewRequest (
291
+ "DELETE" , "http://localhost:9090/api/v1/service-accounts/delete-multi" , requestDataBody )
292
+ if err != nil {
293
+ log .Println (err )
294
+ }
295
+ request .Header .Add ("Cookie" , fmt .Sprintf ("token=%s" , token ))
296
+ request .Header .Add ("Content-Type" , "application/json" )
297
+ response , err := client .Do (request )
298
+ return response , err
299
+ }
300
+
301
+ func TestCreateServiceAccountForUserWithCredentials (t * testing.T ) {
302
+ /*
303
+ To test creation of service account for a user.
304
+ */
305
+
306
+ // Test's variables
307
+ userName := "testcreateserviceaccountforuserwithcredentials1"
308
+ assert := assert .New (t )
309
+ policy := ""
310
+ serviceAccountLengthInBytes := 40 // As observed, update as needed
311
+
312
+ // 1. Create the user
313
+ var groups = []string {}
314
+ var policies = []string {}
315
+ var secretKey = "testcreateserviceaccountforuserwithcrede"
316
+ response , err := AddUser (userName , "secretKey" , groups , policies )
317
+ if err != nil {
318
+ log .Println (err )
319
+ return
320
+ }
321
+ if response != nil {
322
+ fmt .Println ("StatusCode:" , response .StatusCode )
323
+ assert .Equal (201 , response .StatusCode , "Status Code is incorrect" )
324
+ }
325
+
326
+ // Table driven testing part
327
+ type args struct {
328
+ accessKey string
329
+ }
330
+ tests := []struct {
331
+ name string
332
+ args args
333
+ expectedStatus int
334
+ }{
335
+ {
336
+ name : "Service Account With Valid Credentials" ,
337
+ expectedStatus : 201 ,
338
+ args : args {
339
+ accessKey : "testcreateserviceacc" ,
340
+ },
341
+ },
342
+ {
343
+ name : "Service Account With Invalid Credentials" ,
344
+ expectedStatus : 500 ,
345
+ args : args {
346
+ accessKey : "tooooooooooooooooooooolongggggggggggggggggg" ,
347
+ },
348
+ },
349
+ }
350
+ for _ , tt := range tests {
351
+ t .Run (tt .name , func (t * testing.T ) {
352
+ // 2. Create the service account for the user
353
+ createServiceAccountWithCredentialsResponse ,
354
+ createServiceAccountWithCredentialsError := CreateServiceAccountForUserWithCredentials (
355
+ userName ,
356
+ policy ,
357
+ tt .args .accessKey ,
358
+ secretKey ,
359
+ )
360
+ if createServiceAccountWithCredentialsError != nil {
361
+ log .Println (createServiceAccountWithCredentialsError )
362
+ assert .Fail ("Error in createServiceAccountWithCredentialsError" )
363
+ }
364
+ if createServiceAccountWithCredentialsResponse != nil {
365
+ fmt .Println ("StatusCode:" , createServiceAccountWithCredentialsResponse .StatusCode )
366
+ assert .Equal (
367
+ tt .expectedStatus , // different status expected per table's row
368
+ createServiceAccountWithCredentialsResponse .StatusCode ,
369
+ inspectHTTPResponse (createServiceAccountWithCredentialsResponse ),
370
+ )
371
+ }
372
+
373
+ // 3. Verify the service account for the user
374
+ listOfAccountsResponse ,
375
+ listOfAccountsError := ReturnsAListOfServiceAccountsForAUser (userName )
376
+ if listOfAccountsError != nil {
377
+ log .Println (listOfAccountsError )
378
+ assert .Fail ("Error in listOfAccountsError" )
379
+ }
380
+ finalResponse := inspectHTTPResponse (listOfAccountsResponse )
381
+ if listOfAccountsResponse != nil {
382
+ fmt .Println ("StatusCode:" , listOfAccountsResponse .StatusCode )
383
+ assert .Equal (
384
+ 200 , listOfAccountsResponse .StatusCode ,
385
+ finalResponse ,
386
+ )
387
+ }
388
+ assert .Equal (len (finalResponse ), serviceAccountLengthInBytes , finalResponse )
389
+ })
390
+ }
391
+
392
+ // Delete Multiple Service Accounts
393
+ serviceAccount := make ([]string , 1 )
394
+ serviceAccount [0 ] = "testcreateserviceacc"
395
+ response , err = DeleteMultipleServiceAccounts (serviceAccount )
396
+ if err != nil {
397
+ log .Println (err )
398
+ return
399
+ }
400
+ if response != nil {
401
+ fmt .Println ("StatusCode:" , response .StatusCode )
402
+ assert .Equal (
403
+ 204 ,
404
+ response .StatusCode ,
405
+ inspectHTTPResponse (response ),
406
+ )
407
+ }
408
+
409
+ }
0 commit comments