setupCoreAPIWithBillingHook wires an *agent.API to a real test DB with the billing hook pointed at a recording httptest server that responds with hookStatus.
(t *testing.T, secret string, hookStatus int)
| 39 | // billing hook pointed at a recording httptest server that responds with |
| 40 | // hookStatus. |
| 41 | func setupCoreAPIWithBillingHook(t *testing.T, secret string, hookStatus int) (*agent.API, *identity.Store, *recordedHookCall) { |
| 42 | t.Helper() |
| 43 | pool := testutil.TestDB(t) |
| 44 | store := identity.NewStore(pool) |
| 45 | smtpRelay := outbound.NewSMTPRelay(&config.OutboundSMTPConfig{}) |
| 46 | sender := outbound.NewSender(smtpRelay, "test.e2a.dev") |
| 47 | |
| 48 | rec := &recordedHookCall{} |
| 49 | hookSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 50 | body, _ := io.ReadAll(r.Body) |
| 51 | rec.mu.Lock() |
| 52 | rec.body = body |
| 53 | rec.signature = r.Header.Get("X-E2A-Internal-Signature") |
| 54 | rec.called = true |
| 55 | rec.mu.Unlock() |
| 56 | w.WriteHeader(hookStatus) |
| 57 | })) |
| 58 | t.Cleanup(hookSrv.Close) |
| 59 | |
| 60 | api := agent.NewAPI(store, sender, smtpRelay, nil, usage.NewNoopUsageTracker(), |
| 61 | "e2a.dev", "test.e2a.dev", "agents.e2a.dev", "", false) |
| 62 | api.SetInternalAPISecret(secret) |
| 63 | api.SetBillingHookURL(hookSrv.URL) |
| 64 | return api, store, rec |
| 65 | } |
| 66 | |
| 67 | // expectedHMAC re-derives the X-E2A-Internal-Signature for the recorded body. |
| 68 | // Independent computation, so a bug in either side shows up. |
no test coverage detected