Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 212020a

Browse files
committed
Add tests and fixes
1 parent 2fe1716 commit 212020a

File tree

4 files changed

+78
-13
lines changed

4 files changed

+78
-13
lines changed

coderd/coderdtest/coderdtest.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,21 +174,22 @@ func NewProvisionerDaemon(t *testing.T, client *codersdk.Client) io.Closer {
174174
return closer
175175
}
176176

177+
var FirstUserParams = codersdk.CreateFirstUserRequest{
178+
179+
Username: "testuser",
180+
Password: "testpass",
181+
OrganizationName: "testorg",
182+
}
183+
177184
// CreateFirstUser creates a user with preset credentials and authenticates
178185
// with the passed in codersdk client.
179186
func CreateFirstUser(t *testing.T, client *codersdk.Client) codersdk.CreateFirstUserResponse {
180-
req := codersdk.CreateFirstUserRequest{
181-
182-
Username: "testuser",
183-
Password: "testpass",
184-
OrganizationName: "testorg",
185-
}
186-
resp, err := client.CreateFirstUser(context.Background(), req)
187+
resp, err := client.CreateFirstUser(context.Background(), FirstUserParams)
187188
require.NoError(t, err)
188189

189190
login, err := client.LoginWithPassword(context.Background(), codersdk.LoginWithPasswordRequest{
190-
Email: req.Email,
191-
Password: req.Password,
191+
Email: FirstUserParams.Email,
192+
Password: FirstUserParams.Password,
192193
})
193194
require.NoError(t, err)
194195
client.SessionToken = login.SessionToken

coderd/users.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ func (api *api) putUserSuspend(rw http.ResponseWriter, r *http.Request) {
363363
func (api *api) putUserPassword(rw http.ResponseWriter, r *http.Request) {
364364
user := httpmw.UserParam(r)
365365

366-
var params codersdk.UpdateUserHashedPasswordRequest
366+
var params codersdk.UpdateUserPasswordRequest
367367
if !httpapi.Read(rw, r, &params) {
368368
return
369369
}
@@ -406,11 +406,12 @@ func (api *api) putUserPassword(rw http.ResponseWriter, r *http.Request) {
406406
return
407407
}
408408
databaseError := api.Database.UpdateUserHashedPassword(r.Context(), database.UpdateUserHashedPasswordParams{
409+
ID: user.ID,
409410
HashedPassword: []byte(hashedPassword),
410411
})
411412
if databaseError != nil {
412413
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
413-
Message: fmt.Sprintf("put user password: %s", err.Error()),
414+
Message: fmt.Sprintf("put user password: %s", databaseError.Error()),
414415
})
415416
return
416417
}
@@ -635,7 +636,6 @@ func (api *api) postLogin(rw http.ResponseWriter, r *http.Request) {
635636
}
636637

637638
// If the user doesn't exist, it will be a default struct.
638-
639639
equal, err := userpassword.Compare(string(user.HashedPassword), loginWithPassword.Password)
640640
if err != nil {
641641
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{

coderd/users_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,57 @@ func TestUpdateUserProfile(t *testing.T) {
287287
})
288288
}
289289

290+
func TestUpdateUserPassword(t *testing.T) {
291+
t.Parallel()
292+
293+
t.Run("WrongPassword", func(t *testing.T) {
294+
t.Parallel()
295+
client := coderdtest.New(t, nil)
296+
coderdtest.CreateFirstUser(t, client)
297+
err := client.UpdateUserPassword(context.Background(), codersdk.Me, codersdk.UpdateUserPasswordRequest{
298+
Password: "wrongpassword",
299+
NewPassword: "newpassword",
300+
ConfirmNewPassword: "newpassword",
301+
})
302+
var apiErr *codersdk.Error
303+
require.ErrorAs(t, err, &apiErr)
304+
require.Equal(t, http.StatusUnauthorized, apiErr.StatusCode())
305+
})
306+
307+
t.Run("DifferentPasswordConfirmation", func(t *testing.T) {
308+
t.Parallel()
309+
client := coderdtest.New(t, nil)
310+
coderdtest.CreateFirstUser(t, client)
311+
err := client.UpdateUserPassword(context.Background(), codersdk.Me, codersdk.UpdateUserPasswordRequest{
312+
Password: coderdtest.FirstUserParams.Password,
313+
NewPassword: "newpassword",
314+
ConfirmNewPassword: "wrongconfirmation",
315+
})
316+
var apiErr *codersdk.Error
317+
require.ErrorAs(t, err, &apiErr)
318+
require.Equal(t, http.StatusBadRequest, apiErr.StatusCode())
319+
})
320+
321+
t.Run("Success", func(t *testing.T) {
322+
t.Parallel()
323+
client := coderdtest.New(t, nil)
324+
coderdtest.CreateFirstUser(t, client)
325+
err := client.UpdateUserPassword(context.Background(), codersdk.Me, codersdk.UpdateUserPasswordRequest{
326+
Password: coderdtest.FirstUserParams.Password,
327+
NewPassword: "newpassword",
328+
ConfirmNewPassword: "newpassword",
329+
})
330+
require.NoError(t, err, "update password request should be successful")
331+
332+
// Check if the user can login using the new password
333+
_, err = client.LoginWithPassword(context.Background(), codersdk.LoginWithPasswordRequest{
334+
Email: coderdtest.FirstUserParams.Email,
335+
Password: "newpassword",
336+
})
337+
require.NoError(t, err, "login should be successful")
338+
})
339+
}
340+
290341
func TestGrantRoles(t *testing.T) {
291342
t.Parallel()
292343
t.Run("UpdateIncorrectRoles", func(t *testing.T) {

codersdk/users.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ type UpdateUserProfileRequest struct {
7272
Username string `json:"username" validate:"required,username"`
7373
}
7474

75-
type UpdateUserHashedPasswordRequest struct {
75+
type UpdateUserPasswordRequest struct {
7676
Password string `json:"password" validate:"required"`
7777
NewPassword string `json:"new_password" validate:"required"`
7878
ConfirmNewPassword string `json:"confirm_new_password" validate:"required"`
@@ -187,6 +187,19 @@ func (c *Client) SuspendUser(ctx context.Context, userID uuid.UUID) (User, error
187187
return user, json.NewDecoder(res.Body).Decode(&user)
188188
}
189189

190+
// Update user password
191+
func (c *Client) UpdateUserPassword(ctx context.Context, userID uuid.UUID, req UpdateUserPasswordRequest) error {
192+
res, err := c.request(ctx, http.MethodPut, fmt.Sprintf("/api/v2/users/%s/password", uuidOrMe(userID)), req)
193+
if err != nil {
194+
return err
195+
}
196+
defer res.Body.Close()
197+
if res.StatusCode != http.StatusNoContent {
198+
return readBodyAsError(res)
199+
}
200+
return nil
201+
}
202+
190203
// UpdateUserRoles grants the userID the specified roles.
191204
// Include ALL roles the user has.
192205
func (c *Client) UpdateUserRoles(ctx context.Context, userID uuid.UUID, req UpdateRoles) (User, error) {

0 commit comments

Comments
 (0)