Skip to content

Commit

Permalink
Add initial test for GetAllCVEs handler function
Browse files Browse the repository at this point in the history
This commit adds the initial test for the GetAllCVEs handler function in the vulnerability handler. The test uses custom mock implementations for the Auth and Vuln services.

Details:
- Introduced custom mock clients (`MockAuthServiceClientWrapper` and `MockVulnServiceClientWrapper`) to simulate gRPC service behavior.
- Implemented the first version of the GetAllCVEs test, checking the interaction between the handler and the mock services.

This initial test serves as a foundation for future enhancements and ensures basic functionality.
  • Loading branch information
mtnmunuklu committed Jan 31, 2024
1 parent a2d7123 commit 79c9fe0
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 8 deletions.
119 changes: 119 additions & 0 deletions api/handlers/vuln_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ package handlers
import (
"context"
"encoding/json"
"io"
"testing"
"time"

"github.com/gofiber/fiber/v2"
"github.com/mtnmunuklu/bavul/pb"
"github.com/mtnmunuklu/bavul/security"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"gopkg.in/mgo.v2/bson"
)

Expand Down Expand Up @@ -182,6 +185,67 @@ func (c *MockVulnServiceClientWrapper) FetchNVDFeeds(ctx context.Context, req *p
return nil, nil
}

type MockVulnService_GetAllCVEsClientWrapper struct {
cves []*pb.CVE
idx int
}

// Recv simulates receiving CVEs from the server.
func (m *MockVulnService_GetAllCVEsClientWrapper) Recv() (*pb.CVE, error) {
if m.idx < len(m.cves) {
cve := m.cves[m.idx]
m.idx++
return cve, nil
}
return nil, io.EOF
}

// CloseSend simulates closing the send stream.
func (m *MockVulnService_GetAllCVEsClientWrapper) CloseSend() error {
return nil
}

// Context returns the client context.
func (m *MockVulnService_GetAllCVEsClientWrapper) Context() context.Context {
return context.Background()
}

// SendMsg simulates sending a message.
func (m *MockVulnService_GetAllCVEsClientWrapper) SendMsg(m1 interface{}) error {
return nil
}

// RecvMsg simulates receiving a message.
func (m *MockVulnService_GetAllCVEsClientWrapper) RecvMsg(m1 interface{}) error {
return nil
}

// Header returns the header metadata.
func (m *MockVulnService_GetAllCVEsClientWrapper) Header() (metadata.MD, error) {
return nil, nil
}

// Trailer returns the trailer metadata.
func (m *MockVulnService_GetAllCVEsClientWrapper) Trailer() metadata.MD {
return nil
}

// SendHeader simulates sending header metadata.
func (m *MockVulnService_GetAllCVEsClientWrapper) SendHeader(m1 metadata.MD) error {
return nil
}

// SetSendDeadline simulates setting the send deadline.
func (m *MockVulnService_GetAllCVEsClientWrapper) SetSendDeadline(m1 time.Time) error {
return nil
}

// SetRecvDeadline simulates setting the receive deadline.
func (m *MockVulnService_GetAllCVEsClientWrapper) SetRecvDeadline(m1 time.Time) error {
return nil
}

// MockVulnService_GetAllCVEsClient basit bir mock implementasyonu
func TestAddCVE(t *testing.T) {

// Create a custom mock client wrapper for Auth Service
Expand Down Expand Up @@ -243,3 +307,58 @@ func TestAddCVE(t *testing.T) {
// Release the Fiber context
app.ReleaseCtx(fiberContext)
}

func TestGetAllCVEs(t *testing.T) {
// Create a custom mock client wrapper for Auth Service
mockAuthWrapper := &MockAuthServiceClientWrapper{}

// Create a custom mock client wrapper for Vuln Service
mockVulnWrapper := &MockVulnServiceClientWrapper{}

// Create handlers using the custom mock client wrapper
handler := NewVulnHandlers(mockAuthWrapper, mockVulnWrapper)

// Set Auth Service Client in the mockWrapper
mockAuthWrapper.GetUserRoleFunc = func(ctx context.Context, req *pb.GetUserRoleRequest, opts ...grpc.CallOption) (*pb.GetUserRoleResponse, error) {
// Simulate the behavior of the gRPC service
return &pb.GetUserRoleResponse{Role: "admin"}, nil
}

// Set Vuln Service Client in the mockWrapper
mockVulnWrapper.GetAllCVEsFunc = func(ctx context.Context, req *pb.GetAllCVEsRequest, opts ...grpc.CallOption) (pb.VulnService_GetAllCVEsClient, error) {
// Simulate the behavior of the gRPC service
cves := []*pb.CVE{
{Id: "1", CveId: "CVE-2022-1234", Description: "Test CVE 1", Severity: "High", Product: "Test Product 1", Vendor: "Test Vendor 1", Published: "2024-01-27T10:10:10", Modified: "2024-01-27T10:10:10"},
{Id: "2", CveId: "CVE-2022-5678", Description: "Test CVE 2", Severity: "Medium", Product: "Test Product 2", Vendor: "Test Vendor 2", Published: "2024-01-27T10:20:20", Modified: "2024-01-27T10:20:20"},
}

mockStream := &MockVulnService_GetAllCVEsClientWrapper{
cves: cves,
}

return mockStream, nil
}

// Create a dummy Fiber context
app := fiber.New()
fiberContext := app.AcquireCtx(&fasthttp.RequestCtx{})

// Set the request headers in the Fiber context
userId := bson.NewObjectId()
token, err := security.NewToken(userId.Hex())
assert.NoError(t, err)
fiberContext.Request().Header.Set("Authorization", "Bearer "+token+"")

// Your test code here
err = handler.GetAllCVEs(fiberContext)
assert.NoError(t, err)

// Assert that the GetUserRole and GetAllCVEs functions were called with the expected parameters
assert.True(t, mockAuthWrapper.GetUserRoleFuncCalled, "GetUserRole function of mockAuthWrapper should be called")
assert.True(t, mockVulnWrapper.GetAllCVEsFuncCalled, "GetAllCVEs function of mockVulnWrapper should be called")

// Additional assertions based on the expected response can be added if needed

// Release the Fiber context
app.ReleaseCtx(fiberContext)
}
8 changes: 0 additions & 8 deletions api/routes/vuln.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,5 @@ func NewVulnRoutes(vulnHandlers handlers.VulnHandlers) []*Route {
},
AuthRequired: true,
},
{
Method: http.MethodGet,
Path: "/cve-search",
Handler: func(c *fiber.Ctx) error {
return vulnHandlers.FetchNVDFeeds(c)
},
AuthRequired: true,
},
}
}

0 comments on commit 79c9fe0

Please sign in to comment.