|
| 1 | +package v1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/go-cmp/cmp" |
| 8 | + |
| 9 | + "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" |
| 10 | + "github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick" |
| 11 | +) |
| 12 | + |
| 13 | +func TestHubspotV1_Pattern(t *testing.T) { |
| 14 | + d := Scanner{} |
| 15 | + ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d}) |
| 16 | + tests := []struct { |
| 17 | + name string |
| 18 | + input string |
| 19 | + want []string |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "hapikey", |
| 23 | + input: `// const hapikey = 'b714cac4-a45c-42af-9905-da4de8838d75'; |
| 24 | +const { HAPI_KEY } = process.env; |
| 25 | +const hs = new HubSpotAPI({ hapikey: HAPI_KEY });`, |
| 26 | + want: []string{"b714cac4-a45c-42af-9905-da4de8838d75"}, |
| 27 | + }, |
| 28 | + // TODO: Doesn't work because it's more than 40 characters. |
| 29 | + // { |
| 30 | + // name: "hubapi", |
| 31 | + // input: `curl https://api.hubapi.com/contacts/v1/lists/all/contacts/all \ |
| 32 | + //--header "Authorization: Bearer b71aa2ed-9c76-417d-bd8e-c5f4980d21ef"`, |
| 33 | + // want: []string{"b71aa2ed-9c76-417d-bd8e-c5f4980d21ef"}, |
| 34 | + // }, |
| 35 | + { |
| 36 | + name: "hubspot_1", |
| 37 | + input: `const hs = new HubSpotAPI("76a836c8-469d-4426-8a3b-194ca930b7a1"); |
| 38 | +
|
| 39 | +const blogPosts = hs.blog.getPosts({ name: 'Inbound' });`, |
| 40 | + want: []string{"76a836c8-469d-4426-8a3b-194ca930b7a1"}, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "hubspot_2", |
| 44 | + input: ` 'hubspot' => [ |
| 45 | + // 'api_key' => 'e9ff285d-6b7f-455a-a56d-9ec8c4abbd47', // @ts dev`, |
| 46 | + want: []string{"e9ff285d-6b7f-455a-a56d-9ec8c4abbd47"}, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + for _, test := range tests { |
| 51 | + t.Run(test.name, func(t *testing.T) { |
| 52 | + matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input)) |
| 53 | + if len(matchedDetectors) == 0 { |
| 54 | + t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input) |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + results, err := d.FromData(context.Background(), false, []byte(test.input)) |
| 59 | + if err != nil { |
| 60 | + t.Errorf("error = %v", err) |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + if len(results) != len(test.want) { |
| 65 | + if len(results) == 0 { |
| 66 | + t.Errorf("did not receive result") |
| 67 | + } else { |
| 68 | + t.Errorf("expected %d results, only received %d", len(test.want), len(results)) |
| 69 | + } |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + actual := make(map[string]struct{}, len(results)) |
| 74 | + for _, r := range results { |
| 75 | + if len(r.RawV2) > 0 { |
| 76 | + actual[string(r.RawV2)] = struct{}{} |
| 77 | + } else { |
| 78 | + actual[string(r.Raw)] = struct{}{} |
| 79 | + } |
| 80 | + } |
| 81 | + expected := make(map[string]struct{}, len(test.want)) |
| 82 | + for _, v := range test.want { |
| 83 | + expected[v] = struct{}{} |
| 84 | + } |
| 85 | + |
| 86 | + if diff := cmp.Diff(expected, actual); diff != "" { |
| 87 | + t.Errorf("%s diff: (-want +got)\n%s", test.name, diff) |
| 88 | + } |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
0 commit comments