Skip to content

Commit ce9d54c

Browse files
committed
add unit test
1 parent f432127 commit ce9d54c

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

coderd/coderdtest/coderdtest.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -497,36 +497,57 @@ func CreateFirstUser(t testing.TB, client *codersdk.Client) codersdk.CreateFirst
497497

498498
// CreateAnotherUser creates and authenticates a new user.
499499
func CreateAnotherUser(t *testing.T, client *codersdk.Client, organizationID uuid.UUID, roles ...string) (*codersdk.Client, codersdk.User) {
500-
return createAnotherUserRetry(t, client, organizationID, 5, roles...)
500+
return createAnotherUserRetry(t, client, organizationID, 5, roles)
501501
}
502502

503-
func createAnotherUserRetry(t *testing.T, client *codersdk.Client, organizationID uuid.UUID, retries int, roles ...string) (*codersdk.Client, codersdk.User) {
503+
func CreateAnotherUserMutators(t *testing.T, client *codersdk.Client, organizationID uuid.UUID, roles []string, mutators ...func(r *codersdk.CreateUserRequest)) (*codersdk.Client, codersdk.User) {
504+
return createAnotherUserRetry(t, client, organizationID, 5, roles, mutators...)
505+
}
506+
507+
func createAnotherUserRetry(t *testing.T, client *codersdk.Client, organizationID uuid.UUID, retries int, roles []string, mutators ...func(r *codersdk.CreateUserRequest)) (*codersdk.Client, codersdk.User) {
504508
req := codersdk.CreateUserRequest{
505509
Email: namesgenerator.GetRandomName(10) + "@coder.com",
506510
Username: randomUsername(t),
507511
Password: "SomeSecurePassword!",
508512
OrganizationID: organizationID,
509513
}
514+
for _, m := range mutators {
515+
m(&req)
516+
}
510517

511518
user, err := client.CreateUser(context.Background(), req)
512519
var apiError *codersdk.Error
513520
// If the user already exists by username or email conflict, try again up to "retries" times.
514521
if err != nil && retries >= 0 && xerrors.As(err, &apiError) {
515522
if apiError.StatusCode() == http.StatusConflict {
516523
retries--
517-
return createAnotherUserRetry(t, client, organizationID, retries, roles...)
524+
return createAnotherUserRetry(t, client, organizationID, retries, roles)
518525
}
519526
}
520527
require.NoError(t, err)
521528

522-
login, err := client.LoginWithPassword(context.Background(), codersdk.LoginWithPasswordRequest{
523-
Email: req.Email,
524-
Password: req.Password,
525-
})
526-
require.NoError(t, err)
529+
var sessionToken string
530+
if !req.DisableLogin {
531+
login, err := client.LoginWithPassword(context.Background(), codersdk.LoginWithPasswordRequest{
532+
Email: req.Email,
533+
Password: req.Password,
534+
})
535+
require.NoError(t, err)
536+
sessionToken = login.SessionToken
537+
} else {
538+
// Cannot log in with a disabled login user. So make it an api key from
539+
// the client making this user.
540+
token, err := client.CreateToken(context.Background(), user.ID.String(), codersdk.CreateTokenRequest{
541+
Lifetime: time.Hour * 24,
542+
Scope: codersdk.APIKeyScopeAll,
543+
TokenName: "no-password-user-token",
544+
})
545+
require.NoError(t, err)
546+
sessionToken = token.Key
547+
}
527548

528549
other := codersdk.New(client.URL)
529-
other.SetSessionToken(login.SessionToken)
550+
other.SetSessionToken(sessionToken)
530551
t.Cleanup(func() {
531552
other.HTTPClient.CloseIdleConnections()
532553
})

coderd/userauth_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ func TestUserLogin(t *testing.T) {
5656
require.ErrorAs(t, err, &apiErr)
5757
require.Equal(t, http.StatusUnauthorized, apiErr.StatusCode())
5858
})
59+
// Password auth should fail if the user is made without password login.
60+
t.Run("LoginTypeNone", func(t *testing.T) {
61+
t.Parallel()
62+
client := coderdtest.New(t, nil)
63+
user := coderdtest.CreateFirstUser(t, client)
64+
anotherClient, anotherUser := coderdtest.CreateAnotherUserMutators(t, client, user.OrganizationID, nil, func(r *codersdk.CreateUserRequest) {
65+
r.Password = ""
66+
r.DisableLogin = true
67+
})
68+
69+
_, err := anotherClient.LoginWithPassword(context.Background(), codersdk.LoginWithPasswordRequest{
70+
Email: anotherUser.Email,
71+
Password: "SomeSecurePassword!",
72+
})
73+
require.Error(t, err)
74+
})
5975
}
6076

6177
func TestUserAuthMethods(t *testing.T) {

0 commit comments

Comments
 (0)