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

Skip to content

Commit 671c56d

Browse files
committed
Remove user restriction and refactor tests
1 parent 4f9f506 commit 671c56d

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

coderd/httpmw/userparam.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,6 @@ func ExtractUserParam(db database.Store) func(http.Handler) http.Handler {
7676
}
7777
}
7878

79-
apiKey := APIKey(r)
80-
if apiKey.UserID != user.ID {
81-
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
82-
Message: "getting non-personal users isn't supported yet",
83-
})
84-
return
85-
}
86-
8779
ctx := context.WithValue(r.Context(), userParamContextKey{}, user)
8880
next.ServeHTTP(rw, r.WithContext(ctx))
8981
})

coderd/users_test.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,21 +290,38 @@ func TestUpdateUserProfile(t *testing.T) {
290290
func TestUpdateUserPassword(t *testing.T) {
291291
t.Parallel()
292292

293-
t.Run("Success", func(t *testing.T) {
293+
t.Run("MemberCantUpdateAdminPassword", func(t *testing.T) {
294294
t.Parallel()
295295
client := coderdtest.New(t, nil)
296-
coderdtest.CreateFirstUser(t, client)
297-
err := client.UpdateUserPassword(context.Background(), codersdk.Me, codersdk.UpdateUserPasswordRequest{
296+
admin := coderdtest.CreateFirstUser(t, client)
297+
member := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
298+
err := member.UpdateUserPassword(context.Background(), admin.UserID, codersdk.UpdateUserPasswordRequest{
298299
Password: "newpassword",
299300
})
300-
require.NoError(t, err, "update password request should be successful")
301+
require.Error(t, err, "member should not be able to update admin password")
302+
})
301303

302-
// Check if the user can login using the new password
304+
t.Run("AdminCanUpdateMemberPassword", func(t *testing.T) {
305+
t.Parallel()
306+
client := coderdtest.New(t, nil)
307+
admin := coderdtest.CreateFirstUser(t, client)
308+
member, err := client.CreateUser(context.Background(), codersdk.CreateUserRequest{
309+
310+
Username: "coder",
311+
Password: "password",
312+
OrganizationID: admin.OrganizationID,
313+
})
314+
require.NoError(t, err, "create member")
315+
err = client.UpdateUserPassword(context.Background(), member.ID, codersdk.UpdateUserPasswordRequest{
316+
Password: "newpassword",
317+
})
318+
require.NoError(t, err, "admin should be able to update member password")
319+
// Check if the member can login using the new password
303320
_, err = client.LoginWithPassword(context.Background(), codersdk.LoginWithPasswordRequest{
304-
Email: coderdtest.FirstUserParams.Email,
321+
305322
Password: "newpassword",
306323
})
307-
require.NoError(t, err, "login should be successful")
324+
require.NoError(t, err, "member should login successfully with the new password")
308325
})
309326
}
310327

0 commit comments

Comments
 (0)