diff --git a/internal/ingress/controller/template/template_test.go b/internal/ingress/controller/template/template_test.go index ffe2ae234..a25e20b43 100644 --- a/internal/ingress/controller/template/template_test.go +++ b/internal/ingress/controller/template/template_test.go @@ -459,6 +459,56 @@ func TestBuildAuthLocation(t *testing.T) { } } +func TestShouldApplyGlobalAuthWhenDefaultEnableIsChanged(t *testing.T) { + authURL := fooAuthHost + globalAuthURL := "foo.com/global-auth" + cfg := config.Configuration{} + + loc := &ingress.Location{ + ExternalAuth: authreq.Config{ + URL: authURL, + }, + Path: "/cat", + EnableGlobalAuth: true, + } + + testCases := []struct { + title string + authURL string + globalAuthURL string + enableglobalExternalAuth bool + globalAuthDefaultEnable bool + expected bool + }{ + {"authURL, globalAuthURL and enabled", authURL, globalAuthURL, true, true, false}, + {"authURL, globalAuthURL and disabled", authURL, globalAuthURL, false, true, false}, + {"authURL, empty globalAuthURL and enabled", authURL, "", true, true, false}, + {"authURL, empty globalAuthURL and disabled", authURL, "", false, true, false}, + {"globalAuthURL and enabled", "", globalAuthURL, true, true, true}, + {"globalAuthURL and disabled", "", globalAuthURL, false, true, false}, + {"all empty and enabled", "", "", true, true, false}, + {"all empty and disabled", "", "", false, true, false}, + {"authURL, globalAuthURL and enabled, defaultEnable is false", authURL, globalAuthURL, true, false, false}, + {"authURL, globalAuthURL and disabled, defaultEnable is false", authURL, globalAuthURL, false, false, false}, + {"authURL, empty globalAuthURL and enabled, defaultEnable is false", authURL, "", true, false, false}, + {"authURL, empty globalAuthURL and disabled, defaultEnable is false", authURL, "", false, false, false}, + {"globalAuthURL and enabled, defaultEnable is false", "", globalAuthURL, true, true, true}, + {"globalAuthURL and disabled, defaultEnable is false", "", globalAuthURL, false, false, false}, + {"all empty and enabled, defaultEnable is false", "", "", true, false, false}, + {"all empty and disabled, defaultEnable is false", "", "", false, false, false}, + } + + for _, testCase := range testCases { + loc.ExternalAuth.URL = testCase.authURL + loc.EnableGlobalAuth = testCase.enableglobalExternalAuth + + result := shouldApplyGlobalAuth(loc, testCase.globalAuthURL, cfg) + if result != testCase.expected { + t.Errorf("%v: expected '%v' but returned '%v'", testCase.title, testCase.expected, result) + } + } +} + func TestShouldApplyGlobalAuth(t *testing.T) { authURL := fooAuthHost globalAuthURL := "foo.com/global-auth"