chat_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package handler
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/mk6i/retro-aim-server/oscar"
  6. "github.com/mk6i/retro-aim-server/state"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/mock"
  9. )
  10. func TestChatService_ChannelMsgToHostHandler(t *testing.T) {
  11. cases := []struct {
  12. // name is the unit test name
  13. name string
  14. // userSession is the session of the user sending the chat message
  15. userSession *state.Session
  16. // inputSNAC is the SNAC sent by the sender client
  17. inputSNAC oscar.SNACMessage
  18. // mockParams is the list of params sent to mocks that satisfy this
  19. // method's dependencies
  20. mockParams mockParams
  21. // expectSNACToParticipants is the message the server broadcast to chat
  22. // room participants (except the sender)
  23. expectSNACToParticipants oscar.SNACMessage
  24. expectOutput *oscar.SNACMessage
  25. wantErr error
  26. }{
  27. {
  28. name: "send chat room message, expect acknowledgement to sender client",
  29. userSession: newTestSession("user_sending_chat_msg", sessOptCannedSignonTime),
  30. inputSNAC: oscar.SNACMessage{
  31. Frame: oscar.SNACFrame{
  32. RequestID: 1234,
  33. },
  34. Body: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  35. Cookie: 1234,
  36. Channel: 14,
  37. TLVRestBlock: oscar.TLVRestBlock{
  38. TLVList: oscar.TLVList{
  39. {
  40. Tag: oscar.ChatTLVPublicWhisperFlag,
  41. Value: []byte{},
  42. },
  43. {
  44. Tag: oscar.ChatTLVEnableReflectionFlag,
  45. Value: []byte{},
  46. },
  47. },
  48. },
  49. },
  50. },
  51. mockParams: mockParams{
  52. chatRegistryParams: chatRegistryParams{
  53. chatRegistryRetrieveParams: chatRegistryRetrieveParams{
  54. chatID: "the-chat-id",
  55. },
  56. },
  57. },
  58. expectSNACToParticipants: oscar.SNACMessage{
  59. Frame: oscar.SNACFrame{
  60. FoodGroup: oscar.Chat,
  61. SubGroup: oscar.ChatChannelMsgToClient,
  62. },
  63. Body: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  64. Cookie: 1234,
  65. Channel: 14,
  66. TLVRestBlock: oscar.TLVRestBlock{
  67. TLVList: oscar.TLVList{
  68. oscar.NewTLV(oscar.ChatTLVPublicWhisperFlag, []byte{}),
  69. oscar.NewTLV(oscar.ChatTLVEnableReflectionFlag, []byte{}),
  70. oscar.NewTLV(oscar.ChatTLVSenderInformation,
  71. newTestSession("user_sending_chat_msg", sessOptCannedSignonTime).TLVUserInfo()),
  72. },
  73. },
  74. },
  75. },
  76. expectOutput: &oscar.SNACMessage{
  77. Frame: oscar.SNACFrame{
  78. FoodGroup: oscar.Chat,
  79. SubGroup: oscar.ChatChannelMsgToClient,
  80. RequestID: 1234,
  81. },
  82. Body: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  83. Cookie: 1234,
  84. Channel: 14,
  85. TLVRestBlock: oscar.TLVRestBlock{
  86. TLVList: oscar.TLVList{
  87. oscar.NewTLV(oscar.ChatTLVPublicWhisperFlag, []byte{}),
  88. oscar.NewTLV(oscar.ChatTLVEnableReflectionFlag, []byte{}),
  89. oscar.NewTLV(oscar.ChatTLVSenderInformation, newTestSession("user_sending_chat_msg", sessOptCannedSignonTime).TLVUserInfo()),
  90. },
  91. },
  92. },
  93. },
  94. },
  95. {
  96. name: "send chat room message, don't expect acknowledgement to sender client",
  97. userSession: newTestSession("user_sending_chat_msg", sessOptCannedSignonTime),
  98. inputSNAC: oscar.SNACMessage{
  99. Frame: oscar.SNACFrame{
  100. RequestID: 1234,
  101. },
  102. Body: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  103. Cookie: 1234,
  104. Channel: 14,
  105. TLVRestBlock: oscar.TLVRestBlock{
  106. TLVList: oscar.TLVList{
  107. {
  108. Tag: oscar.ChatTLVPublicWhisperFlag,
  109. Value: []byte{},
  110. },
  111. },
  112. },
  113. },
  114. },
  115. mockParams: mockParams{
  116. chatRegistryParams: chatRegistryParams{
  117. chatRegistryRetrieveParams: chatRegistryRetrieveParams{
  118. chatID: "the-chat-id",
  119. },
  120. },
  121. },
  122. expectSNACToParticipants: oscar.SNACMessage{
  123. Frame: oscar.SNACFrame{
  124. FoodGroup: oscar.Chat,
  125. SubGroup: oscar.ChatChannelMsgToClient,
  126. },
  127. Body: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  128. Cookie: 1234,
  129. Channel: 14,
  130. TLVRestBlock: oscar.TLVRestBlock{
  131. TLVList: oscar.TLVList{
  132. oscar.NewTLV(oscar.ChatTLVPublicWhisperFlag, []byte{}),
  133. oscar.NewTLV(oscar.ChatTLVSenderInformation,
  134. newTestSession("user_sending_chat_msg", sessOptCannedSignonTime).TLVUserInfo()),
  135. },
  136. },
  137. },
  138. },
  139. },
  140. {
  141. name: "send chat room message, fail due to missing chat room",
  142. userSession: newTestSession("user_sending_chat_msg", sessOptCannedSignonTime),
  143. inputSNAC: oscar.SNACMessage{
  144. Frame: oscar.SNACFrame{
  145. RequestID: 1234,
  146. },
  147. Body: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  148. Cookie: 1234,
  149. Channel: 14,
  150. TLVRestBlock: oscar.TLVRestBlock{
  151. TLVList: oscar.TLVList{
  152. {
  153. Tag: oscar.ChatTLVPublicWhisperFlag,
  154. Value: []byte{},
  155. },
  156. },
  157. },
  158. },
  159. },
  160. mockParams: mockParams{
  161. chatRegistryParams: chatRegistryParams{
  162. chatRegistryRetrieveParams: chatRegistryRetrieveParams{
  163. chatID: "the-chat-id",
  164. err: state.ErrChatRoomNotFound,
  165. },
  166. },
  167. },
  168. wantErr: state.ErrChatRoomNotFound,
  169. },
  170. }
  171. for _, tc := range cases {
  172. t.Run(tc.name, func(t *testing.T) {
  173. chatID := "the-chat-id"
  174. chatSessMgr := newMockChatMessageRelayer(t)
  175. if tc.mockParams.chatRegistryRetrieveParams.err == nil {
  176. chatSessMgr.EXPECT().
  177. RelayToAllExcept(mock.Anything, tc.userSession, tc.expectSNACToParticipants)
  178. }
  179. chatRegistry := newMockChatRegistry(t)
  180. chatRegistry.EXPECT().
  181. Retrieve(tc.mockParams.chatRegistryRetrieveParams.chatID).
  182. Return(state.ChatRoom{}, chatSessMgr, tc.mockParams.chatRegistryRetrieveParams.err)
  183. svc := NewChatService(chatRegistry)
  184. outputSNAC, err := svc.ChannelMsgToHostHandler(context.Background(), tc.userSession, chatID,
  185. tc.inputSNAC.Frame, tc.inputSNAC.Body.(oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost))
  186. assert.ErrorIs(t, err, tc.wantErr)
  187. assert.Equal(t, tc.expectOutput, outputSNAC)
  188. })
  189. }
  190. }