Skip to content

Commit 3262b8f

Browse files
authored
Adding test for service-account-credentials end point (#1528)
1 parent 090b7e5 commit 3262b8f

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

integration/users_test.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,32 @@ func CreateServiceAccountForUser(userName string, policy string) (*http.Response
246246
return response, err
247247
}
248248

249+
func CreateServiceAccountForUserWithCredentials(userName string, policy string, accessKey string, secretKey string) (*http.Response, error) {
250+
// Helper function to test "Create Service Account for User With Credentials" end point.
251+
client := &http.Client{
252+
Timeout: 3 * time.Second,
253+
}
254+
requestDataAdd := map[string]interface{}{
255+
"policy": policy,
256+
"accessKey": accessKey,
257+
"secretKey": secretKey,
258+
}
259+
requestDataJSON, _ := json.Marshal(requestDataAdd)
260+
requestDataBody := bytes.NewReader(requestDataJSON)
261+
request, err := http.NewRequest(
262+
"POST",
263+
"http://localhost:9090/api/v1/user/"+userName+"/service-account-credentials",
264+
requestDataBody,
265+
)
266+
if err != nil {
267+
log.Println(err)
268+
}
269+
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
270+
request.Header.Add("Content-Type", "application/json")
271+
response, err := client.Do(request)
272+
return response, err
273+
}
274+
249275
func ReturnsAListOfServiceAccountsForAUser(userName string) (*http.Response, error) {
250276
/*
251277
Helper function to return a list of service accounts for a user.
@@ -766,6 +792,98 @@ func TestCreateServiceAccountForUser(t *testing.T) {
766792
assert.Equal(len(finalResponse), serviceAccountLengthInBytes, finalResponse)
767793
}
768794

795+
func TestCreateServiceAccountForUserWithCredentials(t *testing.T) {
796+
/*
797+
To test creation of service account for a user.
798+
*/
799+
800+
// Test's variables
801+
userName := "testcreateserviceaccountforuserwithcredentials1"
802+
assert := assert.New(t)
803+
policy := ""
804+
serviceAccountLengthInBytes := 40 // As observed, update as needed
805+
806+
// 1. Create the user
807+
var groups = []string{}
808+
var policies = []string{}
809+
var secretKey = "testcreateserviceaccountforuserwithcrede"
810+
response, err := AddUser(userName, "secretKey", groups, policies)
811+
if err != nil {
812+
log.Println(err)
813+
return
814+
}
815+
if response != nil {
816+
fmt.Println("StatusCode:", response.StatusCode)
817+
assert.Equal(201, response.StatusCode, "Status Code is incorrect")
818+
}
819+
820+
// Table driven testing part
821+
type args struct {
822+
accessKey string
823+
}
824+
tests := []struct {
825+
name string
826+
args args
827+
expectedStatus int
828+
}{
829+
{
830+
name: "Service Account With Valid Credentials",
831+
expectedStatus: 201,
832+
args: args{
833+
accessKey: "testcreateserviceacc",
834+
},
835+
},
836+
{
837+
name: "Service Account With Invalid Credentials",
838+
expectedStatus: 500,
839+
args: args{
840+
accessKey: "tooooooooooooooooooooolongggggggggggggggggg",
841+
},
842+
},
843+
}
844+
for _, tt := range tests {
845+
t.Run(tt.name, func(t *testing.T) {
846+
// 2. Create the service account for the user
847+
createServiceAccountWithCredentialsResponse,
848+
createServiceAccountWithCredentialsError := CreateServiceAccountForUserWithCredentials(
849+
userName,
850+
policy,
851+
tt.args.accessKey,
852+
secretKey,
853+
)
854+
if createServiceAccountWithCredentialsError != nil {
855+
log.Println(createServiceAccountWithCredentialsError)
856+
assert.Fail("Error in createServiceAccountWithCredentialsError")
857+
}
858+
if createServiceAccountWithCredentialsResponse != nil {
859+
fmt.Println("StatusCode:", createServiceAccountWithCredentialsResponse.StatusCode)
860+
assert.Equal(
861+
tt.expectedStatus, // different status expected per table's row
862+
createServiceAccountWithCredentialsResponse.StatusCode,
863+
inspectHTTPResponse(createServiceAccountWithCredentialsResponse),
864+
)
865+
}
866+
867+
// 3. Verify the service account for the user
868+
listOfAccountsResponse,
869+
listOfAccountsError := ReturnsAListOfServiceAccountsForAUser(userName)
870+
if listOfAccountsError != nil {
871+
log.Println(listOfAccountsError)
872+
assert.Fail("Error in listOfAccountsError")
873+
}
874+
finalResponse := inspectHTTPResponse(listOfAccountsResponse)
875+
if listOfAccountsResponse != nil {
876+
fmt.Println("StatusCode:", listOfAccountsResponse.StatusCode)
877+
assert.Equal(
878+
200, listOfAccountsResponse.StatusCode,
879+
finalResponse,
880+
)
881+
}
882+
assert.Equal(len(finalResponse), serviceAccountLengthInBytes, finalResponse)
883+
})
884+
}
885+
}
886+
769887
func TestUsersGroupsBulk(t *testing.T) {
770888
/*
771889
To test UsersGroupsBulk End Point

0 commit comments

Comments
 (0)