199 lines
5.7 KiB
Go
199 lines
5.7 KiB
Go
package cgpcli
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestMailingLists_Integration(t *testing.T) {
|
|
cli := getTestCli(t)
|
|
defer cli.Close()
|
|
|
|
const (
|
|
testDomain = "test.domain.name"
|
|
testOwner = "testuser1@test.domain.name"
|
|
testList = "test-sdk-list@test.domain.name"
|
|
testSub = "testuser2@test.domain.name"
|
|
)
|
|
|
|
// 1. Создание (проверка обрезки домена)
|
|
t.Run("CreateList_AutoTruncate", func(t *testing.T) {
|
|
err := cli.CreateList(testList, testOwner)
|
|
if err != nil {
|
|
t.Fatalf("CreateList failed with full email: %v", err)
|
|
}
|
|
})
|
|
|
|
// 2. Тест на корректность маппинга структур
|
|
t.Run("GetSubscriberInfo_Types", func(t *testing.T) {
|
|
// Сначала подпишем
|
|
if err := cli.List(testList, "feed", testSub, true, false); err != nil {
|
|
t.Fatalf("Subscribe failed: %v", err)
|
|
}
|
|
|
|
info, err := cli.GetSubscriberInfo(testList, testSub)
|
|
if err != nil {
|
|
t.Fatalf("GetSubscriberInfo failed: %v", err)
|
|
}
|
|
if info == nil {
|
|
t.Fatal("subscriber not found")
|
|
}
|
|
|
|
// Проверка ConfirmationID (int64)
|
|
if info.ConfirmationID == 0 {
|
|
t.Log("Warning: ConfirmationID is 0, check if this is expected for direct subscription")
|
|
}
|
|
|
|
// Проверка дат
|
|
if info.SubscribeTime.IsZero() {
|
|
t.Error("SubscribeTime was not parsed correctly")
|
|
}
|
|
})
|
|
|
|
// 3. Тест GetList / UpdateList (трансляция текста)
|
|
t.Run("TextTranslation", func(t *testing.T) {
|
|
text := "First Line\nSecond Line"
|
|
err := cli.UpdateList(testList, map[string]any{"PolicyText": text})
|
|
if err != nil {
|
|
t.Fatalf("UpdateList failed: %v", err)
|
|
}
|
|
|
|
res, err := cli.GetList(testList)
|
|
if err != nil {
|
|
t.Fatalf("GetList failed: %v", err)
|
|
}
|
|
|
|
if res["PolicyText"] != text {
|
|
t.Errorf("Translation failed: expected %q, got %q", text, res["PolicyText"])
|
|
}
|
|
})
|
|
|
|
// 4. Очистка
|
|
t.Run("Cleanup", func(t *testing.T) {
|
|
if err := cli.DeleteList(testList); err != nil {
|
|
t.Errorf("DeleteList failed: %v", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestMapToSubscriber_Unit(t *testing.T) {
|
|
data := map[string]any{
|
|
"Sub": "postmaster@test.domain.name",
|
|
"RealName": `System\eEngineer`,
|
|
"ConfirmationID": int64(987654321),
|
|
"timeSubscribed": int64(20260118180000),
|
|
}
|
|
|
|
res := mapToSubscriber(data)
|
|
|
|
// Теперь ожидаем "как есть"
|
|
if res.RealName != `System\eEngineer` {
|
|
t.Errorf("RealName changed: expected %q, got %q", `System\eEngineer`, res.RealName)
|
|
}
|
|
|
|
if res.ConfirmationID != 987654321 {
|
|
t.Errorf("ConfirmationID failed: %v", res.ConfirmationID)
|
|
}
|
|
|
|
if res.TimeSubscribed.Year() != 2026 {
|
|
t.Errorf("TimeSubscribed parsing failed: %v", res.TimeSubscribed)
|
|
}
|
|
}
|
|
|
|
func TestMailingLists_Extended(t *testing.T) {
|
|
cli := getTestCli(t)
|
|
defer cli.Close()
|
|
|
|
const (
|
|
domain = "test.domain.name"
|
|
owner = "testuser1@test.domain.name"
|
|
listAddr = "ext-test-list@test.domain.name"
|
|
newName = "renamed-sdk-list"
|
|
testSub = "testuser2@test.domain.name"
|
|
)
|
|
|
|
// Гарантируем чистоту перед тестом
|
|
_ = cli.DeleteList(listAddr)
|
|
_ = cli.DeleteList(newName + "@" + domain)
|
|
|
|
t.Run("ListOperations_And_Renaming", func(t *testing.T) {
|
|
// 1. Создание и переименование
|
|
if err := cli.CreateList(listAddr, owner); err != nil {
|
|
t.Fatalf("Create failed: %v", err)
|
|
}
|
|
|
|
if err := cli.RenameList(listAddr, newName); err != nil {
|
|
t.Errorf("RenameList failed: %v", err)
|
|
}
|
|
|
|
finalAddr := newName + "@" + domain
|
|
defer cli.DeleteList(finalAddr)
|
|
|
|
// 2. Листинги уровней (GetAccountLists, GetDomainLists, ListLists)
|
|
// Проверяем списки аккаунта
|
|
accLists, err := cli.GetAccountLists(owner)
|
|
if err != nil {
|
|
t.Errorf("GetAccountLists failed: %v", err)
|
|
}
|
|
t.Logf("Account lists: %v", accLists)
|
|
|
|
// Проверяем списки домена
|
|
domLists, err := cli.GetDomainLists(domain)
|
|
if err != nil {
|
|
t.Errorf("GetDomainLists failed: %v", err)
|
|
}
|
|
if _, ok := domLists[newName]; !ok {
|
|
t.Errorf("Renamed list %s not found in GetDomainLists", newName)
|
|
}
|
|
|
|
// Проверяем ListLists
|
|
_, err = cli.ListLists("")
|
|
if err != nil {
|
|
t.Errorf("ListLists failed: %v", err)
|
|
}
|
|
|
|
allLists, err := cli.ListLists(domain)
|
|
if err != nil {
|
|
t.Errorf("ListLists failed: %v", err)
|
|
}
|
|
t.Logf("All lists in domain: %v", allLists)
|
|
|
|
// 3. Подписчики и режимы (ListSubscribers, ReadSubscribers, SetPostingMode)
|
|
_ = cli.List(finalAddr, "feed", testSub, true, false)
|
|
|
|
subs, err := cli.ListSubscribers(finalAddr, "", 0)
|
|
if err != nil {
|
|
t.Errorf("ListSubscribers failed: %v", err)
|
|
}
|
|
if len(subs) == 0 {
|
|
t.Error("ListSubscribers returned 0 subscribers")
|
|
}
|
|
|
|
fullSubs, err := cli.ReadSubscribers(finalAddr, "", 10)
|
|
if err != nil {
|
|
t.Errorf("ReadSubscribers failed: %v", err)
|
|
}
|
|
if len(fullSubs) == 0 {
|
|
t.Error("ReadSubscribers returned 0 info structures")
|
|
}
|
|
|
|
// Установка режима публикации
|
|
if err := cli.SetPostingMode(finalAddr, testSub, "MODERATEALL"); err != nil {
|
|
t.Errorf("SetPostingMode failed with MODERATEALL: %v", err)
|
|
}
|
|
|
|
// Или число (модерировать первые 5 сообщений)
|
|
if err := cli.SetPostingMode(finalAddr, testSub, 5); err != nil {
|
|
t.Errorf("SetPostingMode failed with numeric limit: %v", err)
|
|
}
|
|
|
|
// 4. Обработка баунсов (ProcessBounce)
|
|
// Это имитация ошибки доставки для проверки работы метода
|
|
if err := cli.ProcessBounce(finalAddr, testSub, false); err != nil {
|
|
t.Errorf("ProcessBounce (soft) failed: %v", err)
|
|
}
|
|
if err := cli.ProcessBounce(finalAddr, testSub, true); err != nil {
|
|
t.Errorf("ProcessBounce (fatal) failed: %v", err)
|
|
}
|
|
})
|
|
}
|