diff --git a/nexus/client.go b/nexus/client.go index 3b3e344..3e4702d 100644 --- a/nexus/client.go +++ b/nexus/client.go @@ -184,11 +184,15 @@ func (c *Client) newRequest(method, apiPath string, query string, body interface } func (c *Client) get(apiPath string, query string) (*http.Request, error) { - return c.newRequest("GET", apiPath, query, nil) + return c.newRequest(http.MethodGet, apiPath, query, nil) } func (c *Client) post(apiPath string, query string, body interface{}) (*http.Request, error) { - return c.newRequest("POST", apiPath, query, body) + return c.newRequest(http.MethodPost, apiPath, query, body) +} + +func (c *Client) put(apiPath string, query string, body interface{}) (*http.Request, error) { + return c.newRequest(http.MethodPut, apiPath, query, body) } func (c *Client) do(req *http.Request, v interface{}) (*http.Response, error) { diff --git a/nexus/client_test.go b/nexus/client_test.go index 081836b..b150236 100644 --- a/nexus/client_test.go +++ b/nexus/client_test.go @@ -19,6 +19,7 @@ package nexus import ( "net/http" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -40,6 +41,22 @@ func TestClient_IsNonScriptOperationsEnabled_NewServers(t *testing.T) { assert.False(t, enabled) } +func TestClient_put(t *testing.T) { + c := NewDefaultClient("") + apiPath := "test-path" + query := "test-query" + body := "test-body" + req, err := c.put(apiPath, query, body) + assert.Nil(t, err) + assert.True(t, strings.HasSuffix(req.URL.Path, apiPath)) + assert.Equal(t, query, req.URL.RawQuery) + + reqBody := make([]byte, 2*len(body)) + _, err = req.Body.Read(reqBody) + assert.Nil(t, err) + assert.Contains(t, string(reqBody), body) +} + func TestClient_SetCredentials(t *testing.T) { c := &Client{} username := "test-username" diff --git a/nexus/users.go b/nexus/users.go index 278fde2..cd1a2d7 100644 --- a/nexus/users.go +++ b/nexus/users.go @@ -18,6 +18,7 @@ package nexus import ( + "fmt" "net/url" ) @@ -49,6 +50,16 @@ func (u *UserService) List() ([]User, error) { return users, err } +// Update persists a new version of an existing user on the Nexus server +func (u *UserService) Update(user User) error { + req, err := u.client.put(u.client.appendVersion(fmt.Sprintf("/security/users/%s", user.UserID)), "", user) + if err != nil { + return err + } + _, err = u.client.do(req, nil) + return err +} + // GetUserByID Gets the user by it's id (authentication username) func (u *UserService) GetUserByID(userID string) (*User, error) { parsedURL, err := url.ParseQuery("userId=" + userID) diff --git a/nexus/users_test.go b/nexus/users_test.go index 65a1429..d60412f 100644 --- a/nexus/users_test.go +++ b/nexus/users_test.go @@ -53,6 +53,18 @@ var listUsersExpected = `[ } ]` +var defaultUser = User{ + UserID: "test-user", + FirstName: "Test", + LastName: "User", + Email: "test@user", + Source: "default", + ReadOnly: false, + Roles: []string{"nx-admin"}, + Status: "active", + Password: "t3st-us3r", +} + var adminUserResult = `[ { "userId": "admin", @@ -85,6 +97,19 @@ func TestUserService_ListUsers(t *testing.T) { assert.Len(t, users, 2) } +func TestUserService_Update(t *testing.T) { + // first, let's try updating a user which doesn't exist + s := newServerWrapper(t).WithStatusCode(http.StatusNotFound).Build() + assert.Error(t, s.Client().UserService.Update(defaultUser)) + + // now one that does exist + s = newServerWrapper(t).WithStatusCode(http.StatusOK).Build() + // add the user + assert.NoError(t, s.Client().UserService.Add(defaultUser)) + // try to update it + assert.NoError(t, s.Client().UserService.Update(defaultUser)) +} + func TestUserService_GetUserByID(t *testing.T) { s := newServerWrapper(t).WithResponse(adminUserResult).Build() defer s.teardown()