From 0a9068e1dedaf4c42eddebfd3975635cc3d85b57 Mon Sep 17 00:00:00 2001 From: rishabhpoddar Date: Fri, 24 Nov 2023 13:58:05 +0530 Subject: [PATCH] fixes a few tests --- recipe/session/config_test.go | 45 ++++++++++++++++++++++++++--------- recipe/session/utils.go | 8 +++++++ 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/recipe/session/config_test.go b/recipe/session/config_test.go index b7440f83..8e68da68 100644 --- a/recipe/session/config_test.go +++ b/recipe/session/config_test.go @@ -144,7 +144,7 @@ func TestSuperTokensInitWithoutWebsiteDomain(t *testing.T) { defer AfterEach() err := supertokens.Init(configValue) if err != nil { - assert.Equal(t, err.Error(), "Please provide your websiteDomain inside the appInfo object when calling supertokens.init") + assert.Equal(t, err.Error(), "Please provide either Origin, GetOrigin or WebsiteDomain inside the appInfo object when calling supertokens.init") } else { t.Fail() } @@ -437,9 +437,16 @@ func TestSuperTokensInitWithNoneLaxFalseSessionConfigResults(t *testing.T) { if err != nil { t.Error(err.Error()) } - assert.Equal(t, sessionSingletonInstance.Config.AntiCsrfFunctionOrString.StrValue, "NONE") + antiCsrf, err := sessionSingletonInstance.Config.AntiCsrfFunctionOrString.FunctionValue(nil, nil) + if err != nil { + t.Error(err.Error()) + } + assert.Equal(t, antiCsrf, "NONE") + assert.True(t, sessionSingletonInstance.Config.AntiCsrfFunctionOrString.StrValue == "") cookieSameSite, err := sessionSingletonInstance.Config.GetCookieSameSite(nil, nil) - assert.True(t, err != nil) + if err != nil { + t.Error(err.Error()) + } assert.Equal(t, cookieSameSite, "lax") assert.Equal(t, sessionSingletonInstance.Config.CookieSecure, false) } @@ -479,7 +486,9 @@ func TestSuperTokensInitWithCustomHeaderLaxTrueSessionConfigResults(t *testing.T } assert.Equal(t, sessionSingletonInstance.Config.AntiCsrfFunctionOrString.StrValue, "VIA_CUSTOM_HEADER") cookieSameSite, err := sessionSingletonInstance.Config.GetCookieSameSite(nil, nil) - assert.True(t, err != nil) + if err != nil { + t.Error(err.Error()) + } assert.Equal(t, cookieSameSite, "lax") assert.Equal(t, sessionSingletonInstance.Config.CookieSecure, true) } @@ -520,7 +529,9 @@ func TestSuperTokensInitWithCustomHeaderLaxFalseSessionConfigResults(t *testing. } assert.Equal(t, sessionSingletonInstance.Config.AntiCsrfFunctionOrString.StrValue, "VIA_CUSTOM_HEADER") cookieSameSite, err := sessionSingletonInstance.Config.GetCookieSameSite(nil, nil) - assert.True(t, err != nil) + if err != nil { + t.Error(err.Error()) + } assert.Equal(t, cookieSameSite, "lax") assert.Equal(t, sessionSingletonInstance.Config.CookieSecure, false) } @@ -556,7 +567,9 @@ func TestSuperTokensInitWithCustomHeaderNoneTrueSessionConfigResultsWithNormalWe } assert.Equal(t, sessionSingletonInstance.Config.AntiCsrfFunctionOrString.StrValue, "VIA_CUSTOM_HEADER") cookieSameSite, err := sessionSingletonInstance.Config.GetCookieSameSite(nil, nil) - assert.True(t, err != nil) + if err != nil { + t.Error(err.Error()) + } assert.Equal(t, cookieSameSite, "none") assert.Equal(t, sessionSingletonInstance.Config.CookieSecure, true) } @@ -592,7 +605,9 @@ func TestSuperTokensInitWithCustomHeaderNoneTrueSessionConfigResultsWithLocalWeb } assert.Equal(t, sessionSingletonInstance.Config.AntiCsrfFunctionOrString.StrValue, "VIA_CUSTOM_HEADER") cookieSameSite, err := sessionSingletonInstance.Config.GetCookieSameSite(nil, nil) - assert.True(t, err != nil) + if err != nil { + t.Error(err.Error()) + } assert.Equal(t, cookieSameSite, "none") assert.Equal(t, sessionSingletonInstance.Config.CookieSecure, true) } @@ -753,7 +768,9 @@ func TestSuperTokensForTheDefaultCookieValues(t *testing.T) { } assert.Equal(t, singletonSessionRecipeInstance.Config.CookieSecure, true) cookieSameSite, err := singletonSessionRecipeInstance.Config.GetCookieSameSite(nil, nil) - assert.True(t, err != nil) + if err != nil { + t.Error(err.Error()) + } assert.Equal(t, cookieSameSite, "none") } @@ -885,7 +902,9 @@ func TestSuperTokensDefaultCookieConfig(t *testing.T) { } assert.Nil(t, singletonSessionRecipeInstance.Config.CookieDomain) cookieSameSite, err := singletonSessionRecipeInstance.Config.GetCookieSameSite(nil, nil) - assert.True(t, err != nil) + if err != nil { + t.Error(err.Error()) + } assert.Equal(t, cookieSameSite, "lax") assert.Equal(t, singletonSessionRecipeInstance.Config.CookieSecure, true) assert.Equal(t, singletonSessionRecipeInstance.Config.RefreshTokenPath.GetAsStringDangerous(), "/auth/session/refresh") @@ -1271,7 +1290,9 @@ func TestCookieSameSiteWithEC2PublicURL(t *testing.T) { assert.True(t, recipe.Config.CookieDomain == nil) cookieSameSiteValue, err := recipe.Config.GetCookieSameSite(nil, nil) - assert.True(t, err != nil) + if err != nil { + t.Error(err.Error()) + } assert.Equal(t, cookieSameSiteValue, "none") assert.True(t, recipe.Config.CookieSecure) @@ -1310,7 +1331,9 @@ func TestCookieSameSiteWithEC2PublicURL(t *testing.T) { assert.True(t, recipe.Config.CookieDomain == nil) cookieSameSiteValue, err = recipe.Config.GetCookieSameSite(nil, nil) - assert.True(t, err != nil) + if err != nil { + t.Error(err.Error()) + } assert.Equal(t, cookieSameSiteValue, "lax") assert.False(t, recipe.Config.CookieSecure) } diff --git a/recipe/session/utils.go b/recipe/session/utils.go index 017c6df2..7dc287be 100644 --- a/recipe/session/utils.go +++ b/recipe/session/utils.go @@ -57,6 +57,14 @@ func ValidateAndNormaliseUserInput(appInfo supertokens.NormalisedAppinfo, config } } + if config != nil && config.CookieSameSite != nil { + // we have this block just to check if the user input is correct + _, err = normaliseSameSiteOrThrowError(*config.CookieSameSite) + if err != nil { + return sessmodels.TypeNormalisedInput{}, err + } + } + cookieSameSite := func(request *http.Request, userContext supertokens.UserContext) (string, error) { if config != nil && config.CookieSameSite != nil { return normaliseSameSiteOrThrowError(*config.CookieSameSite)