Skip to content

Commit

Permalink
Add unit tests for create role, list role and drop role, improve vali…
Browse files Browse the repository at this point in the history
…dation, fix comments
  • Loading branch information
burmanm committed Nov 12, 2024
1 parent 45bf062 commit 52fa286
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
14 changes: 11 additions & 3 deletions pkg/httphelper/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,17 @@ func (client *NodeMgmtClient) CallSchemaVersionsEndpoint(pod *corev1.Pod) (map[s
return result, nil
}

// Create a new superuser with the given username and password
// CallCreateRoleEndpoint creates a new user with the given username and password
func (client *NodeMgmtClient) CallCreateRoleEndpoint(pod *corev1.Pod, username string, password string, superuser bool) error {
client.Log.Info(
"calling Management API create role - POST /api/v0/ops/auth/role",
"pod", pod.Name,
)

if username == "" || password == "" {
return errors.New("username and password cannot be empty")
}

postData := url.Values{}
postData.Set("username", username)
postData.Set("password", password)
Expand All @@ -324,13 +328,17 @@ func (client *NodeMgmtClient) CallCreateRoleEndpoint(pod *corev1.Pod, username s
return nil
}

// Create a new superuser with the given username and password
// CallDropRoleEndpoint drops an existing role from the cluster
func (client *NodeMgmtClient) CallDropRoleEndpoint(pod *corev1.Pod, username string) error {
client.Log.Info(
"calling Management API drop role - DELETE /api/v0/ops/auth/role",
"pod", pod.Name,
)

if username == "" {
return errors.New("username cannot be empty")
}

postData := url.Values{}
postData.Set("username", username)

Expand Down Expand Up @@ -368,7 +376,7 @@ func parseListRoles(body []byte) ([]User, error) {
return users, nil
}

// Create a new superuser with the given username and password
// CallListRolesEndpoint lists existing roles in the cluster
func (client *NodeMgmtClient) CallListRolesEndpoint(pod *corev1.Pod) ([]User, error) {
client.Log.Info(
"calling Management API list roles - GET /api/v0/ops/auth/role",
Expand Down
46 changes: 45 additions & 1 deletion pkg/httphelper/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,12 +457,56 @@ func TestListRoles(t *testing.T) {
require.NoError(err)
require.Equal(3, len(roles))

mgmtClient := newMockMgmtClient(newMockHttpClient(newHttpResponse(payload, http.StatusOK), nil))
mockHttpClient := mocks.NewHttpClient(t)
mockHttpClient.On("Do",
mock.MatchedBy(
func(req *http.Request) bool {
return req.URL.Path == "/api/v0/ops/auth/role" && req.Method == http.MethodGet
})).
Return(newHttpResponse(payload, http.StatusOK), nil).
Once()

mgmtClient := newMockMgmtClient(mockHttpClient)
roles, err = mgmtClient.CallListRolesEndpoint(goodPod)
require.NoError(err)
require.Equal(3, len(roles))
}

func TestCreateRole(t *testing.T) {
require := require.New(t)
mockHttpClient := mocks.NewHttpClient(t)
mockHttpClient.On("Do",
mock.MatchedBy(
func(req *http.Request) bool {
return req.URL.Path == "/api/v0/ops/auth/role" && req.Method == http.MethodPost && req.URL.Query().Get("username") == "role1" && req.URL.Query().Get("password") == "password1" && req.URL.Query().Get("is_superuser") == "true"
})).
Return(newHttpResponseMarshalled("OK", http.StatusOK), nil).
Once()

mgmtClient := newMockMgmtClient(mockHttpClient)
err := mgmtClient.CallCreateRoleEndpoint(goodPod, "role1", "password1", true)
require.NoError(err)
require.True(mockHttpClient.AssertExpectations(t))
}

func TestDropRole(t *testing.T) {
require := require.New(t)
mockHttpClient := mocks.NewHttpClient(t)
mockHttpClient.On("Do",
mock.MatchedBy(
func(req *http.Request) bool {
return req.URL.Path == "/api/v0/ops/auth/role" && req.Method == http.MethodDelete
})).
Return(newHttpResponseMarshalled("OK", http.StatusOK), nil).
Once()

mgmtClient := newMockMgmtClient(mockHttpClient)
err := mgmtClient.CallDropRoleEndpoint(goodPod, "role1")

require.NoError(err)
require.True(mockHttpClient.AssertExpectations(t))
}

func newMockMgmtClient(httpClient *mocks.HttpClient) *NodeMgmtClient {
return &NodeMgmtClient{
Client: httpClient,
Expand Down

0 comments on commit 52fa286

Please sign in to comment.