Files
trufflehog/pkg/detectors/openrouter/openrouter_test.go
f2cd191b97 feat: add OpenRouter detector (#4500)
* feat: add OpenRouter detector

closes #4499

* refactor: correct doc comment referencing OpenAI to OpenRouter

* refactor: remove unnecessary `AnalysisInfo` field

* fix: data field types

Signed-off-by: Luc Georges <[email protected]>

* fix: handle float fmt correctly

Signed-off-by: Luc Georges <[email protected]>

* fix detector type add feature flag for openrouter detector

* fix integration tests

---------

Signed-off-by: Luc Georges <[email protected]>
Co-authored-by: Shahzad Haider <[email protected]>
Co-authored-by: Shahzad Haider <[email protected]>
Co-authored-by: Muneeb Ullah Khan <[email protected]>
2026-07-03 15:08:49 +05:00

79 lines
2.0 KiB
Go

package openrouter
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
)
func TestOpenRouter_Pattern(t *testing.T) {
d := Scanner{}
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
tests := []struct {
name string
input string
want []string
}{
{
name: "API key",
input: `OPENROUTER_API_KEY = "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"`,
want: []string{"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"},
},
{
name: "invalid pattern",
input: `
[INFO] Sending request to the openrouter API
[DEBUG] Using Key=sk-or-v1-a2Cy8xCLyvrAf7lZKfhQhyCr4RAID9D
[ERROR] Response received: 401 UnAuthorized
`,
want: []string{},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
detectorMatches := ahoCorasickCore.FindDetectorMatches([]byte(test.input))
if len(detectorMatches) == 0 {
t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input)
return
}
results, err := d.FromData(context.Background(), false, []byte(test.input))
if err != nil {
t.Errorf("error = %v", err)
return
}
if len(results) != len(test.want) {
if len(results) == 0 {
t.Errorf("did not receive result")
} else {
t.Errorf("expected %d results, only received %d", len(test.want), len(results))
}
return
}
actual := make(map[string]struct{}, len(results))
for _, r := range results {
if len(r.RawV2) > 0 {
actual[string(r.RawV2)] = struct{}{}
} else {
actual[string(r.Raw)] = struct{}{}
}
}
expected := make(map[string]struct{}, len(test.want))
for _, v := range test.want {
expected[v] = struct{}{}
}
if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf("%s diff: (-want +got)\n%s", test.name, diff)
}
})
}
}