193 lines
5.8 KiB
Go
193 lines
5.8 KiB
Go
package automation
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
func TestMacroService_Create(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, userID := seedTestAccount(dbProvider.DB(), t)
|
|
svc := NewMacroService(dbProvider)
|
|
|
|
macro := &Macro{
|
|
AccountID: accountID,
|
|
Name: "Close and label",
|
|
Actions: Actions{
|
|
{ActionName: "resolve", ActionParams: map[string]interface{}{}},
|
|
{ActionName: "add_label", ActionParams: map[string]interface{}{"labels": []string{"closed"}}},
|
|
},
|
|
Visibility: MacroVisibilityGlobal,
|
|
CreatedByID: userID,
|
|
UpdatedByID: userID,
|
|
}
|
|
|
|
err := svc.Create(context.Background(), macro)
|
|
if err != nil {
|
|
t.Fatalf("expected no error creating macro, got: %v", err)
|
|
}
|
|
if macro.ID == 0 {
|
|
t.Fatal("expected macro ID to be set after creation")
|
|
}
|
|
}
|
|
|
|
func TestMacroService_GetByID(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, userID := seedTestAccount(dbProvider.DB(), t)
|
|
svc := NewMacroService(dbProvider)
|
|
|
|
macro := &Macro{
|
|
AccountID: accountID,
|
|
Name: "Test macro",
|
|
Actions: Actions{},
|
|
Visibility: MacroVisibilityGlobal,
|
|
CreatedByID: userID,
|
|
UpdatedByID: userID,
|
|
}
|
|
if err := svc.Create(context.Background(), macro); err != nil {
|
|
t.Fatalf("setup: %v", err)
|
|
}
|
|
|
|
found, err := svc.GetByID(context.Background(), macro.ID)
|
|
if err != nil {
|
|
t.Fatalf("expected no error getting macro, got: %v", err)
|
|
}
|
|
if found.ID != macro.ID {
|
|
t.Fatalf("expected macro ID %d, got %d", macro.ID, found.ID)
|
|
}
|
|
if found.Name != "Test macro" {
|
|
t.Fatalf("expected name 'Test macro', got %q", found.Name)
|
|
}
|
|
}
|
|
|
|
func TestMacroService_GetByID_NotFound(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
svc := NewMacroService(dbProvider)
|
|
|
|
_, err := svc.GetByID(context.Background(), 9999)
|
|
if err == nil {
|
|
t.Fatal("expected error for non-existent macro, got nil")
|
|
}
|
|
}
|
|
|
|
func TestMacroService_ListByAccount(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, userID := seedTestAccount(dbProvider.DB(), t)
|
|
svc := NewMacroService(dbProvider)
|
|
|
|
// Create global macro
|
|
m1 := &Macro{
|
|
AccountID: accountID, Name: "Global macro", Actions: Actions{},
|
|
Visibility: MacroVisibilityGlobal, CreatedByID: userID, UpdatedByID: userID,
|
|
}
|
|
if err := svc.Create(context.Background(), m1); err != nil {
|
|
t.Fatalf("setup: %v", err)
|
|
}
|
|
|
|
// Create personal macro owned by userID
|
|
m2 := &Macro{
|
|
AccountID: accountID, Name: "Personal macro (mine)", Actions: Actions{},
|
|
Visibility: MacroVisibilityPersonal, CreatedByID: userID, UpdatedByID: userID,
|
|
}
|
|
if err := svc.Create(context.Background(), m2); err != nil {
|
|
t.Fatalf("setup: %v", err)
|
|
}
|
|
|
|
macros, err := svc.ListByAccount(context.Background(), accountID, userID)
|
|
if err != nil {
|
|
t.Fatalf("expected no error listing macros, got: %v", err)
|
|
}
|
|
// Should see both global and personal (owned by this user)
|
|
if len(macros) != 2 {
|
|
t.Fatalf("expected 2 macros (global + personal-own), got %d", len(macros))
|
|
}
|
|
}
|
|
|
|
func TestMacroService_ListByAccount_PersonalFiltering(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, userID := seedTestAccount(dbProvider.DB(), t)
|
|
svc := NewMacroService(dbProvider)
|
|
|
|
// Create a second user in the same account
|
|
otherUser := &model.User{Name: "Other User", Email: "other@example.com"}
|
|
db := dbProvider.DB()
|
|
if err := db.Create(otherUser).Error; err != nil {
|
|
t.Fatalf("setup: %v", err)
|
|
}
|
|
|
|
// Create personal macro owned by OTHER user (should NOT be visible to userID)
|
|
m := &Macro{
|
|
AccountID: accountID, Name: "Someone else's macro", Actions: Actions{},
|
|
Visibility: MacroVisibilityPersonal, CreatedByID: otherUser.ID, UpdatedByID: otherUser.ID,
|
|
}
|
|
if err := svc.Create(context.Background(), m); err != nil {
|
|
t.Fatalf("setup: %v", err)
|
|
}
|
|
|
|
// userID should not see other user's personal macro
|
|
macros, err := svc.ListByAccount(context.Background(), accountID, userID)
|
|
if err != nil {
|
|
t.Fatalf("expected no error, got: %v", err)
|
|
}
|
|
for _, mc := range macros {
|
|
if mc.Visibility == MacroVisibilityPersonal && mc.CreatedByID != userID {
|
|
t.Fatalf("should not see other user's personal macro: %q (created_by_id=%d)", mc.Name, mc.CreatedByID)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMacroService_Update(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, userID := seedTestAccount(dbProvider.DB(), t)
|
|
svc := NewMacroService(dbProvider)
|
|
|
|
macro := &Macro{
|
|
AccountID: accountID, Name: "Original", Actions: Actions{},
|
|
Visibility: MacroVisibilityPersonal, CreatedByID: userID, UpdatedByID: userID,
|
|
}
|
|
if err := svc.Create(context.Background(), macro); err != nil {
|
|
t.Fatalf("setup: %v", err)
|
|
}
|
|
|
|
macro.Name = "Updated"
|
|
macro.Visibility = MacroVisibilityGlobal
|
|
err := svc.Update(context.Background(), macro)
|
|
if err != nil {
|
|
t.Fatalf("expected no error updating macro, got: %v", err)
|
|
}
|
|
|
|
found, _ := svc.GetByID(context.Background(), macro.ID)
|
|
if found.Name != "Updated" {
|
|
t.Fatalf("expected name 'Updated', got %q", found.Name)
|
|
}
|
|
if found.Visibility != MacroVisibilityGlobal {
|
|
t.Fatal("expected visibility to be global after update")
|
|
}
|
|
}
|
|
|
|
func TestMacroService_Delete(t *testing.T) {
|
|
dbProvider := setupAutomationTestDBProvider(t)
|
|
accountID, userID := seedTestAccount(dbProvider.DB(), t)
|
|
svc := NewMacroService(dbProvider)
|
|
|
|
macro := &Macro{
|
|
AccountID: accountID, Name: "Delete me", Actions: Actions{},
|
|
Visibility: MacroVisibilityGlobal, CreatedByID: userID, UpdatedByID: userID,
|
|
}
|
|
if err := svc.Create(context.Background(), macro); err != nil {
|
|
t.Fatalf("setup: %v", err)
|
|
}
|
|
|
|
err := svc.Delete(context.Background(), macro.ID)
|
|
if err != nil {
|
|
t.Fatalf("expected no error deleting macro, got: %v", err)
|
|
}
|
|
|
|
_, err = svc.GetByID(context.Background(), macro.ID)
|
|
if err == nil {
|
|
t.Fatal("expected error getting deleted macro, got nil")
|
|
}
|
|
}
|