From a50d935c33eda2f4f0c373eca6f817167309ac65 Mon Sep 17 00:00:00 2001 From: jay brown Date: Mon, 14 Apr 2025 14:46:28 -0700 Subject: [PATCH] fix tests add feature flag for Auth --- internal/cognitoauth/auth.go | 12 ++++++++++++ internal/cognitoauth/readme.md | 4 ++++ internal/serviceconfig/auth/config_test.go | 14 ++++++-------- scripts/tests.yml | 2 +- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/internal/cognitoauth/auth.go b/internal/cognitoauth/auth.go index 12ee2cf7..ca54bfca 100644 --- a/internal/cognitoauth/auth.go +++ b/internal/cognitoauth/auth.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "os" "queryorchestration/internal/serviceconfig/auth" @@ -15,6 +16,17 @@ import ( // RegisterRoutes registers all authentication-related routes to the Echo engine func RegisterRoutes(e *echo.Echo, config auth.ConfigProvider) { + // check the auth feature flag + enableAuth := os.Getenv("ENABLE_AUTH") + if enableAuth == "" || strings.ToLower(enableAuth) == "false" { + return // Do nothing if auth is not enabled + } + + // Only proceed if ENABLE_AUTH is "true" or "auth" + if strings.ToLower(enableAuth) != "true" { + return + } + // Register the login route - the middleware will handle this e.GET(config.GetAuthLoginPath(), func(c echo.Context) error { // This is handled by the middleware diff --git a/internal/cognitoauth/readme.md b/internal/cognitoauth/readme.md index cdf11ce1..74b21ac5 100644 --- a/internal/cognitoauth/readme.md +++ b/internal/cognitoauth/readme.md @@ -12,6 +12,10 @@ A reusable Go package for AWS Cognito authentication and authorization with Echo - Default home page with authentication status - Simple integration with Echo framework +## Feature flag for Auth +To enable all of these features for all endpoints you must set +`ENABLE_AUTH=true` in your environment. + ## Installation ```bash diff --git a/internal/serviceconfig/auth/config_test.go b/internal/serviceconfig/auth/config_test.go index ee3ad232..c124883f 100644 --- a/internal/serviceconfig/auth/config_test.go +++ b/internal/serviceconfig/auth/config_test.go @@ -322,19 +322,15 @@ func TestPrettyPrintDebugOnly(t *testing.T) { cfg.PrettyPrintDebugOnly() + // Close writer and restore stdout w.Close() os.Stdout = oldStdout + // Read captured output var buf bytes.Buffer if _, err := io.Copy(&buf, r); err != nil { t.Fatalf("failed to copy output: %v", err) } - - // And later in the same function: - buf.Reset() - if _, err := io.Copy(&buf, r); err != nil { - t.Fatalf("failed to copy output: %v", err) - } output := buf.String() // Verify output contains expected values @@ -353,7 +349,9 @@ func TestPrettyPrintDebugOnly(t *testing.T) { } // Test with DEBUG=false - os.Unsetenv("DEBUG") + os.Setenv("DEBUG", "false") + + // Capture stdout again r, w, _ = os.Pipe() os.Stdout = w @@ -367,7 +365,7 @@ func TestPrettyPrintDebugOnly(t *testing.T) { t.Fatalf("failed to copy output: %v", err) } - if output != "" { + if buf.String() != "" { t.Error("Expected no output when DEBUG=false") } } diff --git a/scripts/tests.yml b/scripts/tests.yml index b5c5da6c..058e51ff 100644 --- a/scripts/tests.yml +++ b/scripts/tests.yml @@ -10,7 +10,7 @@ vars: PKG: "./pkg/..." CMD: "./cmd/..." # yamllint disable-line rule:line-length - EXCLUDED_FILES: ".gen.go|internal/serviceconfig/observability/prometheus/generator/main.go|internal/cognitoauth/auth.go|internal/cognitoauth/handler.go|internal/cognitoauth/jwks.go|internal/cognitoauth/middleware.go|internal/cognitoauth/models.go|internal/cognitoauth/token.go|internal/cognitoauth/utils.go|api/queryAPI/authHandlers.go" + EXCLUDED_FILES: ".gen.go|internal/serviceconfig/observability/prometheus/generator/main.go|internal/cognitoauth/auth.go|internal/cognitoauth/handler.go|internal/cognitoauth/jwks.go|internal/cognitoauth/middleware.go|internal/cognitoauth/models.go|internal/cognitoauth/token.go|internal/cognitoauth/utils.go|api/queryAPI/authHandlers.go|api/queryAPI/homehandler.go" includes: docker: