Skip to content

Commit cf38ce3

Browse files
Add. Test cases for login api of auth handler
1 parent adda49f commit cf38ce3

File tree

2 files changed

+128
-1
lines changed

2 files changed

+128
-1
lines changed

.talismanrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ fileignoreconfig:
1818
- filename: services/userService_test.go
1919
checksum: 0a33b7d59b18068b08834b502de2331cfdf1d2345d01ac30544ab0cd265e332e
2020
- filename: handlers/authHandler_test.go
21-
checksum: 453b1fcce9c5b0e26e40b43dd7e663a3ea9f54a52bbc669d46796340a83b4f2a
21+
checksum: f5316497149e9912225ff9ee24bae463b746e552edd3830710a5d9371ce970e0
2222
version: ""

handlers/authHandler_test.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,130 @@ func TestAuthHandler_SignupHandler(t *testing.T) {
150150
mockUserService.AssertExpectations(t)
151151
})
152152
}
153+
154+
func TestAuthHandler_LoginHandler(t *testing.T) {
155+
userServiceError := errors.New("error from user service")
156+
authServiceError := errors.New("error from auth service")
157+
userMock := &models.User{
158+
ID: 1,
159+
FirstName: "test_first_name",
160+
LastName: "test_last_name",
161+
Username: "test_username",
162+
Password: "test_password",
163+
Role: "test_role",
164+
Email: "test_email@gmail.com",
165+
}
166+
loginRequest := &requests.LoginRequest{
167+
Username: userMock.Username,
168+
Password: userMock.Password,
169+
}
170+
171+
t.Run("should be able to login", func(t *testing.T) {
172+
mockUserService := new(mocks.UserService)
173+
mockUserService.On("Login", loginRequest).Return(userMock, nil)
174+
mockAuthService := new(mocks.AuthService)
175+
mockAuthService.On("SaveTokensByUsername", userMock.Username, mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(nil)
176+
177+
at, rt, _ := helpers.GenerateToken(userMock.Username, userMock.Role)
178+
179+
ah := NewAuthHandler(mockUserService, mockAuthService)
180+
router := gin.Default()
181+
router.POST(constants.LoginUserEndpoint, ah.LoginHandler)
182+
183+
res := httptest.NewRecorder()
184+
body, _ := json.Marshal(loginRequest)
185+
req, _ := http.NewRequest("POST", constants.LoginUserEndpoint, bytes.NewBuffer(body))
186+
router.ServeHTTP(res, req)
187+
188+
var resBody responses.SuccessResponse
189+
_ = json.Unmarshal(res.Body.Bytes(), &resBody)
190+
191+
assert.Equal(t, constants.Success, resBody.Status)
192+
assert.Equal(t, http.StatusText(http.StatusOK), resBody.Code)
193+
assert.Equal(t, "successfully logged in", resBody.Message)
194+
assert.Equal(t, nil, resBody.Data)
195+
assert.Equal(t, constants.AccessTokenCookie, res.Result().Cookies()[0].Name)
196+
assert.Equal(t, at, res.Result().Cookies()[0].Value)
197+
assert.Equal(t, constants.RefreshTokenCookie, res.Result().Cookies()[1].Name)
198+
assert.Equal(t, rt, res.Result().Cookies()[1].Value)
199+
mockAuthService.AssertExpectations(t)
200+
mockUserService.AssertExpectations(t)
201+
})
202+
203+
t.Run("should not be able to login when username is not provided", func(t *testing.T) {
204+
invalidLoginReq := &requests.LoginRequest{
205+
Password: userMock.Password,
206+
}
207+
mockUserService := new(mocks.UserService)
208+
mockAuthService := new(mocks.AuthService)
209+
210+
ah := NewAuthHandler(mockUserService, mockAuthService)
211+
router := gin.Default()
212+
router.POST(constants.LoginUserEndpoint, ah.LoginHandler)
213+
214+
res := httptest.NewRecorder()
215+
body, _ := json.Marshal(invalidLoginReq)
216+
req, _ := http.NewRequest("POST", constants.LoginUserEndpoint, bytes.NewBuffer(body))
217+
router.ServeHTTP(res, req)
218+
219+
var resBody responses.SuccessResponse
220+
_ = json.Unmarshal(res.Body.Bytes(), &resBody)
221+
222+
assert.Equal(t, constants.Error, resBody.Status)
223+
assert.Equal(t, http.StatusText(http.StatusBadRequest), resBody.Code)
224+
assert.Equal(t, nil, resBody.Data)
225+
mockAuthService.AssertExpectations(t)
226+
mockUserService.AssertExpectations(t)
227+
})
228+
229+
t.Run("should not be able to login when there is error from user service", func(t *testing.T) {
230+
mockUserService := new(mocks.UserService)
231+
mockAuthService := new(mocks.AuthService)
232+
mockUserService.On("Login", loginRequest).Return(nil, userServiceError)
233+
234+
ah := NewAuthHandler(mockUserService, mockAuthService)
235+
router := gin.Default()
236+
router.POST(constants.LoginUserEndpoint, ah.LoginHandler)
237+
238+
res := httptest.NewRecorder()
239+
body, _ := json.Marshal(loginRequest)
240+
req, _ := http.NewRequest("POST", constants.LoginUserEndpoint, bytes.NewBuffer(body))
241+
router.ServeHTTP(res, req)
242+
243+
var resBody responses.SuccessResponse
244+
_ = json.Unmarshal(res.Body.Bytes(), &resBody)
245+
246+
assert.Equal(t, constants.Error, resBody.Status)
247+
assert.Equal(t, http.StatusText(http.StatusInternalServerError), resBody.Code)
248+
assert.Equal(t, userServiceError.Error(), resBody.Message)
249+
assert.Equal(t, nil, resBody.Data)
250+
mockAuthService.AssertExpectations(t)
251+
mockUserService.AssertExpectations(t)
252+
})
253+
254+
t.Run("should not be able to login when there is error from auth service", func(t *testing.T) {
255+
mockUserService := new(mocks.UserService)
256+
mockAuthService := new(mocks.AuthService)
257+
mockUserService.On("Login", loginRequest).Return(userMock, nil)
258+
mockAuthService.On("SaveTokensByUsername", userMock.Username, mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(authServiceError)
259+
260+
ah := NewAuthHandler(mockUserService, mockAuthService)
261+
router := gin.Default()
262+
router.POST(constants.LoginUserEndpoint, ah.LoginHandler)
263+
264+
res := httptest.NewRecorder()
265+
body, _ := json.Marshal(loginRequest)
266+
req, _ := http.NewRequest("POST", constants.LoginUserEndpoint, bytes.NewBuffer(body))
267+
router.ServeHTTP(res, req)
268+
269+
var resBody responses.SuccessResponse
270+
_ = json.Unmarshal(res.Body.Bytes(), &resBody)
271+
272+
assert.Equal(t, constants.Error, resBody.Status)
273+
assert.Equal(t, http.StatusText(http.StatusInternalServerError), resBody.Code)
274+
assert.Equal(t, authServiceError.Error(), resBody.Message)
275+
assert.Equal(t, nil, resBody.Data)
276+
mockAuthService.AssertExpectations(t)
277+
mockUserService.AssertExpectations(t)
278+
})
279+
}

0 commit comments

Comments
 (0)