Lint / golangci-lint (push) Waiting to run
Lint / semgrep (push) Waiting to run
Release / Release (push) Waiting to run
Scan for secrets / test (push) Waiting to run
Test / test (push) Waiting to run
Test / test-community (push) Waiting to run
* added analyzer initial working structure * restructured the resources * added history read api * added history delete api * fixed http method issue * added delete dubbing api and refactored the code * added dubbing read api * optimized requests and added projects api * added pronunciation dictionaries apis * fixed linter * added models,audionative,workspace apis * added some last apis * Deleted \ * fixed linter * few more enhancements and test cases added * added go generate command * optimized the code and resolved the comments * fixed linter * fixed indentation * resolved comments * kicked imposter print statement * formatted cli.go * Update pkg/analyzer/analyzers/elevenlabs/elevenlabs.go Co-authored-by: Eng Zer Jun <[email protected]> * resolved comments * added secret info methods which safely append and read permission and resources --------- Co-authored-by: Eng Zer Jun <[email protected]>
103 lines
2.8 KiB
Go
103 lines
2.8 KiB
Go
package elevenlabs
|
|
|
|
import (
|
|
_ "embed"
|
|
"encoding/json"
|
|
"sort"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers"
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/config"
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
|
|
)
|
|
|
|
//go:embed result_output.json
|
|
var expectedOutput []byte
|
|
|
|
func TestAnalyzer_Analyze(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
|
|
defer cancel()
|
|
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors5")
|
|
if err != nil {
|
|
t.Fatalf("could not get test secrets from GCP: %s", err)
|
|
}
|
|
|
|
key := testSecrets.MustGetField("ELEVENLABS")
|
|
|
|
tests := []struct {
|
|
name string
|
|
key string
|
|
want []byte // JSON string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "valid ElevenLabs full access key",
|
|
key: key,
|
|
want: expectedOutput,
|
|
wantErr: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
a := Analyzer{Cfg: &config.Config{}}
|
|
got, err := a.Analyze(ctx, map[string]string{"key": tt.key})
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("Analyzer.Analyze() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
|
|
// Bindings need to be in the same order to be comparable
|
|
sortBindings(got.Bindings)
|
|
|
|
// Marshal the actual result to JSON
|
|
gotJSON, err := json.Marshal(got)
|
|
if err != nil {
|
|
t.Fatalf("could not marshal got to JSON: %s", err)
|
|
}
|
|
|
|
// Parse the expected JSON string
|
|
var wantObj analyzers.AnalyzerResult
|
|
if err := json.Unmarshal([]byte(tt.want), &wantObj); err != nil {
|
|
t.Fatalf("could not unmarshal want JSON string: %s", err)
|
|
}
|
|
|
|
// Bindings need to be in the same order to be comparable
|
|
sortBindings(wantObj.Bindings)
|
|
|
|
// Marshal the expected result to JSON (to normalize)
|
|
wantJSON, err := json.Marshal(wantObj)
|
|
if err != nil {
|
|
t.Fatalf("could not marshal want to JSON: %s", err)
|
|
}
|
|
|
|
// Compare the JSON strings
|
|
if string(gotJSON) != string(wantJSON) {
|
|
// Pretty-print both JSON strings for easier comparison
|
|
var gotIndented, wantIndented []byte
|
|
gotIndented, err = json.MarshalIndent(got, "", " ")
|
|
if err != nil {
|
|
t.Fatalf("could not marshal got to indented JSON: %s", err)
|
|
}
|
|
wantIndented, err = json.MarshalIndent(wantObj, "", " ")
|
|
if err != nil {
|
|
t.Fatalf("could not marshal want to indented JSON: %s", err)
|
|
}
|
|
t.Errorf("Analyzer.Analyze() = %s, want %s", gotIndented, wantIndented)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Helper function to sort bindings
|
|
func sortBindings(bindings []analyzers.Binding) {
|
|
sort.SliceStable(bindings, func(i, j int) bool {
|
|
if bindings[i].Resource.Name == bindings[j].Resource.Name {
|
|
return bindings[i].Permission.Value < bindings[j].Permission.Value
|
|
}
|
|
return bindings[i].Resource.Name < bindings[j].Resource.Name
|
|
})
|
|
}
|