levee / internal /api /api_test.go
runitx's picture
Claude Opus 5.5
Add Levee: open-source flood zone determination and monitoring
b5754ad unverified
Raw History Blame Contribute Delete
4.49 kB
package api
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"levee/internal/arcgis"
"levee/internal/determine"
"levee/internal/fema"
"levee/internal/geocode"
"levee/internal/monitor"
"levee/internal/store"
)
type fake struct{}
func (fake) Geocode(context.Context, string) (*geocode.Match, error) {
return &geocode.Match{Input: "1 Main St", Matched: "1 MAIN ST", Lat: 29.7, Lon: -95.4, Exact: true, Candidates: 1}, nil
}
func (fake) ZonesAt(context.Context, arcgis.Point) ([]fema.Zone, error) {
return []fema.Zone{{DFIRMID: "48201C", Zone: "AE", SFHA: true}}, nil
}
func (fake) SFHANear(context.Context, arcgis.Point, float64) ([]fema.Zone, error) { return nil, nil }
func (fake) PanelsAt(context.Context, arcgis.Point) ([]fema.Panel, error) {
return []fema.Panel{{FirmPan: "48201C0860L", DFIRMID: "48201C"}}, nil
}
func (fake) JurisdictionsAt(context.Context, arcgis.Point) ([]fema.Jurisdiction, error) {
return []fema.Jurisdiction{{CID: "480296", DFIRMID: "48201C"}}, nil
}
func (fake) LOMRsAt(context.Context, arcgis.Point) ([]fema.LOMR, error) { return nil, nil }
func (fake) LOMAsNear(context.Context, arcgis.Point, float64) ([]fema.LOMA, error) { return nil, nil }
func (fake) HasDigitalData(context.Context, arcgis.Point) (bool, error) { return true, nil }
func (fake) PendingAt(context.Context, arcgis.Point) (*fema.Pending, error) { return nil, nil }
func (fake) Community(context.Context, string) (*fema.Community, error) {
return &fema.Community{CID: "480296", Participating: true, Program: "Regular"}, nil
}
func server(t *testing.T) http.Handler {
st, err := store.Open(filepath.Join(t.TempDir(), "levee.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { st.Close() })
eng := &determine.Engine{Geocoder: fake{}, Map: fake{}, Communities: fake{}}
s := &Server{Store: st, Engine: eng, Monitor: &monitor.Monitor{Store: st, Engine: eng}, Token: "secret"}
return s.Handler()
}
func do(h http.Handler, method, path, token, body string) *httptest.ResponseRecorder {
req := httptest.NewRequest(method, path, strings.NewReader(body))
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
return rec
}
func TestPublicDetermine(t *testing.T) {
h := server(t)
rec := do(h, "GET", "/v1/determine?address=1+Main+St", "", "")
if rec.Code != 200 {
t.Fatalf("code %d: %s", rec.Code, rec.Body)
}
var body struct {
Determination determine.Determination `json:"determination"`
Disclaimer string `json:"disclaimer"`
}
json.Unmarshal(rec.Body.Bytes(), &body)
if !body.Determination.InSFHA || body.Disclaimer == "" {
t.Fatalf("body = %s", rec.Body)
}
if rec := do(h, "GET", "/v1/determine", "", ""); rec.Code != 400 {
t.Fatalf("missing address: code %d", rec.Code)
}
if rec := do(h, "GET", "/v1/determine.pdf?lat=29.7&lon=-95.4", "", ""); rec.Code != 200 || rec.Header().Get("Content-Type") != "application/pdf" {
t.Fatalf("pdf: code %d type %s", rec.Code, rec.Header().Get("Content-Type"))
}
}
func TestPortfolioRequiresToken(t *testing.T) {
h := server(t)
for _, p := range []string{"/v1/loans", "/v1/events", "/v1/summary", "/v1/audit/verify"} {
if rec := do(h, "GET", p, "", ""); rec.Code != 401 {
t.Errorf("%s without token: %d", p, rec.Code)
}
if rec := do(h, "GET", p, "wrong", ""); rec.Code != 401 {
t.Errorf("%s with wrong token: %d", p, rec.Code)
}
}
}
func TestLoanLifecycle(t *testing.T) {
h := server(t)
rec := do(h, "POST", "/v1/loans", "secret", `{"loan_number":"L-1","address":"1 Main St","lender_name":"Bank"}`)
if rec.Code != 200 {
t.Fatalf("create: %d %s", rec.Code, rec.Body)
}
if rec := do(h, "GET", "/v1/loans/L-1", "secret", ""); rec.Code != 200 || !strings.Contains(rec.Body.String(), `"zone": "AE"`) {
t.Fatalf("get: %d %s", rec.Code, rec.Body)
}
if rec := do(h, "GET", "/v1/loans/L-1/sfhdf.pdf", "secret", ""); rec.Code != 200 || !strings.HasPrefix(rec.Body.String(), "%PDF") {
t.Fatalf("pdf: %d", rec.Code)
}
if rec := do(h, "GET", "/v1/audit/verify", "secret", ""); !strings.Contains(rec.Body.String(), `"ok": true`) {
t.Fatalf("verify: %s", rec.Body)
}
if rec := do(h, "DELETE", "/v1/loans/L-1", "secret", ""); rec.Code != 200 {
t.Fatalf("delete: %d", rec.Code)
}
if rec := do(h, "GET", "/v1/loans/nope", "secret", ""); rec.Code != 404 {
t.Fatalf("missing loan: %d", rec.Code)
}
}