| 143 | } |
| 144 | |
| 145 | func TestRegRateLimited(t *testing.T) { |
| 146 | srv := httptest.NewServer(New(Deps{ |
| 147 | Authenticator: func(r *http.Request) (*identity.User, error) { |
| 148 | return &identity.User{ID: "u_1"}, nil |
| 149 | }, |
| 150 | CreateAgent: func(ctx context.Context, email, domain, name, webhookURL, agentMode, userID string) (*identity.AgentIdentity, error) { |
| 151 | t.Error("CreateAgent must NOT be reached when reg-limited") |
| 152 | return nil, nil |
| 153 | }, |
| 154 | RegLimit: func(key string) (bool, time.Duration, int, int, int) { |
| 155 | return false, 30 * time.Second, 200, 0, 3600 |
| 156 | }, |
| 157 | })) |
| 158 | t.Cleanup(srv.Close) |
| 159 | |
| 160 | code, body := postJSON(t, srv.URL+"/v1/agents", "good", map[string]any{ |
| 161 | "slug": "bot", |
| 162 | }) |
| 163 | if code != 429 || errCode(body) != "rate_limited" { |
| 164 | t.Fatalf("want 429 rate_limited, got %d %v", code, body) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // serverWithSendLimit builds a minimal server whose SendLimit always blocks, |
| 169 | // to assert the 429 path on the outbound chokepoint. |