Skip to content
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
}