236 lines
5.3 KiB
Go
236 lines
5.3 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
domainAuth "base/internal/domain/auth"
|
|
)
|
|
|
|
func TestRoleRepository_Create(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := createTestRoleRepository(db)
|
|
ctx := context.Background()
|
|
|
|
t.Run("create role successfully", func(t *testing.T) {
|
|
role := &domainAuth.Role{
|
|
ID: uuid.New(),
|
|
Name: "admin",
|
|
Description: "Administrator role",
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
|
|
err := repo.Create(ctx, role)
|
|
assert.NoError(t, err)
|
|
assert.NotEqual(t, uuid.Nil, role.ID)
|
|
|
|
// Verify role was created
|
|
found, err := repo.FindByID(ctx, role.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, role.Name, found.Name)
|
|
assert.Equal(t, role.Description, found.Description)
|
|
})
|
|
|
|
t.Run("create role with duplicate name fails", func(t *testing.T) {
|
|
name := "duplicate"
|
|
role1 := &domainAuth.Role{
|
|
ID: uuid.New(),
|
|
Name: name,
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
|
|
err := repo.Create(ctx, role1)
|
|
assert.NoError(t, err)
|
|
|
|
role2 := &domainAuth.Role{
|
|
ID: uuid.New(),
|
|
Name: name,
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
|
|
err = repo.Create(ctx, role2)
|
|
assert.Error(t, err)
|
|
})
|
|
}
|
|
|
|
func TestRoleRepository_FindByID(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := createTestRoleRepository(db)
|
|
ctx := context.Background()
|
|
|
|
t.Run("find existing role by id", func(t *testing.T) {
|
|
role := &domainAuth.Role{
|
|
ID: uuid.New(),
|
|
Name: "find",
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
|
|
err := repo.Create(ctx, role)
|
|
require.NoError(t, err)
|
|
|
|
found, err := repo.FindByID(ctx, role.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, role.ID, found.ID)
|
|
assert.Equal(t, role.Name, found.Name)
|
|
})
|
|
|
|
t.Run("find non-existent role", func(t *testing.T) {
|
|
nonExistentID := uuid.New()
|
|
found, err := repo.FindByID(ctx, nonExistentID)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
})
|
|
}
|
|
|
|
func TestRoleRepository_FindByName(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := createTestRoleRepository(db)
|
|
ctx := context.Background()
|
|
|
|
t.Run("find existing role by name", func(t *testing.T) {
|
|
name := "findbyname"
|
|
role := &domainAuth.Role{
|
|
ID: uuid.New(),
|
|
Name: name,
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
|
|
err := repo.Create(ctx, role)
|
|
require.NoError(t, err)
|
|
|
|
found, err := repo.FindByName(ctx, name)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, role.ID, found.ID)
|
|
assert.Equal(t, name, found.Name)
|
|
})
|
|
|
|
t.Run("find non-existent role by name", func(t *testing.T) {
|
|
found, err := repo.FindByName(ctx, "nonexistent")
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
})
|
|
}
|
|
|
|
func TestRoleRepository_Update(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := createTestRoleRepository(db)
|
|
ctx := context.Background()
|
|
|
|
t.Run("update role successfully", func(t *testing.T) {
|
|
role := &domainAuth.Role{
|
|
ID: uuid.New(),
|
|
Name: "update",
|
|
Description: "Original description",
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
|
|
err := repo.Create(ctx, role)
|
|
require.NoError(t, err)
|
|
|
|
// Update role
|
|
role.Description = "Updated description"
|
|
|
|
err = repo.Update(ctx, role)
|
|
assert.NoError(t, err)
|
|
|
|
// Verify update
|
|
found, err := repo.FindByID(ctx, role.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "Updated description", found.Description)
|
|
})
|
|
}
|
|
|
|
func TestRoleRepository_Delete(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := createTestRoleRepository(db)
|
|
ctx := context.Background()
|
|
|
|
t.Run("delete role successfully", func(t *testing.T) {
|
|
role := &domainAuth.Role{
|
|
ID: uuid.New(),
|
|
Name: "delete",
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
|
|
err := repo.Create(ctx, role)
|
|
require.NoError(t, err)
|
|
|
|
err = repo.Delete(ctx, role.ID)
|
|
assert.NoError(t, err)
|
|
|
|
// Verify deletion (soft delete)
|
|
found, err := repo.FindByID(ctx, role.ID)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, found)
|
|
})
|
|
}
|
|
|
|
func TestRoleRepository_List(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := createTestRoleRepository(db)
|
|
ctx := context.Background()
|
|
|
|
// Create multiple roles
|
|
for i := 0; i < 5; i++ {
|
|
role := &domainAuth.Role{
|
|
ID: uuid.New(),
|
|
Name: "role" + strconv.Itoa(i),
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
err := repo.Create(ctx, role)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
t.Run("list roles with limit and offset", func(t *testing.T) {
|
|
roles, err := repo.List(ctx, 3, 0)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, roles, 3)
|
|
|
|
roles, err = repo.List(ctx, 3, 3)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, roles, 2) // Remaining 2 roles
|
|
})
|
|
}
|
|
|
|
func TestRoleRepository_Count(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := createTestRoleRepository(db)
|
|
ctx := context.Background()
|
|
|
|
t.Run("count roles", func(t *testing.T) {
|
|
initialCount, err := repo.Count(ctx)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(0), initialCount)
|
|
|
|
// Create roles
|
|
for i := 0; i < 3; i++ {
|
|
role := &domainAuth.Role{
|
|
ID: uuid.New(),
|
|
Name: "count" + strconv.Itoa(i),
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
err := repo.Create(ctx, role)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
count, err := repo.Count(ctx)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(3), count)
|
|
})
|
|
}
|