| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- package handler
- import (
- "context"
- "github.com/mkaminski/goaim/oscar"
- "github.com/mkaminski/goaim/server"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/mock"
- "testing"
- )
- func TestSendAndReceiveChatChannelMsgToHost(t *testing.T) {
- cases := []struct {
- // name is the unit test name
- name string
- // userSession is the session of the user sending the chat message
- userSession *server.Session
- // inputSNAC is the SNAC sent by the sender client
- inputSNAC oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost
- // expectSNACToParticipants is the message the server broadcast to chat
- // room participants (except the sender)
- expectSNACToParticipants oscar.XMessage
- expectOutput *oscar.XMessage
- }{
- {
- name: "send chat room message, expect acknowledgement to sender client",
- userSession: newTestSession("user_sending_chat_msg", sessOptCannedSignonTime),
- inputSNAC: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- {
- TType: oscar.ChatTLVPublicWhisperFlag,
- Val: []byte{},
- },
- {
- TType: oscar.ChatTLVEnableReflectionFlag,
- Val: []byte{},
- },
- },
- },
- },
- expectSNACToParticipants: oscar.XMessage{
- SnacFrame: oscar.SnacFrame{
- FoodGroup: oscar.CHAT,
- SubGroup: oscar.ChatChannelMsgToClient,
- },
- SnacOut: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- oscar.NewTLV(oscar.ChatTLVPublicWhisperFlag, []byte{}),
- oscar.NewTLV(oscar.ChatTLVEnableReflectionFlag, []byte{}),
- oscar.NewTLV(oscar.ChatTLVSenderInformation,
- newTestSession("user_sending_chat_msg", sessOptCannedSignonTime).TLVUserInfo()),
- },
- },
- },
- },
- expectOutput: &oscar.XMessage{
- SnacFrame: oscar.SnacFrame{
- FoodGroup: oscar.CHAT,
- SubGroup: oscar.ChatChannelMsgToClient,
- },
- SnacOut: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- oscar.NewTLV(oscar.ChatTLVPublicWhisperFlag, []byte{}),
- oscar.NewTLV(oscar.ChatTLVEnableReflectionFlag, []byte{}),
- oscar.NewTLV(oscar.ChatTLVSenderInformation, newTestSession("user_sending_chat_msg", sessOptCannedSignonTime).TLVUserInfo()),
- },
- },
- },
- },
- },
- {
- name: "send chat room message, don't expect acknowledgement to sender client",
- userSession: newTestSession("user_sending_chat_msg", sessOptCannedSignonTime),
- inputSNAC: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- {
- TType: oscar.ChatTLVPublicWhisperFlag,
- Val: []byte{},
- },
- },
- },
- },
- expectSNACToParticipants: oscar.XMessage{
- SnacFrame: oscar.SnacFrame{
- FoodGroup: oscar.CHAT,
- SubGroup: oscar.ChatChannelMsgToClient,
- },
- SnacOut: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- oscar.NewTLV(oscar.ChatTLVPublicWhisperFlag, []byte{}),
- oscar.NewTLV(oscar.ChatTLVSenderInformation,
- newTestSession("user_sending_chat_msg", sessOptCannedSignonTime).TLVUserInfo()),
- },
- },
- },
- },
- expectOutput: &oscar.XMessage{},
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- //
- // initialize dependencies
- //
- crm := server.NewMockChatSessionManager(t)
- crm.EXPECT().
- BroadcastExcept(mock.Anything, tc.userSession, tc.expectSNACToParticipants)
- //
- // send input SNAC
- //
- svc := ChatService{}
- outputSNAC, err := svc.ChannelMsgToHostHandler(context.Background(), tc.userSession, crm, tc.inputSNAC)
- assert.NoError(t, err)
- if tc.expectOutput.SnacFrame == (oscar.SnacFrame{}) {
- return // handler doesn't return response
- }
- assert.Equal(t, tc.expectOutput, outputSNAC)
- })
- }
- }
|