Skip to content

Commit e13626e

Browse files
cniackzcniackzdvaldivia
authored
Add new test for service account for user (#1469)
Co-authored-by: cniackz <[email protected]> Co-authored-by: Daniel Valdivia <[email protected]>
1 parent 0286010 commit e13626e

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

integration/users_test.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,58 @@ func UpdateGroupsForAUser(userName string, groups []string) (*http.Response, err
216216
return response, err
217217
}
218218

219+
func CreateServiceAccountForUser(userName string, policy string) (*http.Response, error) {
220+
/*
221+
Helper function to Create Service Account for user
222+
POST: api/v1/user/username/service-accounts
223+
{
224+
"policy": "ad magna"
225+
}
226+
*/
227+
client := &http.Client{
228+
Timeout: 3 * time.Second,
229+
}
230+
requestDataAdd := map[string]interface{}{
231+
"policy": policy,
232+
}
233+
requestDataJSON, _ := json.Marshal(requestDataAdd)
234+
requestDataBody := bytes.NewReader(requestDataJSON)
235+
request, err := http.NewRequest(
236+
"POST",
237+
"http://localhost:9090/api/v1/user/"+userName+"/service-accounts",
238+
requestDataBody,
239+
)
240+
if err != nil {
241+
log.Println(err)
242+
}
243+
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
244+
request.Header.Add("Content-Type", "application/json")
245+
response, err := client.Do(request)
246+
return response, err
247+
}
248+
249+
func ReturnsAListOfServiceAccountsForAUser(userName string) (*http.Response, error) {
250+
/*
251+
Helper function to return a list of service accounts for a user.
252+
GET: {{baseUrl}}/user/:name/service-accounts
253+
*/
254+
client := &http.Client{
255+
Timeout: 3 * time.Second,
256+
}
257+
request, err := http.NewRequest(
258+
"GET",
259+
"http://localhost:9090/api/v1/user/"+userName+"/service-accounts",
260+
nil,
261+
)
262+
if err != nil {
263+
log.Println(err)
264+
}
265+
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
266+
request.Header.Add("Content-Type", "application/json")
267+
response, err := client.Do(request)
268+
return response, err
269+
}
270+
219271
func TestAddUser(t *testing.T) {
220272
/*
221273
This is an API Test to add a user via api/v1/users, the intention
@@ -588,3 +640,63 @@ func TestUpdateGroupsForAUser(t *testing.T) {
588640
finalResponse, groupName+strconv.Itoa(i)), finalResponse)
589641
}
590642
}
643+
644+
func TestCreateServiceAccountForUser(t *testing.T) {
645+
/*
646+
To test creation of service account for a user.
647+
*/
648+
649+
// Test's variables
650+
userName := "testcreateserviceaccountforuser1"
651+
assert := assert.New(t)
652+
policy := ""
653+
serviceAccountLengthInBytes := 40 // As observed, update as needed
654+
655+
// 1. Create the user
656+
var groups = []string{}
657+
var policies = []string{}
658+
response, err := AddUser(userName, "secretKey", groups, policies)
659+
if err != nil {
660+
log.Println(err)
661+
return
662+
}
663+
if response != nil {
664+
fmt.Println("StatusCode:", response.StatusCode)
665+
assert.Equal(201, response.StatusCode, "Status Code is incorrect")
666+
}
667+
668+
// 2. Create the service account for the user
669+
createServiceAccountResponse,
670+
createServiceAccountError := CreateServiceAccountForUser(
671+
userName,
672+
policy,
673+
)
674+
if createServiceAccountError != nil {
675+
log.Println(createServiceAccountError)
676+
assert.Fail("Error in createServiceAccountError")
677+
}
678+
if createServiceAccountResponse != nil {
679+
fmt.Println("StatusCode:", createServiceAccountResponse.StatusCode)
680+
assert.Equal(
681+
201, createServiceAccountResponse.StatusCode,
682+
inspectHTTPResponse(createServiceAccountResponse),
683+
)
684+
}
685+
686+
// 3. Verify the service account for the user
687+
listOfAccountsResponse,
688+
listOfAccountsError := ReturnsAListOfServiceAccountsForAUser(userName)
689+
if listOfAccountsError != nil {
690+
log.Println(listOfAccountsError)
691+
assert.Fail("Error in listOfAccountsError")
692+
}
693+
finalResponse := inspectHTTPResponse(listOfAccountsResponse)
694+
if listOfAccountsResponse != nil {
695+
fmt.Println("StatusCode:", listOfAccountsResponse.StatusCode)
696+
assert.Equal(
697+
200, listOfAccountsResponse.StatusCode,
698+
finalResponse,
699+
)
700+
}
701+
assert.Equal(len(finalResponse), serviceAccountLengthInBytes, finalResponse)
702+
}

0 commit comments

Comments
 (0)