Skip to content

Commit

Permalink
Update handlers test and increase test coverage of auth module
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicatarra committed Dec 16, 2023
1 parent 6b4223c commit 558517b
Showing 1 changed file with 77 additions and 4 deletions.
81 changes: 77 additions & 4 deletions ms/auth/internal/infrastructure/http/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestResource_Create(t *testing.T) {

// Mock CreateUseCase and GetByEmailUseCase
mockApp.On("CreateUseCase", expectedInput, mock.AnythingOfType("string")).Return(expectedUser, nil)
mockApp.On("GetByEmailUseCase", "johndoe@example.com").Return(nil, errors.New("record not found"))
mockApp.On("GetByEmailUseCase", "johndoe@example.com").Return(nil, domain.ErrRecordNotFound)

// Act
res.createUser(resRec, req)
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestResource_Create(t *testing.T) {

// Mock CreateUseCase and GetByEmailUseCase
mockApp.On("CreateUseCase", expectedInput, mock.AnythingOfType("string")).Return(expectedUser, nil)
mockApp.On("GetByEmailUseCase", "johndoe@example.com").Return(nil, errors.New("record not found"))
mockApp.On("GetByEmailUseCase", "johndoe@example.com").Return(nil, domain.ErrRecordNotFound)

// Act
res.createUser(resRec, req)
Expand Down Expand Up @@ -182,7 +182,7 @@ func TestResource_Create(t *testing.T) {

// Mock CreateUseCase and GetByEmailUseCase
mockApp.On("CreateUseCase", expectedInput, mock.AnythingOfType("string")).Return(expectedUser, nil)
mockApp.On("GetByEmailUseCase", "johndoe@example.com").Return(nil, errors.New("record not found"))
mockApp.On("GetByEmailUseCase", "johndoe@example.com").Return(nil, domain.ErrRecordNotFound)

// Act
res.createUser(resRec, req)
Expand All @@ -191,6 +191,58 @@ func TestResource_Create(t *testing.T) {
assertStatusCode(t, resRec, http.StatusBadRequest)
})

t.Run("error - CreateUseCase return ErrDuplicateEmail error", func(t *testing.T) {
// Arrange
// Arrange
mockApp, res := setupRouterAndMocks()
expectedInput := &domain.CreateUserRequest{
Name: "John Doe",
Email: "johndoe@example.com",
Password: "password123",
}

requestBody := createRequestBody()
req := httptest.NewRequest(http.MethodPost, "/v1/users", bytes.NewBuffer(requestBody))
req.Header.Set("Content-Type", "application/json")
resRec := httptest.NewRecorder()

// Mock CreateUseCase and GetByEmailUseCase
mockApp.On("CreateUseCase", expectedInput, mock.AnythingOfType("string")).Return(nil, domain.ErrDuplicateEmail)
mockApp.On("GetByEmailUseCase", "johndoe@example.com").Return(nil, domain.ErrRecordNotFound)

// Act
res.createUser(resRec, req)

// Assert
assertStatusCode(t, resRec, http.StatusUnprocessableEntity)
})

t.Run("error - CreateUseCase return error", func(t *testing.T) {
// Arrange
// Arrange
mockApp, res := setupRouterAndMocks()
expectedInput := &domain.CreateUserRequest{
Name: "John Doe",
Email: "johndoe@example.com",
Password: "password123",
}

requestBody := createRequestBody()
req := httptest.NewRequest(http.MethodPost, "/v1/users", bytes.NewBuffer(requestBody))
req.Header.Set("Content-Type", "application/json")
resRec := httptest.NewRecorder()

// Mock CreateUseCase and GetByEmailUseCase
mockApp.On("CreateUseCase", expectedInput, mock.AnythingOfType("string")).Return(nil, errors.New("error"))
mockApp.On("GetByEmailUseCase", "johndoe@example.com").Return(nil, domain.ErrRecordNotFound)

// Act
res.createUser(resRec, req)

// Assert
assertStatusCode(t, resRec, http.StatusInternalServerError)
})

t.Run("error", func(t *testing.T) {
// Arrange
mockApp, res := setupRouterAndMocks()
Expand All @@ -208,7 +260,7 @@ func TestResource_Create(t *testing.T) {

// Mock CreateUseCase and GetByEmailUseCase
mockApp.On("CreateUseCase", expectedInput, mock.AnythingOfType("string")).Return(nil, expectedErr)
mockApp.On("GetByEmailUseCase", "johndoe@example.com").Return(nil, errors.New("record not found"))
mockApp.On("GetByEmailUseCase", "johndoe@example.com").Return(nil, domain.ErrRecordNotFound)

// Act
res.createUser(resRec, req)
Expand Down Expand Up @@ -260,6 +312,27 @@ func TestResource_Activate(t *testing.T) {
mockApp.AssertCalled(t, "ActivateUseCase", expectedInput.TokenPlaintext)
})

t.Run("error - validate token error", func(t *testing.T) {
// Arrange
mockApp, res := setupRouterAndMocks()
expectedInput := domain.ActivateUserRequest{
TokenPlaintext: "",
}

req := httptest.NewRequest(http.MethodPut, "/v1/users/activate/?token=", nil)
resRec := httptest.NewRecorder()
expectedErr := errors.New("The server encountered a problem and could not process your request")

// Mock ActivateUseCase
mockApp.On("ActivateUseCase", expectedInput.TokenPlaintext).Return(nil, expectedErr)

// Act
res.activateUser(resRec, req)

// Assert
assertStatusCode(t, resRec, http.StatusUnprocessableEntity)
})

t.Run("error", func(t *testing.T) {
// Arrange
mockApp, res := setupRouterAndMocks()
Expand Down

0 comments on commit 558517b

Please sign in to comment.