Skip to content

Commit

Permalink
Allow user updates (#15)
Browse files Browse the repository at this point in the history
* Allow user updates

Signed-off-by: Lucas Caparelli <[email protected]>

* Make suggested change

Signed-off-by: Lucas Caparelli <[email protected]>
  • Loading branch information
LCaparelli authored Jul 15, 2020
1 parent 2668b02 commit a49334b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
8 changes: 6 additions & 2 deletions nexus/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions nexus/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package nexus

import (
"net/http"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -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"
Expand Down
11 changes: 11 additions & 0 deletions nexus/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package nexus

import (
"fmt"
"net/url"
)

Expand Down Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions nexus/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a49334b

Please sign in to comment.