Skip to content

Commit

Permalink
Update application test file
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicatarra committed Dec 14, 2023
1 parent 66443a2 commit c316ec2
Showing 1 changed file with 67 additions and 1 deletion.
68 changes: 67 additions & 1 deletion ms/auth/internal/application/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ func TestAppl_CreateAuthTokenUseCase(t *testing.T) {
t.Run("Success", func(t *testing.T) {
// Arrange
userRepo, tokenRepo, permissionRepo, cfg, wg := Init()

appl := NewAppl(&userRepo, &tokenRepo, &permissionRepo, &wg, cfg)

expectedUserID := int64(1)
Expand Down Expand Up @@ -313,3 +312,70 @@ func TestAppl_CreateAuthTokenUseCase(t *testing.T) {
assert.Error(t, err)
})
}

func TestAppl_ValidateAuthTokenUseCase(t *testing.T) {
t.Run("Success", func(t *testing.T) {
// Arrange
userRepo, tokenRepo, permissionRepo, cfg, wg := Init()
appl := NewAppl(&userRepo, &tokenRepo, &permissionRepo, &wg, cfg)
expectedUserID := int64(1)
expectedUser := &domain.User{
ID: int64(1),
Name: "John Doe",
Email: "john@example.com",
Activated: true,
}
userRepo.On("GetUserById", mock.AnythingOfType("int64")).Return(expectedUser, nil)

// Act
tokenBytes, err := appl.CreateAuthTokenUseCase(expectedUserID)
token := string(tokenBytes)
user, err := appl.ValidateAuthTokenUseCase(token)

// Assert
assert.NoError(t, err)
assert.Equal(t, user.ID, expectedUserID)
})

t.Run("Error - JWT Secret", func(t *testing.T) {
// Arrange
userRepo, tokenRepo, permissionRepo, _, wg := Init()
cfg := config.Config{
BaseURL: "localhost:8082",
}
appl := NewAppl(&userRepo, &tokenRepo, &permissionRepo, &wg, cfg)
expectedUserID := int64(1)
expectedUser := &domain.User{
ID: int64(1),
Name: "John Doe",
Email: "john@example.com",
Activated: true,
}
userRepo.On("GetUserById", mock.AnythingOfType("int64")).Return(expectedUser, nil)

// Act
tokenBytes, err := appl.CreateAuthTokenUseCase(expectedUserID)
token := string(tokenBytes)
_, err = appl.ValidateAuthTokenUseCase(token)

// Assert
assert.Error(t, err)
})

t.Run("Error - User database", func(t *testing.T) {
// Arrange
userRepo, tokenRepo, permissionRepo, cfg, wg := Init()
appl := NewAppl(&userRepo, &tokenRepo, &permissionRepo, &wg, cfg)
expectedUserID := int64(1)
userRepo.On("GetUserById", mock.AnythingOfType("int64")).Return(nil, errors.New("record not found"))

// Act
tokenBytes, err := appl.CreateAuthTokenUseCase(expectedUserID)
token := string(tokenBytes)
_, err = appl.ValidateAuthTokenUseCase(token)

// Assert
assert.Error(t, err)
})

}

0 comments on commit c316ec2

Please sign in to comment.