8d631f8fab
Optimized README for better discoverability with keywords: api, habits, self-hosted. Consolidated content to reduce redundancy while maintaining clarity. Added comprehensive test coverage across multiple layers: - Infrastructure: bcrypt hashing, JWT tokens, configuration validation - Application commands: user deletion, password reset, token revocation, email verification - Application queries: login, token refresh - Domain entities: refresh tokens, password reset tokens Coverage increased from 42.5% to 50.8% with meaningful business logic tests. Fixed integration test handler initialization with correct parameters.
135 lines
3.2 KiB
Go
135 lines
3.2 KiB
Go
package commands
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"apocapoc-api/internal/domain/entities"
|
|
"apocapoc-api/internal/shared/errors"
|
|
)
|
|
|
|
type mockDeleteUserRepo struct {
|
|
findByIDFunc func(ctx context.Context, id string) (*entities.User, error)
|
|
deleteFunc func(ctx context.Context, id string) error
|
|
}
|
|
|
|
func (m *mockDeleteUserRepo) FindByID(ctx context.Context, id string) (*entities.User, error) {
|
|
if m.findByIDFunc != nil {
|
|
return m.findByIDFunc(ctx, id)
|
|
}
|
|
return nil, errors.ErrNotFound
|
|
}
|
|
|
|
func (m *mockDeleteUserRepo) FindByEmail(ctx context.Context, email string) (*entities.User, error) {
|
|
return nil, errors.ErrNotFound
|
|
}
|
|
|
|
func (m *mockDeleteUserRepo) FindByVerificationToken(ctx context.Context, token string) (*entities.User, error) {
|
|
return nil, errors.ErrNotFound
|
|
}
|
|
|
|
func (m *mockDeleteUserRepo) Create(ctx context.Context, user *entities.User) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *mockDeleteUserRepo) Update(ctx context.Context, user *entities.User) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *mockDeleteUserRepo) Delete(ctx context.Context, id string) error {
|
|
if m.deleteFunc != nil {
|
|
return m.deleteFunc(ctx, id)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func TestDeleteUserHandler_Success(t *testing.T) {
|
|
var deletedID string
|
|
|
|
repo := &mockDeleteUserRepo{
|
|
findByIDFunc: func(ctx context.Context, id string) (*entities.User, error) {
|
|
user := entities.NewUser("test@example.com", "hashedPassword", "UTC")
|
|
user.ID = id
|
|
return user, nil
|
|
},
|
|
deleteFunc: func(ctx context.Context, id string) error {
|
|
deletedID = id
|
|
return nil
|
|
},
|
|
}
|
|
|
|
handler := NewDeleteUserHandler(repo)
|
|
|
|
cmd := DeleteUserCommand{
|
|
UserID: "user-123",
|
|
}
|
|
|
|
err := handler.Handle(context.Background(), cmd)
|
|
if err != nil {
|
|
t.Fatalf("Handle() unexpected error = %v", err)
|
|
}
|
|
|
|
if deletedID != "user-123" {
|
|
t.Errorf("deletedID = %v, want %v", deletedID, "user-123")
|
|
}
|
|
}
|
|
|
|
func TestDeleteUserHandler_EmptyUserID(t *testing.T) {
|
|
repo := &mockDeleteUserRepo{}
|
|
handler := NewDeleteUserHandler(repo)
|
|
|
|
cmd := DeleteUserCommand{
|
|
UserID: "",
|
|
}
|
|
|
|
err := handler.Handle(context.Background(), cmd)
|
|
if err != errors.ErrInvalidInput {
|
|
t.Errorf("Handle() error = %v, want %v", err, errors.ErrInvalidInput)
|
|
}
|
|
}
|
|
|
|
func TestDeleteUserHandler_UserNotFound(t *testing.T) {
|
|
repo := &mockDeleteUserRepo{
|
|
findByIDFunc: func(ctx context.Context, id string) (*entities.User, error) {
|
|
return nil, errors.ErrNotFound
|
|
},
|
|
}
|
|
|
|
handler := NewDeleteUserHandler(repo)
|
|
|
|
cmd := DeleteUserCommand{
|
|
UserID: "non-existent-user",
|
|
}
|
|
|
|
err := handler.Handle(context.Background(), cmd)
|
|
if err != errors.ErrNotFound {
|
|
t.Errorf("Handle() error = %v, want %v", err, errors.ErrNotFound)
|
|
}
|
|
}
|
|
|
|
func TestDeleteUserHandler_DeleteError(t *testing.T) {
|
|
customError := errors.ErrNotFound
|
|
|
|
repo := &mockDeleteUserRepo{
|
|
findByIDFunc: func(ctx context.Context, id string) (*entities.User, error) {
|
|
user := entities.NewUser("test@example.com", "hashedPassword", "UTC")
|
|
user.ID = id
|
|
return user, nil
|
|
},
|
|
deleteFunc: func(ctx context.Context, id string) error {
|
|
return customError
|
|
},
|
|
}
|
|
|
|
handler := NewDeleteUserHandler(repo)
|
|
|
|
cmd := DeleteUserCommand{
|
|
UserID: "user-123",
|
|
}
|
|
|
|
err := handler.Handle(context.Background(), cmd)
|
|
if err != customError {
|
|
t.Errorf("Handle() error = %v, want %v", err, customError)
|
|
}
|
|
}
|