189 lines
5.5 KiB
Go
189 lines
5.5 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"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 Audit tests.
|
|
func createAuditTestAccount(t *testing.T, db *gorm.DB) *model.Account {
|
|
t.Helper()
|
|
account := &model.Account{Name: "AuditTestOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
return account
|
|
}
|
|
|
|
// Helper: create a test Audit entry.
|
|
func createTestAudit(t *testing.T, db *gorm.DB, accountID uint, action string, auditableType string, auditableID uint) *model.Audit {
|
|
t.Helper()
|
|
audit := &model.Audit{
|
|
AccountID: &accountID,
|
|
AuditableType: auditableType,
|
|
AuditableID: auditableID,
|
|
Action: action,
|
|
AuditedChanges: json.RawMessage(`{"field":"status","from":"open","to":"closed"}`),
|
|
}
|
|
require.NoError(t, db.Create(audit).Error)
|
|
return audit
|
|
}
|
|
|
|
// ========== 1. FindByID ==========
|
|
|
|
func TestAuditRepo_FindByID(t *testing.T) {
|
|
db := setupTestDB(t, &model.Audit{})
|
|
repo := NewAuditRepo(db)
|
|
|
|
account := createAuditTestAccount(t, db)
|
|
audit := createTestAudit(t, db, account.ID, "update", "Conversation", 1)
|
|
|
|
found, err := repo.FindByID(context.Background(), audit.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, audit.ID, found.ID)
|
|
assert.Equal(t, "update", found.Action)
|
|
assert.Equal(t, "Conversation", found.AuditableType)
|
|
}
|
|
|
|
func TestAuditRepo_FindByID_NotFound(t *testing.T) {
|
|
db := setupTestDB(t, &model.Audit{})
|
|
repo := NewAuditRepo(db)
|
|
|
|
_, err := repo.FindByID(context.Background(), 9999)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
// ========== 2. FindByAccount ==========
|
|
|
|
func TestAuditRepo_FindByAccount(t *testing.T) {
|
|
db := setupTestDB(t, &model.Audit{})
|
|
repo := NewAuditRepo(db)
|
|
|
|
account := createAuditTestAccount(t, db)
|
|
createTestAudit(t, db, account.ID, "create", "Conversation", 1)
|
|
createTestAudit(t, db, account.ID, "update", "Conversation", 2)
|
|
createTestAudit(t, db, account.ID, "delete", "Contact", 3)
|
|
|
|
audits, total, err := repo.FindByAccount(context.Background(), account.ID, "", "", 0, 10)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(3), total)
|
|
assert.Len(t, audits, 3)
|
|
}
|
|
|
|
func TestAuditRepo_FindByAccount_ActionFilter(t *testing.T) {
|
|
db := setupTestDB(t, &model.Audit{})
|
|
repo := NewAuditRepo(db)
|
|
|
|
account := createAuditTestAccount(t, db)
|
|
createTestAudit(t, db, account.ID, "create", "Conversation", 1)
|
|
createTestAudit(t, db, account.ID, "update", "Conversation", 2)
|
|
createTestAudit(t, db, account.ID, "delete", "Contact", 3)
|
|
|
|
audits, total, err := repo.FindByAccount(context.Background(), account.ID, "update", "", 0, 10)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(1), total)
|
|
assert.Len(t, audits, 1)
|
|
assert.Equal(t, "update", audits[0].Action)
|
|
}
|
|
|
|
func TestAuditRepo_FindByAccount_AuditableTypeFilter(t *testing.T) {
|
|
db := setupTestDB(t, &model.Audit{})
|
|
repo := NewAuditRepo(db)
|
|
|
|
account := createAuditTestAccount(t, db)
|
|
createTestAudit(t, db, account.ID, "create", "Conversation", 1)
|
|
createTestAudit(t, db, account.ID, "update", "Conversation", 2)
|
|
createTestAudit(t, db, account.ID, "delete", "Contact", 3)
|
|
|
|
audits, total, err := repo.FindByAccount(context.Background(), account.ID, "", "Conversation", 0, 10)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(2), total)
|
|
assert.Len(t, audits, 2)
|
|
}
|
|
|
|
func TestAuditRepo_FindByAccount_Pagination(t *testing.T) {
|
|
db := setupTestDB(t, &model.Audit{})
|
|
repo := NewAuditRepo(db)
|
|
|
|
account := createAuditTestAccount(t, db)
|
|
for i := 0; i < 5; i++ {
|
|
createTestAudit(t, db, account.ID, "create", "Conversation", uint(i+1))
|
|
}
|
|
|
|
// Page 1: limit=2
|
|
audits, total, err := repo.FindByAccount(context.Background(), account.ID, "", "", 0, 2)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(5), total)
|
|
assert.Len(t, audits, 2)
|
|
|
|
// Page 2: offset=2, limit=2
|
|
audits2, total2, err := repo.FindByAccount(context.Background(), account.ID, "", "", 2, 2)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(5), total2)
|
|
assert.Len(t, audits2, 2)
|
|
}
|
|
|
|
func TestAuditRepo_FindByAccount_NoMatches(t *testing.T) {
|
|
db := setupTestDB(t, &model.Audit{})
|
|
repo := NewAuditRepo(db)
|
|
|
|
account := createAuditTestAccount(t, db)
|
|
// No audits created for this account
|
|
|
|
audits, total, err := repo.FindByAccount(context.Background(), account.ID, "", "", 0, 10)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(0), total)
|
|
assert.Len(t, audits, 0)
|
|
}
|
|
|
|
// ========== 3. Create ==========
|
|
|
|
func TestAuditRepo_Create(t *testing.T) {
|
|
db := setupTestDB(t, &model.Audit{})
|
|
repo := NewAuditRepo(db)
|
|
|
|
account := createAuditTestAccount(t, db)
|
|
audit := &model.Audit{
|
|
AccountID: &account.ID,
|
|
AuditableType: "Message",
|
|
AuditableID: 42,
|
|
Action: "delete",
|
|
AuditedChanges: json.RawMessage(`{"content":"removed"}`),
|
|
}
|
|
|
|
err := repo.Create(context.Background(), audit)
|
|
assert.NoError(t, err)
|
|
assert.NotZero(t, audit.ID)
|
|
}
|
|
|
|
// ========== 4. Delete ==========
|
|
|
|
func TestAuditRepo_Delete(t *testing.T) {
|
|
db := setupTestDB(t, &model.Audit{})
|
|
repo := NewAuditRepo(db)
|
|
|
|
account := createAuditTestAccount(t, db)
|
|
audit := createTestAudit(t, db, account.ID, "create", "Conversation", 1)
|
|
|
|
err := repo.Delete(context.Background(), audit.ID)
|
|
assert.NoError(t, err)
|
|
|
|
_, err = repo.FindByID(context.Background(), audit.ID)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestAuditRepo_Delete_NotFound(t *testing.T) {
|
|
db := setupTestDB(t, &model.Audit{})
|
|
repo := NewAuditRepo(db)
|
|
|
|
err := repo.Delete(context.Background(), 9999)
|
|
// GORM delete on non-existent record does not return an error (0 rows affected)
|
|
// but we still verify the call completes without panic
|
|
assert.NoError(t, err)
|
|
}
|