chat_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. sessOptChatID("the-chat-id")),
  31. inputSNAC: oscar.SNACMessage{
  32. Frame: oscar.SNACFrame{
  33. RequestID: 1234,
  34. },
  35. Body: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  36. Cookie: 1234,
  37. Channel: 14,
  38. TLVRestBlock: oscar.TLVRestBlock{
  39. TLVList: oscar.TLVList{
  40. {
  41. Tag: oscar.ChatTLVPublicWhisperFlag,
  42. Value: []byte{},
  43. },
  44. {
  45. Tag: oscar.ChatTLVEnableReflectionFlag,
  46. Value: []byte{},
  47. },
  48. },
  49. },
  50. },
  51. },
  52. mockParams: mockParams{
  53. chatRegistryParams: chatRegistryParams{
  54. chatRegistryRetrieveParams: chatRegistryRetrieveParams{
  55. chatID: "the-chat-id",
  56. },
  57. },
  58. },
  59. expectSNACToParticipants: oscar.SNACMessage{
  60. Frame: oscar.SNACFrame{
  61. FoodGroup: oscar.Chat,
  62. SubGroup: oscar.ChatChannelMsgToClient,
  63. },
  64. Body: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  65. Cookie: 1234,
  66. Channel: 14,
  67. TLVRestBlock: oscar.TLVRestBlock{
  68. TLVList: oscar.TLVList{
  69. oscar.NewTLV(oscar.ChatTLVPublicWhisperFlag, []byte{}),
  70. oscar.NewTLV(oscar.ChatTLVEnableReflectionFlag, []byte{}),
  71. oscar.NewTLV(oscar.ChatTLVSenderInformation,
  72. newTestSession("user_sending_chat_msg", sessOptCannedSignonTime).TLVUserInfo()),
  73. },
  74. },
  75. },
  76. },
  77. expectOutput: &oscar.SNACMessage{
  78. Frame: oscar.SNACFrame{
  79. FoodGroup: oscar.Chat,
  80. SubGroup: oscar.ChatChannelMsgToClient,
  81. RequestID: 1234,
  82. },
  83. Body: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  84. Cookie: 1234,
  85. Channel: 14,
  86. TLVRestBlock: oscar.TLVRestBlock{
  87. TLVList: oscar.TLVList{
  88. oscar.NewTLV(oscar.ChatTLVPublicWhisperFlag, []byte{}),
  89. oscar.NewTLV(oscar.ChatTLVEnableReflectionFlag, []byte{}),
  90. oscar.NewTLV(oscar.ChatTLVSenderInformation, newTestSession("user_sending_chat_msg", sessOptCannedSignonTime).TLVUserInfo()),
  91. },
  92. },
  93. },
  94. },
  95. },
  96. {
  97. name: "send chat room message, don't expect acknowledgement to sender client",
  98. userSession: newTestSession("user_sending_chat_msg", sessOptCannedSignonTime,
  99. sessOptChatID("the-chat-id")),
  100. inputSNAC: oscar.SNACMessage{
  101. Frame: oscar.SNACFrame{
  102. RequestID: 1234,
  103. },
  104. Body: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  105. Cookie: 1234,
  106. Channel: 14,
  107. TLVRestBlock: oscar.TLVRestBlock{
  108. TLVList: oscar.TLVList{
  109. {
  110. Tag: oscar.ChatTLVPublicWhisperFlag,
  111. Value: []byte{},
  112. },
  113. },
  114. },
  115. },
  116. },
  117. mockParams: mockParams{
  118. chatRegistryParams: chatRegistryParams{
  119. chatRegistryRetrieveParams: chatRegistryRetrieveParams{
  120. chatID: "the-chat-id",
  121. },
  122. },
  123. },
  124. expectSNACToParticipants: oscar.SNACMessage{
  125. Frame: oscar.SNACFrame{
  126. FoodGroup: oscar.Chat,
  127. SubGroup: oscar.ChatChannelMsgToClient,
  128. },
  129. Body: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  130. Cookie: 1234,
  131. Channel: 14,
  132. TLVRestBlock: oscar.TLVRestBlock{
  133. TLVList: oscar.TLVList{
  134. oscar.NewTLV(oscar.ChatTLVPublicWhisperFlag, []byte{}),
  135. oscar.NewTLV(oscar.ChatTLVSenderInformation,
  136. newTestSession("user_sending_chat_msg", sessOptCannedSignonTime).TLVUserInfo()),
  137. },
  138. },
  139. },
  140. },
  141. },
  142. {
  143. name: "send chat room message, fail due to missing chat room",
  144. userSession: newTestSession("user_sending_chat_msg", sessOptCannedSignonTime,
  145. sessOptChatID("the-chat-id")),
  146. inputSNAC: oscar.SNACMessage{
  147. Frame: oscar.SNACFrame{
  148. RequestID: 1234,
  149. },
  150. Body: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  151. Cookie: 1234,
  152. Channel: 14,
  153. TLVRestBlock: oscar.TLVRestBlock{
  154. TLVList: oscar.TLVList{
  155. {
  156. Tag: oscar.ChatTLVPublicWhisperFlag,
  157. Value: []byte{},
  158. },
  159. },
  160. },
  161. },
  162. },
  163. mockParams: mockParams{
  164. chatRegistryParams: chatRegistryParams{
  165. chatRegistryRetrieveParams: chatRegistryRetrieveParams{
  166. chatID: "the-chat-id",
  167. err: state.ErrChatRoomNotFound,
  168. },
  169. },
  170. },
  171. wantErr: state.ErrChatRoomNotFound,
  172. },
  173. }
  174. for _, tc := range cases {
  175. t.Run(tc.name, func(t *testing.T) {
  176. chatSessMgr := newMockChatMessageRelayer(t)
  177. if tc.mockParams.chatRegistryRetrieveParams.err == nil {
  178. chatSessMgr.EXPECT().
  179. RelayToAllExcept(mock.Anything, tc.userSession, tc.expectSNACToParticipants)
  180. }
  181. chatRegistry := newMockChatRegistry(t)
  182. chatRegistry.EXPECT().
  183. Retrieve(tc.mockParams.chatRegistryRetrieveParams.chatID).
  184. Return(state.ChatRoom{}, chatSessMgr, tc.mockParams.chatRegistryRetrieveParams.err)
  185. svc := NewChatService(chatRegistry)
  186. outputSNAC, err := svc.ChannelMsgToHostHandler(context.Background(), tc.userSession, tc.inputSNAC.Frame,
  187. tc.inputSNAC.Body.(oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost))
  188. assert.ErrorIs(t, err, tc.wantErr)
  189. assert.Equal(t, tc.expectOutput, outputSNAC)
  190. })
  191. }
  192. }