Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fake test cases sample #3813

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
3a06796
added fake test cases for meraki detector
kashifkhan0771 Nov 18, 2024
13e7c82
Merge branch 'main' into tests/fake-testcase-sample
kashifkhan0771 Nov 18, 2024
2184cd7
enhanced the test cases
kashifkhan0771 Nov 19, 2024
51cd748
Merge branch 'main' into tests/fake-testcase-sample
kashifkhan0771 Nov 20, 2024
2402f1f
updated test cases
kashifkhan0771 Nov 20, 2024
795b34b
Merge branch 'main' into tests/fake-testcase-sample
kashifkhan0771 Dec 23, 2024
c717615
added additional handling
kashifkhan0771 Dec 23, 2024
964d369
updated response status handling
kashifkhan0771 Dec 23, 2024
1cf403e
updated response status handling
kashifkhan0771 Dec 23, 2024
b41f082
Merge branch 'main' into tests/fake-testcase-sample
kashifkhan0771 Dec 26, 2024
6de2bb1
updated approach
kashifkhan0771 Dec 26, 2024
3ed2cc7
Merge branch 'main' into tests/fake-testcase-sample
kashifkhan0771 Dec 31, 2024
ffe3094
Merge branch 'main' into tests/fake-testcase-sample
kashifkhan0771 Dec 31, 2024
599338c
Merge branch 'main' into tests/fake-testcase-sample
kashifkhan0771 Dec 31, 2024
08e9e59
Merge branch 'main' into tests/fake-testcase-sample
kashifkhan0771 Jan 7, 2025
1c7360d
Merge branch 'main' into tests/fake-testcase-sample
kashifkhan0771 Jan 8, 2025
dadf5b4
Merge branch 'main' into tests/fake-testcase-sample
kashifkhan0771 Jan 10, 2025
c4a3c13
Merge branch 'main' into tests/fake-testcase-sample
kashifkhan0771 Jan 13, 2025
4b4b143
Merge branch 'main' into tests/fake-testcase-sample
kashifkhan0771 Jan 14, 2025
6f9b8ec
Merge branch 'main' into tests/fake-testcase-sample
kashifkhan0771 Jan 15, 2025
01940b4
Merge branch 'main' into tests/fake-testcase-sample
kashifkhan0771 Jan 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/detectors/meraki/meraki.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result

if verify {
client := s.getClient()
organizations, isVerified, verificationErr := verifyMerakiApiKey(ctx, client, match)
organizations, isVerified, verificationErr := verifyMerakiApiKey(ctx, client, "https://api.meraki.com/api/v1/organizations", match)
s1.Verified = isVerified
if verificationErr != nil {
s1.SetVerificationError(verificationErr)
Expand Down Expand Up @@ -101,8 +101,8 @@ func (s Scanner) Type() detectorspb.DetectorType {
verifyMerakiApiKey verifies if the passed matched api key for meraki is active or not.
docs: https://developer.cisco.com/meraki/api-v1/authorization/#authorization
*/
func verifyMerakiApiKey(ctx context.Context, client *http.Client, match string) ([]merakiOrganizations, bool, error) {
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.meraki.com/api/v1/organizations", http.NoBody)
func verifyMerakiApiKey(ctx context.Context, client *http.Client, apiURL, match string) ([]merakiOrganizations, bool, error) {
req, err := http.NewRequestWithContext(ctx, "GET", apiURL, http.NoBody)
if err != nil {
return nil, false, err
}
Expand Down
62 changes: 62 additions & 0 deletions pkg/detectors/meraki/meraki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package meraki

import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"

"github.com/google/go-cmp/cmp"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
)
Expand Down Expand Up @@ -100,3 +103,62 @@ func TestMeraki_Pattern(t *testing.T) {
})
}
}

func TestMeraki_Fake(t *testing.T) {
// mock response data
mockOrganizations := []merakiOrganizations{
{ID: "123", Name: "Example Organization 1"},
{ID: "456", Name: "Example Organization 2"},
}
mockResponse, err := json.Marshal(mockOrganizations)
if err != nil {
t.Fatalf("failed to marshal mock organizations: %v", err)
}

// test cases
tests := []struct {
name string
client *http.Client
secret string
verified bool
wantErr bool
}{
{
name: "success - 200 OK",
client: common.ConstantResponseHttpClient(http.StatusOK, string(mockResponse)),
secret: "e9e0f062f587b423bb6cc6328eb786d75b45783e",
verified: true,
wantErr: false,
},
{
name: "fail - 401 UnAuthorized",
client: common.ConstantResponseHttpClient(http.StatusUnauthorized, ""),
secret: "e9e0f062f587b423bb6cc6328eb786d75b45783f",
verified: false,
wantErr: false,
},
{
name: "fail - 400 unexpected status code error",
client: common.ConstantResponseHttpClient(http.StatusBadRequest, ""),
secret: "",
verified: false,
wantErr: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// calling FromData does not work cause APIURLs are hardcoded
_, isVerified, verificationErr := verifyMerakiApiKey(context.Background(), test.client, "http://example.com", test.secret)
if (verificationErr != nil) != test.wantErr {
t.Errorf("[%s] unexpected error: got %v, wantErr: %t", test.name, verificationErr, test.wantErr)
}

if isVerified != test.verified {
t.Errorf("[%s] verification status mismatch: got %t, want %t", test.name, isVerified, test.verified)
}

// additional checks if required
})
}
}
Loading