|
1 | 1 | package postman
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "reflect"
|
5 | 6 | "sort"
|
6 | 7 | "strings"
|
7 | 8 | "testing"
|
| 9 | + "time" |
8 | 10 |
|
9 | 11 | "github.com/trufflesecurity/trufflehog/v3/pkg/context"
|
| 12 | + "gopkg.in/h2non/gock.v1" |
10 | 13 |
|
11 | 14 | "github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
|
12 | 15 | "github.com/trufflesecurity/trufflehog/v3/pkg/sources"
|
@@ -238,3 +241,56 @@ func TestSource_ScanVariableData(t *testing.T) {
|
238 | 241 | })
|
239 | 242 | }
|
240 | 243 | }
|
| 244 | + |
| 245 | +func TestSource_ScanEnumerateRateLimit(t *testing.T) { |
| 246 | + defer gock.Off() |
| 247 | + // Mock the API response for workspaces |
| 248 | + numWorkspaces := 3 |
| 249 | + workspaceBodyString := `{"workspaces":[` |
| 250 | + for i := 0; i < numWorkspaces; i++ { |
| 251 | + workspaceBodyString += fmt.Sprintf(`{"id": "%d", "name": "workspace-%d", "type": "personal", "visibility": "personal", "createdBy": "1234"}`, i, i) |
| 252 | + if i == numWorkspaces-1 { |
| 253 | + workspaceBodyString += `]}` |
| 254 | + } else { |
| 255 | + workspaceBodyString += `,` |
| 256 | + } |
| 257 | + } |
| 258 | + gock.New("https://api.getpostman.com"). |
| 259 | + Get("/workspaces"). |
| 260 | + Reply(200). |
| 261 | + BodyString(workspaceBodyString) |
| 262 | + // Mock the API response for each individual workspace |
| 263 | + for i := 0; i < numWorkspaces; i++ { |
| 264 | + gock.New("https://api.getpostman.com"). |
| 265 | + Get(fmt.Sprintf("/workspaces/%d", i)). |
| 266 | + Reply(200). |
| 267 | + BodyString(fmt.Sprintf(`{"workspace":{"id":"%d","name":"workspace-%d","type":"personal","description":"Test workspace number %d", |
| 268 | + "visibility":"personal","createdBy":"1234","updatedBy":"1234","createdAt":"2024-12-12T23:32:27.000Z","updatedAt":"2024-12-12T23:33:01.000Z", |
| 269 | + "collections":[{"id":"abc%d","name":"test-collection-1","uid":"1234-abc%d"},{"id":"def%d","name":"test-collection-2","uid":"1234-def%d"}], |
| 270 | + "environments":[{"id":"ghi%d","name":"test-environment-1","uid":"1234-ghi%d"},{"id":"jkl%d","name":"test-environment-2","uid":"1234-jkl%d"}]}}`, i, i, i, i, i, i, i, i, i, i, i)) |
| 271 | + } |
| 272 | + |
| 273 | + ctx := context.Background() |
| 274 | + s, conn := createTestSource(&sourcespb.Postman{ |
| 275 | + Credential: &sourcespb.Postman_Token{ |
| 276 | + Token: "super-secret-token", |
| 277 | + }, |
| 278 | + }) |
| 279 | + err := s.Init(ctx, "test - postman", 0, 1, false, conn, 1) |
| 280 | + if err != nil { |
| 281 | + t.Fatalf("init error: %v", err) |
| 282 | + } |
| 283 | + gock.InterceptClient(s.client.HTTPClient) |
| 284 | + |
| 285 | + start := time.Now() |
| 286 | + _, err = s.client.EnumerateWorkspaces(ctx) |
| 287 | + if err != nil { |
| 288 | + t.Fatalf("enumeration error: %v", err) |
| 289 | + } |
| 290 | + elapsed := time.Since(start) |
| 291 | + // With <numWorkspaces> requests at 1 per second rate limit, |
| 292 | + // elapsed time should be at least <numWorkspaces - 1> seconds |
| 293 | + if elapsed < time.Duration(numWorkspaces-1)*time.Second { |
| 294 | + t.Errorf("Rate limiting not working as expected. Elapsed time: %v, expected at least %d seconds", elapsed, numWorkspaces-1) |
| 295 | + } |
| 296 | +} |
0 commit comments