Files
davidandClaude Opus 5.5 82d569da9c Phase 2 part 5: find people and verify emails with Hunter.io
Review gets Find people (Domain Search by the departments, seniority and
count set in Settings → Lead sources), Find their email (Email Finder for
a named person at a company), and Verify (one, or every unchecked address
in New). Every call is kept in enrich_runs (schema 9) with its result and
credits, and shown on the card. Nothing runs on a schedule. Sources are
shown by name (Hunter.io, origami.chat, explee.com).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 05:53:52 -07:00

82 lines
3.5 KiB
Go

package web
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"supermarket/internal/enrich"
"supermarket/internal/prospects"
"supermarket/internal/secrets"
)
func TestHunterEnrichment(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/account":
w.Write([]byte(`{"data":{"plan_name":"Free","requests":{"credits":{"used":1,"available":50,"remaining":49}}}}`))
case "/domain-search":
w.Header().Set("X-Credits-Used", "1")
w.Write([]byte(`{"data":{"emails":[{"value":"pat@acme.example","type":"personal","confidence":94,"first_name":"Pat","last_name":"Lee","position":"CTO"}]}}`))
case "/email-verifier":
w.Header().Set("X-Credits-Used", "0.5")
w.Write([]byte(`{"data":{"status":"valid"}}`))
}
}))
defer srv.Close()
hunterBase = srv.URL
defer func() { hunterBase = "" }()
s, h := newTestServer(t, nil)
postForm(h, "/prospects/new", url.Values{"company_name": {"Acme"}, "website": {"acme.example"}})
// Without a key there's no button, and the action explains.
if body := do(h, http.MethodGet, "/prospects", nil, nil).Body.String(); strings.Contains(body, "Find people at") {
t.Error("Hunter button without a key")
}
if rec := postForm(h, "/prospects/company/1/people", url.Values{"tab": {"new"}}); !strings.Contains(rec.Body.String(), "save a Hunter.io API key first") {
t.Errorf("no key: %d", rec.Code)
}
// Settings: the key, whom to look for, and the test.
rec := postForm(h, "/settings/sources", url.Values{secrets.HunterAPIKey: {"k"},
"hunter_department": {"it", "finance"}, "hunter_seniority": {"executive"}, "hunter_per_company": {"10"}})
if rec.Code != http.StatusSeeOther {
t.Fatalf("save: %d %s", rec.Code, rec.Body)
}
if set := enrich.LoadSettings(s.DB); strings.Join(set.Departments, ",") != "it,finance" || set.PerCompany != 10 {
t.Errorf("settings: %+v", set)
}
if body := do(h, http.MethodGet, "/settings/sources", nil, nil).Body.String(); !strings.Contains(body, `value="finance" checked`) {
t.Error("the settings page doesn't show the saved departments")
}
if body := postForm(h, "/settings/sources/test/hunter", nil).Body.String(); !strings.Contains(body, "The key works (Free plan): 49 of 50 credits left") {
t.Errorf("test: %s", body)
}
// Find people, then verify the address.
if body := do(h, http.MethodGet, "/prospects", nil, nil).Body.String(); !strings.Contains(body, "Find people at acme.example") {
t.Error("no Hunter button with a key")
}
if rec := postForm(h, "/prospects/company/1/people", url.Values{"tab": {"new"}}); rec.Code != http.StatusSeeOther {
t.Fatalf("people: %d %s", rec.Code, rec.Body)
}
body := do(h, http.MethodGet, "/prospects", nil, nil).Body.String()
for _, want := range []string{"Pat Lee", "Found by Hunter.io", "1 person found, 1 new one.", "(1 credit)", "1 address hasn't been verified", "Verify the address"} {
if !strings.Contains(body, want) {
t.Errorf("review lacks %q", want)
}
}
if rec := postForm(h, "/prospects/verify", url.Values{}); rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/prospects?status=new&msg=verified" {
t.Fatalf("verify: %d %s", rec.Code, rec.Header().Get("Location"))
}
if p, _ := prospects.GetContact(s.DB, 1); p.EmailStatus != prospects.EmailValid {
t.Errorf("status: %+v", p)
}
if body := do(h, http.MethodGet, "/prospects", nil, nil).Body.String(); !strings.Contains(body, "verified</span>") || !strings.Contains(body, "receives mail") {
t.Error("the verification isn't shown")
}
}