Skip to content

Commit 144979b

Browse files
cniackzdvaldivia
andauthored
Delete Multiple Service Accounts (#1737)
Co-authored-by: Daniel Valdivia <18384552+dvaldivia@users.noreply.github.com>
1 parent e29fa04 commit 144979b

File tree

3 files changed

+135
-94
lines changed

3 files changed

+135
-94
lines changed

.github/workflows/jobs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ jobs:
830830
result=${result%\%}
831831
echo "result:"
832832
echo $result
833-
threshold=51.8
833+
threshold=52.1
834834
if (( $(echo "$result >= $threshold" |bc -l) )); then
835835
echo "It is equal or greater than threshold, passed!"
836836
else

integration/service_account_test.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,137 @@ func Test_ServiceAccountsAPI(t *testing.T) {
273273
}
274274

275275
}
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+
}

integration/users_test.go

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -765,99 +765,6 @@ func TestCreateServiceAccountForUser(t *testing.T) {
765765

766766
}
767767

768-
func TestCreateServiceAccountForUserWithCredentials(t *testing.T) {
769-
/*
770-
To test creation of service account for a user.
771-
*/
772-
773-
// Test's variables
774-
userName := "testcreateserviceaccountforuserwithcredentials1"
775-
assert := assert.New(t)
776-
policy := ""
777-
serviceAccountLengthInBytes := 40 // As observed, update as needed
778-
779-
// 1. Create the user
780-
var groups = []string{}
781-
var policies = []string{}
782-
var secretKey = "testcreateserviceaccountforuserwithcrede"
783-
response, err := AddUser(userName, "secretKey", groups, policies)
784-
if err != nil {
785-
log.Println(err)
786-
return
787-
}
788-
if response != nil {
789-
fmt.Println("StatusCode:", response.StatusCode)
790-
assert.Equal(201, response.StatusCode, "Status Code is incorrect")
791-
}
792-
793-
// Table driven testing part
794-
type args struct {
795-
accessKey string
796-
}
797-
tests := []struct {
798-
name string
799-
args args
800-
expectedStatus int
801-
}{
802-
{
803-
name: "Service Account With Valid Credentials",
804-
expectedStatus: 201,
805-
args: args{
806-
accessKey: "testcreateserviceacc",
807-
},
808-
},
809-
{
810-
name: "Service Account With Invalid Credentials",
811-
expectedStatus: 500,
812-
args: args{
813-
accessKey: "tooooooooooooooooooooolongggggggggggggggggg",
814-
},
815-
},
816-
}
817-
for _, tt := range tests {
818-
t.Run(tt.name, func(t *testing.T) {
819-
// 2. Create the service account for the user
820-
createServiceAccountWithCredentialsResponse,
821-
createServiceAccountWithCredentialsError := CreateServiceAccountForUserWithCredentials(
822-
userName,
823-
policy,
824-
tt.args.accessKey,
825-
secretKey,
826-
)
827-
if createServiceAccountWithCredentialsError != nil {
828-
log.Println(createServiceAccountWithCredentialsError)
829-
assert.Fail("Error in createServiceAccountWithCredentialsError")
830-
}
831-
if createServiceAccountWithCredentialsResponse != nil {
832-
fmt.Println("StatusCode:", createServiceAccountWithCredentialsResponse.StatusCode)
833-
assert.Equal(
834-
tt.expectedStatus, // different status expected per table's row
835-
createServiceAccountWithCredentialsResponse.StatusCode,
836-
inspectHTTPResponse(createServiceAccountWithCredentialsResponse),
837-
)
838-
}
839-
840-
// 3. Verify the service account for the user
841-
listOfAccountsResponse,
842-
listOfAccountsError := ReturnsAListOfServiceAccountsForAUser(userName)
843-
if listOfAccountsError != nil {
844-
log.Println(listOfAccountsError)
845-
assert.Fail("Error in listOfAccountsError")
846-
}
847-
finalResponse := inspectHTTPResponse(listOfAccountsResponse)
848-
if listOfAccountsResponse != nil {
849-
fmt.Println("StatusCode:", listOfAccountsResponse.StatusCode)
850-
assert.Equal(
851-
200, listOfAccountsResponse.StatusCode,
852-
finalResponse,
853-
)
854-
}
855-
assert.Equal(len(finalResponse), serviceAccountLengthInBytes, finalResponse)
856-
})
857-
}
858-
859-
}
860-
861768
func TestUsersGroupsBulk(t *testing.T) {
862769
/*
863770
To test UsersGroupsBulk End Point

0 commit comments

Comments
 (0)