Skip to content

fix: Add route for user to change own password #1812

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 10 commits into from
May 27, 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
Add basic test
  • Loading branch information
f0ssel committed May 27, 2022
commit a4902925c3cbef6b1f4d6cb2298f3e97ffdac417
26 changes: 26 additions & 0 deletions coderd/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,32 @@ func TestUpdateUserPassword(t *testing.T) {
})
require.NoError(t, err, "member should login successfully with the new password")
})

t.Run("MemberCantUpdateOwnPasswordViaAdminRoute", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
admin := coderdtest.CreateFirstUser(t, client)
member := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
err := member.UpdateUserPassword(context.Background(), "me", codersdk.UpdateUserPasswordRequest{
Password: "newpassword",
})
require.Error(t, err, "member should not be able to update own password via admin route")
})
}

func TestUpdateUserOwnPassword(t *testing.T) {
t.Parallel()
t.Run("MemberCanUpdateOwnPassword", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
admin := coderdtest.CreateFirstUser(t, client)
member := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
err := member.UpdateUserOwnPassword(context.Background(), codersdk.UpdateUserOwnPasswordRequest{
OldPassword: "testpass",
Password: "newpassword",
})
require.NoError(t, err, "member should be able to update own password")
})
}

func TestGrantRoles(t *testing.T) {
Expand Down
13 changes: 13 additions & 0 deletions codersdk/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,16 @@ func (c *Client) AuthMethods(ctx context.Context) (AuthMethods, error) {
var userAuth AuthMethods
return userAuth, json.NewDecoder(res.Body).Decode(&userAuth)
}

// UpdateUserOwnPassword updates a user's own password.
func (c *Client) UpdateUserOwnPassword(ctx context.Context, req UpdateUserOwnPasswordRequest) error {
res, err := c.Request(ctx, http.MethodPut, fmt.Sprintf("/api/v2/users/me/ownpassword"), req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusNoContent {
return readBodyAsError(res)
}
return nil
}