fix: persist settings for websocket groups
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/tinode/chat/server/auth"
|
||||
"github.com/tinode/chat/server/store"
|
||||
"github.com/tinode/chat/server/store/types"
|
||||
)
|
||||
|
||||
func TestInitTopicNewGrpCreatesBusinessSettings(t *testing.T) {
|
||||
previous := store.GroupBusiness
|
||||
groupStore := &testGroupBusinessStore{}
|
||||
store.GroupBusiness = groupStore
|
||||
defer func() { store.GroupBusiness = previous }()
|
||||
|
||||
owner := types.Uid(42)
|
||||
topic := &Topic{
|
||||
tenantID: 7,
|
||||
name: "grpWebsocket",
|
||||
xoriginal: "new",
|
||||
perUser: make(map[types.Uid]perUserData),
|
||||
}
|
||||
request := &ClientComMessage{
|
||||
RcptTo: topic.name,
|
||||
AsUser: owner.UserId(),
|
||||
AuthLvl: int(auth.LevelAuth),
|
||||
Sub: &MsgClientSub{Topic: "new", Set: &MsgSetQuery{Desc: &MsgSetDesc{Public: map[string]any{
|
||||
"fn": "WebSocket 测试群",
|
||||
"description": "业务资料需要原子创建",
|
||||
}}}},
|
||||
}
|
||||
|
||||
if err := initTopicNewGrp(topic, request, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if groupStore.settings == nil || groupStore.settings.Topic != topic.name || groupStore.settings.OwnerUserID != owner {
|
||||
t.Fatalf("business settings were not created: %#v", groupStore.settings)
|
||||
}
|
||||
if !request.Sub.Created || !request.Sub.Newsub || topic.subCnt != 1 || topic.xoriginal != topic.name {
|
||||
t.Fatalf("unexpected initialized topic: created=%v newsub=%v subcnt=%d original=%q",
|
||||
request.Sub.Created, request.Sub.Newsub, topic.subCnt, topic.xoriginal)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewWebsocketGroupSettings(t *testing.T) {
|
||||
now := types.TimeNow()
|
||||
owner := types.Uid(42)
|
||||
topic := &types.Topic{
|
||||
ObjHeader: types.ObjHeader{Id: "grpWebsocket"},
|
||||
Public: map[string]any{
|
||||
"fn": "WebSocket 测试群",
|
||||
"avatar_file_id": "avatar.png",
|
||||
"description": "通过 sub topic:new 创建",
|
||||
},
|
||||
}
|
||||
|
||||
settings := newWebsocketGroupSettings(topic, owner, now)
|
||||
if settings.Topic != topic.Id || settings.OwnerUserID != owner || settings.CreatedBy != owner {
|
||||
t.Fatalf("unexpected group identity: %#v", settings)
|
||||
}
|
||||
if settings.Name != "WebSocket 测试群" || settings.AvatarFileID != "avatar.png" ||
|
||||
settings.Description != "通过 sub topic:new 创建" {
|
||||
t.Fatalf("unexpected group profile: %#v", settings)
|
||||
}
|
||||
if settings.GroupStatus != types.GroupStatusActive || !settings.MemberUploadFiles ||
|
||||
!settings.MemberPrivateChat || !settings.MemberInvite || settings.MemberCount != 1 ||
|
||||
settings.MaxAdminCount != groupMaxAdminCount || settings.Version != 1 {
|
||||
t.Fatalf("unexpected group defaults: %#v", settings)
|
||||
}
|
||||
if !settings.CreatedAt.Equal(now) || !settings.UpdatedAt.Equal(now) {
|
||||
t.Fatalf("unexpected group timestamps: %#v", settings)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewWebsocketGroupSettingsFallsBackToTopicName(t *testing.T) {
|
||||
topic := &types.Topic{ObjHeader: types.ObjHeader{Id: "grpFallback"}}
|
||||
settings := newWebsocketGroupSettings(topic, types.Uid(7), types.TimeNow())
|
||||
if settings.Name != topic.Id {
|
||||
t.Fatalf("name: got %q, want %q", settings.Name, topic.Id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureLegacyWebsocketGroupSettingsRepairsMissingRecord(t *testing.T) {
|
||||
previous := store.GroupBusiness
|
||||
groupStore := &testGroupBusinessStore{getSettingsErr: types.ErrNotFound}
|
||||
store.GroupBusiness = groupStore
|
||||
defer func() { store.GroupBusiness = previous }()
|
||||
|
||||
created := types.TimeNow().Add(-time.Hour)
|
||||
updated := types.TimeNow()
|
||||
topic := &types.Topic{
|
||||
ObjHeader: types.ObjHeader{Id: "grpLegacy", CreatedAt: created, UpdatedAt: updated},
|
||||
Public: map[string]any{"fn": "历史 WebSocket 群"},
|
||||
}
|
||||
owner := types.Uid(42)
|
||||
if err := ensureLegacyWebsocketGroupSettings(7, topic, owner, 3); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if groupStore.settings == nil || groupStore.settings.Topic != topic.Id || groupStore.settings.MemberCount != 3 {
|
||||
t.Fatalf("legacy settings were not repaired: %#v", groupStore.settings)
|
||||
}
|
||||
if !groupStore.settings.CreatedAt.Equal(created) || !groupStore.settings.UpdatedAt.Equal(updated) {
|
||||
t.Fatalf("legacy timestamps were not preserved: %#v", groupStore.settings)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user