//go:build detectors
// +build detectors

package artifactory

import (
	"context"
	"fmt"
	"testing"
	"time"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
	"github.com/stretchr/testify/require"
	"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"

	"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb"
)

func TestArtifactory_FromChunk(t *testing.T) {
	// NOTE: Using mock secrets because JFrog deprecated AKCp API keys (disabled creation end of Q3 2024).
	// Real AKCp keys can no longer be generated, so we cannot test actual verification scenarios.
	// These mock keys follow the correct format: AKCp + 69 alphanumeric characters = 73 total
	// Reference: https://jfrog.com/help/r/jfrog-release-information/artifactory-7.47.10-cloud-self-hosted
	mockSecret := "AKCp5bueTFpfypEqQbGJPp7eHFi28fBivfWczrjbPb9erDff9LbXZbj6UsRExVXA8asWGc9fM"
	appURL := "trufflehog.jfrog.io"

	type args struct {
		ctx    context.Context
		data   []byte
		verify bool
	}
	tests := []struct {
		name    string
		s       Scanner
		args    args
		want    []detectors.Result
		wantErr bool
	}{
		{
			name: "found, unverified - mock key (cannot verify deprecated AKCp format)",
			s:    Scanner{},
			args: args{
				ctx:    context.Background(),
				data:   []byte(fmt.Sprintf("You can find a artifactory secret %s and domain %s", mockSecret, appURL)),
				verify: false, // Cannot verify - AKCp API keys are deprecated and no valid keys available
			},
			want: []detectors.Result{
				{
					DetectorType: detector_typepb.DetectorType_ArtifactoryAccessToken,
					Verified:     false,
				},
			},
			wantErr: false,
		},
		{
			name: "not found",
			s:    Scanner{},
			args: args{
				ctx:    context.Background(),
				data:   []byte("You cannot find the secret within"),
				verify: true,
			},
			want:    nil,
			wantErr: false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			tt.s.UseFoundEndpoints(true)

			got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
			if (err != nil) != tt.wantErr {
				t.Errorf("Artifactory.FromData() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			for i := range got {
				if len(got[i].Raw) == 0 {
					t.Fatalf("no raw secret present: \n %+v", got[i])
				}
				gotErr := ""
				if got[i].VerificationError() != nil {
					gotErr = got[i].VerificationError().Error()
				}
				wantErr := ""
				if tt.want[i].VerificationError() != nil {
					wantErr = tt.want[i].VerificationError().Error()
				}
				if gotErr != wantErr {
					t.Fatalf("wantVerificationError = %v, verification error = %v", tt.want[i].VerificationError(), got[i].VerificationError())
				}
			}
			ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "RawV2", "verificationError", "primarySecret")
			if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" {
				t.Errorf("Artifactory.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
			}
		})
	}
}

func TestArtifactory_FromChunk_WithCustomEndpoint(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
	defer cancel()
	mockSecret := "AKCp5bueTFpfypEqQbGJPp7eHFi28fBivfWczrjbPb9erDff9LbXZbj6UsRExVXA8asWGc9fM"
	appURL := "trufflesecurity.com"

	s := Scanner{}
	s.UseFoundEndpoints(true)
	err := s.SetConfiguredEndpoints(appURL)
	if err != nil {
		t.Fatal("Error in setting configured endpoint")
	}
	data := []byte(fmt.Sprintf("You can find a artifactory secret %s ", mockSecret))

	got, err := s.FromData(ctx, true, data)

	require.NoError(t, err, "unexpected error from FromData")
	require.Greaterf(t, len(got), 0, "Expected Alteast 1 result")

	expectedRawV2 := []byte(mockSecret + appURL)
	if string(got[0].RawV2) != string(expectedRawV2) {
		t.Errorf("Artifactory.FromData() rawV2 secret mismatch: got %s, want %s", string(got[0].RawV2), string(expectedRawV2))
	}
}

func BenchmarkFromData(benchmark *testing.B) {
	ctx := context.Background()
	s := Scanner{}
	for name, data := range detectors.MustGetBenchmarkData() {
		benchmark.Run(name, func(b *testing.B) {
			b.ResetTimer()
			for n := 0; n < b.N; n++ {
				_, err := s.FromData(ctx, false, data)
				if err != nil {
					b.Fatal(err)
				}
			}
		})
	}
}
