From a7a1006b3eed423355adacf157c521ea46652aa0 Mon Sep 17 00:00:00 2001 From: mtnmunuklu Date: Sun, 11 Feb 2024 00:02:47 +0300 Subject: [PATCH] Add caching tests for GetAllCVEs, FetchNVDFeeds, SearchCVE, GetUser, and ListUsers --- api/handlers/auth.go | 6 ++-- api/handlers/auth_test.go | 47 ++++++++++++++++++++++--- api/handlers/vuln.go | 18 +++++----- api/handlers/vuln_test.go | 72 ++++++++++++++++++++++++++++++++++----- 4 files changed, 119 insertions(+), 24 deletions(-) diff --git a/api/handlers/auth.go b/api/handlers/auth.go index d23a3da..9ea1777 100644 --- a/api/handlers/auth.go +++ b/api/handlers/auth.go @@ -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) @@ -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) diff --git a/api/handlers/auth_test.go b/api/handlers/auth_test.go index 10f12d2..df1c18b 100644 --- a/api/handlers/auth_test.go +++ b/api/handlers/auth_test.go @@ -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" @@ -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 } @@ -298,7 +299,7 @@ func TestGetUser(t *testing.T) { fiberContext.Request().Header.Set("Authorization", "Bearer "+token+"") fiberContext.Request().Header.Set("Email", "testemail1@test.com.tr") - // Test the GetUser handler + // Test the GetUser handler for the first time err = handler.GetUser(fiberContext) assert.NoError(t, err) @@ -306,6 +307,25 @@ func TestGetUser(t *testing.T) { 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:testemail1@test.com.tr") + 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:testemail1@test.com.tr") + 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) } @@ -563,7 +583,7 @@ 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) @@ -571,6 +591,25 @@ func TestListUsers(t *testing.T) { 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) } diff --git a/api/handlers/vuln.go b/api/handlers/vuln.go index 205fa40..54d402b 100644 --- a/api/handlers/vuln.go +++ b/api/handlers/vuln.go @@ -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 @@ -221,8 +221,8 @@ 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 { @@ -230,12 +230,12 @@ func (h *vulnHandlers) SearchCVE(c *fiber.Ctx) error { } 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) diff --git a/api/handlers/vuln_test.go b/api/handlers/vuln_test.go index 5dbdb3d..b0d9c29 100644 --- a/api/handlers/vuln_test.go +++ b/api/handlers/vuln_test.go @@ -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" @@ -245,7 +246,7 @@ 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) @@ -253,6 +254,25 @@ func TestGetAllCVEs(t *testing.T) { 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) } @@ -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{} @@ -402,9 +421,9 @@ 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) @@ -412,13 +431,30 @@ func TestFetchNVDFeeds(t *testing.T) { 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{} @@ -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) }