The new server test logs in and out on pixa built as pixad builds it, starts pixa again on the same state directory, and checks that a cookie copied before the logout is refused, which fails if pixad keeps logouts in memory only. The new handler test closes the database so the logout cannot be saved, and checks for 500, no session cookie set, and the session still valid. Model: opus-5-5
113 lines
2.7 KiB
Go
113 lines
2.7 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"go.uber.org/fx"
|
|
"go.uber.org/fx/fxtest"
|
|
|
|
"sneak.berlin/go/pixa/internal/config"
|
|
"sneak.berlin/go/pixa/internal/database"
|
|
"sneak.berlin/go/pixa/internal/globals"
|
|
"sneak.berlin/go/pixa/internal/handlers"
|
|
"sneak.berlin/go/pixa/internal/healthcheck"
|
|
"sneak.berlin/go/pixa/internal/logger"
|
|
"sneak.berlin/go/pixa/internal/middleware"
|
|
"sneak.berlin/go/pixa/internal/session"
|
|
)
|
|
|
|
// startPixa starts the components pixad's fx app builds, from cfg, and
|
|
// returns the server with its routes, as newTestServer does.
|
|
func startPixa(t *testing.T, cfg *config.Config) *Server {
|
|
t.Helper()
|
|
|
|
var (
|
|
h *handlers.Handlers
|
|
mw *middleware.Middleware
|
|
)
|
|
|
|
app := fxtest.New(t,
|
|
fx.Supply(cfg),
|
|
fx.Provide(
|
|
globals.New,
|
|
logger.New,
|
|
database.New,
|
|
healthcheck.New,
|
|
handlers.New,
|
|
middleware.New,
|
|
),
|
|
fx.Populate(&h, &mw),
|
|
)
|
|
app.RequireStart()
|
|
t.Cleanup(app.RequireStop)
|
|
|
|
s := &Server{config: cfg, mw: mw, h: h}
|
|
s.SetupRoutes()
|
|
|
|
return s
|
|
}
|
|
|
|
// opensGeneratorPage reports whether GET / with cookie shows s's generator
|
|
// page, which only a valid login session opens.
|
|
func opensGeneratorPage(t *testing.T, s *Server, cookie *http.Cookie) bool {
|
|
t.Helper()
|
|
|
|
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/", nil)
|
|
req.AddCookie(cookie)
|
|
|
|
rec := httptest.NewRecorder()
|
|
s.ServeHTTP(rec, req)
|
|
|
|
return rec.Code == http.StatusOK &&
|
|
strings.Contains(rec.Body.String(), `action="/generate"`)
|
|
}
|
|
|
|
// TestLogoutOutlastsRestart verifies that a copy of the session cookie taken
|
|
// before /logout is still refused by a pixa started again on the same state
|
|
// directory, as pixad is after a restart.
|
|
func TestLogoutOutlastsRestart(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
s := newTestServer(t)
|
|
|
|
login := postLogin(t, s, firstClient, "", testSigningKey)
|
|
|
|
var copied *http.Cookie
|
|
|
|
for _, c := range login.Result().Cookies() {
|
|
if c.Name == session.CookieName {
|
|
copied = c
|
|
}
|
|
}
|
|
|
|
if copied == nil || !opensGeneratorPage(t, s, copied) {
|
|
t.Fatal("logging in gave no session cookie that opens the generator page")
|
|
}
|
|
|
|
req := httptest.NewRequestWithContext(
|
|
t.Context(), http.MethodGet, "/logout", nil)
|
|
req.AddCookie(copied)
|
|
|
|
logout := httptest.NewRecorder()
|
|
s.ServeHTTP(logout, req)
|
|
|
|
if logout.Code != http.StatusSeeOther {
|
|
t.Fatalf("GET /logout status = %d, want %d",
|
|
logout.Code, http.StatusSeeOther)
|
|
}
|
|
|
|
restarted := startPixa(t, s.config)
|
|
|
|
opens := opensGeneratorPage(t, restarted, copied)
|
|
t.Logf("after the restart, the copied cookie opens the generator page: %v",
|
|
opens)
|
|
|
|
if opens {
|
|
t.Error("after a restart, a cookie copied before /logout " +
|
|
"still opens the generator page")
|
|
}
|
|
}
|