285 lines
9.4 KiB
Go
285 lines
9.4 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
func TestAgentBotRepo_Create(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentBot{}, &model.AgentBotInbox{})
|
|
repo := NewAgentBotRepo(db)
|
|
|
|
account := &model.Account{Name: "ABOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
bot := &model.AgentBot{AccountID: &account.ID, Name: "TestBot", BotType: "webhook"}
|
|
err := repo.Create(context.Background(), bot)
|
|
assert.NoError(t, err)
|
|
assert.NotZero(t, bot.ID)
|
|
}
|
|
|
|
func TestAgentBotRepo_DB(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentBot{})
|
|
repo := NewAgentBotRepo(db)
|
|
assert.NotNil(t, repo.DB())
|
|
}
|
|
|
|
func TestAgentBotRepo_DB_NilReceiver(t *testing.T) {
|
|
var repo *AgentBotRepo
|
|
assert.Nil(t, repo.DB())
|
|
}
|
|
|
|
func TestAgentBotRepo_GetByID(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentBot{}, &model.AgentBotInbox{})
|
|
repo := NewAgentBotRepo(db)
|
|
|
|
account := &model.Account{Name: "ABGetOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
bot := &model.AgentBot{AccountID: &account.ID, Name: "GetBot", BotType: "webhook"}
|
|
require.NoError(t, repo.Create(context.Background(), bot))
|
|
|
|
found, err := repo.GetByID(context.Background(), bot.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, bot.ID, found.ID)
|
|
assert.Equal(t, "GetBot", found.Name)
|
|
}
|
|
|
|
func TestAgentBotRepo_FindByID_NotFound(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentBot{})
|
|
repo := NewAgentBotRepo(db)
|
|
|
|
_, err := repo.FindByID(context.Background(), 9999)
|
|
assert.Error(t, err)
|
|
assert.ErrorIs(t, err, gorm.ErrRecordNotFound)
|
|
}
|
|
|
|
func TestAgentBotRepo_FindAccessibleByID(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentBot{}, &model.AgentBotInbox{})
|
|
repo := NewAgentBotRepo(db)
|
|
|
|
account := &model.Account{Name: "ABAccOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
bot := &model.AgentBot{AccountID: &account.ID, Name: "AccBot", BotType: "webhook"}
|
|
require.NoError(t, repo.Create(context.Background(), bot))
|
|
|
|
found, err := repo.FindAccessibleByID(context.Background(), account.ID, bot.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, bot.ID, found.ID)
|
|
}
|
|
|
|
func TestAgentBotRepo_FindAccessibleByID_GlobalBot(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentBot{}, &model.AgentBotInbox{})
|
|
repo := NewAgentBotRepo(db)
|
|
|
|
account := &model.Account{Name: "ABGlobalOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
bot := &model.AgentBot{Name: "GlobalBot", BotType: "webhook"}
|
|
require.NoError(t, repo.Create(context.Background(), bot))
|
|
|
|
found, err := repo.FindAccessibleByID(context.Background(), account.ID, bot.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, bot.ID, found.ID)
|
|
}
|
|
|
|
func TestAgentBotRepo_FindAccessibleByID_NotFound(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentBot{})
|
|
repo := NewAgentBotRepo(db)
|
|
|
|
_, err := repo.FindAccessibleByID(context.Background(), 1, 9999)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestAgentBotRepo_FindByAccountAndID(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentBot{}, &model.AgentBotInbox{})
|
|
repo := NewAgentBotRepo(db)
|
|
|
|
account := &model.Account{Name: "ABAcc2Org", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
bot := &model.AgentBot{AccountID: &account.ID, Name: "OwnedBot", BotType: "webhook"}
|
|
require.NoError(t, repo.Create(context.Background(), bot))
|
|
|
|
found, err := repo.FindByAccountAndID(context.Background(), account.ID, bot.ID)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, bot.ID, found.ID)
|
|
}
|
|
|
|
func TestAgentBotRepo_FindByAccountAndID_NotFound(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentBot{})
|
|
repo := NewAgentBotRepo(db)
|
|
|
|
_, err := repo.FindByAccountAndID(context.Background(), 1, 9999)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestAgentBotRepo_FindByAccount(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentBot{}, &model.AgentBotInbox{})
|
|
repo := NewAgentBotRepo(db)
|
|
|
|
account := &model.Account{Name: "ABListOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
for i := 0; i < 3; i++ {
|
|
require.NoError(t, repo.Create(context.Background(), &model.AgentBot{
|
|
AccountID: &account.ID, Name: "Bot", BotType: "webhook", Secret: fmt.Sprintf("secret-%d", i), AccessToken: fmt.Sprintf("token-%d", i),
|
|
}))
|
|
}
|
|
|
|
bots, total, err := repo.FindByAccount(context.Background(), account.ID, 0, 10)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(3), total)
|
|
assert.Len(t, bots, 3)
|
|
}
|
|
|
|
func TestAgentBotRepo_FindByAccountID(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentBot{}, &model.AgentBotInbox{})
|
|
repo := NewAgentBotRepo(db)
|
|
|
|
account := &model.Account{Name: "ABList2Org", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
for i := 0; i < 2; i++ {
|
|
require.NoError(t, repo.Create(context.Background(), &model.AgentBot{
|
|
AccountID: &account.ID, Name: "Bot", BotType: "webhook", Secret: fmt.Sprintf("secret-fa-%d", i), AccessToken: fmt.Sprintf("token-fa-%d", i),
|
|
}))
|
|
}
|
|
|
|
bots, err := repo.FindByAccountID(context.Background(), account.ID)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, bots, 2)
|
|
}
|
|
|
|
func TestAgentBotRepo_CreateWithToken(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentBot{}, &model.AgentBotInbox{})
|
|
repo := NewAgentBotRepo(db)
|
|
|
|
account := &model.Account{Name: "ABTokenOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
bot := &model.AgentBot{AccountID: &account.ID, Name: "TokenBot", BotType: "webhook"}
|
|
err := repo.CreateWithToken(context.Background(), bot, "secret-token")
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "secret-token", bot.AccessToken)
|
|
}
|
|
|
|
func TestAgentBotRepo_Update(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentBot{}, &model.AgentBotInbox{})
|
|
repo := NewAgentBotRepo(db)
|
|
|
|
account := &model.Account{Name: "ABUpdOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
bot := &model.AgentBot{AccountID: &account.ID, Name: "Original", BotType: "webhook", Secret: "upd-secret", AccessToken: "upd-token"}
|
|
require.NoError(t, repo.Create(context.Background(), bot))
|
|
|
|
bot.Name = "Updated"
|
|
err := repo.Update(context.Background(), bot)
|
|
assert.NoError(t, err)
|
|
|
|
found, err := repo.GetByID(context.Background(), bot.ID)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, found)
|
|
assert.Equal(t, "Updated", found.Name)
|
|
}
|
|
|
|
func TestAgentBotRepo_UpdateAccessToken(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentBot{}, &model.AgentBotInbox{})
|
|
repo := NewAgentBotRepo(db)
|
|
|
|
account := &model.Account{Name: "ABTokenUpdOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
bot := &model.AgentBot{AccountID: &account.ID, Name: "Bot", BotType: "webhook", AccessToken: "old-token"}
|
|
require.NoError(t, repo.Create(context.Background(), bot))
|
|
|
|
err := repo.UpdateAccessToken(context.Background(), bot.ID, "new-token")
|
|
assert.NoError(t, err)
|
|
|
|
found, _ := repo.GetByID(context.Background(), bot.ID)
|
|
assert.Equal(t, "new-token", found.AccessToken)
|
|
}
|
|
|
|
func TestAgentBotRepo_Delete(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentBot{})
|
|
repo := NewAgentBotRepo(db)
|
|
|
|
account := &model.Account{Name: "ABDelOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
bot := &model.AgentBot{AccountID: &account.ID, Name: "DelBot", BotType: "webhook"}
|
|
require.NoError(t, repo.Create(context.Background(), bot))
|
|
|
|
err := repo.Delete(context.Background(), bot.ID)
|
|
assert.NoError(t, err)
|
|
|
|
_, err = repo.GetByID(context.Background(), bot.ID)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestAgentBotRepo_DeleteByAccount(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentBot{})
|
|
repo := NewAgentBotRepo(db)
|
|
|
|
account := &model.Account{Name: "ABDelByAccOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
bot := &model.AgentBot{AccountID: &account.ID, Name: "DelBot", BotType: "webhook"}
|
|
require.NoError(t, repo.Create(context.Background(), bot))
|
|
|
|
err := repo.DeleteByAccount(context.Background(), account.ID, bot.ID)
|
|
assert.NoError(t, err)
|
|
|
|
_, err = repo.GetByID(context.Background(), bot.ID)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestAgentBotRepo_FindAccessible(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentBot{}, &model.AgentBotInbox{})
|
|
repo := NewAgentBotRepo(db)
|
|
|
|
account := &model.Account{Name: "ABFAOrg", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
// Account-owned bot
|
|
require.NoError(t, repo.Create(context.Background(), &model.AgentBot{
|
|
AccountID: &account.ID, Name: "AccBot", BotType: "webhook", Secret: "fa-secret-1", AccessToken: "fa-token-1",
|
|
}))
|
|
// Global bot
|
|
require.NoError(t, repo.Create(context.Background(), &model.AgentBot{
|
|
Name: "GlobalBot", BotType: "webhook", Secret: "fa-secret-2", AccessToken: "fa-token-2",
|
|
}))
|
|
|
|
bots, err := repo.FindAccessible(context.Background(), account.ID)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, bots, 2)
|
|
}
|
|
|
|
func TestAgentBotRepo_FindAccessiblePaginated(t *testing.T) {
|
|
db := setupTestDB(t, &model.AgentBot{}, &model.AgentBotInbox{})
|
|
repo := NewAgentBotRepo(db)
|
|
|
|
account := &model.Account{Name: "ABFAP Org", Locale: "en", Active: true}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
for i := 0; i < 3; i++ {
|
|
require.NoError(t, repo.Create(context.Background(), &model.AgentBot{
|
|
AccountID: &account.ID, Name: "Bot", BotType: "webhook", Secret: fmt.Sprintf("secret-%d", i), AccessToken: fmt.Sprintf("token-%d", i),
|
|
}))
|
|
}
|
|
|
|
bots, total, err := repo.FindAccessiblePaginated(context.Background(), account.ID, 0, 2)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, int64(3), total)
|
|
assert.Len(t, bots, 2)
|
|
}
|