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
Next Next commit
added fake test cases for meraki detector
  • Loading branch information
kashifkhan0771 committed Nov 18, 2024
commit 3a06796dd8de01a5d6a9731b7af13e6e73b9981d
6 changes: 3 additions & 3 deletions pkg/detectors/meraki/meraki.go
Original file line number Diff line number Diff line change
@@ -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)
@@ -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
}
3 changes: 3 additions & 0 deletions pkg/detectors/meraki/meraki_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build detectors
// +build detectors

package meraki

import (
87 changes: 87 additions & 0 deletions pkg/detectors/meraki/meraki_test.go
Original file line number Diff line number Diff line change
@@ -2,7 +2,10 @@ package meraki

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

"github.com/google/go-cmp/cmp"
@@ -100,3 +103,87 @@ 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)
}

// create a fake HTTP handler function
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Verify the Authorization Header
if r.Header.Get("X-Cisco-Meraki-API-Key") != "e9e0f062f587b423bb6cc6328eb786d75b45783e" {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}

// this is to test the failed decoding scenario
if r.Header.Get("X-Cisco-Meraki-API-Key") == "failed-to-decode" {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"id": 123}`))

return
}

// Send back mock response for 200 OK
w.WriteHeader(http.StatusOK)
_, _ = w.Write(mockResponse)
})
// create a mock server
server := CreateMockServer(handler)
defer server.Close()

// test cases
tests := []struct {
name string
secret string
verified bool
wantErr bool
}{
{
name: "success - 200 OK",
secret: "e9e0f062f587b423bb6cc6328eb786d75b45783e",
verified: true,
wantErr: false,
},
{
name: "fail - 401 UnAuthorized",
secret: "e9e0f062f587b423bb6cc6328eb786d75b45783f",
verified: false,
wantErr: false,
},
{
name: "fail - failed to decode response",
secret: "failed-to-decode",
verified: false,
wantErr: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, isVerified, verificationErr := verifyMerakiApiKey(context.Background(), server.Client(), server.URL, test.secret)
if verificationErr != nil && !test.wantErr {
t.Errorf("verification failed; got error, want: %t", test.wantErr)
}

if isVerified != test.verified {
t.Errorf("verification failed; isVerified: %t, want: %t", isVerified, test.verified)
}

// additional checks if required
})
}
}

// 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))
}