chat_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package webhooks
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/owncast/owncast/core/chat/events"
  6. "github.com/owncast/owncast/core/user"
  7. "github.com/owncast/owncast/models"
  8. )
  9. func TestSendChatEvent(t *testing.T) {
  10. timestamp := time.Unix(72, 6).UTC()
  11. user := user.User{
  12. ID: "user id",
  13. DisplayName: "display name",
  14. DisplayColor: 4,
  15. CreatedAt: time.Unix(3, 26).UTC(),
  16. DisabledAt: nil,
  17. PreviousNames: []string{"somebody"},
  18. NameChangedAt: nil,
  19. Scopes: []string{},
  20. IsBot: false,
  21. AuthenticatedAt: nil,
  22. Authenticated: false,
  23. }
  24. checkPayload(t, models.MessageSent, func() {
  25. SendChatEvent(&events.UserMessageEvent{
  26. Event: events.Event{
  27. Type: events.MessageSent,
  28. ID: "id",
  29. Timestamp: timestamp,
  30. },
  31. UserEvent: events.UserEvent{
  32. User: &user,
  33. ClientID: 51,
  34. HiddenAt: nil,
  35. },
  36. MessageEvent: events.MessageEvent{
  37. OutboundEvent: nil,
  38. Body: "body",
  39. RawBody: "raw body",
  40. },
  41. })
  42. }, `{
  43. "body": "body",
  44. "clientId": 51,
  45. "id": "id",
  46. "rawBody": "raw body",
  47. "timestamp": "1970-01-01T00:01:12.000000006Z",
  48. "user": {
  49. "authenticated": false,
  50. "createdAt": "1970-01-01T00:00:03.000000026Z",
  51. "displayColor": 4,
  52. "displayName": "display name",
  53. "id": "user id",
  54. "isBot": false,
  55. "previousNames": ["somebody"]
  56. },
  57. "visible": true
  58. }`)
  59. }
  60. func TestSendChatEventUsernameChanged(t *testing.T) {
  61. timestamp := time.Unix(72, 6).UTC()
  62. user := user.User{
  63. ID: "user id",
  64. DisplayName: "display name",
  65. DisplayColor: 4,
  66. CreatedAt: time.Unix(3, 26).UTC(),
  67. DisabledAt: nil,
  68. PreviousNames: []string{"somebody"},
  69. NameChangedAt: nil,
  70. Scopes: []string{},
  71. IsBot: false,
  72. AuthenticatedAt: nil,
  73. Authenticated: false,
  74. }
  75. checkPayload(t, models.UserNameChanged, func() {
  76. SendChatEventUsernameChanged(events.NameChangeEvent{
  77. Event: events.Event{
  78. Type: events.UserNameChanged,
  79. ID: "id",
  80. Timestamp: timestamp,
  81. },
  82. UserEvent: events.UserEvent{
  83. User: &user,
  84. ClientID: 51,
  85. HiddenAt: nil,
  86. },
  87. NewName: "new name",
  88. })
  89. }, `{
  90. "clientId": 51,
  91. "id": "id",
  92. "newName": "new name",
  93. "timestamp": "1970-01-01T00:01:12.000000006Z",
  94. "type": "NAME_CHANGE",
  95. "user": {
  96. "authenticated": false,
  97. "createdAt": "1970-01-01T00:00:03.000000026Z",
  98. "displayColor": 4,
  99. "displayName": "display name",
  100. "id": "user id",
  101. "isBot": false,
  102. "previousNames": ["somebody"]
  103. }
  104. }`)
  105. }
  106. func TestSendChatEventUserJoined(t *testing.T) {
  107. timestamp := time.Unix(72, 6).UTC()
  108. user := user.User{
  109. ID: "user id",
  110. DisplayName: "display name",
  111. DisplayColor: 4,
  112. CreatedAt: time.Unix(3, 26).UTC(),
  113. DisabledAt: nil,
  114. PreviousNames: []string{"somebody"},
  115. NameChangedAt: nil,
  116. Scopes: []string{},
  117. IsBot: false,
  118. AuthenticatedAt: nil,
  119. Authenticated: false,
  120. }
  121. checkPayload(t, models.UserJoined, func() {
  122. SendChatEventUserJoined(events.UserJoinedEvent{
  123. Event: events.Event{
  124. Type: events.UserJoined,
  125. ID: "id",
  126. Timestamp: timestamp,
  127. },
  128. UserEvent: events.UserEvent{
  129. User: &user,
  130. ClientID: 51,
  131. HiddenAt: nil,
  132. },
  133. })
  134. }, `{
  135. "clientId": 51,
  136. "id": "id",
  137. "type": "USER_JOINED",
  138. "timestamp": "1970-01-01T00:01:12.000000006Z",
  139. "user": {
  140. "authenticated": false,
  141. "createdAt": "1970-01-01T00:00:03.000000026Z",
  142. "displayColor": 4,
  143. "displayName": "display name",
  144. "id": "user id",
  145. "isBot": false,
  146. "previousNames": ["somebody"]
  147. }
  148. }`)
  149. }
  150. func TestSendChatEventSetMessageVisibility(t *testing.T) {
  151. timestamp := time.Unix(72, 6).UTC()
  152. checkPayload(t, models.VisibiltyToggled, func() {
  153. SendChatEventSetMessageVisibility(events.SetMessageVisibilityEvent{
  154. Event: events.Event{
  155. Type: events.VisibiltyUpdate,
  156. ID: "id",
  157. Timestamp: timestamp,
  158. },
  159. UserMessageEvent: events.UserMessageEvent{},
  160. MessageIDs: []string{"message1", "message2"},
  161. Visible: false,
  162. })
  163. }, `{
  164. "MessageIDs": [
  165. "message1",
  166. "message2"
  167. ],
  168. "Visible": false,
  169. "body": "",
  170. "id": "id",
  171. "timestamp": "1970-01-01T00:01:12.000000006Z",
  172. "type": "VISIBILITY-UPDATE",
  173. "user": null
  174. }`)
  175. }