Skip to content

fix: remove shared mutable state between oidc tests #17179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2025
Merged
Changes from all commits
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
30 changes: 19 additions & 11 deletions coderd/userauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1988,30 +1988,36 @@ func TestUserLogout(t *testing.T) {
func TestOIDCDomainErrorMessage(t *testing.T) {
t.Parallel()

fake := oidctest.NewFakeIDP(t, oidctest.WithServing())

allowedDomains := []string{"allowed1.com", "allowed2.org", "company.internal"}
cfg := fake.OIDCConfig(t, nil, func(cfg *coderd.OIDCConfig) {
cfg.EmailDomain = allowedDomains
cfg.AllowSignups = true
})

server := coderdtest.New(t, &coderdtest.Options{
OIDCConfig: cfg,
})
setup := func() (*oidctest.FakeIDP, *codersdk.Client) {
fake := oidctest.NewFakeIDP(t, oidctest.WithServing())

cfg := fake.OIDCConfig(t, nil, func(cfg *coderd.OIDCConfig) {
cfg.EmailDomain = allowedDomains
cfg.AllowSignups = true
})

client := coderdtest.New(t, &coderdtest.Options{
OIDCConfig: cfg,
})
return fake, client
}

// Test case 1: Email domain not in allowed list
t.Run("ErrorMessageOmitsDomains", func(t *testing.T) {
t.Parallel()

fake, client := setup()

// Prepare claims with email from unauthorized domain
claims := jwt.MapClaims{
"email": "user@unauthorized.com",
"email_verified": true,
"sub": uuid.NewString(),
}

_, resp := fake.AttemptLogin(t, server, claims)
_, resp := fake.AttemptLogin(t, client, claims)
defer resp.Body.Close()

require.Equal(t, http.StatusForbidden, resp.StatusCode)
Expand All @@ -2031,14 +2037,16 @@ func TestOIDCDomainErrorMessage(t *testing.T) {
t.Run("MalformedEmailErrorOmitsDomains", func(t *testing.T) {
t.Parallel()

fake, client := setup()

// Prepare claims with an invalid email format (no @ symbol)
claims := jwt.MapClaims{
"email": "invalid-email-without-domain",
"email_verified": true,
"sub": uuid.NewString(),
}

_, resp := fake.AttemptLogin(t, server, claims)
_, resp := fake.AttemptLogin(t, client, claims)
defer resp.Body.Close()

require.Equal(t, http.StatusForbidden, resp.StatusCode)
Expand Down
Loading