fix(HH-581): keep widget websocket on visitor auth (#152)

Co-authored-by: Rogee <rogee@ipao.vip>
This commit is contained in:
Rogee
2026-08-24 10:54:38 +08:00
committed by GitHub
co-authored by rogee
parent c4ac664deb
commit 2c6bee2b37
5 changed files with 78 additions and 4 deletions
@@ -3,6 +3,7 @@ package app
import (
"bufio"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
@@ -15,6 +16,7 @@ import (
"github.com/alicebob/miniredis/v2"
miniredisserver "github.com/alicebob/miniredis/v2/server"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/require"
"gorm.io/driver/postgres"
"gorm.io/gorm"
@@ -77,11 +79,16 @@ func TestBootstrapRoutesDurableRealtimeToHubAndSSEWithoutDuplicates(t *testing.T
user := model.User{AccountID: account.ID, Name: "Bootstrap SSE", Email: fmt.Sprintf("bootstrap-sse-%d@example.test", stamp), Password: "unused", Provider: "email", Active: true}
require.NoError(t, application.db.Create(&user).Error)
require.NoError(t, application.db.Create(&model.AccountUser{UserID: user.ID, AccountID: account.ID, Role: "administrator"}).Error)
visitorToken := fmt.Sprintf("bootstrap-visitor-%d", stamp)
inbox := model.Inbox{AccountID: account.ID, Name: "Bootstrap widget", ChannelType: "Channel::WebWidget", Enabled: true}
require.NoError(t, application.db.Create(&inbox).Error)
contact := model.Contact{AccountID: account.ID, Name: "Bootstrap visitor"}
require.NoError(t, application.db.Create(&contact).Error)
require.NoError(t, application.db.Create(&model.ContactInbox{ContactID: contact.ID, InboxID: inbox.ID, PubsubToken: visitorToken}).Error)
dashboard := handlerws.NewClient(user.ID, account.ID, nil, application.wsHub)
dashboard.Identifier = fmt.Sprintf(`{"channel":"AccountChannel","account_id":%d}`, account.ID)
application.wsHub.Register(dashboard)
visitorToken := fmt.Sprintf("bootstrap-visitor-%d", stamp)
visitor := handlerws.NewClient(0, account.ID, nil, application.wsHub)
visitor.IsContact = true
visitor.PubsubToken = visitorToken
@@ -94,6 +101,22 @@ func TestBootstrapRoutesDurableRealtimeToHubAndSSEWithoutDuplicates(t *testing.T
server := httptest.NewServer(application.Handler())
t.Cleanup(server.Close)
visitorConn, _, err := websocket.DefaultDialer.Dial(
"ws"+strings.TrimPrefix(server.URL, "http")+"/cable?pubsub_token="+url.QueryEscape(visitorToken),
nil,
)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, visitorConn.Close()) })
require.NoError(t, visitorConn.SetReadDeadline(time.Now().Add(time.Second)))
_, _, err = visitorConn.ReadMessage()
require.NoError(t, err)
identifier := fmt.Sprintf(`{"channel":"RoomChannel","pubsub_token":%q}`, visitorToken)
require.NoError(t, visitorConn.WriteJSON(map[string]any{"command": "subscribe", "identifier": identifier}))
var confirmation map[string]any
require.NoError(t, visitorConn.ReadJSON(&confirmation))
require.Equal(t, "confirm_subscription", confirmation["type"])
require.JSONEq(t, identifier, confirmation["identifier"].(string))
requestCtx, cancelRequest := context.WithCancel(context.Background())
t.Cleanup(cancelRequest)
req, err := http.NewRequestWithContext(requestCtx, http.MethodGet, fmt.Sprintf("%s/api/v1/accounts/%d/events", server.URL, account.ID), nil)
@@ -144,6 +167,16 @@ func TestBootstrapRoutesDurableRealtimeToHubAndSSEWithoutDuplicates(t *testing.T
require.True(t, processed)
require.NoError(t, err)
requireHubEvent(t, visitor.Send)
var delivered struct {
Identifier string `json:"identifier"`
Message json.RawMessage `json:"message"`
}
require.NoError(t, visitorConn.ReadJSON(&delivered))
require.JSONEq(t, identifier, delivered.Identifier)
var widgetEvent wspkg.WSMessage
require.NoError(t, json.Unmarshal(delivered.Message, &widgetEvent))
require.Equal(t, wspkg.EventMessageCreated, widgetEvent.Event)
require.Equal(t, float64(7), widgetEvent.Data.(map[string]any)["id"])
require.True(t, tokenFailed.Load())
assertNoHubEvent(t, dashboard.Send)
assertNoHubEvent(t, visitor.Send)