Skip to content

feat: Add update profile endpoint #916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tests: Add tests
  • Loading branch information
BrunoQuaresma committed Apr 7, 2022
commit 27adb0996bd247936705479d204a7a5a04fc0d2c
1 change: 1 addition & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func New(options *Options) (http.Handler, func()) {
r.Route("/{user}", func(r chi.Router) {
r.Use(httpmw.ExtractUserParam(options.Database))
r.Get("/", api.userByName)
r.Patch("/", api.patchUser)
r.Get("/organizations", api.organizationsByUser)
r.Post("/organizations", api.postOrganizationsByUser)
r.Post("/keys", api.postAPIKey)
Expand Down
16 changes: 16 additions & 0 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,22 @@ func (q *fakeQuerier) InsertUser(_ context.Context, arg database.InsertUserParam
return user, nil
}

func (q *fakeQuerier) UpdateUser(_ context.Context, arg database.UpdateUserParams) (database.User, error) {
q.mutex.Lock()
defer q.mutex.Unlock()

for index, user := range q.users {
if user.ID != arg.ID {
continue
}
user.Email = arg.Email
user.Username = arg.Username
q.users[index] = user
return user, nil
}
return database.User{}, sql.ErrNoRows
}

func (q *fakeQuerier) InsertWorkspace(_ context.Context, arg database.InsertWorkspaceParams) (database.Workspace, error) {
q.mutex.Lock()
defer q.mutex.Unlock()
Expand Down
1 change: 1 addition & 0 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ func (api *api) patchUser(rw http.ResponseWriter, r *http.Request) {
return
}

render.Status(r, http.StatusOK)
render.JSON(rw, r, convertUser(updatedUser))
}

Expand Down
13 changes: 13 additions & 0 deletions coderd/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,19 @@ func TestPostUsers(t *testing.T) {
})
}

func TestPatchUser(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
coderdtest.CreateFirstUser(t, client)
user, err := client.PatchUser(context.Background(), codersdk.Me, codersdk.PatchUserRequest{
Username: "newusername",
Email: "newemail@coder.com",
})
require.NoError(t, err)
require.Equal(t, user.Username, "newusername")
require.Equal(t, user.Email, "newemail@coder.com")
}

func TestUserByName(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
Expand Down
16 changes: 15 additions & 1 deletion codersdk/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type CreateUserRequest struct {
type PatchUserRequest struct {
Email string `json:"email" validate:"required,email"`
Username string `json:"username" validate:"required,username"`
Name string `json:"name" validate:"username"`
Name string `json:"name"`
}

// LoginWithPasswordRequest enables callers to authenticate with email and password.
Expand Down Expand Up @@ -121,6 +121,20 @@ func (c *Client) CreateUser(ctx context.Context, req CreateUserRequest) (User, e
return user, json.NewDecoder(res.Body).Decode(&user)
}

// Patch User
func (c *Client) PatchUser(ctx context.Context, userID uuid.UUID, req PatchUserRequest) (User, error) {
res, err := c.request(ctx, http.MethodPatch, fmt.Sprintf("/api/v2/users/%s", uuidOrMe(userID)), req)
if err != nil {
return User{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return User{}, readBodyAsError(res)
}
var user User
return user, json.NewDecoder(res.Body).Decode(&user)
}

// CreateAPIKey generates an API key for the user ID provided.
func (c *Client) CreateAPIKey(ctx context.Context, userID uuid.UUID) (*GenerateAPIKeyResponse, error) {
res, err := c.request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/users/%s/keys", uuidOrMe(userID)), nil)
Expand Down