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

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
25 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
63df1f6
Merge branch 'main' into tests/fake-testcase-sample
kashifkhan0771 Jan 21, 2025
de3951c
Merge branch 'main' into tests/fake-testcase-sample
kashifkhan0771 Jan 21, 2025
c051ab6
Merge branch 'main' into tests/fake-testcase-sample
kashifkhan0771 Jan 29, 2025
265e3ea
Merge branch 'main' into tests/fake-testcase-sample
kashifkhan0771 Feb 21, 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
Prev Previous commit
Next Next commit
updated approach
  • Loading branch information
kashifkhan0771 committed Dec 26, 2024
commit 6de2bb17df22183bf472dc151a996a699c18d1b2
37 changes: 6 additions & 31 deletions pkg/detectors/meraki/meraki_test.go
Original file line number Diff line number Diff line change
@@ -5,11 +5,11 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"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"
)
@@ -115,49 +115,31 @@ func TestMeraki_Fake(t *testing.T) {
t.Fatalf("failed to marshal mock organizations: %v", err)
}

// create a fake HTTP handler function
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Header.Get("X-Cisco-Meraki-API-Key") {
case "e9e0f062f587b423bb6cc6328eb786d75b45783e":
// send back mock response for 200 OK
w.WriteHeader(http.StatusOK)
_, _ = w.Write(mockResponse)
return
case "e9e0f062f587b423bb6cc6328eb786d75b45783f":
// send back mock 401 error for mock expired key
w.WriteHeader(http.StatusUnauthorized)
return
case "":
// if not auth header is sent, return 400
w.WriteHeader(http.StatusBadRequest)
return
}
})
// create a mock server
server := CreateMockServer(handler)
defer server.Close()

// 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,
@@ -167,7 +149,7 @@ func TestMeraki_Fake(t *testing.T) {
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(), server.Client(), server.URL, test.secret)
_, 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)
}
@@ -180,10 +162,3 @@ func TestMeraki_Fake(t *testing.T) {
})
}
}

// this i am thinking to move to common
// CreateMockServer creates a mock HTTP server with a given handler function.
func CreateMockServer(handler func(w http.ResponseWriter, r *http.Request)) *httptest.Server {
// Create and return a new mock server
return httptest.NewServer(http.HandlerFunc(handler))
}
Loading
Oops, something went wrong.