File size: 4,485 Bytes
b5754ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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)
	}
}