Skip to content

Commit

Permalink
Add caching tests for GetAllCVEs, FetchNVDFeeds, SearchCVE, GetUser, …
Browse files Browse the repository at this point in the history
…and ListUsers
  • Loading branch information
mtnmunuklu committed Feb 10, 2024
1 parent 5078e1e commit a7a1006
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 24 deletions.
6 changes: 3 additions & 3 deletions api/handlers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ func (h *authHandlers) GetUser(c *fiber.Ctx) error {
}

// Cache key creation
cacheKey := "GetUser:" + c.Query("Email")
cacheKey := "GetUser:" + c.Get("Email")

// Get value from cache
if cachedData, found := util.GetFromCache(cacheKey); found {
return util.WriteAsJSON(c, http.StatusOK, cachedData)
}

email := c.Query("Email")
email := c.Get("Email")
getUserRequest := &pb.GetUserRequest{Email: email}

getedUser, err := h.authSvcClient.GetUser(c.Context(), getUserRequest)
Expand Down Expand Up @@ -119,7 +119,7 @@ func (h *authHandlers) DeleteUser(c *fiber.Ctx) error {
return util.WriteError(c, http.StatusUnauthorized, util.ErrUnauthorized)
}

email := c.Query("Email")
email := c.Get("Email")
deleteUserRequest := &pb.DeleteUserRequest{Email: email}

deletedUser, err := h.authSvcClient.DeleteUser(c.Context(), deleteUserRequest)
Expand Down
47 changes: 43 additions & 4 deletions api/handlers/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (

"github.com/gofiber/fiber/v2"
"github.com/mtnmunuklu/bavul/api/handlers"
"github.com/mtnmunuklu/bavul/authentication/util"
"github.com/mtnmunuklu/bavul/api/util"
authUtil "github.com/mtnmunuklu/bavul/authentication/util"
"github.com/mtnmunuklu/bavul/pb"
"github.com/mtnmunuklu/bavul/security"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -237,7 +238,7 @@ func TestSignIn(t *testing.T) {
user := &pb.User{Id: "1", Name: "Test User1", Email: req.GetEmail(), Role: "user", Created: "2024-02-02T18:18:00", Updated: "2024-02-02T18:18:00"}
token, err := security.NewToken(user.Id)
if err != nil {
return nil, util.ErrFailedSignIn
return nil, authUtil.ErrFailedSignIn
}
return &pb.SignInResponse{User: user, Token: token}, nil
}
Expand Down Expand Up @@ -298,14 +299,33 @@ func TestGetUser(t *testing.T) {
fiberContext.Request().Header.Set("Authorization", "Bearer "+token+"")
fiberContext.Request().Header.Set("Email", "[email protected]")

// Test the GetUser handler
// Test the GetUser handler for the first time
err = handler.GetUser(fiberContext)
assert.NoError(t, err)

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

// Get the cached result for the first time
cachedDataFirstTime, foundFirstTime := util.GetFromCache("GetUser:[email protected]")
assert.True(t, foundFirstTime, "Result should be in cache for the first time")

// Test the GetUser handler for the second time
err = handler.GetUser(fiberContext)
assert.NoError(t, err)

// Assert that the GetUserRole and GetUser functions were called again (second time) with the expected parameters
assert.True(t, mockAuthWrapper.GetUserRoleFuncCalled, "GetUserRole function of mockWrapper should be called again (second time)")
assert.True(t, mockAuthWrapper.GetUserFuncCalled, "GetUser function of mockWrapper should be called again (second time)")

// Get the cached result for the second time
cachedDataSecondTime, foundSecondTime := util.GetFromCache("GetUser:[email protected]")
assert.True(t, foundSecondTime, "Result should be in cache for the second time")

// Assert that the cached results for the first and second times are the same
assert.Equal(t, cachedDataFirstTime, cachedDataSecondTime, "Cached results for the first and second times should be the same")

// Release the Fiber context
app.ReleaseCtx(fiberContext)
}
Expand Down Expand Up @@ -563,14 +583,33 @@ func TestListUsers(t *testing.T) {
assert.NoError(t, err)
fiberContext.Request().Header.Set("Authorization", "Bearer "+token+"")

// Test the ListUsers handler
// Test the ListUsers handler for the first time
err = handler.ListUsers(fiberContext)
assert.NoError(t, err)

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

// Get the cached result for the first time
cachedDataFirstTime, foundFirstTime := util.GetFromCache("ListUsers")
assert.True(t, foundFirstTime, "Result should be in cache for the first time")

// Test the ListUsers handler for the second time
err = handler.ListUsers(fiberContext)
assert.NoError(t, err)

// Assert that the GetUserRole and ListUsers functions were called again (second time) with the expected parameters
assert.True(t, mockAuthWrapper.GetUserRoleFuncCalled, "GetUserRole function of mockWrapper should be called again (second time)")
assert.True(t, mockAuthWrapper.ListUsersFuncCalled, "ListUsers function of mockWrapper should be called again (second time)")

// Get the cached result for the second time
cachedDataSecondTime, foundSecondTime := util.GetFromCache("ListUsers")
assert.True(t, foundSecondTime, "Result should be in cache for the second time")

// Assert that the cached results for the first and second times are the same
assert.Equal(t, cachedDataFirstTime, cachedDataSecondTime, "Cached results for the first and second times should be the same")

// Release the Fiber context
app.ReleaseCtx(fiberContext)
}
18 changes: 9 additions & 9 deletions api/handlers/vuln.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (h *vulnHandlers) FetchNVDFeeds(c *fiber.Ctx) error {
return util.WriteError(c, http.StatusUnauthorized, util.ErrUnauthorized)
}

apiKey := c.Query("ApiKey")
apiKey := c.Get("ApiKey")

// Cache key creation
cacheKey := "FetchNVDFeeds:" + apiKey
Expand Down Expand Up @@ -221,21 +221,21 @@ func (h *vulnHandlers) FetchNVDFeeds(c *fiber.Ctx) error {
func (h *vulnHandlers) SearchCVE(c *fiber.Ctx) error {
// Cache key creation
cacheKey := fmt.Sprintf("SearchCVE:%s:%s:%s:%s:%s:%s",
c.Query("CveId"), c.Query("Severity"), c.Query("Product"),
c.Query("Vendor"), c.Query("StartDate"), c.Query("EndDate"))
c.Get("CveId"), c.Get("Severity"), c.Get("Product"),
c.Get("Vendor"), c.Get("StartDate"), c.Get("EndDate"))

// Get value from cache
if cachedData, found := util.GetFromCache(cacheKey); found {
return util.WriteAsJSON(c, http.StatusOK, cachedData)
}

searchCVEsRequest := &pb.SearchCVERequest{
CveId: c.Query("CveId"),
Severity: c.Query("Severity"),
Product: c.Query("Product"),
Vendor: c.Query("Vendor"),
StartDate: c.Query("StartDate"),
EndDate: c.Query("EndDate"),
CveId: c.Get("CveId"),
Severity: c.Get("Severity"),
Product: c.Get("Product"),
Vendor: c.Get("Vendor"),
StartDate: c.Get("StartDate"),
EndDate: c.Get("EndDate"),
}

stream, err := h.vulnSvcClient.SearchCVE(c.Context(), searchCVEsRequest)
Expand Down
72 changes: 64 additions & 8 deletions api/handlers/vuln_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/gofiber/fiber/v2"
"github.com/mtnmunuklu/bavul/api/handlers"
"github.com/mtnmunuklu/bavul/api/util"
"github.com/mtnmunuklu/bavul/pb"
"github.com/mtnmunuklu/bavul/security"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -245,14 +246,33 @@ func TestGetAllCVEs(t *testing.T) {
assert.NoError(t, err)
fiberContext.Request().Header.Set("Authorization", "Bearer "+token+"")

// Test the GetAllCVEs handler
// Test the GetAllCVEs handler for the first time
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")

// Get the cached result for the first time
cachedDataFirstTime, foundFirstTime := util.GetFromCache("GetAllCVEs")
assert.True(t, foundFirstTime, "Result should be in cache for the first time")

// Test the GetAllCVEs handler for the second time
err = handler.GetAllCVEs(fiberContext)
assert.NoError(t, err)

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

// Get the cached result for the second time
cachedDataSecondTime, foundSecondTime := util.GetFromCache("GetAllCVEs")
assert.True(t, foundSecondTime, "Result should be in cache for the second time")

// Assert that the cached results for the first and second times are the same
assert.Equal(t, cachedDataFirstTime, cachedDataSecondTime, "Cached results for the first and second times should be the same")

// Release the Fiber context
app.ReleaseCtx(fiberContext)
}
Expand Down Expand Up @@ -362,7 +382,6 @@ func TestUpdateCVE(t *testing.T) {
}

func TestFetchNVDFeeds(t *testing.T) {

// Create a custom mock client wrapper for Auth Service
mockAuthWrapper := &MockAuthServiceClientWrapper{}

Expand Down Expand Up @@ -402,23 +421,40 @@ func TestFetchNVDFeeds(t *testing.T) {
token, err := security.NewToken(userId.Hex())
assert.NoError(t, err)
fiberContext.Request().Header.Set("Authorization", "Bearer "+token+"")
fiberContext.Request().Header.Set("CveId", "123")
fiberContext.Request().Header.Set("ApiKey", "test-api-key")

// Test the FetchNVDFeeds handler
// Test the FetchNVDFeeds handler for the first time
err = handler.FetchNVDFeeds(fiberContext)
assert.NoError(t, err)

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

// Get the cached result for the first time
cachedDataFirstTime, foundFirstTime := util.GetFromCache("FetchNVDFeeds:test-api-key")
assert.True(t, foundFirstTime, "Result should be in cache for the first time")

// Test the FetchNVDFeeds handler for the second time
err = handler.FetchNVDFeeds(fiberContext)
assert.NoError(t, err)

// Assert that the GetUserRole and FetchNVDFeeds functions were called again (second time) with the expected parameters
assert.True(t, mockAuthWrapper.GetUserRoleFuncCalled, "GetUserRole function of mockWrapper should be called again (second time)")
assert.True(t, mockVulnWrapper.FetchNVDFeedsFuncCalled, "FetchNVDFeeds function of mockWrapper should be called again (second time)")

// Get the cached result for the second time
cachedDataSecondTime, foundSecondTime := util.GetFromCache("FetchNVDFeeds:test-api-key")
assert.True(t, foundSecondTime, "Result should be in cache for the second time")

// Assert that the cached results for the first and second times are the same
assert.Equal(t, cachedDataFirstTime, cachedDataSecondTime, "Cached results for the first and second times should be the same")

// Release the Fiber context
app.ReleaseCtx(fiberContext)

}

func TestSearchCVE(t *testing.T) {

// Create a custom mock client wrapper for Auth Service
mockAuthWrapper := &MockAuthServiceClientWrapper{}

Expand Down Expand Up @@ -453,14 +489,34 @@ func TestSearchCVE(t *testing.T) {
assert.NoError(t, err)
fiberContext.Request().Header.Set("Authorization", "Bearer "+token+"")
fiberContext.Request().Header.Set("Severity", "High")
fiberContext.Request().Header.Set("Product", "Test Product 1")
fiberContext.Request().Header.Set("Vendor", "Test Vendor 1")

// Test the SearchCVE handler
// Test the SearchCVE handler for the first time
err = handler.SearchCVE(fiberContext)
assert.NoError(t, err)

// Assert that the SearchCVE functions were called with the expected parameters
// Assert that the SearchCVE function was called with the expected parameters
assert.True(t, mockVulnWrapper.SearchCVEFuncCalled, "SearchCVE function of mockWrapper should be called")

// Get the cached result for the first time
cachedDataFirstTime, foundFirstTime := util.GetFromCache("SearchCVE::High:Test Product 1:Test Vendor 1::")
assert.True(t, foundFirstTime, "Result should be in cache for the first time")

// Test the SearchCVE handler for the second time
err = handler.SearchCVE(fiberContext)
assert.NoError(t, err)

// Assert that the SearchCVE function was called again (second time) with the expected parameters
assert.True(t, mockVulnWrapper.SearchCVEFuncCalled, "SearchCVE function of mockWrapper should be called again (second time)")

// Get the cached result for the second time
cachedDataSecondTime, foundSecondTime := util.GetFromCache("SearchCVE::High:Test Product 1:Test Vendor 1::")
assert.True(t, foundSecondTime, "Result should be in cache for the second time")

// Assert that the cached results for the first and second times are the same
assert.Equal(t, cachedDataFirstTime, cachedDataSecondTime, "Cached results for the first and second times should be the same")

// Release the Fiber context
app.ReleaseCtx(fiberContext)
}

0 comments on commit a7a1006

Please sign in to comment.