From 538be2fed17882a936bdf3da17d6b8eb8ee80e2d Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Mon, 11 Mar 2024 21:36:35 -0700 Subject: [PATCH] Replaced explicit controller handler tests by http request tests --- src/controller/account_test.go | 128 +++++++++++------------------- src/controller/api_test.go | 25 +----- src/controller/controller_test.go | 16 ++++ 3 files changed, 67 insertions(+), 102 deletions(-) diff --git a/src/controller/account_test.go b/src/controller/account_test.go index 6609d0a..cea0024 100644 --- a/src/controller/account_test.go +++ b/src/controller/account_test.go @@ -8,133 +8,106 @@ import ( "net/http/httptest" "testing" - "github.com/gorilla/mux" "github.com/stretchr/testify/assert" "github.com/switcherapi/switcher-gitops/src/model" ) -func TestAccountRegisterRoutes(t *testing.T) { - // Create a router - r := mux.NewRouter() - - // Test - accountController.RegisterRoutes(r) - - // Assert - assert.NotNil(t, r) - assert.NotNil(t, r.GetRoute("GetAccount")) - assert.NotNil(t, r.GetRoute("CreateAccount")) - assert.NotNil(t, r.GetRoute("UpdateAccount")) - assert.NotNil(t, r.GetRoute("DeleteAccount")) -} - func TestCreateAccountHandler(t *testing.T) { - // Create a request and response recorder - w, r := givenAccountRequest(accountV1) - // Test - accountController.CreateAccountHandler(w, r) + payload, _ := json.Marshal(accountV1) + req, _ := http.NewRequest(http.MethodPost, accountController.RouteAccountPath, bytes.NewBuffer(payload)) + response := executeRequest(req) // Assert var accountResponse model.Account - err := json.NewDecoder(w.Body).Decode(&accountResponse) + err := json.NewDecoder(response.Body).Decode(&accountResponse) - assert.Equal(t, http.StatusCreated, w.Code) + assert.Equal(t, http.StatusCreated, response.Code) assert.Nil(t, err) assert.Equal(t, accountV1.Repository, accountResponse.Repository) } func TestCreateAccountHandlerInvalidRequest(t *testing.T) { - // Create a request and response recorder - w := httptest.NewRecorder() - r := httptest.NewRequest(http.MethodPost, accountController.RouteAccountPath, nil) - // Test - accountController.CreateAccountHandler(w, r) + payload := []byte("") + req, _ := http.NewRequest(http.MethodPost, accountController.RouteAccountPath, bytes.NewBuffer(payload)) + response := executeRequest(req) // Assert - assert.Equal(t, http.StatusBadRequest, w.Code) - assert.Equal(t, "{\"error\":\"Invalid request\"}", w.Body.String()) + assert.Equal(t, http.StatusBadRequest, response.Code) + assert.Equal(t, "{\"error\":\"Invalid request\"}", response.Body.String()) } func TestCreateAccountHandlerErrorCreatingAccount(t *testing.T) { // Create an account accountController.CreateAccountHandler(givenAccountRequest(accountV1)) - // Create a request and response recorder - w, r := givenAccountRequest(accountV1) - // Test - accountController.CreateAccountHandler(w, r) + payload, _ := json.Marshal(accountV1) + req, _ := http.NewRequest(http.MethodPost, accountController.RouteAccountPath, bytes.NewBuffer(payload)) + response := executeRequest(req) // Assert - assert.Equal(t, http.StatusInternalServerError, w.Code) - assert.Equal(t, "{\"error\":\"Error creating account\"}", w.Body.String()) + assert.Equal(t, http.StatusInternalServerError, response.Code) + assert.Equal(t, "{\"error\":\"Error creating account\"}", response.Body.String()) } func TestFetchAccountHandlerByDomainId(t *testing.T) { // Create an account accountController.CreateAccountHandler(givenAccountRequest(accountV1)) - // Create a request and response recorder - w := httptest.NewRecorder() - r := httptest.NewRequest(http.MethodGet, accountController.RouteAccountPath+"/123", nil) - // Test - accountController.FetchAccountHandler(w, r) + payload := []byte("") + req, _ := http.NewRequest(http.MethodGet, accountController.RouteAccountPath+"/123", bytes.NewBuffer(payload)) + response := executeRequest(req) // Assert var accountResponse model.Account - err := json.NewDecoder(w.Body).Decode(&accountResponse) + err := json.NewDecoder(response.Body).Decode(&accountResponse) - assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, http.StatusOK, response.Code) assert.Nil(t, err) assert.Equal(t, accountV1.Repository, accountResponse.Repository) } func TestFetchAccountHandlerByDomainIdNotFound(t *testing.T) { - // Create a request and response recorder - w := httptest.NewRecorder() - r := httptest.NewRequest(http.MethodGet, accountController.RouteAccountPath+"/111", nil) - // Test - accountController.FetchAccountHandler(w, r) + payload := []byte("") + req, _ := http.NewRequest(http.MethodGet, accountController.RouteAccountPath+"/111", bytes.NewBuffer(payload)) + response := executeRequest(req) // Assert - assert.Equal(t, http.StatusNotFound, w.Code) - assert.Equal(t, "{\"error\":\"Account not found\"}", w.Body.String()) + assert.Equal(t, http.StatusNotFound, response.Code) + assert.Equal(t, "{\"error\":\"Account not found\"}", response.Body.String()) } func TestUpdateAccountHandler(t *testing.T) { // Create an account accountController.CreateAccountHandler(givenAccountRequest(accountV1)) - // Create a request and response recorder - w, r := givenAccountRequest(accountV2) - // Test - accountController.UpdateAccountHandler(w, r) + payload, _ := json.Marshal(accountV2) + req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath+"/123", bytes.NewBuffer(payload)) + response := executeRequest(req) // Assert var accountResponse model.Account - err := json.NewDecoder(w.Body).Decode(&accountResponse) + err := json.NewDecoder(response.Body).Decode(&accountResponse) - assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, http.StatusOK, response.Code) assert.Nil(t, err) assert.Equal(t, accountV2.Repository, accountResponse.Repository) } func TestUpdateAccountHandlerInvalidRequest(t *testing.T) { - // Create a request and response recorder - w := httptest.NewRecorder() - r := httptest.NewRequest(http.MethodPut, accountController.RouteAccountPath, nil) - // Test - accountController.UpdateAccountHandler(w, r) + payload := []byte("") + req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath+"/123", bytes.NewBuffer(payload)) + response := executeRequest(req) // Assert - assert.Equal(t, http.StatusBadRequest, w.Code) - assert.Equal(t, "{\"error\":\"Invalid request\"}", w.Body.String()) + assert.Equal(t, http.StatusBadRequest, response.Code) + assert.Equal(t, "{\"error\":\"Invalid request\"}", response.Body.String()) } func TestUpdateAccountHandlerErrorUpdatingAccount(t *testing.T) { @@ -145,43 +118,36 @@ func TestUpdateAccountHandlerErrorUpdatingAccount(t *testing.T) { // Replace the domain ID to force an error accountV1Copy.Domain.ID = "111" - // Create a request and response recorder - w, r := givenAccountRequest(accountV1Copy) - // Test - accountController.UpdateAccountHandler(w, r) + payload, _ := json.Marshal(accountV1Copy) + req, _ := http.NewRequest(http.MethodPut, accountController.RouteAccountPath+"/123", bytes.NewBuffer(payload)) + response := executeRequest(req) // Assert - assert.Equal(t, http.StatusInternalServerError, w.Code) - assert.Equal(t, "{\"error\":\"Error updating account\"}", w.Body.String()) + assert.Equal(t, http.StatusInternalServerError, response.Code) + assert.Equal(t, "{\"error\":\"Error updating account\"}", response.Body.String()) } func TestDeleteAccountHandler(t *testing.T) { // Create an account accountController.CreateAccountHandler(givenAccountRequest(accountV1)) - // Create a request and response recorder - w := httptest.NewRecorder() - r := httptest.NewRequest(http.MethodDelete, accountController.RouteAccountPath+"/123", nil) - // Test - accountController.DeleteAccountHandler(w, r) + req, _ := http.NewRequest(http.MethodDelete, accountController.RouteAccountPath+"/123", nil) + response := executeRequest(req) // Assert - assert.Equal(t, http.StatusNoContent, w.Code) + assert.Equal(t, http.StatusNoContent, response.Code) } func TestDeleteAccountHandlerNotFound(t *testing.T) { - // Create a request and response recorder - w := httptest.NewRecorder() - r := httptest.NewRequest(http.MethodDelete, accountController.RouteAccountPath+"/111", nil) - // Test - accountController.DeleteAccountHandler(w, r) + req, _ := http.NewRequest(http.MethodDelete, accountController.RouteAccountPath+"/111", nil) + response := executeRequest(req) // Assert - assert.Equal(t, http.StatusInternalServerError, w.Code) - assert.Equal(t, "{\"error\":\"Error deleting account: Account not found for domain.id: 111\"}", w.Body.String()) + assert.Equal(t, http.StatusInternalServerError, response.Code) + assert.Equal(t, "{\"error\":\"Error deleting account: Account not found for domain.id: 111\"}", response.Body.String()) } // Helpers diff --git a/src/controller/api_test.go b/src/controller/api_test.go index 467bf7a..6984e87 100644 --- a/src/controller/api_test.go +++ b/src/controller/api_test.go @@ -2,32 +2,15 @@ package controller import ( "net/http" - "net/http/httptest" "testing" - "github.com/gorilla/mux" "github.com/stretchr/testify/assert" ) -func TestApiRegisterRoutes(t *testing.T) { - r := mux.NewRouter() - apiController.RegisterRoutes(r) - - assert.NotNil(t, r) - assert.NotNil(t, r.GetRoute("CheckApi")) -} - func TestCheckApiHandler(t *testing.T) { - w, r := givenApiRequest() - apiController.CheckApiHandler(w, r) - - assert.Equal(t, http.StatusOK, w.Code) - assert.Equal(t, `{"message":"API is working"}`, w.Body.String()) -} - -func givenApiRequest() (*httptest.ResponseRecorder, *http.Request) { - w := httptest.NewRecorder() - r := httptest.NewRequest("GET", "/api/check", nil) + req, _ := http.NewRequest(http.MethodGet, "/api/check", nil) + response := executeRequest(req) - return w, r + assert.Equal(t, http.StatusOK, response.Code) + assert.Equal(t, "{\"message\":\"API is working\"}", response.Body.String()) } diff --git a/src/controller/controller_test.go b/src/controller/controller_test.go index ce98aa3..d0ff2bd 100644 --- a/src/controller/controller_test.go +++ b/src/controller/controller_test.go @@ -2,9 +2,12 @@ package controller import ( "context" + "net/http" + "net/http/httptest" "os" "testing" + "github.com/gorilla/mux" "github.com/switcherapi/switcher-gitops/src/config" "github.com/switcherapi/switcher-gitops/src/db" "github.com/switcherapi/switcher-gitops/src/model" @@ -15,6 +18,7 @@ import ( var mongoDb *mongo.Database var accountController *AccountController var apiController *ApiController +var r *mux.Router func TestMain(m *testing.M) { setup() @@ -28,10 +32,15 @@ func setup() { config.InitEnv() mongoDb = db.InitDb() + // Init Routes accountRepository := repository.NewAccountRepositoryMongo(mongoDb) apiController = NewApiController() accountController = NewAccountController(accountRepository) + + r = mux.NewRouter() + apiController.RegisterRoutes(r) + accountController.RegisterRoutes(r) } func shutdown() { @@ -39,6 +48,13 @@ func shutdown() { mongoDb.Client().Disconnect(context.Background()) } +func executeRequest(req *http.Request) *httptest.ResponseRecorder { + rr := httptest.NewRecorder() + r.ServeHTTP(rr, req) + + return rr +} + // Fixtures var accountV1 = model.Account{