|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/dieg0code/shared/json/request" |
| 12 | + "github.com/dieg0code/shared/json/response" |
| 13 | + "github.com/dieg0code/shared/mocks" |
| 14 | + "github.com/gin-gonic/gin" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/mock" |
| 17 | +) |
| 18 | + |
| 19 | +func TestLogInUser(t *testing.T) { |
| 20 | + t.Run("LogInUser_Success", func(t *testing.T) { |
| 21 | + userService := new(mocks.MockUserService) |
| 22 | + userController := NewUserControllerImpl(userService) |
| 23 | + |
| 24 | + gin.SetMode(gin.TestMode) |
| 25 | + |
| 26 | + router := gin.Default() |
| 27 | + |
| 28 | + router.POST("/login", userController.LogInUser) |
| 29 | + |
| 30 | + userService.On("LogInUser", mock.Anything).Return(response.LogInUserResponse{ |
| 31 | + Token: "token", |
| 32 | + }, nil) |
| 33 | + |
| 34 | + loginRequest := request.LogInUserRequest{ |
| 35 | + Email: "test@test.com", |
| 36 | + Password: "password", |
| 37 | + } |
| 38 | + |
| 39 | + reqBody, err := json.Marshal(loginRequest) |
| 40 | + assert.NoError(t, err, "Expected no error marshalling request body") |
| 41 | + |
| 42 | + req, err := http.NewRequest(http.MethodPost, "/login", bytes.NewBuffer(reqBody)) |
| 43 | + assert.NoError(t, err, "Expected no error creating request") |
| 44 | + |
| 45 | + rec := httptest.NewRecorder() |
| 46 | + router.ServeHTTP(rec, req) |
| 47 | + |
| 48 | + assert.Equal(t, http.StatusOK, rec.Code, "Expected status code 200") |
| 49 | + |
| 50 | + var response response.BaseResponse |
| 51 | + err = json.Unmarshal(rec.Body.Bytes(), &response) |
| 52 | + assert.NoError(t, err, "Expected no error unmarshalling response body") |
| 53 | + assert.Equal(t, "success", response.Status, "Expected response status to be success") |
| 54 | + assert.Equal(t, "User logged in successfully", response.Message, "Expected response message to be 'User logged in successfully'") |
| 55 | + assert.Equal(t, "Bearer token", rec.Header().Get("Authorization"), "Expected Authorization header to be 'Bearer token'") |
| 56 | + assert.Equal(t, "token", response.Data.(map[string]interface{})["token"], "Expected response data token to be 'token'") |
| 57 | + userService.AssertExpectations(t) |
| 58 | + }) |
| 59 | + |
| 60 | + t.Run("LogInUser_ErrorBindingJSON", func(t *testing.T) { |
| 61 | + userService := new(mocks.MockUserService) |
| 62 | + userController := NewUserControllerImpl(userService) |
| 63 | + |
| 64 | + gin.SetMode(gin.TestMode) |
| 65 | + |
| 66 | + router := gin.Default() |
| 67 | + |
| 68 | + router.POST("/login", userController.LogInUser) |
| 69 | + |
| 70 | + req, err := http.NewRequest(http.MethodPost, "/login", bytes.NewBuffer([]byte(""))) |
| 71 | + assert.NoError(t, err, "Expected no error creating request") |
| 72 | + |
| 73 | + rec := httptest.NewRecorder() |
| 74 | + router.ServeHTTP(rec, req) |
| 75 | + |
| 76 | + assert.Equal(t, http.StatusBadRequest, rec.Code, "Expected status code 400") |
| 77 | + |
| 78 | + var response response.BaseResponse |
| 79 | + err = json.Unmarshal(rec.Body.Bytes(), &response) |
| 80 | + assert.NoError(t, err, "Expected no error unmarshalling response body") |
| 81 | + assert.Equal(t, "error", response.Status, "Expected response status to be error") |
| 82 | + assert.Equal(t, "Invalid request body", response.Message, "Expected response message to be 'Invalid request body'") |
| 83 | + |
| 84 | + userService.AssertExpectations(t) |
| 85 | + }) |
| 86 | + |
| 87 | + t.Run("LogInUser_ErrorLoggingInUser", func(t *testing.T) { |
| 88 | + userService := new(mocks.MockUserService) |
| 89 | + userController := NewUserControllerImpl(userService) |
| 90 | + |
| 91 | + gin.SetMode(gin.TestMode) |
| 92 | + |
| 93 | + router := gin.Default() |
| 94 | + |
| 95 | + router.POST("/login", userController.LogInUser) |
| 96 | + |
| 97 | + userService.On("LogInUser", mock.Anything).Return(response.LogInUserResponse{}, assert.AnError) |
| 98 | + |
| 99 | + loginRequest := request.LogInUserRequest{ |
| 100 | + Email: "test@test.com", |
| 101 | + Password: "password", |
| 102 | + } |
| 103 | + |
| 104 | + reqBody, err := json.Marshal(loginRequest) |
| 105 | + assert.NoError(t, err, "Expected no error marshalling request body") |
| 106 | + |
| 107 | + req, err := http.NewRequest(http.MethodPost, "/login", bytes.NewBuffer(reqBody)) |
| 108 | + assert.NoError(t, err, "Expected no error creating request") |
| 109 | + |
| 110 | + rec := httptest.NewRecorder() |
| 111 | + router.ServeHTTP(rec, req) |
| 112 | + |
| 113 | + assert.Equal(t, http.StatusInternalServerError, rec.Code, "Expected status code 500") |
| 114 | + |
| 115 | + var response response.BaseResponse |
| 116 | + err = json.Unmarshal(rec.Body.Bytes(), &response) |
| 117 | + assert.NoError(t, err, "Expected no error unmarshalling response body") |
| 118 | + assert.Equal(t, "error", response.Status, "Expected response status to be error") |
| 119 | + assert.Equal(t, "Error logging in user", response.Message, "Expected response message to be 'Error logging in user'") |
| 120 | + |
| 121 | + userService.AssertExpectations(t) |
| 122 | + }) |
| 123 | +} |
| 124 | + |
| 125 | +func TestGetAllUsers(t *testing.T) { |
| 126 | + t.Run("GetAllUsers_Success", func(t *testing.T) { |
| 127 | + userService := new(mocks.MockUserService) |
| 128 | + userController := NewUserControllerImpl(userService) |
| 129 | + |
| 130 | + gin.SetMode(gin.TestMode) |
| 131 | + |
| 132 | + router := gin.Default() |
| 133 | + |
| 134 | + router.GET("/users", userController.GetAllUsers) |
| 135 | + |
| 136 | + userService.On("GetAllUsers").Return([]response.UserResponse{ |
| 137 | + { |
| 138 | + UserID: "uuid", |
| 139 | + Email: "test@test.com", |
| 140 | + Username: "test", |
| 141 | + }, |
| 142 | + { |
| 143 | + UserID: "uuid2", |
| 144 | + Email: "test2@test.com", |
| 145 | + Username: "test2", |
| 146 | + }, |
| 147 | + }, nil) |
| 148 | + |
| 149 | + req, err := http.NewRequest(http.MethodGet, "/users", nil) |
| 150 | + assert.NoError(t, err, "Expected no error creating request") |
| 151 | + |
| 152 | + rec := httptest.NewRecorder() |
| 153 | + router.ServeHTTP(rec, req) |
| 154 | + |
| 155 | + assert.Equal(t, http.StatusOK, rec.Code, "Expected status code 200") |
| 156 | + |
| 157 | + var response response.BaseResponse |
| 158 | + err = json.Unmarshal(rec.Body.Bytes(), &response) |
| 159 | + assert.NoError(t, err, "Expected no error unmarshalling response body") |
| 160 | + assert.Equal(t, "success", response.Status, "Expected response status to be success") |
| 161 | + assert.Equal(t, "Success getting all users", response.Message, "Expected response message to be 'Success getting all users'") |
| 162 | + assert.Equal(t, 2, len(response.Data.([]interface{})), "Expected response data to have 2 users") |
| 163 | + |
| 164 | + userService.AssertExpectations(t) |
| 165 | + |
| 166 | + }) |
| 167 | + |
| 168 | + t.Run("GetAllUsers_ErrorGettingAllUsers", func(t *testing.T) { |
| 169 | + userService := new(mocks.MockUserService) |
| 170 | + userController := NewUserControllerImpl(userService) |
| 171 | + |
| 172 | + gin.SetMode(gin.TestMode) |
| 173 | + |
| 174 | + router := gin.Default() |
| 175 | + |
| 176 | + router.GET("/users", userController.GetAllUsers) |
| 177 | + |
| 178 | + userService.On("GetAllUsers").Return([]response.UserResponse{}, assert.AnError) |
| 179 | + |
| 180 | + req, err := http.NewRequest(http.MethodGet, "/users", nil) |
| 181 | + assert.NoError(t, err, "Expected no error creating request") |
| 182 | + |
| 183 | + rec := httptest.NewRecorder() |
| 184 | + router.ServeHTTP(rec, req) |
| 185 | + |
| 186 | + assert.Equal(t, http.StatusInternalServerError, rec.Code, "Expected status code 500") |
| 187 | + |
| 188 | + var response response.BaseResponse |
| 189 | + err = json.Unmarshal(rec.Body.Bytes(), &response) |
| 190 | + assert.NoError(t, err, "Expected no error unmarshalling response body") |
| 191 | + assert.Equal(t, "error", response.Status, "Expected response status to be error") |
| 192 | + assert.Equal(t, "Error getting all users", response.Message, "Expected response message to be 'Error getting all users'") |
| 193 | + |
| 194 | + userService.AssertExpectations(t) |
| 195 | + }) |
| 196 | + |
| 197 | +} |
| 198 | + |
| 199 | +func TestGetUserByID(t *testing.T) { |
| 200 | + t.Run("GetUserByID_Success", func(t *testing.T) { |
| 201 | + userService := new(mocks.MockUserService) |
| 202 | + userController := NewUserControllerImpl(userService) |
| 203 | + |
| 204 | + gin.SetMode(gin.TestMode) |
| 205 | + |
| 206 | + router := gin.Default() |
| 207 | + |
| 208 | + router.GET("/users/:userID", userController.GetUserByID) |
| 209 | + |
| 210 | + userID := "uuid" |
| 211 | + |
| 212 | + userService.On("GetUserByID", userID).Return(response.UserResponse{ |
| 213 | + UserID: "uuid", |
| 214 | + Username: "test", |
| 215 | + Email: "test@test.com", |
| 216 | + }, nil) |
| 217 | + |
| 218 | + req, err := http.NewRequest(http.MethodGet, "/users/uuid", nil) |
| 219 | + assert.NoError(t, err, "Expected no error creating request") |
| 220 | + |
| 221 | + rec := httptest.NewRecorder() |
| 222 | + router.ServeHTTP(rec, req) |
| 223 | + |
| 224 | + assert.Equal(t, http.StatusOK, rec.Code, "Status code shoud be 200") |
| 225 | + |
| 226 | + var response response.BaseResponse |
| 227 | + err = json.Unmarshal(rec.Body.Bytes(), &response) |
| 228 | + assert.NoError(t, err, "Expected no error unmarshalling response body") |
| 229 | + assert.Equal(t, "success", response.Status, "Expected response status to be 'success'") |
| 230 | + |
| 231 | + userService.AssertExpectations(t) |
| 232 | + |
| 233 | + }) |
| 234 | + |
| 235 | + t.Run("GetUserByID_Error", func(t *testing.T) { |
| 236 | + userService := new(mocks.MockUserService) |
| 237 | + userController := NewUserControllerImpl(userService) |
| 238 | + |
| 239 | + gin.SetMode(gin.TestMode) |
| 240 | + |
| 241 | + router := gin.Default() |
| 242 | + |
| 243 | + router.GET("/users/:userID", userController.GetUserByID) |
| 244 | + |
| 245 | + userID := "invalid-id" |
| 246 | + |
| 247 | + userService.On("GetUserByID", userID).Return(response.UserResponse{}, errors.New("error getting user")) |
| 248 | + |
| 249 | + req, err := http.NewRequest(http.MethodGet, "/users/invalid-id", nil) |
| 250 | + assert.NoError(t, err, "Expected no error creating request") |
| 251 | + |
| 252 | + rec := httptest.NewRecorder() |
| 253 | + router.ServeHTTP(rec, req) |
| 254 | + |
| 255 | + assert.Equal(t, http.StatusInternalServerError, rec.Code, "Status code shoud be 500") |
| 256 | + |
| 257 | + var response response.BaseResponse |
| 258 | + err = json.Unmarshal(rec.Body.Bytes(), &response) |
| 259 | + assert.NoError(t, err, "Expected no error unmarshalling response body") |
| 260 | + assert.Equal(t, "error", response.Status, "Expected response status to be 'error'") |
| 261 | + |
| 262 | + userService.AssertExpectations(t) |
| 263 | + }) |
| 264 | +} |
0 commit comments