- TestLogout_Success: POST /logout with valid cookie -> 303, cookie cleared, session deleted - TestLogout_UnauthRedirectsToLogin: POST /logout without cookie -> 303 from RequireAuth - TestLogout_HXRedirect: HTMX logout -> 200 + HX-Redirect: /login - TestLogout_AfterLogoutSubsequentRequestUnauth: stale cookie blocked after logout - TestProtected_HomeUnauthRedirects: GET / without session -> 303 /login - TestProtected_HomeUnauthHXRedirect: HTMX GET / without session -> 200 + HX-Redirect - TestProtected_HomeAuthRendersUserEmail: authed GET / -> 200 with user email - TestLayout_LogoutFormVisibleWhenAuthed: Layout with user shows logout form - TestLayout_LogoutFormHiddenWhenUnauthed: Layout with nil user hides logout form
44 lines
1.3 KiB
Go
44 lines
1.3 KiB
Go
package templates
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"backend/internal/auth"
|
|
)
|
|
|
|
// TestLayout_LogoutFormVisibleWhenAuthed verifies that the logout form is
|
|
// rendered in the header when Layout receives a non-nil user (D-22).
|
|
func TestLayout_LogoutFormVisibleWhenAuthed(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
user := &auth.User{Email: "a@b.c"}
|
|
err := Layout("Test", user).Render(context.Background(), &buf)
|
|
if err != nil {
|
|
t.Fatalf("Layout.Render: %v", err)
|
|
}
|
|
body := buf.String()
|
|
|
|
if !strings.Contains(body, `action="/logout"`) {
|
|
t.Errorf("Layout body missing action=\"/logout\"; want logout form when authed\nbody: %s", body)
|
|
}
|
|
if !strings.Contains(body, `method="POST"`) {
|
|
t.Errorf("Layout body missing method=\"POST\"; logout must be a POST form (D-22)")
|
|
}
|
|
}
|
|
|
|
// TestLayout_LogoutFormHiddenWhenUnauthed verifies that no logout form is
|
|
// rendered when Layout receives a nil user (unauthenticated request).
|
|
func TestLayout_LogoutFormHiddenWhenUnauthed(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
err := Layout("Test", nil).Render(context.Background(), &buf)
|
|
if err != nil {
|
|
t.Fatalf("Layout.Render: %v", err)
|
|
}
|
|
body := buf.String()
|
|
|
|
if strings.Contains(body, `action="/logout"`) {
|
|
t.Errorf("Layout body must NOT contain action=\"/logout\" when user is nil (unauthenticated)")
|
|
}
|
|
}
|