Files
gochat/internal/repository/custom_role_repo_test.go
T
2026-06-04 15:44:48 +08:00

198 lines
5.9 KiB
Go

package repository
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
"github.com/gochat/gochat/internal/model"
)
// Helper: create prerequisite Account for CustomRole tests.
func createCustomRoleTestAccount(t *testing.T, db *gorm.DB) *model.Account {
t.Helper()
account := &model.Account{Name: "CustomRoleTestOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
return account
}
// Helper: create a test CustomRole.
func createTestCustomRole(t *testing.T, db *gorm.DB, accountID uint, name string) *model.CustomRole {
t.Helper()
role := &model.CustomRole{
AccountID: accountID,
Name: name,
Permissions: `{"conversation_manage":"full","conversation_delete":"none","contact_manage":"read","report_manage":"none","knowledge_base_manage":"none","automation_manage":"none"}`,
}
require.NoError(t, db.Create(role).Error)
return role
}
// ========== 1. FindByID ==========
func TestCustomRoleRepo_FindByID(t *testing.T) {
db := setupTestDB(t, &model.CustomRole{})
repo := NewCustomRoleRepo(db)
account := createCustomRoleTestAccount(t, db)
role := createTestCustomRole(t, db, account.ID, "Supervisor")
found, err := repo.FindByID(context.Background(), role.ID)
assert.NoError(t, err)
assert.Equal(t, role.ID, found.ID)
assert.Equal(t, "Supervisor", found.Name)
}
func TestCustomRoleRepo_FindByID_NotFound(t *testing.T) {
db := setupTestDB(t, &model.CustomRole{})
repo := NewCustomRoleRepo(db)
_, err := repo.FindByID(context.Background(), 9999)
assert.Error(t, err)
}
// ========== 2. FindByIDAndAccount ==========
func TestCustomRoleRepo_FindByIDAndAccount(t *testing.T) {
db := setupTestDB(t, &model.CustomRole{})
repo := NewCustomRoleRepo(db)
account := createCustomRoleTestAccount(t, db)
role := createTestCustomRole(t, db, account.ID, "TeamLead")
found, err := repo.FindByIDAndAccount(context.Background(), role.ID, account.ID)
assert.NoError(t, err)
assert.Equal(t, role.ID, found.ID)
assert.Equal(t, account.ID, found.AccountID)
}
func TestCustomRoleRepo_FindByIDAndAccount_WrongAccount(t *testing.T) {
db := setupTestDB(t, &model.CustomRole{})
repo := NewCustomRoleRepo(db)
account := createCustomRoleTestAccount(t, db)
role := createTestCustomRole(t, db, account.ID, "TeamLead")
// Wrong account ID should not find it
_, err := repo.FindByIDAndAccount(context.Background(), role.ID, 9999)
assert.Error(t, err)
}
// ========== 3. FindByAccount ==========
func TestCustomRoleRepo_FindByAccount(t *testing.T) {
db := setupTestDB(t, &model.CustomRole{})
repo := NewCustomRoleRepo(db)
account := createCustomRoleTestAccount(t, db)
createTestCustomRole(t, db, account.ID, "Supervisor")
createTestCustomRole(t, db, account.ID, "TeamLead")
roles, total, err := repo.FindByAccount(context.Background(), account.ID, 0, 10)
assert.NoError(t, err)
assert.Equal(t, int64(2), total)
assert.Len(t, roles, 2)
}
func TestCustomRoleRepo_FindByAccount_Pagination(t *testing.T) {
db := setupTestDB(t, &model.CustomRole{})
repo := NewCustomRoleRepo(db)
account := createCustomRoleTestAccount(t, db)
for i := 0; i < 5; i++ {
createTestCustomRole(t, db, account.ID, "Role"+string(rune('A'+i)))
}
roles, total, err := repo.FindByAccount(context.Background(), account.ID, 0, 2)
assert.NoError(t, err)
assert.Equal(t, int64(5), total)
assert.Len(t, roles, 2)
roles2, total2, err := repo.FindByAccount(context.Background(), account.ID, 2, 2)
assert.NoError(t, err)
assert.Equal(t, int64(5), total2)
assert.Len(t, roles2, 2)
}
func TestCustomRoleRepo_FindByAccount_NoMatches(t *testing.T) {
db := setupTestDB(t, &model.CustomRole{})
repo := NewCustomRoleRepo(db)
account := createCustomRoleTestAccount(t, db)
// No custom roles created
roles, total, err := repo.FindByAccount(context.Background(), account.ID, 0, 10)
assert.NoError(t, err)
assert.Equal(t, int64(0), total)
assert.Len(t, roles, 0)
}
// ========== 4. Create ==========
func TestCustomRoleRepo_Create(t *testing.T) {
db := setupTestDB(t, &model.CustomRole{})
repo := NewCustomRoleRepo(db)
account := createCustomRoleTestAccount(t, db)
role := &model.CustomRole{
AccountID: account.ID,
Name: "NewRole",
Permissions: `{"conversation_manage":"full","conversation_delete":"read","contact_manage":"none","report_manage":"none","knowledge_base_manage":"none","automation_manage":"none"}`,
}
err := repo.Create(context.Background(), role)
assert.NoError(t, err)
assert.NotZero(t, role.ID)
}
// ========== 5. Update ==========
func TestCustomRoleRepo_Update(t *testing.T) {
db := setupTestDB(t, &model.CustomRole{})
repo := NewCustomRoleRepo(db)
account := createCustomRoleTestAccount(t, db)
role := createTestCustomRole(t, db, account.ID, "Supervisor")
role.Name = "Senior Supervisor"
err := repo.Update(context.Background(), role)
assert.NoError(t, err)
found, err := repo.FindByID(context.Background(), role.ID)
assert.NoError(t, err)
assert.Equal(t, "Senior Supervisor", found.Name)
}
// ========== 6. Delete (soft-delete) ==========
func TestCustomRoleRepo_Delete(t *testing.T) {
db := setupTestDB(t, &model.CustomRole{})
repo := NewCustomRoleRepo(db)
account := createCustomRoleTestAccount(t, db)
role := createTestCustomRole(t, db, account.ID, "Supervisor")
err := repo.Delete(context.Background(), role.ID, account.ID)
assert.NoError(t, err)
// FindByIDAndAccount should not find soft-deleted record
_, err = repo.FindByIDAndAccount(context.Background(), role.ID, account.ID)
assert.Error(t, err)
// FindByID (Unscoped) should still find it
found, err := repo.FindByID(context.Background(), role.ID)
assert.NoError(t, err)
assert.NotNil(t, found.DeletedAt)
}
func TestCustomRoleRepo_Delete_NotFound(t *testing.T) {
db := setupTestDB(t, &model.CustomRole{})
repo := NewCustomRoleRepo(db)
err := repo.Delete(context.Background(), 9999, 9999)
assert.NoError(t, err) // GORM: 0 rows affected is not an error
}