forked from baron/baron-sso
- FE: Vitest 환경 구축 및 공통 UI 컴포넌트(Badge, Button) 테스트 추가 - FE: Playwright E2E 테스트(Auth, Tenant CRUD 및 Validation) 시나리오 보강 - BE: Testcontainers 기반 Repository 통합 테스트(PostgreSQL) 추가 - BE: TenantRepository 계층 구조(Hierarchy), DB 제약조건(Unique) 테스트 - BE: UserRepository 통합 테스트(CRUD, Delete) 추가 - BE: PasswordPolicy 유틸리티 테스트 보강 - BE: TenantService 엣지 케이스(중복 슬러그, 권한 등) 검증 로직 추가 - Fix: 하위 테넌트 생성 시 ParentID 누락 문제 해결
78 lines
1.9 KiB
Go
78 lines
1.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"baron-sso-backend/internal/domain"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestUserRepository(t *testing.T) {
|
|
repo := NewUserRepository(testDB)
|
|
ctx := context.Background()
|
|
|
|
// Ensure User table exists and clean for tests
|
|
_ = testDB.AutoMigrate(&domain.User{})
|
|
|
|
t.Run("Create and FindByEmail", func(t *testing.T) {
|
|
user := &domain.User{
|
|
Email: "test@example.com",
|
|
Name: "Test User",
|
|
Role: "user",
|
|
}
|
|
|
|
err := repo.Create(ctx, user)
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, user.ID)
|
|
|
|
found, err := repo.FindByEmail(ctx, "test@example.com")
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, user.ID, found.ID)
|
|
assert.Equal(t, "Test User", found.Name)
|
|
})
|
|
|
|
t.Run("Update User Info", func(t *testing.T) {
|
|
user := &domain.User{
|
|
Email: "update@example.com",
|
|
Name: "Before Update",
|
|
Role: "user",
|
|
}
|
|
_ = repo.Create(ctx, user)
|
|
|
|
user.Name = "After Update"
|
|
user.Phone = "010-1234-5678"
|
|
err := repo.Update(ctx, user)
|
|
assert.NoError(t, err)
|
|
|
|
found, err := repo.FindByEmail(ctx, "update@example.com")
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "After Update", found.Name)
|
|
assert.Equal(t, "010-1234-5678", found.Phone)
|
|
})
|
|
|
|
t.Run("List Users with Search", func(t *testing.T) {
|
|
// Add some users
|
|
_ = repo.Create(ctx, &domain.User{Email: "alice@test.com", Name: "Alice", Role: "user"})
|
|
_ = repo.Create(ctx, &domain.User{Email: "bob@test.com", Name: "Bob", Role: "user"})
|
|
|
|
users, total, err := repo.List(ctx, 0, 10, "Alice")
|
|
assert.NoError(t, err)
|
|
assert.True(t, total >= 1)
|
|
assert.Equal(t, "Alice", users[0].Name)
|
|
})
|
|
|
|
t.Run("Delete User", func(t *testing.T) {
|
|
user := &domain.User{Email: "delete@example.com", Name: "To Delete"}
|
|
_ = repo.Create(ctx, user)
|
|
|
|
err := repo.Delete(ctx, user.ID)
|
|
assert.NoError(t, err)
|
|
|
|
found, err := repo.FindByEmail(ctx, "delete@example.com")
|
|
assert.Error(t, err) // Should not be found
|
|
assert.Nil(t, found)
|
|
})
|
|
}
|