Files
gochat/backend/internal/service/integration_hook_service_test.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

261 lines
7.8 KiB
Go

package service
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/repository"
)
func setupIntegrationHookTestDB(t *testing.T) (*repository.IntegrationHookRepo, *repository.IntegrationAppRepo) {
t.Helper()
db, err := gorm.Open(sqlite.Open("file::memory:"), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
require.NoError(t, err)
require.NoError(t, db.AutoMigrate(
&model.Account{},
&model.User{},
&model.AccountUser{},
&model.Inbox{},
&model.IntegrationHook{},
&model.IntegrationApp{},
))
return repository.NewIntegrationHookRepo(db), repository.NewIntegrationAppRepo(db)
}
func TestIntegrationHookService_Create(t *testing.T) {
hookRepo, appRepo := setupIntegrationHookTestDB(t)
svc := NewIntegrationHookService(hookRepo, appRepo, nil) // nil registry — webhook processing tests are separate
ctx := context.Background()
req := CreateHookRequest{
HookType: "webhook",
URL: "https://example.com/webhook",
}
hook, err := svc.Create(ctx, 1, req)
require.NoError(t, err)
assert.Equal(t, "webhook", hook.AppID)
assert.Equal(t, model.HookTypeWebhook, hook.HookType)
assert.Equal(t, "https://example.com/webhook", hook.URL)
assert.Equal(t, uint(1), hook.AccountID)
}
func TestIntegrationHookService_Create_InvalidHookType(t *testing.T) {
hookRepo, appRepo := setupIntegrationHookTestDB(t)
svc := NewIntegrationHookService(hookRepo, appRepo, nil) // nil registry — webhook processing tests are separate
ctx := context.Background()
req := CreateHookRequest{
HookType: "invalid_type",
URL: "https://example.com/hook",
}
_, err := svc.Create(ctx, 1, req)
assert.Error(t, err, "should reject invalid hook type")
}
func TestIntegrationHookService_Create_Slack(t *testing.T) {
hookRepo, appRepo := setupIntegrationHookTestDB(t)
svc := NewIntegrationHookService(hookRepo, appRepo, nil) // nil registry — webhook processing tests are separate
ctx := context.Background()
req := CreateHookRequest{
HookType: "slack",
URL: "https://slack.example.com/hook",
}
hook, err := svc.Create(ctx, 1, req)
require.NoError(t, err)
assert.Equal(t, "slack", hook.AppID)
assert.Equal(t, model.HookTypeSlack, hook.HookType)
}
func TestIntegrationHookService_Create_WithAppID(t *testing.T) {
hookRepo, appRepo := setupIntegrationHookTestDB(t)
svc := NewIntegrationHookService(hookRepo, appRepo, nil)
ctx := context.Background()
req := CreateHookRequest{
AppID: "openai",
Settings: map[string]interface{}{
"api_key": "sk-test",
},
}
hook, err := svc.Create(ctx, 1, req)
require.NoError(t, err)
assert.Equal(t, "openai", hook.AppID)
assert.Equal(t, model.HookType("openai"), hook.HookType)
}
func TestIntegrationHookService_Get(t *testing.T) {
hookRepo, appRepo := setupIntegrationHookTestDB(t)
svc := NewIntegrationHookService(hookRepo, appRepo, nil) // nil registry — webhook processing tests are separate
ctx := context.Background()
req := CreateHookRequest{
HookType: "webhook",
URL: "https://example.com/get-test",
}
hook, err := svc.Create(ctx, 1, req)
require.NoError(t, err)
found, err := svc.Get(ctx, hook.ID)
require.NoError(t, err)
assert.Equal(t, hook.ID, found.ID)
}
func TestIntegrationHookService_GetScoped(t *testing.T) {
hookRepo, appRepo := setupIntegrationHookTestDB(t)
svc := NewIntegrationHookService(hookRepo, appRepo, nil)
ctx := context.Background()
hook, err := svc.Create(ctx, 1, CreateHookRequest{AppID: "webhook"})
require.NoError(t, err)
found, err := svc.GetScoped(ctx, 1, hook.ID)
require.NoError(t, err)
assert.Equal(t, hook.ID, found.ID)
_, err = svc.GetScoped(ctx, 2, hook.ID)
assert.Error(t, err)
}
func TestIntegrationHookService_List(t *testing.T) {
hookRepo, appRepo := setupIntegrationHookTestDB(t)
svc := NewIntegrationHookService(hookRepo, appRepo, nil) // nil registry — webhook processing tests are separate
ctx := context.Background()
for _, ht := range []string{"webhook", "slack"} {
req := CreateHookRequest{
HookType: ht,
URL: "https://example.com/" + ht,
}
_, err := svc.Create(ctx, 1, req)
require.NoError(t, err)
}
hooks, total, err := svc.List(ctx, 1, 0, 10)
require.NoError(t, err)
assert.Equal(t, int64(2), total)
assert.Len(t, hooks, 2)
}
func TestIntegrationHookService_Update(t *testing.T) {
hookRepo, appRepo := setupIntegrationHookTestDB(t)
svc := NewIntegrationHookService(hookRepo, appRepo, nil) // nil registry — webhook processing tests are separate
ctx := context.Background()
req := CreateHookRequest{
HookType: "webhook",
URL: "https://example.com/old-url",
}
hook, err := svc.Create(ctx, 1, req)
require.NoError(t, err)
updateReq := UpdateHookRequest{
URL: "https://example.com/new-url",
Status: "inactive",
}
updated, err := svc.Update(ctx, hook.ID, updateReq)
require.NoError(t, err)
assert.Equal(t, "https://example.com/new-url", updated.URL)
assert.Equal(t, model.HookStatusInactive, updated.Status)
}
func TestIntegrationHookService_UpdateScoped(t *testing.T) {
hookRepo, appRepo := setupIntegrationHookTestDB(t)
svc := NewIntegrationHookService(hookRepo, appRepo, nil)
ctx := context.Background()
hook, err := svc.Create(ctx, 1, CreateHookRequest{AppID: "webhook"})
require.NoError(t, err)
updated, err := svc.UpdateScoped(ctx, 1, hook.ID, UpdateHookRequest{
Status: "disabled",
ReferenceID: "ref-1",
Settings: map[string]interface{}{
"channel": "support",
},
})
require.NoError(t, err)
assert.Equal(t, model.HookStatusInactive, updated.Status)
assert.Equal(t, "ref-1", updated.ReferenceID)
_, err = svc.UpdateScoped(ctx, 2, hook.ID, UpdateHookRequest{Status: "enabled"})
assert.Error(t, err)
}
func TestIntegrationHookService_Delete(t *testing.T) {
hookRepo, appRepo := setupIntegrationHookTestDB(t)
svc := NewIntegrationHookService(hookRepo, appRepo, nil) // nil registry — webhook processing tests are separate
ctx := context.Background()
req := CreateHookRequest{
HookType: "webhook",
URL: "https://example.com/to-delete",
}
hook, err := svc.Create(ctx, 1, req)
require.NoError(t, err)
require.NoError(t, svc.Delete(ctx, hook.ID))
_, err = svc.Get(ctx, hook.ID)
assert.Error(t, err, "should not find deleted hook")
}
func TestIntegrationHookService_DeleteScoped(t *testing.T) {
hookRepo, appRepo := setupIntegrationHookTestDB(t)
svc := NewIntegrationHookService(hookRepo, appRepo, nil)
ctx := context.Background()
hook, err := svc.Create(ctx, 1, CreateHookRequest{AppID: "webhook"})
require.NoError(t, err)
assert.Error(t, svc.DeleteScoped(ctx, 2, hook.ID))
require.NoError(t, svc.DeleteScoped(ctx, 1, hook.ID))
_, err = svc.Get(ctx, hook.ID)
assert.Error(t, err)
}
func TestIntegrationHookService_Create_WithInboxID(t *testing.T) {
hookRepo, appRepo := setupIntegrationHookTestDB(t)
svc := NewIntegrationHookService(hookRepo, appRepo, nil) // nil registry — webhook processing tests are separate
ctx := context.Background()
inboxID := uint(10)
req := CreateHookRequest{
HookType: "webhook",
URL: "https://example.com/inbox-hook",
InboxID: &inboxID,
}
hook, err := svc.Create(ctx, 1, req)
require.NoError(t, err)
assert.Equal(t, inboxID, *hook.InboxID)
}
func TestIntegrationHookService_Create_WithSettings(t *testing.T) {
hookRepo, appRepo := setupIntegrationHookTestDB(t)
svc := NewIntegrationHookService(hookRepo, appRepo, nil) // nil registry — webhook processing tests are separate
ctx := context.Background()
settings := map[string]interface{}{
"channel": "#alerts",
"notify": true,
}
req := CreateHookRequest{
HookType: "slack",
URL: "https://slack.example.com/hook",
Settings: settings,
}
hook, err := svc.Create(ctx, 1, req)
require.NoError(t, err)
assert.NotNil(t, hook.Settings)
}