From 9e4c283370997a217497136e32fb95e83567205b Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Mon, 23 Mar 2026 21:03:34 +0000 Subject: [PATCH] test: share coderdtest instances in OAuth2 validation tests (#23455) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Consolidates invocations of `coderdtest.New` to a single shared instance per parent for the following tests: - `TestOAuth2ClientMetadataValidation` - `TestOAuth2ClientNameValidation` - `TestOAuth2ClientScopeValidation` - `TestOAuth2ClientMetadataEdgeCases` > 🤖 This PR was created with the help of Coder Agents, and was reviewed by my human. 🧑‍💻 --- coderd/oauth2_metadata_validation_test.go | 48 ++++++++--------------- coderd/oauth2provider/validation_test.go | 48 ++++++++--------------- 2 files changed, 32 insertions(+), 64 deletions(-) diff --git a/coderd/oauth2_metadata_validation_test.go b/coderd/oauth2_metadata_validation_test.go index 889f402be2..d880973ce1 100644 --- a/coderd/oauth2_metadata_validation_test.go +++ b/coderd/oauth2_metadata_validation_test.go @@ -18,12 +18,13 @@ import ( func TestOAuth2ClientMetadataValidation(t *testing.T) { t.Parallel() + // Single instance shared across all sub-tests. Each registers independent OAuth2 apps with unique client names. + client := coderdtest.New(t, nil) + _ = coderdtest.CreateFirstUser(t, client) + t.Run("RedirectURIValidation", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) - tests := []struct { name string redirectURIs []string @@ -132,9 +133,6 @@ func TestOAuth2ClientMetadataValidation(t *testing.T) { t.Run("ClientURIValidation", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) - tests := []struct { name string clientURI string @@ -207,9 +205,6 @@ func TestOAuth2ClientMetadataValidation(t *testing.T) { t.Run("LogoURIValidation", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) - tests := []struct { name string logoURI string @@ -272,9 +267,6 @@ func TestOAuth2ClientMetadataValidation(t *testing.T) { t.Run("GrantTypeValidation", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) - tests := []struct { name string grantTypes []codersdk.OAuth2ProviderGrantType @@ -347,9 +339,6 @@ func TestOAuth2ClientMetadataValidation(t *testing.T) { t.Run("ResponseTypeValidation", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) - tests := []struct { name string responseTypes []codersdk.OAuth2ProviderResponseType @@ -407,9 +396,6 @@ func TestOAuth2ClientMetadataValidation(t *testing.T) { t.Run("TokenEndpointAuthMethodValidation", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) - tests := []struct { name string authMethod codersdk.OAuth2TokenEndpointAuthMethod @@ -479,6 +465,10 @@ func TestOAuth2ClientMetadataValidation(t *testing.T) { func TestOAuth2ClientNameValidation(t *testing.T) { t.Parallel() + // Single instance shared across all sub-tests. Each registers independent OAuth2 apps. + client := coderdtest.New(t, nil) + _ = coderdtest.CreateFirstUser(t, client) + tests := []struct { name string clientName string @@ -530,8 +520,6 @@ func TestOAuth2ClientNameValidation(t *testing.T) { t.Run(test.name, func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) ctx := testutil.Context(t, testutil.WaitLong) req := codersdk.OAuth2ClientRegistrationRequest{ @@ -554,6 +542,10 @@ func TestOAuth2ClientNameValidation(t *testing.T) { func TestOAuth2ClientScopeValidation(t *testing.T) { t.Parallel() + // Single instance shared across all sub-tests. Each registers independent OAuth2 apps. + client := coderdtest.New(t, nil) + _ = coderdtest.CreateFirstUser(t, client) + tests := []struct { name string scope string @@ -615,8 +607,6 @@ func TestOAuth2ClientScopeValidation(t *testing.T) { t.Run(test.name, func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) ctx := testutil.Context(t, testutil.WaitLong) req := codersdk.OAuth2ClientRegistrationRequest{ @@ -682,11 +672,13 @@ func TestOAuth2ClientMetadataDefaults(t *testing.T) { func TestOAuth2ClientMetadataEdgeCases(t *testing.T) { t.Parallel() + // Single instance shared across all sub-tests. Each registers independent OAuth2 apps with unique client names. + client := coderdtest.New(t, nil) + _ = coderdtest.CreateFirstUser(t, client) + t.Run("ExtremelyLongRedirectURI", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) ctx := testutil.Context(t, testutil.WaitLong) // Create a very long but valid HTTPS URI @@ -709,8 +701,6 @@ func TestOAuth2ClientMetadataEdgeCases(t *testing.T) { t.Run("ManyRedirectURIs", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) ctx := testutil.Context(t, testutil.WaitLong) // Test with many redirect URIs @@ -732,8 +722,6 @@ func TestOAuth2ClientMetadataEdgeCases(t *testing.T) { t.Run("URIWithUnusualPort", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) ctx := testutil.Context(t, testutil.WaitLong) req := codersdk.OAuth2ClientRegistrationRequest{ @@ -748,8 +736,6 @@ func TestOAuth2ClientMetadataEdgeCases(t *testing.T) { t.Run("URIWithComplexPath", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) ctx := testutil.Context(t, testutil.WaitLong) req := codersdk.OAuth2ClientRegistrationRequest{ @@ -764,8 +750,6 @@ func TestOAuth2ClientMetadataEdgeCases(t *testing.T) { t.Run("URIWithEncodedCharacters", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) ctx := testutil.Context(t, testutil.WaitLong) // Test with URL-encoded characters diff --git a/coderd/oauth2provider/validation_test.go b/coderd/oauth2provider/validation_test.go index 8e556e0937..9367079ea6 100644 --- a/coderd/oauth2provider/validation_test.go +++ b/coderd/oauth2provider/validation_test.go @@ -18,12 +18,13 @@ import ( func TestOAuth2ClientMetadataValidation(t *testing.T) { t.Parallel() + // Single instance shared across all sub-tests. Each registers independent OAuth2 apps with unique client names. + client := coderdtest.New(t, nil) + _ = coderdtest.CreateFirstUser(t, client) + t.Run("RedirectURIValidation", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) - tests := []struct { name string redirectURIs []string @@ -132,9 +133,6 @@ func TestOAuth2ClientMetadataValidation(t *testing.T) { t.Run("ClientURIValidation", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) - tests := []struct { name string clientURI string @@ -207,9 +205,6 @@ func TestOAuth2ClientMetadataValidation(t *testing.T) { t.Run("LogoURIValidation", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) - tests := []struct { name string logoURI string @@ -272,9 +267,6 @@ func TestOAuth2ClientMetadataValidation(t *testing.T) { t.Run("GrantTypeValidation", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) - tests := []struct { name string grantTypes []codersdk.OAuth2ProviderGrantType @@ -347,9 +339,6 @@ func TestOAuth2ClientMetadataValidation(t *testing.T) { t.Run("ResponseTypeValidation", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) - tests := []struct { name string responseTypes []codersdk.OAuth2ProviderResponseType @@ -407,9 +396,6 @@ func TestOAuth2ClientMetadataValidation(t *testing.T) { t.Run("TokenEndpointAuthMethodValidation", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) - tests := []struct { name string authMethod codersdk.OAuth2TokenEndpointAuthMethod @@ -479,6 +465,10 @@ func TestOAuth2ClientMetadataValidation(t *testing.T) { func TestOAuth2ClientNameValidation(t *testing.T) { t.Parallel() + // Single instance shared across all sub-tests. Each registers independent OAuth2 apps. + client := coderdtest.New(t, nil) + _ = coderdtest.CreateFirstUser(t, client) + tests := []struct { name string clientName string @@ -530,8 +520,6 @@ func TestOAuth2ClientNameValidation(t *testing.T) { t.Run(test.name, func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) ctx := testutil.Context(t, testutil.WaitLong) req := codersdk.OAuth2ClientRegistrationRequest{ @@ -554,6 +542,10 @@ func TestOAuth2ClientNameValidation(t *testing.T) { func TestOAuth2ClientScopeValidation(t *testing.T) { t.Parallel() + // Single instance shared across all sub-tests. Each registers independent OAuth2 apps. + client := coderdtest.New(t, nil) + _ = coderdtest.CreateFirstUser(t, client) + tests := []struct { name string scope string @@ -615,8 +607,6 @@ func TestOAuth2ClientScopeValidation(t *testing.T) { t.Run(test.name, func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) ctx := testutil.Context(t, testutil.WaitLong) req := codersdk.OAuth2ClientRegistrationRequest{ @@ -682,11 +672,13 @@ func TestOAuth2ClientMetadataDefaults(t *testing.T) { func TestOAuth2ClientMetadataEdgeCases(t *testing.T) { t.Parallel() + // Single instance shared across all sub-tests. Each registers independent OAuth2 apps with unique client names. + client := coderdtest.New(t, nil) + _ = coderdtest.CreateFirstUser(t, client) + t.Run("ExtremelyLongRedirectURI", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) ctx := testutil.Context(t, testutil.WaitLong) // Create a very long but valid HTTPS URI @@ -709,8 +701,6 @@ func TestOAuth2ClientMetadataEdgeCases(t *testing.T) { t.Run("ManyRedirectURIs", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) ctx := testutil.Context(t, testutil.WaitLong) // Test with many redirect URIs @@ -732,8 +722,6 @@ func TestOAuth2ClientMetadataEdgeCases(t *testing.T) { t.Run("URIWithUnusualPort", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) ctx := testutil.Context(t, testutil.WaitLong) req := codersdk.OAuth2ClientRegistrationRequest{ @@ -748,8 +736,6 @@ func TestOAuth2ClientMetadataEdgeCases(t *testing.T) { t.Run("URIWithComplexPath", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) ctx := testutil.Context(t, testutil.WaitLong) req := codersdk.OAuth2ClientRegistrationRequest{ @@ -764,8 +750,6 @@ func TestOAuth2ClientMetadataEdgeCases(t *testing.T) { t.Run("URIWithEncodedCharacters", func(t *testing.T) { t.Parallel() - client := coderdtest.New(t, nil) - _ = coderdtest.CreateFirstUser(t, client) ctx := testutil.Context(t, testutil.WaitLong) // Test with URL-encoded characters