server_firebase_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package server
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "firebase.google.com/go/messaging"
  6. "fmt"
  7. "github.com/stretchr/testify/require"
  8. "heckel.io/ntfy/auth"
  9. "strings"
  10. "testing"
  11. )
  12. type testAuther struct {
  13. Allow bool
  14. }
  15. func (t testAuther) Authenticate(_, _ string) (*auth.User, error) {
  16. return nil, errors.New("not used")
  17. }
  18. func (t testAuther) Authorize(_ *auth.User, _ string, _ auth.Permission) error {
  19. if t.Allow {
  20. return nil
  21. }
  22. return errors.New("unauthorized")
  23. }
  24. func TestToFirebaseMessage_Keepalive(t *testing.T) {
  25. m := newKeepaliveMessage("mytopic")
  26. fbm, err := toFirebaseMessage(m, nil)
  27. require.Nil(t, err)
  28. require.Equal(t, "mytopic", fbm.Topic)
  29. require.Nil(t, fbm.Android)
  30. require.Equal(t, map[string]string{
  31. "id": m.ID,
  32. "time": fmt.Sprintf("%d", m.Time),
  33. "event": m.Event,
  34. "topic": m.Topic,
  35. }, fbm.Data)
  36. }
  37. func TestToFirebaseMessage_Open(t *testing.T) {
  38. m := newOpenMessage("mytopic")
  39. fbm, err := toFirebaseMessage(m, nil)
  40. require.Nil(t, err)
  41. require.Equal(t, "mytopic", fbm.Topic)
  42. require.Nil(t, fbm.Android)
  43. require.Equal(t, map[string]string{
  44. "id": m.ID,
  45. "time": fmt.Sprintf("%d", m.Time),
  46. "event": m.Event,
  47. "topic": m.Topic,
  48. }, fbm.Data)
  49. }
  50. func TestToFirebaseMessage_Message_Normal_Allowed(t *testing.T) {
  51. m := newDefaultMessage("mytopic", "this is a message")
  52. m.Priority = 4
  53. m.Tags = []string{"tag 1", "tag2"}
  54. m.Click = "https://google.com"
  55. m.Title = "some title"
  56. m.Attachment = &attachment{
  57. Name: "some file.jpg",
  58. Type: "image/jpeg",
  59. Size: 12345,
  60. Expires: 98765543,
  61. URL: "https://example.com/file.jpg",
  62. Owner: "some-owner",
  63. }
  64. fbm, err := toFirebaseMessage(m, &testAuther{Allow: true})
  65. require.Nil(t, err)
  66. require.Equal(t, "mytopic", fbm.Topic)
  67. require.Equal(t, &messaging.AndroidConfig{
  68. Priority: "high",
  69. }, fbm.Android)
  70. require.Equal(t, map[string]string{
  71. "id": m.ID,
  72. "time": fmt.Sprintf("%d", m.Time),
  73. "event": "message",
  74. "topic": "mytopic",
  75. "priority": "4",
  76. "tags": strings.Join(m.Tags, ","),
  77. "click": "https://google.com",
  78. "title": "some title",
  79. "message": "this is a message",
  80. "encoding": "",
  81. "attachment_name": "some file.jpg",
  82. "attachment_type": "image/jpeg",
  83. "attachment_size": "12345",
  84. "attachment_expires": "98765543",
  85. "attachment_url": "https://example.com/file.jpg",
  86. }, fbm.Data)
  87. }
  88. func TestToFirebaseMessage_Message_Normal_Not_Allowed(t *testing.T) {
  89. m := newDefaultMessage("mytopic", "this is a message")
  90. m.Priority = 5
  91. fbm, err := toFirebaseMessage(m, &testAuther{Allow: false}) // Not allowed!
  92. require.Nil(t, err)
  93. require.Equal(t, "mytopic", fbm.Topic)
  94. require.Equal(t, &messaging.AndroidConfig{
  95. Priority: "high",
  96. }, fbm.Android)
  97. require.Equal(t, "", fbm.Data["message"])
  98. require.Equal(t, "", fbm.Data["priority"])
  99. require.Equal(t, map[string]string{
  100. "id": m.ID,
  101. "time": fmt.Sprintf("%d", m.Time),
  102. "event": "poll_request",
  103. "topic": "mytopic",
  104. }, fbm.Data)
  105. }
  106. func TestMaybeTruncateFCMMessage(t *testing.T) {
  107. origMessage := strings.Repeat("this is a long string", 300)
  108. origFCMMessage := &messaging.Message{
  109. Topic: "mytopic",
  110. Data: map[string]string{
  111. "id": "abcdefg",
  112. "time": "1641324761",
  113. "event": "message",
  114. "topic": "mytopic",
  115. "priority": "0",
  116. "tags": "",
  117. "title": "",
  118. "message": origMessage,
  119. },
  120. Android: &messaging.AndroidConfig{
  121. Priority: "high",
  122. },
  123. }
  124. origMessageLength := len(origFCMMessage.Data["message"])
  125. serializedOrigFCMMessage, _ := json.Marshal(origFCMMessage)
  126. require.Greater(t, len(serializedOrigFCMMessage), fcmMessageLimit) // Pre-condition
  127. truncatedFCMMessage := maybeTruncateFCMMessage(origFCMMessage)
  128. truncatedMessageLength := len(truncatedFCMMessage.Data["message"])
  129. serializedTruncatedFCMMessage, _ := json.Marshal(truncatedFCMMessage)
  130. require.Equal(t, fcmMessageLimit, len(serializedTruncatedFCMMessage))
  131. require.Equal(t, "1", truncatedFCMMessage.Data["truncated"])
  132. require.NotEqual(t, origMessageLength, truncatedMessageLength)
  133. }
  134. func TestMaybeTruncateFCMMessage_NotTooLong(t *testing.T) {
  135. origMessage := "not really a long string"
  136. origFCMMessage := &messaging.Message{
  137. Topic: "mytopic",
  138. Data: map[string]string{
  139. "id": "abcdefg",
  140. "time": "1641324761",
  141. "event": "message",
  142. "topic": "mytopic",
  143. "priority": "0",
  144. "tags": "",
  145. "title": "",
  146. "message": origMessage,
  147. },
  148. }
  149. origMessageLength := len(origFCMMessage.Data["message"])
  150. serializedOrigFCMMessage, _ := json.Marshal(origFCMMessage)
  151. require.LessOrEqual(t, len(serializedOrigFCMMessage), fcmMessageLimit) // Pre-condition
  152. notTruncatedFCMMessage := maybeTruncateFCMMessage(origFCMMessage)
  153. notTruncatedMessageLength := len(notTruncatedFCMMessage.Data["message"])
  154. serializedNotTruncatedFCMMessage, _ := json.Marshal(notTruncatedFCMMessage)
  155. require.Equal(t, origMessageLength, notTruncatedMessageLength)
  156. require.Equal(t, len(serializedOrigFCMMessage), len(serializedNotTruncatedFCMMessage))
  157. require.Equal(t, "", notTruncatedFCMMessage.Data["truncated"])
  158. }