webapi_event_converter_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package handlers
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/mk6i/open-oscar-server/server/webapi/types"
  6. )
  7. // The AMF3 converter re-flattens PresenceEvent through an explicit allowlist, so a
  8. // field absent from that allowlist never reaches an AMF3 client. buddyIcon must be
  9. // on it.
  10. func TestConvertEventForAMF3_PresenceCarriesBuddyIcon(t *testing.T) {
  11. t.Run("buddyIcon is included when set", func(t *testing.T) {
  12. out := ConvertEventForAMF3(types.Event{
  13. Type: types.EventTypePresence,
  14. Data: types.PresenceEvent{
  15. AimID: "mikekelly",
  16. State: "online",
  17. UserType: "aim",
  18. BuddyIcon: "http://api.example.com/expressions/get?t=mikekelly&type=buddyIcon&bartId=dead",
  19. },
  20. })
  21. eventData := out["eventData"].(map[string]interface{})
  22. assert.Equal(t,
  23. "http://api.example.com/expressions/get?t=mikekelly&type=buddyIcon&bartId=dead",
  24. eventData["buddyIcon"])
  25. })
  26. t.Run("buddyIcon is omitted when empty", func(t *testing.T) {
  27. out := ConvertEventForAMF3(types.Event{
  28. Type: types.EventTypePresence,
  29. Data: types.PresenceEvent{AimID: "mikekelly", State: "offline", UserType: "aim"},
  30. })
  31. eventData := out["eventData"].(map[string]interface{})
  32. _, ok := eventData["buddyIcon"]
  33. assert.False(t, ok)
  34. })
  35. }
  36. // The AMF3 converter flattens OfflineIMEvent through an explicit allowlist. The
  37. // client keys its conversation list and chat-log cache by msgId, so an event that
  38. // loses it collides with every other offline message.
  39. func TestConvertEventForAMF3_OfflineIM(t *testing.T) {
  40. out := ConvertEventForAMF3(types.Event{
  41. Type: types.EventTypeOfflineIM,
  42. Data: types.OfflineIMEvent{
  43. AimID: "mikekelly",
  44. Message: "sent while you were out",
  45. MsgID: "beefcafe",
  46. Timestamp: 1700000000,
  47. },
  48. })
  49. eventData := out["eventData"].(map[string]interface{})
  50. assert.Equal(t, "mikekelly", eventData["aimId"])
  51. assert.Equal(t, "sent while you were out", eventData["message"])
  52. assert.Equal(t, "beefcafe", eventData["msgId"])
  53. assert.Equal(t, float64(1700000000), eventData["timestamp"])
  54. assert.Equal(t, false, eventData["autoresponse"])
  55. }