| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- package handler
- import (
- "context"
- "testing"
- "github.com/mk6i/retro-aim-server/oscar"
- "github.com/mk6i/retro-aim-server/state"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/mock"
- )
- func TestChatService_ChannelMsgToHostHandler(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 *state.Session
- // inputSNAC is the SNAC sent by the sender client
- inputSNAC oscar.SNACMessage
- // mockParams is the list of params sent to mocks that satisfy this
- // method's dependencies
- mockParams mockParams
- // expectSNACToParticipants is the message the server broadcast to chat
- // room participants (except the sender)
- expectSNACToParticipants oscar.SNACMessage
- expectOutput *oscar.SNACMessage
- wantErr error
- }{
- {
- name: "send chat room message, expect acknowledgement to sender client",
- userSession: newTestSession("user_sending_chat_msg", sessOptCannedSignonTime),
- inputSNAC: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- {
- Tag: oscar.ChatTLVPublicWhisperFlag,
- Value: []byte{},
- },
- {
- Tag: oscar.ChatTLVEnableReflectionFlag,
- Value: []byte{},
- },
- },
- },
- },
- },
- mockParams: mockParams{
- chatRegistryParams: chatRegistryParams{
- chatRegistryRetrieveParams: chatRegistryRetrieveParams{
- chatID: "the-chat-id",
- },
- },
- },
- expectSNACToParticipants: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.Chat,
- SubGroup: oscar.ChatChannelMsgToClient,
- },
- Body: 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.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.Chat,
- SubGroup: oscar.ChatChannelMsgToClient,
- RequestID: 1234,
- },
- Body: 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.SNACMessage{
- Frame: oscar.SNACFrame{
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- {
- Tag: oscar.ChatTLVPublicWhisperFlag,
- Value: []byte{},
- },
- },
- },
- },
- },
- mockParams: mockParams{
- chatRegistryParams: chatRegistryParams{
- chatRegistryRetrieveParams: chatRegistryRetrieveParams{
- chatID: "the-chat-id",
- },
- },
- },
- expectSNACToParticipants: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- FoodGroup: oscar.Chat,
- SubGroup: oscar.ChatChannelMsgToClient,
- },
- Body: 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()),
- },
- },
- },
- },
- },
- {
- name: "send chat room message, fail due to missing chat room",
- userSession: newTestSession("user_sending_chat_msg", sessOptCannedSignonTime),
- inputSNAC: oscar.SNACMessage{
- Frame: oscar.SNACFrame{
- RequestID: 1234,
- },
- Body: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
- Cookie: 1234,
- Channel: 14,
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- {
- Tag: oscar.ChatTLVPublicWhisperFlag,
- Value: []byte{},
- },
- },
- },
- },
- },
- mockParams: mockParams{
- chatRegistryParams: chatRegistryParams{
- chatRegistryRetrieveParams: chatRegistryRetrieveParams{
- chatID: "the-chat-id",
- err: state.ErrChatRoomNotFound,
- },
- },
- },
- wantErr: state.ErrChatRoomNotFound,
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- chatID := "the-chat-id"
- chatSessMgr := newMockChatMessageRelayer(t)
- if tc.mockParams.chatRegistryRetrieveParams.err == nil {
- chatSessMgr.EXPECT().
- RelayToAllExcept(mock.Anything, tc.userSession, tc.expectSNACToParticipants)
- }
- chatRegistry := newMockChatRegistry(t)
- chatRegistry.EXPECT().
- Retrieve(tc.mockParams.chatRegistryRetrieveParams.chatID).
- Return(state.ChatRoom{}, chatSessMgr, tc.mockParams.chatRegistryRetrieveParams.err)
- svc := NewChatService(chatRegistry)
- outputSNAC, err := svc.ChannelMsgToHostHandler(context.Background(), tc.userSession, chatID,
- tc.inputSNAC.Frame, tc.inputSNAC.Body.(oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost))
- assert.ErrorIs(t, err, tc.wantErr)
- assert.Equal(t, tc.expectOutput, outputSNAC)
- })
- }
- }
|