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
feat: check if there is a user with the email or username
  • Loading branch information
BrunoQuaresma committed Apr 8, 2022
commit edd8c1fd1faeceafb194c60e8f556b2beea2333e
23 changes: 23 additions & 0 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,29 @@ func (api *api) patchUserProfile(rw http.ResponseWriter, r *http.Request) {
return
}

existentUser, err := api.Database.GetUserByEmailOrUsername(r.Context(), database.GetUserByEmailOrUsernameParams{
Email: patchUserProfile.Email,
Username: patchUserProfile.Username,
})
isDifferentUser := existentUser.ID != user.ID

if err == nil && isDifferentUser {
message := "Email is already on use"
if existentUser.Username == patchUserProfile.Username {
message = "Username is already on use"
}
httpapi.Write(rw, http.StatusConflict, httpapi.Response{
Message: message,
})
return
}
if !errors.Is(err, sql.ErrNoRows) && isDifferentUser {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: fmt.Sprintf("get user: %s", err),
})
return
}

updatedUserProfile, err := api.Database.UpdateUserProfile(r.Context(), database.UpdateUserProfileParams{
ID: user.ID,
Name: patchUserProfile.Name,
Expand Down
54 changes: 53 additions & 1 deletion coderd/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,45 @@ func TestPatchUserProfile(t *testing.T) {
require.Equal(t, http.StatusBadRequest, apiErr.StatusCode())
})

t.Run("Patch", func(t *testing.T) {
t.Run("Conflicting email", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
existentUser, err := client.CreateUser(context.Background(), codersdk.CreateUserRequest{
Email: "bruno@coder.com",
Username: "bruno",
Password: "password",
OrganizationID: user.OrganizationID,
})
_, err = client.PatchUserProfile(context.Background(), codersdk.Me, codersdk.PatchUserProfileRequest{
Username: "newusername",
Email: existentUser.Email,
})
var apiErr *codersdk.Error
require.ErrorAs(t, err, &apiErr)
require.Equal(t, http.StatusConflict, apiErr.StatusCode())
})

t.Run("Conflicting username", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
existentUser, err := client.CreateUser(context.Background(), codersdk.CreateUserRequest{
Email: "bruno@coder.com",
Username: "bruno",
Password: "password",
OrganizationID: user.OrganizationID,
})
_, err = client.PatchUserProfile(context.Background(), codersdk.Me, codersdk.PatchUserProfileRequest{
Username: existentUser.Username,
Email: "newemail@coder.com",
})
var apiErr *codersdk.Error
require.ErrorAs(t, err, &apiErr)
require.Equal(t, http.StatusConflict, apiErr.StatusCode())
})

t.Run("Full Patch", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
coderdtest.CreateFirstUser(t, client)
Expand All @@ -229,6 +267,20 @@ func TestPatchUserProfile(t *testing.T) {
require.Equal(t, userProfile.Username, "newusername")
require.Equal(t, userProfile.Email, "newemail@coder.com")
})

t.Run("Partial Patch", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
coderdtest.CreateFirstUser(t, client)
me, err := client.User(context.Background(), codersdk.Me)
userProfile, err := client.PatchUserProfile(context.Background(), codersdk.Me, codersdk.PatchUserProfileRequest{
Username: me.Username,
Email: "newemail@coder.com",
})
require.NoError(t, err)
require.Equal(t, userProfile.Username, me.Username)
require.Equal(t, userProfile.Email, "newemail@coder.com")
})
}

func TestUserByName(t *testing.T) {
Expand Down