@@ -2,6 +2,7 @@ package postman
2
2
3
3
import (
4
4
"fmt"
5
+ "github.com/stretchr/testify/assert"
5
6
"reflect"
6
7
"sort"
7
8
"strings"
@@ -334,6 +335,110 @@ func TestSource_ScanGeneralRateLimit(t *testing.T) {
334
335
}
335
336
}
336
337
338
+ func TestSource_BadPostmanWorkspaceApiResponseDoesntEndScan (t * testing.T ) {
339
+ // The goal here is to make sure that, if we get a bad ID (or other issue) for a workspace and the Postman API
340
+ // gives us a non 200 responses, it doesn't stop the whole scan. To do that we're going to have it get a set
341
+ // of 3 workspaces from /workspaces/ and then mock all but the last as a bad request. Then we'll check that the
342
+ // third one was properly requested.
343
+ defer gock .Off ()
344
+
345
+ // We'll use the IDs later in a couple of places
346
+ id1WorkspaceBadRequest := "1f0df51a-8658-4ee8-a2a1-d2567dfa09a9"
347
+ id2WorkspaceBadId := "a0f46158-1529-11ee-be56-0242ac120002"
348
+ id3WorkspaceGood := "f8801e9e-03a4-4c7b-b31e-5db5cd771696"
349
+
350
+ // Mock the workspace list response. This gives EnumerateWorkspaces what it needs
351
+ // to make calls for the individual workspaces details
352
+ gock .New ("https://api.getpostman.com" ).
353
+ Get ("/workspaces" ).
354
+ Reply (200 ).
355
+ JSON (map [string ]interface {}{
356
+ "workspaces" : []map [string ]interface {}{
357
+ {
358
+ "id" : id1WorkspaceBadRequest ,
359
+ "name" : "My Workspace" ,
360
+ "createdBy" : "12345678" ,
361
+ "type" : "personal" ,
362
+ "visibility" : "personal" ,
363
+ },
364
+ {
365
+ "id" : id2WorkspaceBadId ,
366
+ "name" : "Private Workspace" ,
367
+ "createdBy" : "12345678" ,
368
+ "type" : "team" ,
369
+ "visibility" : "private" ,
370
+ },
371
+ {
372
+ "id" : id3WorkspaceGood ,
373
+ "name" : "Team Workspace" ,
374
+ "createdBy" : "12345678" ,
375
+ "type" : "team" ,
376
+ "visibility" : "team" ,
377
+ },
378
+ },
379
+ })
380
+
381
+ // Make a call for the first workspace respond with a malformed response
382
+ gock .New ("https://api.getpostman.com" ).
383
+ Get (fmt .Sprintf ("/workspaces/%s" , id1WorkspaceBadRequest )).
384
+ Reply (200 ).
385
+ BodyString ("INTENTIONALLY MALFORMED RESPONSE BODY" )
386
+ // Make a call for the second workspace respond not found
387
+ gock .New ("https://api.getpostman.com" ).
388
+ Get (fmt .Sprintf ("/workspaces/%s" , id2WorkspaceBadId )).
389
+ Reply (404 ).
390
+ JSON (map [string ]interface {}{
391
+ "error" : map [string ]interface {}{
392
+ "name" : "workspaceNotFoundError" ,
393
+ "mesage" : "workspace not found" ,
394
+ "statusCode" : 404 ,
395
+ },
396
+ })
397
+ // Make a call for the third workspace succeed
398
+ gock .New ("https://api.getpostman.com" ).
399
+ Get (fmt .Sprintf ("/workspaces/%s" , id3WorkspaceGood )).
400
+ Reply (200 ).
401
+ JSON (map [string ]interface {}{
402
+ "workspace" : map [string ]interface {}{
403
+ "id" : id3WorkspaceGood ,
404
+ "name" : "Team Workspace" ,
405
+ "type" : "team" ,
406
+ "description" : "This is a team workspace." ,
407
+ "visibility" : "team" ,
408
+ "createdBy" : "12345678" ,
409
+ "updatedBy" : "12345678" ,
410
+ "createdAt" : "2022-07-06T16:18:32.000Z" ,
411
+ "updatedAt" : "2022-07-06T20:55:13.000Z" ,
412
+ "collections" : []map [string ]interface {}{},
413
+ "environments" : []map [string ]interface {}{},
414
+ "mocks" : []map [string ]interface {}{},
415
+ "monitors" : []map [string ]interface {}{},
416
+ "apis" : []map [string ]interface {}{},
417
+ },
418
+ })
419
+
420
+ // Set up the source and inject the mocks
421
+ ctx := context .Background ()
422
+ s , conn := createTestSource (& sourcespb.Postman {
423
+ Credential : & sourcespb.Postman_Token {
424
+ Token : "super-secret-token" ,
425
+ },
426
+ })
427
+ err := s .Init (ctx , "test - postman" , 0 , 1 , false , conn , 1 )
428
+ if err != nil {
429
+ t .Fatalf ("init error: %v" , err )
430
+ }
431
+ gock .InterceptClient (s .client .HTTPClient )
432
+ defer gock .RestoreClient (s .client .HTTPClient )
433
+
434
+ // Do the thing
435
+ _ , _ = s .client .EnumerateWorkspaces (ctx )
436
+
437
+ // If all the calls were made, then we know the one bad request didn't cause explosions
438
+ assert .True (t , gock .IsDone ())
439
+
440
+ }
441
+
337
442
func TestSource_UnmarshalMultipleHeaderTypes (t * testing.T ) {
338
443
defer gock .Off ()
339
444
// Mock a collection with request and response headers of KeyValue type
0 commit comments