chat_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package handler
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/mkaminski/goaim/oscar"
  6. "github.com/mkaminski/goaim/state"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/mock"
  9. )
  10. func TestSendAndReceiveChatChannelMsgToHost(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. // expectSNACToParticipants is the message the server broadcast to chat
  19. // room participants (except the sender)
  20. expectSNACToParticipants oscar.SNACMessage
  21. expectOutput *oscar.SNACMessage
  22. }{
  23. {
  24. name: "send chat room message, expect acknowledgement to sender client",
  25. userSession: newTestSession("user_sending_chat_msg", sessOptCannedSignonTime),
  26. inputSNAC: oscar.SNACMessage{
  27. Frame: oscar.SNACFrame{
  28. RequestID: 1234,
  29. },
  30. Body: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  31. Cookie: 1234,
  32. Channel: 14,
  33. TLVRestBlock: oscar.TLVRestBlock{
  34. TLVList: oscar.TLVList{
  35. {
  36. TType: oscar.ChatTLVPublicWhisperFlag,
  37. Val: []byte{},
  38. },
  39. {
  40. TType: oscar.ChatTLVEnableReflectionFlag,
  41. Val: []byte{},
  42. },
  43. },
  44. },
  45. },
  46. },
  47. expectSNACToParticipants: oscar.SNACMessage{
  48. Frame: oscar.SNACFrame{
  49. FoodGroup: oscar.Chat,
  50. SubGroup: oscar.ChatChannelMsgToClient,
  51. },
  52. Body: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  53. Cookie: 1234,
  54. Channel: 14,
  55. TLVRestBlock: oscar.TLVRestBlock{
  56. TLVList: oscar.TLVList{
  57. oscar.NewTLV(oscar.ChatTLVPublicWhisperFlag, []byte{}),
  58. oscar.NewTLV(oscar.ChatTLVEnableReflectionFlag, []byte{}),
  59. oscar.NewTLV(oscar.ChatTLVSenderInformation,
  60. newTestSession("user_sending_chat_msg", sessOptCannedSignonTime).TLVUserInfo()),
  61. },
  62. },
  63. },
  64. },
  65. expectOutput: &oscar.SNACMessage{
  66. Frame: oscar.SNACFrame{
  67. FoodGroup: oscar.Chat,
  68. SubGroup: oscar.ChatChannelMsgToClient,
  69. RequestID: 1234,
  70. },
  71. Body: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  72. Cookie: 1234,
  73. Channel: 14,
  74. TLVRestBlock: oscar.TLVRestBlock{
  75. TLVList: oscar.TLVList{
  76. oscar.NewTLV(oscar.ChatTLVPublicWhisperFlag, []byte{}),
  77. oscar.NewTLV(oscar.ChatTLVEnableReflectionFlag, []byte{}),
  78. oscar.NewTLV(oscar.ChatTLVSenderInformation, newTestSession("user_sending_chat_msg", sessOptCannedSignonTime).TLVUserInfo()),
  79. },
  80. },
  81. },
  82. },
  83. },
  84. {
  85. name: "send chat room message, don't expect acknowledgement to sender client",
  86. userSession: newTestSession("user_sending_chat_msg", sessOptCannedSignonTime),
  87. inputSNAC: oscar.SNACMessage{
  88. Frame: oscar.SNACFrame{
  89. RequestID: 1234,
  90. },
  91. Body: oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost{
  92. Cookie: 1234,
  93. Channel: 14,
  94. TLVRestBlock: oscar.TLVRestBlock{
  95. TLVList: oscar.TLVList{
  96. {
  97. TType: oscar.ChatTLVPublicWhisperFlag,
  98. Val: []byte{},
  99. },
  100. },
  101. },
  102. },
  103. },
  104. expectSNACToParticipants: oscar.SNACMessage{
  105. Frame: oscar.SNACFrame{
  106. FoodGroup: oscar.Chat,
  107. SubGroup: oscar.ChatChannelMsgToClient,
  108. },
  109. Body: oscar.SNAC_0x0E_0x06_ChatChannelMsgToClient{
  110. Cookie: 1234,
  111. Channel: 14,
  112. TLVRestBlock: oscar.TLVRestBlock{
  113. TLVList: oscar.TLVList{
  114. oscar.NewTLV(oscar.ChatTLVPublicWhisperFlag, []byte{}),
  115. oscar.NewTLV(oscar.ChatTLVSenderInformation,
  116. newTestSession("user_sending_chat_msg", sessOptCannedSignonTime).TLVUserInfo()),
  117. },
  118. },
  119. },
  120. },
  121. expectOutput: &oscar.SNACMessage{},
  122. },
  123. }
  124. for _, tc := range cases {
  125. t.Run(tc.name, func(t *testing.T) {
  126. chatID := "the-chat-id"
  127. chatSessMgr := newMockChatMessageRelayer(t)
  128. chatSessMgr.EXPECT().
  129. RelayToAllExcept(mock.Anything, tc.userSession, tc.expectSNACToParticipants)
  130. svc := ChatService{
  131. chatRegistry: state.NewChatRegistry(),
  132. }
  133. svc.chatRegistry.Register(state.ChatRoom{Cookie: chatID}, chatSessMgr)
  134. outputSNAC, err := svc.ChannelMsgToHostHandler(context.Background(), tc.userSession, chatID,
  135. tc.inputSNAC.Frame, tc.inputSNAC.Body.(oscar.SNAC_0x0E_0x05_ChatChannelMsgToHost))
  136. assert.NoError(t, err)
  137. if tc.expectOutput.Frame == (oscar.SNACFrame{}) {
  138. return // handler doesn't return response
  139. }
  140. assert.Equal(t, tc.expectOutput, outputSNAC)
  141. })
  142. }
  143. }