Skip to content

Commit 11b7a67

Browse files
committed
Add unit test for oidc and no login type create
1 parent bdec01f commit 11b7a67

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

cli/usercreate.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ func (r *RootCmd) userCreate() *clibase.Cmd {
5757
}
5858
}
5959
userLoginType := codersdk.LoginTypePassword
60+
if disableLogin && loginType != "" {
61+
return xerrors.New("You cannot specify both --disable-login and --login-type")
62+
}
6063
if disableLogin {
6164
userLoginType = codersdk.LoginTypeNone
6265
} else if loginType != "" {
63-
if disableLogin {
64-
return xerrors.New("You cannot specify both --disable-login and --login-type")
65-
}
6666
userLoginType = codersdk.LoginType(loginType)
6767
}
6868

coderd/users_test.go

+66
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010
"time"
1111

12+
"github.com/golang-jwt/jwt"
1213
"github.com/google/uuid"
1314
"github.com/stretchr/testify/assert"
1415
"github.com/stretchr/testify/require"
@@ -565,6 +566,71 @@ func TestPostUsers(t *testing.T) {
565566
}
566567
}
567568
})
569+
570+
t.Run("CreateNoneLoginType", func(t *testing.T) {
571+
t.Parallel()
572+
client := coderdtest.New(t, nil)
573+
first := coderdtest.CreateFirstUser(t, client)
574+
575+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
576+
defer cancel()
577+
578+
user, err := client.CreateUser(ctx, codersdk.CreateUserRequest{
579+
OrganizationID: first.OrganizationID,
580+
Email: "another@user.org",
581+
Username: "someone-else",
582+
Password: "",
583+
UserLoginType: codersdk.LoginTypeNone,
584+
})
585+
require.NoError(t, err)
586+
587+
found, err := client.User(ctx, user.ID.String())
588+
require.NoError(t, err)
589+
require.Equal(t, found.LoginType, codersdk.LoginTypeNone)
590+
})
591+
592+
t.Run("CreateOIDCLoginType", func(t *testing.T) {
593+
t.Parallel()
594+
email := "another@user.org"
595+
conf := coderdtest.NewOIDCConfig(t, "")
596+
config := conf.OIDCConfig(t, jwt.MapClaims{
597+
"email": email,
598+
})
599+
config.AllowSignups = false
600+
config.IgnoreUserInfo = true
601+
602+
client := coderdtest.New(t, &coderdtest.Options{
603+
OIDCConfig: config,
604+
})
605+
first := coderdtest.CreateFirstUser(t, client)
606+
607+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
608+
defer cancel()
609+
610+
_, err := client.CreateUser(ctx, codersdk.CreateUserRequest{
611+
OrganizationID: first.OrganizationID,
612+
Email: email,
613+
Username: "someone-else",
614+
Password: "",
615+
UserLoginType: codersdk.LoginTypeOIDC,
616+
})
617+
require.NoError(t, err)
618+
619+
// Try to log in with OIDC.
620+
userClient := codersdk.New(client.URL)
621+
resp := oidcCallback(t, userClient, conf.EncodeClaims(t, jwt.MapClaims{
622+
"email": email,
623+
}))
624+
require.Equal(t, resp.StatusCode, http.StatusTemporaryRedirect)
625+
// Set the client to use this OIDC context
626+
authCookie := authCookieValue(resp.Cookies())
627+
userClient.SetSessionToken(authCookie)
628+
_ = resp.Body.Close()
629+
630+
found, err := userClient.User(ctx, "me")
631+
require.NoError(t, err)
632+
require.Equal(t, found.LoginType, codersdk.LoginTypeOIDC)
633+
})
568634
}
569635

570636
func TestUpdateUserProfile(t *testing.T) {

0 commit comments

Comments
 (0)