From bd08dcda880f8c10df3139aa807fbb70c7a9466f Mon Sep 17 00:00:00 2001 From: Prafulla Mahindrakar Date: Tue, 7 Jun 2022 18:14:47 +0530 Subject: [PATCH] Added more coverage Signed-off-by: Prafulla Mahindrakar --- auth/cookie_manager_test.go | 47 +++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/auth/cookie_manager_test.go b/auth/cookie_manager_test.go index b7f69ca64..9d1c51353 100644 --- a/auth/cookie_manager_test.go +++ b/auth/cookie_manager_test.go @@ -121,3 +121,50 @@ func TestCookieManager_DeleteCookies(t *testing.T) { assert.True(t, time.Now().After(cookies[0].Expires)) assert.True(t, time.Now().After(cookies[1].Expires)) } + +func TestGetHTTPSameSitePolicy(t *testing.T) { + ctx := context.Background() + + // These were generated for unit testing only. + hashKeyEncoded := "wG4pE1ccdw/pHZ2ml8wrD5VJkOtLPmBpWbKHmezWXktGaFbRoAhXidWs8OpbA3y7N8vyZhz1B1E37+tShWC7gA" //nolint:goconst + blockKeyEncoded := "afyABVgGOvWJFxVyOvCWCupoTn6BkNl4SOHmahho16Q" //nolint:goconst + cookieSetting := config.CookieSettings{ + SameSitePolicy: config.SameSiteDefaultMode, + DomainMatchPolicy: config.DomainMatchSubdomains, + } + + manager, err := NewCookieManager(ctx, hashKeyEncoded, blockKeyEncoded, cookieSetting) + assert.NoError(t, err) + assert.Equal(t, http.SameSiteDefaultMode, manager.getHTTPSameSitePolicy()) + + manager.sameSitePolicy = config.SameSiteLaxMode + assert.Equal(t, http.SameSiteLaxMode, manager.getHTTPSameSitePolicy()) + + manager.sameSitePolicy = config.SameSiteStrictMode + assert.Equal(t, http.SameSiteStrictMode, manager.getHTTPSameSitePolicy()) + + manager.sameSitePolicy = config.SameSiteNoneMode + assert.Equal(t, http.SameSiteNoneMode, manager.getHTTPSameSitePolicy()) +} + +func TestGetCookieDomain(t *testing.T) { + ctx := context.Background() + + // These were generated for unit testing only. + hashKeyEncoded := "wG4pE1ccdw/pHZ2ml8wrD5VJkOtLPmBpWbKHmezWXktGaFbRoAhXidWs8OpbA3y7N8vyZhz1B1E37+tShWC7gA" //nolint:goconst + blockKeyEncoded := "afyABVgGOvWJFxVyOvCWCupoTn6BkNl4SOHmahho16Q" //nolint:goconst + cookieSetting := config.CookieSettings{ + SameSitePolicy: config.SameSiteDefaultMode, + DomainMatchPolicy: config.DomainMatchSubdomains, + } + + req, err := http.NewRequest("GET", "http://localhost/api/v1/projects", nil) + assert.NoError(t, err) + + manager, err := NewCookieManager(ctx, hashKeyEncoded, blockKeyEncoded, cookieSetting) + assert.NoError(t, err) + assert.Equal(t, ".localhost", manager.getCookieDomain(req)) + + manager.domainMatchPolicy = config.DomainMatchExact + assert.Equal(t, "", manager.getCookieDomain(req)) +}