chat_test.go 4.1 KB

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