package view
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func renderSignedOut(t *testing.T, page string, data any) string {
t.Helper()
engine, err := New("9.9.9-test")
if err != nil {
t.Fatalf("New: %v", err)
}
rec := httptest.NewRecorder()
engine.Render(rec, http.StatusOK, page, data)
if rec.Code != http.StatusOK {
t.Fatalf("render %s: status %d: %s", page, rec.Code, rec.Body.String())
}
return rec.Body.String()
}
func TestLoginFormPostsToLoginWithItsFields(t *testing.T) {
out := renderSignedOut(t, "login", NewLogin("mail.example.org", ""))
for _, want := range []string{
`action="/login"`, `method="post"`,
`name="username"`, `autocomplete="username"`, `autofocus`,
`name="password"`, `autocomplete="current-password"`,
`mail.example.org`,
} {
if !strings.Contains(out, want) {
t.Errorf("login page is missing %q", want)
}
}
if strings.Contains(out, "notification") {
t.Error("an untouched login form shows an error")
}
}
func TestLoginShowsAnErrorEscaped(t *testing.T) {
out := renderSignedOut(t, "login", NewLogin("", `bad `))
if !strings.Contains(out, "notification is-danger is-light") {
t.Error("the refusal is not shown as the error flash")
}
if strings.Contains(out, "