(t *testing.T, opts ...func(*Deps))
| 38 | } |
| 39 | |
| 40 | func attTestServer(t *testing.T, opts ...func(*Deps)) *httptest.Server { |
| 41 | t.Helper() |
| 42 | u := &identity.User{ID: "u_1", Email: "owner@acme.com"} |
| 43 | deps := Deps{ |
| 44 | PrincipalAuthenticator: func(r *http.Request) (*identity.Principal, error) { |
| 45 | switch r.Header.Get("Authorization") { |
| 46 | case "Bearer acct": |
| 47 | return &identity.Principal{User: u, Scope: identity.ScopeAccount}, nil |
| 48 | case "Bearer agtOther": |
| 49 | return &identity.Principal{User: u, Scope: identity.ScopeAgent, AgentID: "other@acme.com"}, nil |
| 50 | } |
| 51 | return nil, errors.New("unauthorized") |
| 52 | }, |
| 53 | AuthChallenge: func(r *http.Request) string { return `Bearer realm="e2a"` }, |
| 54 | GetAgent: func(ctx context.Context, address string) (*identity.AgentIdentity, error) { |
| 55 | switch address { |
| 56 | case "support@acme.com": |
| 57 | a := sampleAgent() |
| 58 | return &a, nil |
| 59 | case "other@acme.com": |
| 60 | // A second agent of the same owner — used to prove a download token |
| 61 | // minted for support's message can't be replayed against other's path. |
| 62 | a := sampleAgent() |
| 63 | a.ID = "other@acme.com" |
| 64 | return &a, nil |
| 65 | } |
| 66 | return nil, errors.New("not found") |
| 67 | }, |
| 68 | GetMessage: func(ctx context.Context, id, agentID string) (*identity.Message, error) { |
| 69 | if agentID != "support@acme.com" { |
| 70 | return nil, errors.New("not found") |
| 71 | } |
| 72 | switch id { |
| 73 | case "msg_att": |
| 74 | return &identity.Message{ID: id, Direction: "inbound", Recipient: "support@acme.com", RawMessage: attMultipart()}, nil |
| 75 | case "msg_big": |
| 76 | return &identity.Message{ID: id, Direction: "inbound", Recipient: "support@acme.com", RawMessage: attBig()}, nil |
| 77 | } |
| 78 | return nil, errors.New("not found") |
| 79 | }, |
| 80 | AttachmentStore: NewNativeAttachmentStore("test-secret-test-secret", "http://att.test"), |
| 81 | Legacy: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusTeapot) }), |
| 82 | } |
| 83 | for _, o := range opts { |
| 84 | o(&deps) |
| 85 | } |
| 86 | srv := httptest.NewServer(New(deps).Router) |
| 87 | t.Cleanup(srv.Close) |
| 88 | return srv |
| 89 | } |
| 90 | |
| 91 | func getJSONAtt(t *testing.T, url, bearer string) (int, map[string]any) { |
| 92 | t.Helper() |
no test coverage detected