(t *testing.T)
| 11 | ) |
| 12 | |
| 13 | func TestBuildSessionCookies(t *testing.T) { |
| 14 | tests := []struct { |
| 15 | name string |
| 16 | hostname string |
| 17 | secure bool |
| 18 | sameSite http.SameSite |
| 19 | wantDomain string // expected `.example.com`-style domain on the domain-scoped cookie |
| 20 | }{ |
| 21 | {"production https", "https://auth.example.com", true, http.SameSiteNoneMode, ".example.com"}, |
| 22 | {"localhost dev", "http://localhost:8080", false, http.SameSiteLaxMode, "localhost"}, |
| 23 | {"subdomain", "https://auth.svc.example.com", true, http.SameSiteStrictMode, ".example.com"}, |
| 24 | } |
| 25 | for _, tt := range tests { |
| 26 | t.Run(tt.name, func(t *testing.T) { |
| 27 | cookies := BuildSessionCookies(tt.hostname, "session-id", tt.secure, tt.sameSite) |
| 28 | require.Len(t, cookies, 2, "BuildSessionCookies must return exactly the host-scoped and domain-scoped pair") |
| 29 | |
| 30 | for _, c := range cookies { |
| 31 | assert.Equal(t, "session-id", c.Value) |
| 32 | assert.Equal(t, tt.secure, c.Secure) |
| 33 | assert.True(t, c.HttpOnly, "session cookies must be HttpOnly") |
| 34 | assert.Equal(t, "/", c.Path) |
| 35 | assert.Equal(t, tt.sameSite, c.SameSite) |
| 36 | assert.Equal(t, 24*60*60, c.MaxAge, "session cookie MaxAge must be 1 day") |
| 37 | } |
| 38 | |
| 39 | // Sanity-check cookie names. |
| 40 | assert.Equal(t, constants.AppCookieName+"_session", cookies[0].Name) |
| 41 | assert.Equal(t, constants.AppCookieName+"_session_domain", cookies[1].Name) |
| 42 | // Domain-scoped cookie picks up the apex. |
| 43 | assert.Equal(t, tt.wantDomain, cookies[1].Domain) |
| 44 | }) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestBuildMfaSessionCookies(t *testing.T) { |
| 49 | cookies := BuildMfaSessionCookies("https://auth.example.com", "mfa-id", true) |
nothing calls this directly
no test coverage detected