117 lines
4.0 KiB
Go
117 lines
4.0 KiB
Go
package ws
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestCheckOriginUsesConfiguredAllowlist(t *testing.T) {
|
|
SetAllowedOrigins([]string{"https://portal.example"})
|
|
defer SetAllowedOrigins([]string{"*"})
|
|
allowed := httptest.NewRequest("GET", "/api/ws", nil)
|
|
allowed.Header.Set("Origin", "https://portal.example")
|
|
if !checkOrigin(allowed) {
|
|
t.Fatal("白名单来源应允许 WebSocket 升级")
|
|
}
|
|
denied := httptest.NewRequest("GET", "/api/ws", nil)
|
|
denied.Header.Set("Origin", "https://evil.example")
|
|
if checkOrigin(denied) {
|
|
t.Fatal("非白名单来源不应允许 WebSocket 升级")
|
|
}
|
|
}
|
|
|
|
func TestBroadcastToSessionRestrictsRecipients(t *testing.T) {
|
|
hub := NewHub()
|
|
go hub.Run()
|
|
|
|
sessionID := uint(12)
|
|
agentID := uint(7)
|
|
assignedAgent := &Client{TenantID: 1, UserID: agentID, Role: "agent", Kind: "agent", Send: make(chan []byte, 1)}
|
|
otherAgent := &Client{TenantID: 1, UserID: 8, Role: "agent", Kind: "agent", Send: make(chan []byte, 1)}
|
|
supervisor := &Client{TenantID: 1, UserID: 9, Role: "supervisor", Kind: "agent", Send: make(chan []byte, 1)}
|
|
visitor := &Client{TenantID: 1, Kind: "visitor", SessionID: &sessionID, Send: make(chan []byte, 1)}
|
|
otherSessionID := uint(13)
|
|
otherVisitor := &Client{TenantID: 1, Kind: "visitor", SessionID: &otherSessionID, Send: make(chan []byte, 1)}
|
|
otherTenant := &Client{TenantID: 2, UserID: 7, Role: "agent", Kind: "agent", Send: make(chan []byte, 1)}
|
|
|
|
for _, client := range []*Client{assignedAgent, otherAgent, supervisor, visitor, otherVisitor, otherTenant} {
|
|
hub.register <- client
|
|
}
|
|
|
|
hub.BroadcastToSession(1, sessionID, &agentID, []byte(`{"type":"message"}`))
|
|
for _, client := range []*Client{assignedAgent, supervisor, visitor} {
|
|
select {
|
|
case <-client.Send:
|
|
case <-time.After(time.Second):
|
|
t.Fatalf("应收到会话消息的客户端未收到:%+v", client)
|
|
}
|
|
}
|
|
for _, client := range []*Client{otherAgent, otherVisitor, otherTenant} {
|
|
select {
|
|
case <-client.Send:
|
|
t.Fatalf("不应收到会话消息的客户端收到消息:%+v", client)
|
|
default:
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBroadcastToVisitorRestrictsSession(t *testing.T) {
|
|
hub := NewHub()
|
|
go hub.Run()
|
|
|
|
sessionID := uint(21)
|
|
otherSessionID := uint(22)
|
|
visitor := &Client{TenantID: 1, Kind: "visitor", SessionID: &sessionID, Send: make(chan []byte, 1)}
|
|
otherVisitor := &Client{TenantID: 1, Kind: "visitor", SessionID: &otherSessionID, Send: make(chan []byte, 1)}
|
|
agent := &Client{TenantID: 1, Kind: "agent", UserID: 1, Role: "agent", Send: make(chan []byte, 1)}
|
|
for _, client := range []*Client{visitor, otherVisitor, agent} {
|
|
hub.register <- client
|
|
}
|
|
|
|
hub.BroadcastToVisitor(1, sessionID, []byte(`{"type":"typing"}`))
|
|
select {
|
|
case <-visitor.Send:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("目标访客未收到输入状态")
|
|
}
|
|
for _, client := range []*Client{otherVisitor, agent} {
|
|
select {
|
|
case <-client.Send:
|
|
t.Fatalf("无关客户端收到访客状态:%+v", client)
|
|
default:
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBroadcastToSessionStaffExcludesVisitor(t *testing.T) {
|
|
hub := NewHub()
|
|
go hub.Run()
|
|
|
|
sessionID := uint(31)
|
|
agentID := uint(3)
|
|
assigned := &Client{TenantID: 1, UserID: agentID, Role: "agent", Kind: "agent", Send: make(chan []byte, 1)}
|
|
otherAgent := &Client{TenantID: 1, UserID: 4, Role: "agent", Kind: "agent", Send: make(chan []byte, 1)}
|
|
supervisor := &Client{TenantID: 1, UserID: 5, Role: "supervisor", Kind: "agent", Send: make(chan []byte, 1)}
|
|
visitor := &Client{TenantID: 1, Kind: "visitor", SessionID: &sessionID, Send: make(chan []byte, 1)}
|
|
for _, client := range []*Client{assigned, otherAgent, supervisor, visitor} {
|
|
hub.register <- client
|
|
}
|
|
|
|
hub.BroadcastToSessionStaff(1, &agentID, []byte(`{"type":"typing","data":{"from":"visitor"}}`))
|
|
for _, client := range []*Client{assigned, supervisor} {
|
|
select {
|
|
case <-client.Send:
|
|
case <-time.After(time.Second):
|
|
t.Fatalf("工作人员未收到输入状态:%+v", client)
|
|
}
|
|
}
|
|
for _, client := range []*Client{otherAgent, visitor} {
|
|
select {
|
|
case <-client.Send:
|
|
t.Fatalf("不应收到的客户端收到消息:%+v", client)
|
|
default:
|
|
}
|
|
}
|
|
}
|