Files
gochat/internal/repository/portal_repo_test.go
T
2026-06-04 15:44:48 +08:00

249 lines
7.6 KiB
Go

package repository
import (
"context"
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/gochat/gochat/internal/model"
)
func newTestPortal(accountID uint, name string) *model.Portal {
return &model.Portal{
AccountID: accountID,
Name: name,
Slug: name + "-slug",
Description: "Test portal description",
Locale: "en",
PortalConfiguration: json.RawMessage(`{"theme":"light"}`),
SSLSettings: json.RawMessage(`{}`),
}
}
// ========== Create ==========
func TestPortalRepo_Create(t *testing.T) {
db := setupTestDB(t, &model.Portal{}, &model.Account{})
repo := NewPortalRepo(db)
portal := newTestPortal(1, "TestPortal")
err := repo.Create(context.Background(), portal)
require.NoError(t, err)
assert.NotZero(t, portal.ID, "ID should be set after Create")
assert.Equal(t, uint(1), portal.AccountID)
assert.Equal(t, "TestPortal", portal.Name)
}
// ========== GetByID ==========
func TestPortalRepo_GetByID(t *testing.T) {
db := setupTestDB(t, &model.Portal{}, &model.Account{}, &model.Category{}, &model.Article{}, &model.PortalMember{})
repo := NewPortalRepo(db)
portal := newTestPortal(1, "FindByIDPortal")
err := repo.Create(context.Background(), portal)
require.NoError(t, err)
found, err := repo.GetByID(context.Background(), portal.ID)
require.NoError(t, err)
assert.Equal(t, portal.ID, found.ID)
assert.Equal(t, portal.Name, found.Name)
}
func TestPortalRepo_GetByID_NotFound(t *testing.T) {
db := setupTestDB(t, &model.Portal{}, &model.Account{})
repo := NewPortalRepo(db)
found, err := repo.GetByID(context.Background(), 9999)
assert.Error(t, err)
assert.Nil(t, found)
}
// ========== Update ==========
func TestPortalRepo_Update(t *testing.T) {
db := setupTestDB(t, &model.Portal{}, &model.Account{}, &model.Category{}, &model.Article{}, &model.PortalMember{})
repo := NewPortalRepo(db)
portal := newTestPortal(1, "BeforeUpdate")
err := repo.Create(context.Background(), portal)
require.NoError(t, err)
portal.Name = "AfterUpdate"
portal.Description = "Updated description"
err = repo.Update(context.Background(), portal)
require.NoError(t, err)
found, err := repo.GetByID(context.Background(), portal.ID)
require.NoError(t, err)
assert.Equal(t, "AfterUpdate", found.Name)
assert.Equal(t, "Updated description", found.Description)
}
// ========== Delete ==========
func TestPortalRepo_Delete(t *testing.T) {
db := setupTestDB(t, &model.Portal{}, &model.Account{})
repo := NewPortalRepo(db)
portal := newTestPortal(1, "DeletePortal")
err := repo.Create(context.Background(), portal)
require.NoError(t, err)
err = repo.Delete(context.Background(), portal.ID)
require.NoError(t, err)
found, err := repo.GetByID(context.Background(), portal.ID)
assert.Error(t, err)
assert.Nil(t, found)
}
// ========== FindByAccountID ==========
func TestPortalRepo_FindByAccountID(t *testing.T) {
db := setupTestDB(t, &model.Portal{}, &model.Account{})
repo := NewPortalRepo(db)
accountID := uint(10)
for i := 0; i < 3; i++ {
p := newTestPortal(accountID, "Portal"+string(rune('A'+i)))
p.Slug = "slug-" + string(rune('a'+i))
err := repo.Create(context.Background(), p)
require.NoError(t, err)
}
// Create portal for different account
otherPortal := newTestPortal(99, "OtherAccountPortal")
otherPortal.Slug = "other-slug"
err := repo.Create(context.Background(), otherPortal)
require.NoError(t, err)
portals, count, err := repo.FindByAccountID(context.Background(), accountID, 0, 10)
require.NoError(t, err)
assert.Equal(t, int64(3), count)
assert.Len(t, portals, 3)
for _, p := range portals {
assert.Equal(t, accountID, p.AccountID)
}
}
// ========== FindBySlug ==========
func TestPortalRepo_FindBySlug(t *testing.T) {
db := setupTestDB(t, &model.Portal{}, &model.Account{})
repo := NewPortalRepo(db)
portal := newTestPortal(1, "MyPortal")
portal.Slug = "unique-slug"
err := repo.Create(context.Background(), portal)
require.NoError(t, err)
found, err := repo.FindBySlug(context.Background(), "unique-slug")
require.NoError(t, err)
assert.Equal(t, portal.ID, found.ID)
assert.Equal(t, "unique-slug", found.Slug)
}
func TestPortalRepo_FindBySlug_NotFound(t *testing.T) {
db := setupTestDB(t, &model.Portal{}, &model.Account{})
repo := NewPortalRepo(db)
found, err := repo.FindBySlug(context.Background(), "nonexistent-slug")
assert.Error(t, err)
assert.Nil(t, found)
}
// ========== Archive ==========
func TestPortalRepo_Archive(t *testing.T) {
db := setupTestDB(t, &model.Portal{}, &model.Account{}, &model.Category{}, &model.Article{}, &model.PortalMember{})
repo := NewPortalRepo(db)
portal := newTestPortal(1, "ArchivePortal")
err := repo.Create(context.Background(), portal)
require.NoError(t, err)
assert.False(t, portal.Archived, "portal should not be archived initially")
err = repo.Archive(context.Background(), portal.ID)
require.NoError(t, err)
found, err := repo.GetByID(context.Background(), portal.ID)
require.NoError(t, err)
assert.True(t, found.Archived, "portal should be archived after Archive call")
}
// ========== RemoveLogo ==========
func TestPortalRepo_RemoveLogo(t *testing.T) {
db := setupTestDB(t, &model.Portal{}, &model.Account{}, &model.Category{}, &model.Article{}, &model.PortalMember{})
repo := NewPortalRepo(db)
portal := newTestPortal(1, "LogoPortal")
portal.LogoURL = "https://example.com/logo.png"
err := repo.Create(context.Background(), portal)
require.NoError(t, err)
assert.Equal(t, "https://example.com/logo.png", portal.LogoURL, "logo should be set initially")
err = repo.RemoveLogo(context.Background(), portal.ID)
require.NoError(t, err)
found, err := repo.GetByID(context.Background(), portal.ID)
require.NoError(t, err)
assert.Equal(t, "", found.LogoURL, "logo should be empty after RemoveLogo call")
}
// ========== GetSSLStatus ==========
func TestPortalRepo_GetSSLStatus_NoCustomDomain(t *testing.T) {
db := setupTestDB(t, &model.Portal{}, &model.Account{})
repo := NewPortalRepo(db)
portal := newTestPortal(1, "NoDomainPortal")
// newTestPortal leaves CustomDomain empty by default
err := repo.Create(context.Background(), portal)
require.NoError(t, err)
status, err := repo.GetSSLStatus(context.Background(), portal.ID)
require.NoError(t, err)
assert.Equal(t, "no_custom_domain", status.SSLState)
assert.Equal(t, "", status.CustomDomain)
}
func TestPortalRepo_GetSSLStatus_WithCustomDomain(t *testing.T) {
db := setupTestDB(t, &model.Portal{}, &model.Account{})
repo := NewPortalRepo(db)
portal := newTestPortal(1, "CustomDomainPortal")
portal.CustomDomain = "help.example.com"
err := repo.Create(context.Background(), portal)
require.NoError(t, err)
status, err := repo.GetSSLStatus(context.Background(), portal.ID)
require.NoError(t, err)
assert.Equal(t, "pending", status.SSLState)
assert.Equal(t, "help.example.com", status.CustomDomain)
}
func TestPortalRepo_GetSSLStatus_WithSSLSettings(t *testing.T) {
db := setupTestDB(t, &model.Portal{}, &model.Account{})
repo := NewPortalRepo(db)
portal := newTestPortal(1, "SSLPortal")
portal.CustomDomain = "secure.example.com"
portal.SSLSettings = json.RawMessage(`{"state":"success","certificate_expiry":"2025-12-01","cname_valid":true}`)
err := repo.Create(context.Background(), portal)
require.NoError(t, err)
status, err := repo.GetSSLStatus(context.Background(), portal.ID)
require.NoError(t, err)
assert.Equal(t, "success", status.SSLState)
assert.Equal(t, "2025-12-01", status.CertificateExpiry)
assert.True(t, status.CNAMEValid)
assert.Equal(t, "secure.example.com", status.CustomDomain)
}