feat: add im relationship flows

This commit is contained in:
richjohnrambo
2026-08-12 11:31:16 +08:00
parent 5bd35f97a6
commit d02041659e
11 changed files with 1100 additions and 12 deletions
+65 -10
View File
@@ -49,6 +49,13 @@ type TopicTestHelper struct {
uu *mock_store.MockUsersPersistenceInterface
tt *mock_store.MockTopicsPersistenceInterface
ss *mock_store.MockSubsPersistenceInterface
oldMessages store.MessagesPersistenceInterface
oldUsers store.UsersPersistenceInterface
oldTopics store.TopicsPersistenceInterface
oldSubs store.SubsPersistenceInterface
oldRelations store.RelationsPersistenceInterface
oldModeCP2P types.AccessMode
}
func (b *TopicTestHelper) finish() {
@@ -67,12 +74,14 @@ func (b *TopicTestHelper) finish() {
func (b *TopicTestHelper) newSession(sid string, uid types.Uid) (*Session, *responses) {
s := &Session{
tenantID: 1,
sid: sid,
uid: uid,
subs: make(map[string]*Subscription),
send: make(chan any, 10),
detach: make(chan string, 10),
tenantID: 1,
tenantCode: "test",
sid: sid,
uid: uid,
authLvl: auth.LevelAuth,
subs: make(map[string]*Subscription),
send: make(chan any, 10),
detach: make(chan string, 10),
}
r := &responses{}
b.sessWg.Add(1)
@@ -95,10 +104,18 @@ func (b *TopicTestHelper) setUp(t *testing.T, numUsers int, cat types.TopicCat,
b.uu = mock_store.NewMockUsersPersistenceInterface(b.ctrl)
b.tt = mock_store.NewMockTopicsPersistenceInterface(b.ctrl)
b.ss = mock_store.NewMockSubsPersistenceInterface(b.ctrl)
b.oldMessages = store.Messages
b.oldUsers = store.Users
b.oldTopics = store.Topics
b.oldSubs = store.Subs
store.Messages = b.mm
store.Users = b.uu
store.Topics = b.tt
store.Subs = b.ss
b.oldRelations = store.Relations
store.Relations = nil
b.oldModeCP2P = globals.typesModeCP2P
globals.typesModeCP2P = types.ModeCP2P
// Sessions.
b.sessions = make([]*Session, b.numUsers)
b.results = make([]*responses, b.numUsers)
@@ -162,10 +179,12 @@ func (b *TopicTestHelper) setUp(t *testing.T, numUsers int, cat types.TopicCat,
func (b *TopicTestHelper) tearDown() {
globals.hub = nil
store.Messages = nil
store.Users = nil
store.Topics = nil
store.Subs = nil
store.Messages = b.oldMessages
store.Users = b.oldUsers
store.Topics = b.oldTopics
store.Subs = b.oldSubs
store.Relations = b.oldRelations
globals.typesModeCP2P = b.oldModeCP2P
b.ctrl.Finish()
}
@@ -279,6 +298,42 @@ func TestHandleBroadcastDataP2P(t *testing.T) {
}
}
func TestHandleBroadcastDataP2PRequiresRelationship(t *testing.T) {
numUsers := 2
helper := TopicTestHelper{}
helper.setUp(t, numUsers, types.TopicCatP2P, "p2p-test" /*attach=*/, true)
defer helper.tearDown()
oldStore := store.Store
mockStore := mock_store.NewMockPersistentStorageInterface(helper.ctrl)
mockStore.EXPECT().IsOpen().Return(true)
store.Store = mockStore
canSend := false
store.Relations = &testRelationsStore{canSend: &canSend, reason: types.RelationshipNone}
defer func() {
store.Store = oldStore
}()
from := helper.uids[0].UserId()
msg := &ClientComMessage{
AsUser: from,
Original: from,
Pub: &MsgClientPub{
Topic: "p2p",
Content: "test",
NoEcho: true,
},
sess: helper.sessions[0],
}
helper.topic.handleClientMsg(msg)
helper.finish()
verifyResponseCodes(helper.results[0], []int{http.StatusForbidden}, t)
if len(helper.results[1].messages) != 0 {
t.Fatalf("peer: expected 0 messages, got %d", len(helper.results[1].messages))
}
}
func TestHandleBroadcastCall(t *testing.T) {
numUsers := 2
helper := TopicTestHelper{}