75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
|
|
package app
|
||
|
|
|
||
|
|
import (
|
||
|
|
"io"
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"net/url"
|
||
|
|
"strconv"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestPublicRoutes(t *testing.T) {
|
||
|
|
srv := newTestServer(t)
|
||
|
|
handler := srv.Routes()
|
||
|
|
|
||
|
|
for _, path := range []string{"/", "/projects", "/about", "/projects/courtyard-house"} {
|
||
|
|
req := httptest.NewRequest(http.MethodGet, path, nil)
|
||
|
|
rec := httptest.NewRecorder()
|
||
|
|
handler.ServeHTTP(rec, req)
|
||
|
|
if rec.Code != http.StatusOK {
|
||
|
|
t.Fatalf("%s returned %d", path, rec.Code)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestContactSubmissionPersists(t *testing.T) {
|
||
|
|
srv := newTestServer(t)
|
||
|
|
form := url.Values{"name": {"Jane"}, "email": {"jane@example.com"}, "message": {"New project"}}
|
||
|
|
req := httptest.NewRequest(http.MethodPost, "/contact", strings.NewReader(form.Encode()))
|
||
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||
|
|
rec := httptest.NewRecorder()
|
||
|
|
|
||
|
|
srv.Routes().ServeHTTP(rec, req)
|
||
|
|
|
||
|
|
if rec.Code != http.StatusOK {
|
||
|
|
t.Fatalf("expected ok, got %d", rec.Code)
|
||
|
|
}
|
||
|
|
body, _ := io.ReadAll(rec.Result().Body)
|
||
|
|
if !strings.Contains(string(body), "saved") {
|
||
|
|
t.Fatalf("expected success message, got %s", body)
|
||
|
|
}
|
||
|
|
requests, err := srv.store.ContactRequests(req.Context())
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if len(requests) != 1 || requests[0].Email != "jane@example.com" {
|
||
|
|
t.Fatalf("unexpected contact requests: %+v", requests)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestProjectImageOverlay(t *testing.T) {
|
||
|
|
srv := newTestServer(t)
|
||
|
|
project, err := srv.store.ProjectBySlug(t.Context(), "courtyard-house")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if len(project.Images) == 0 {
|
||
|
|
t.Fatal("expected seeded project images")
|
||
|
|
}
|
||
|
|
path := "/projects/" + project.Slug + "/images/" + strconv.FormatInt(project.Images[0].ID, 10) + "/overlay"
|
||
|
|
req := httptest.NewRequest(http.MethodGet, path, nil)
|
||
|
|
rec := httptest.NewRecorder()
|
||
|
|
|
||
|
|
srv.Routes().ServeHTTP(rec, req)
|
||
|
|
|
||
|
|
if rec.Code != http.StatusOK {
|
||
|
|
t.Fatalf("expected ok, got %d", rec.Code)
|
||
|
|
}
|
||
|
|
body, _ := io.ReadAll(rec.Result().Body)
|
||
|
|
if !strings.Contains(string(body), "data-overlay") || !strings.Contains(string(body), project.Images[0].Path) {
|
||
|
|
t.Fatalf("overlay fragment missing expected content: %s", body)
|
||
|
|
}
|
||
|
|
}
|