From 144979b3722c65ae3f6db6942696d0e7d6704993 Mon Sep 17 00:00:00 2001 From: Cesar Celis Hernandez Date: Sat, 19 Mar 2022 20:30:20 -0400 Subject: [PATCH] Delete Multiple Service Accounts (#1737) Co-authored-by: Daniel Valdivia <18384552+dvaldivia@users.noreply.github.com> --- .github/workflows/jobs.yaml | 2 +- integration/service_account_test.go | 134 ++++++++++++++++++++++++++++ integration/users_test.go | 93 ------------------- 3 files changed, 135 insertions(+), 94 deletions(-) diff --git a/.github/workflows/jobs.yaml b/.github/workflows/jobs.yaml index a07030a6a..299fb7ff0 100644 --- a/.github/workflows/jobs.yaml +++ b/.github/workflows/jobs.yaml @@ -830,7 +830,7 @@ jobs: result=${result%\%} echo "result:" echo $result - threshold=51.8 + threshold=52.1 if (( $(echo "$result >= $threshold" |bc -l) )); then echo "It is equal or greater than threshold, passed!" else diff --git a/integration/service_account_test.go b/integration/service_account_test.go index 7324392d4..80e49e5f8 100644 --- a/integration/service_account_test.go +++ b/integration/service_account_test.go @@ -273,3 +273,137 @@ func Test_ServiceAccountsAPI(t *testing.T) { } } + +func DeleteMultipleServiceAccounts(serviceAccounts []string) (*http.Response, error) { + /* + Helper function to delete multiple service accounts + URL: http://localhost:9001/api/v1/service-accounts/delete-multi + HTTP Verb: DELETE + Data: ["U3RADB7J2ZZHELR0WSBB","ZE8H1HYOA6AVGKFCV6YU"] + Response: Status Code: 204 No Content + */ + client := &http.Client{ + Timeout: 3 * time.Second, + } + requestDataJSON, _ := json.Marshal(serviceAccounts) + requestDataBody := bytes.NewReader(requestDataJSON) + request, err := http.NewRequest( + "DELETE", "http://localhost:9090/api/v1/service-accounts/delete-multi", requestDataBody) + if err != nil { + log.Println(err) + } + request.Header.Add("Cookie", fmt.Sprintf("token=%s", token)) + request.Header.Add("Content-Type", "application/json") + response, err := client.Do(request) + return response, err +} + +func TestCreateServiceAccountForUserWithCredentials(t *testing.T) { + /* + To test creation of service account for a user. + */ + + // Test's variables + userName := "testcreateserviceaccountforuserwithcredentials1" + assert := assert.New(t) + policy := "" + serviceAccountLengthInBytes := 40 // As observed, update as needed + + // 1. Create the user + var groups = []string{} + var policies = []string{} + var secretKey = "testcreateserviceaccountforuserwithcrede" + response, err := AddUser(userName, "secretKey", groups, policies) + if err != nil { + log.Println(err) + return + } + if response != nil { + fmt.Println("StatusCode:", response.StatusCode) + assert.Equal(201, response.StatusCode, "Status Code is incorrect") + } + + // Table driven testing part + type args struct { + accessKey string + } + tests := []struct { + name string + args args + expectedStatus int + }{ + { + name: "Service Account With Valid Credentials", + expectedStatus: 201, + args: args{ + accessKey: "testcreateserviceacc", + }, + }, + { + name: "Service Account With Invalid Credentials", + expectedStatus: 500, + args: args{ + accessKey: "tooooooooooooooooooooolongggggggggggggggggg", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // 2. Create the service account for the user + createServiceAccountWithCredentialsResponse, + createServiceAccountWithCredentialsError := CreateServiceAccountForUserWithCredentials( + userName, + policy, + tt.args.accessKey, + secretKey, + ) + if createServiceAccountWithCredentialsError != nil { + log.Println(createServiceAccountWithCredentialsError) + assert.Fail("Error in createServiceAccountWithCredentialsError") + } + if createServiceAccountWithCredentialsResponse != nil { + fmt.Println("StatusCode:", createServiceAccountWithCredentialsResponse.StatusCode) + assert.Equal( + tt.expectedStatus, // different status expected per table's row + createServiceAccountWithCredentialsResponse.StatusCode, + inspectHTTPResponse(createServiceAccountWithCredentialsResponse), + ) + } + + // 3. Verify the service account for the user + listOfAccountsResponse, + listOfAccountsError := ReturnsAListOfServiceAccountsForAUser(userName) + if listOfAccountsError != nil { + log.Println(listOfAccountsError) + assert.Fail("Error in listOfAccountsError") + } + finalResponse := inspectHTTPResponse(listOfAccountsResponse) + if listOfAccountsResponse != nil { + fmt.Println("StatusCode:", listOfAccountsResponse.StatusCode) + assert.Equal( + 200, listOfAccountsResponse.StatusCode, + finalResponse, + ) + } + assert.Equal(len(finalResponse), serviceAccountLengthInBytes, finalResponse) + }) + } + + // Delete Multiple Service Accounts + serviceAccount := make([]string, 1) + serviceAccount[0] = "testcreateserviceacc" + response, err = DeleteMultipleServiceAccounts(serviceAccount) + if err != nil { + log.Println(err) + return + } + if response != nil { + fmt.Println("StatusCode:", response.StatusCode) + assert.Equal( + 204, + response.StatusCode, + inspectHTTPResponse(response), + ) + } + +} diff --git a/integration/users_test.go b/integration/users_test.go index 0f2ac449b..661e1ccba 100644 --- a/integration/users_test.go +++ b/integration/users_test.go @@ -765,99 +765,6 @@ func TestCreateServiceAccountForUser(t *testing.T) { } -func TestCreateServiceAccountForUserWithCredentials(t *testing.T) { - /* - To test creation of service account for a user. - */ - - // Test's variables - userName := "testcreateserviceaccountforuserwithcredentials1" - assert := assert.New(t) - policy := "" - serviceAccountLengthInBytes := 40 // As observed, update as needed - - // 1. Create the user - var groups = []string{} - var policies = []string{} - var secretKey = "testcreateserviceaccountforuserwithcrede" - response, err := AddUser(userName, "secretKey", groups, policies) - if err != nil { - log.Println(err) - return - } - if response != nil { - fmt.Println("StatusCode:", response.StatusCode) - assert.Equal(201, response.StatusCode, "Status Code is incorrect") - } - - // Table driven testing part - type args struct { - accessKey string - } - tests := []struct { - name string - args args - expectedStatus int - }{ - { - name: "Service Account With Valid Credentials", - expectedStatus: 201, - args: args{ - accessKey: "testcreateserviceacc", - }, - }, - { - name: "Service Account With Invalid Credentials", - expectedStatus: 500, - args: args{ - accessKey: "tooooooooooooooooooooolongggggggggggggggggg", - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - // 2. Create the service account for the user - createServiceAccountWithCredentialsResponse, - createServiceAccountWithCredentialsError := CreateServiceAccountForUserWithCredentials( - userName, - policy, - tt.args.accessKey, - secretKey, - ) - if createServiceAccountWithCredentialsError != nil { - log.Println(createServiceAccountWithCredentialsError) - assert.Fail("Error in createServiceAccountWithCredentialsError") - } - if createServiceAccountWithCredentialsResponse != nil { - fmt.Println("StatusCode:", createServiceAccountWithCredentialsResponse.StatusCode) - assert.Equal( - tt.expectedStatus, // different status expected per table's row - createServiceAccountWithCredentialsResponse.StatusCode, - inspectHTTPResponse(createServiceAccountWithCredentialsResponse), - ) - } - - // 3. Verify the service account for the user - listOfAccountsResponse, - listOfAccountsError := ReturnsAListOfServiceAccountsForAUser(userName) - if listOfAccountsError != nil { - log.Println(listOfAccountsError) - assert.Fail("Error in listOfAccountsError") - } - finalResponse := inspectHTTPResponse(listOfAccountsResponse) - if listOfAccountsResponse != nil { - fmt.Println("StatusCode:", listOfAccountsResponse.StatusCode) - assert.Equal( - 200, listOfAccountsResponse.StatusCode, - finalResponse, - ) - } - assert.Equal(len(finalResponse), serviceAccountLengthInBytes, finalResponse) - }) - } - -} - func TestUsersGroupsBulk(t *testing.T) { /* To test UsersGroupsBulk End Point