server_firebase_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. "updated": "0",
  74. "event": "message",
  75. "topic": "mytopic",
  76. "priority": "4",
  77. "tags": strings.Join(m.Tags, ","),
  78. "click": "https://google.com",
  79. "title": "some title",
  80. "message": "this is a message",
  81. "encoding": "",
  82. "attachment_name": "some file.jpg",
  83. "attachment_type": "image/jpeg",
  84. "attachment_size": "12345",
  85. "attachment_expires": "98765543",
  86. "attachment_url": "https://example.com/file.jpg",
  87. }, fbm.Data)
  88. }
  89. func TestToFirebaseMessage_Message_Normal_Not_Allowed(t *testing.T) {
  90. m := newDefaultMessage("mytopic", "this is a message")
  91. m.Priority = 5
  92. fbm, err := toFirebaseMessage(m, &testAuther{Allow: false}) // Not allowed!
  93. require.Nil(t, err)
  94. require.Equal(t, "mytopic", fbm.Topic)
  95. require.Equal(t, &messaging.AndroidConfig{
  96. Priority: "high",
  97. }, fbm.Android)
  98. require.Equal(t, "", fbm.Data["message"])
  99. require.Equal(t, "", fbm.Data["priority"])
  100. require.Equal(t, map[string]string{
  101. "id": m.ID,
  102. "time": fmt.Sprintf("%d", m.Time),
  103. "event": "poll_request",
  104. "topic": "mytopic",
  105. }, fbm.Data)
  106. }
  107. func TestMaybeTruncateFCMMessage(t *testing.T) {
  108. origMessage := strings.Repeat("this is a long string", 300)
  109. origFCMMessage := &messaging.Message{
  110. Topic: "mytopic",
  111. Data: map[string]string{
  112. "id": "abcdefg",
  113. "time": "1641324761",
  114. "event": "message",
  115. "topic": "mytopic",
  116. "priority": "0",
  117. "tags": "",
  118. "title": "",
  119. "message": origMessage,
  120. },
  121. Android: &messaging.AndroidConfig{
  122. Priority: "high",
  123. },
  124. }
  125. origMessageLength := len(origFCMMessage.Data["message"])
  126. serializedOrigFCMMessage, _ := json.Marshal(origFCMMessage)
  127. require.Greater(t, len(serializedOrigFCMMessage), fcmMessageLimit) // Pre-condition
  128. truncatedFCMMessage := maybeTruncateFCMMessage(origFCMMessage)
  129. truncatedMessageLength := len(truncatedFCMMessage.Data["message"])
  130. serializedTruncatedFCMMessage, _ := json.Marshal(truncatedFCMMessage)
  131. require.Equal(t, fcmMessageLimit, len(serializedTruncatedFCMMessage))
  132. require.Equal(t, "1", truncatedFCMMessage.Data["truncated"])
  133. require.NotEqual(t, origMessageLength, truncatedMessageLength)
  134. }
  135. func TestMaybeTruncateFCMMessage_NotTooLong(t *testing.T) {
  136. origMessage := "not really a long string"
  137. origFCMMessage := &messaging.Message{
  138. Topic: "mytopic",
  139. Data: map[string]string{
  140. "id": "abcdefg",
  141. "time": "1641324761",
  142. "event": "message",
  143. "topic": "mytopic",
  144. "priority": "0",
  145. "tags": "",
  146. "title": "",
  147. "message": origMessage,
  148. },
  149. }
  150. origMessageLength := len(origFCMMessage.Data["message"])
  151. serializedOrigFCMMessage, _ := json.Marshal(origFCMMessage)
  152. require.LessOrEqual(t, len(serializedOrigFCMMessage), fcmMessageLimit) // Pre-condition
  153. notTruncatedFCMMessage := maybeTruncateFCMMessage(origFCMMessage)
  154. notTruncatedMessageLength := len(notTruncatedFCMMessage.Data["message"])
  155. serializedNotTruncatedFCMMessage, _ := json.Marshal(notTruncatedFCMMessage)
  156. require.Equal(t, origMessageLength, notTruncatedMessageLength)
  157. require.Equal(t, len(serializedOrigFCMMessage), len(serializedNotTruncatedFCMMessage))
  158. require.Equal(t, "", notTruncatedFCMMessage.Data["truncated"])
  159. }