178 lines
4.8 KiB
Go
178 lines
4.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
func TestIntegrationHookRepo_CreateAndGetByID(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewIntegrationHookRepo(db)
|
|
ctx := context.Background()
|
|
|
|
hook := &model.IntegrationHook{
|
|
AccountID: 1,
|
|
HookType: model.HookTypeWebhook,
|
|
Status: model.HookStatusActive,
|
|
URL: "https://example.com/webhook",
|
|
AccessToken: "test-token",
|
|
}
|
|
require.NoError(t, repo.Create(ctx, hook))
|
|
assert.NotZero(t, hook.ID)
|
|
|
|
found, err := repo.GetByID(ctx, hook.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, model.HookTypeWebhook, found.HookType)
|
|
assert.Equal(t, "https://example.com/webhook", found.URL)
|
|
}
|
|
|
|
func TestIntegrationHookRepo_FindByAccount(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewIntegrationHookRepo(db)
|
|
ctx := context.Background()
|
|
|
|
// Create hooks for account 1
|
|
for i := 0; i < 3; i++ {
|
|
hook := &model.IntegrationHook{
|
|
AccountID: 1,
|
|
HookType: model.HookTypeWebhook,
|
|
Status: model.HookStatusActive,
|
|
URL: "https://example.com/hook" + string(rune('a'+i)),
|
|
AccessToken: "token" + string(rune('a'+i)),
|
|
}
|
|
require.NoError(t, repo.Create(ctx, hook))
|
|
}
|
|
// Create hook for account 2
|
|
hook2 := &model.IntegrationHook{
|
|
AccountID: 2,
|
|
HookType: model.HookTypeSlack,
|
|
Status: model.HookStatusActive,
|
|
URL: "https://slack.example.com",
|
|
AccessToken: "slack-token",
|
|
}
|
|
require.NoError(t, repo.Create(ctx, hook2))
|
|
|
|
hooks, total, err := repo.FindByAccount(ctx, 1, 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(3), total)
|
|
assert.Len(t, hooks, 3)
|
|
|
|
hooks2, total2, err := repo.FindByAccount(ctx, 2, 0, 10)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(1), total2)
|
|
assert.Len(t, hooks2, 1)
|
|
}
|
|
|
|
func TestIntegrationHookRepo_FindByAccountAndType(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewIntegrationHookRepo(db)
|
|
ctx := context.Background()
|
|
|
|
hook1 := &model.IntegrationHook{
|
|
AccountID: 1,
|
|
HookType: model.HookTypeWebhook,
|
|
Status: model.HookStatusActive,
|
|
URL: "https://example.com/webhook1",
|
|
AccessToken: "t1",
|
|
}
|
|
require.NoError(t, repo.Create(ctx, hook1))
|
|
|
|
hook2 := &model.IntegrationHook{
|
|
AccountID: 1,
|
|
HookType: model.HookTypeSlack,
|
|
Status: model.HookStatusActive,
|
|
URL: "https://slack.example.com",
|
|
AccessToken: "t2",
|
|
}
|
|
require.NoError(t, repo.Create(ctx, hook2))
|
|
|
|
webhooks, err := repo.FindByAccountAndType(ctx, 1, model.HookTypeWebhook)
|
|
require.NoError(t, err)
|
|
assert.Len(t, webhooks, 1)
|
|
assert.Equal(t, model.HookTypeWebhook, webhooks[0].HookType)
|
|
|
|
slacks, err := repo.FindByAccountAndType(ctx, 1, model.HookTypeSlack)
|
|
require.NoError(t, err)
|
|
assert.Len(t, slacks, 1)
|
|
}
|
|
|
|
func TestIntegrationHookRepo_FindByInbox(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewIntegrationHookRepo(db)
|
|
ctx := context.Background()
|
|
inboxID := uint(10)
|
|
|
|
hook := &model.IntegrationHook{
|
|
AccountID: 1,
|
|
HookType: model.HookTypeWebhook,
|
|
Status: model.HookStatusActive,
|
|
URL: "https://example.com/inbox-hook",
|
|
AccessToken: "t1",
|
|
InboxID: &inboxID,
|
|
}
|
|
require.NoError(t, repo.Create(ctx, hook))
|
|
|
|
// Hook without inbox
|
|
hook2 := &model.IntegrationHook{
|
|
AccountID: 1,
|
|
HookType: model.HookTypeSlack,
|
|
Status: model.HookStatusActive,
|
|
URL: "https://slack.example.com",
|
|
AccessToken: "t2",
|
|
}
|
|
require.NoError(t, repo.Create(ctx, hook2))
|
|
|
|
found, err := repo.FindByInbox(ctx, 1, inboxID)
|
|
require.NoError(t, err)
|
|
assert.Len(t, found, 1)
|
|
assert.Equal(t, inboxID, *found[0].InboxID)
|
|
}
|
|
|
|
func TestIntegrationHookRepo_Update(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewIntegrationHookRepo(db)
|
|
ctx := context.Background()
|
|
|
|
hook := &model.IntegrationHook{
|
|
AccountID: 1,
|
|
HookType: model.HookTypeWebhook,
|
|
Status: model.HookStatusActive,
|
|
URL: "https://example.com/old",
|
|
AccessToken: "old-token",
|
|
}
|
|
require.NoError(t, repo.Create(ctx, hook))
|
|
|
|
hook.URL = "https://example.com/new"
|
|
hook.Status = model.HookStatusInactive
|
|
require.NoError(t, repo.Update(ctx, hook))
|
|
|
|
found, err := repo.GetByID(ctx, hook.ID)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "https://example.com/new", found.URL)
|
|
assert.Equal(t, model.HookStatusInactive, found.Status)
|
|
}
|
|
|
|
func TestIntegrationHookRepo_Delete(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := NewIntegrationHookRepo(db)
|
|
ctx := context.Background()
|
|
|
|
hook := &model.IntegrationHook{
|
|
AccountID: 1,
|
|
HookType: model.HookTypeWebhook,
|
|
Status: model.HookStatusActive,
|
|
URL: "https://example.com/to-delete",
|
|
AccessToken: "token",
|
|
}
|
|
require.NoError(t, repo.Create(ctx, hook))
|
|
|
|
require.NoError(t, repo.Delete(ctx, hook.ID))
|
|
|
|
_, err := repo.GetByID(ctx, hook.ID)
|
|
assert.Error(t, err, "should not find deleted hook")
|
|
} |