1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-12-26 19:52:30 +01:00
ntfy/server/server_firebase_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

346 lines
10 KiB
Go
Raw Normal View History

2022-02-01 14:47:01 +01:00
package server
import (
"encoding/json"
"errors"
"fmt"
2022-12-28 04:14:14 +01:00
"heckel.io/ntfy/user"
"net/netip"
2022-02-01 14:47:01 +01:00
"strings"
2022-06-01 05:55:05 +02:00
"sync"
2022-02-01 14:47:01 +01:00
"testing"
"firebase.google.com/go/v4/messaging"
"github.com/stretchr/testify/require"
2022-02-01 14:47:01 +01:00
)
type testAuther struct {
Allow bool
}
2022-12-28 04:14:14 +01:00
var _ user.Auther = (*testAuther)(nil)
func (t testAuther) Authenticate(_, _ string) (*user.User, error) {
2022-02-01 14:47:01 +01:00
return nil, errors.New("not used")
}
func (t testAuther) Authorize(_ *user.User, _ string, _ user.Permission) error {
2022-02-01 14:47:01 +01:00
if t.Allow {
return nil
}
return errors.New("unauthorized")
}
2022-06-01 05:16:44 +02:00
type testFirebaseSender struct {
allowed int
messages []*messaging.Message
2022-06-01 05:55:05 +02:00
mu sync.Mutex
2022-06-01 05:16:44 +02:00
}
func newTestFirebaseSender(allowed int) *testFirebaseSender {
return &testFirebaseSender{
allowed: allowed,
messages: make([]*messaging.Message, 0),
}
}
2022-06-01 05:55:05 +02:00
2022-06-01 05:16:44 +02:00
func (s *testFirebaseSender) Send(m *messaging.Message) error {
2022-06-01 05:55:05 +02:00
s.mu.Lock()
defer s.mu.Unlock()
2022-06-01 05:16:44 +02:00
if len(s.messages)+1 > s.allowed {
return errFirebaseQuotaExceeded
}
s.messages = append(s.messages, m)
return nil
}
2022-06-01 05:55:05 +02:00
func (s *testFirebaseSender) Messages() []*messaging.Message {
s.mu.Lock()
defer s.mu.Unlock()
2022-06-01 15:05:21 +02:00
return append(make([]*messaging.Message, 0), s.messages...)
2022-06-01 05:55:05 +02:00
}
2022-02-01 14:47:01 +01:00
func TestToFirebaseMessage_Keepalive(t *testing.T) {
m := newKeepaliveMessage("mytopic")
fbm, err := toFirebaseMessage(m, nil)
require.Nil(t, err)
require.Equal(t, "mytopic", fbm.Topic)
require.Nil(t, fbm.Android)
2022-05-28 02:30:20 +02:00
require.Equal(t, &messaging.APNSConfig{
Headers: map[string]string{
"apns-push-type": "background",
"apns-priority": "5",
},
Payload: &messaging.APNSPayload{
Aps: &messaging.Aps{
ContentAvailable: true,
},
2022-10-01 22:31:48 +02:00
CustomData: map[string]any{
2022-05-28 02:30:20 +02:00
"id": m.ID,
"time": fmt.Sprintf("%d", m.Time),
"event": m.Event,
"topic": m.Topic,
},
},
}, fbm.APNS)
2022-02-01 14:47:01 +01:00
require.Equal(t, map[string]string{
"id": m.ID,
"time": fmt.Sprintf("%d", m.Time),
"event": m.Event,
"topic": m.Topic,
}, fbm.Data)
}
func TestToFirebaseMessage_Open(t *testing.T) {
m := newOpenMessage("mytopic")
fbm, err := toFirebaseMessage(m, nil)
require.Nil(t, err)
require.Equal(t, "mytopic", fbm.Topic)
require.Nil(t, fbm.Android)
2022-05-28 02:30:20 +02:00
require.Equal(t, &messaging.APNSConfig{
Headers: map[string]string{
"apns-push-type": "background",
"apns-priority": "5",
},
Payload: &messaging.APNSPayload{
Aps: &messaging.Aps{
ContentAvailable: true,
},
2022-10-01 22:31:48 +02:00
CustomData: map[string]any{
2022-05-28 02:30:20 +02:00
"id": m.ID,
"time": fmt.Sprintf("%d", m.Time),
"event": m.Event,
"topic": m.Topic,
},
},
}, fbm.APNS)
2022-02-01 14:47:01 +01:00
require.Equal(t, map[string]string{
"id": m.ID,
"time": fmt.Sprintf("%d", m.Time),
"event": m.Event,
"topic": m.Topic,
}, fbm.Data)
}
func TestToFirebaseMessage_Message_Normal_Allowed(t *testing.T) {
m := newDefaultMessage("mytopic", "this is a message")
m.Priority = 4
m.Tags = []string{"tag 1", "tag2"}
m.Click = "https://google.com"
2022-07-17 23:40:24 +02:00
m.Icon = "https://ntfy.sh/static/img/ntfy.png"
2022-02-01 14:47:01 +01:00
m.Title = "some title"
2022-05-28 02:30:20 +02:00
m.Actions = []*action{
{
ID: "123",
Action: "view",
Label: "Open page",
Clear: true,
URL: "https://ntfy.sh",
},
{
ID: "456",
Action: "http",
Label: "Close door",
URL: "https://door.com/close",
Method: "PUT",
Headers: map[string]string{
"really": "yes",
},
},
}
2022-02-01 14:47:01 +01:00
m.Attachment = &attachment{
Name: "some file.jpg",
Type: "image/jpeg",
Size: 12345,
Expires: 98765543,
URL: "https://example.com/file.jpg",
}
fbm, err := toFirebaseMessage(m, &testAuther{Allow: true})
require.Nil(t, err)
require.Equal(t, "mytopic", fbm.Topic)
require.Equal(t, &messaging.AndroidConfig{
Priority: "high",
}, fbm.Android)
2022-05-28 02:30:20 +02:00
require.Equal(t, &messaging.APNSConfig{
Payload: &messaging.APNSPayload{
Aps: &messaging.Aps{
MutableContent: true,
Alert: &messaging.ApsAlert{
Title: "some title",
Body: "this is a message",
},
},
2022-10-01 22:31:48 +02:00
CustomData: map[string]any{
2022-05-28 02:30:20 +02:00
"id": m.ID,
"time": fmt.Sprintf("%d", m.Time),
"event": "message",
"topic": "mytopic",
"priority": "4",
"tags": strings.Join(m.Tags, ","),
"click": "https://google.com",
2022-07-17 23:40:24 +02:00
"icon": "https://ntfy.sh/static/img/ntfy.png",
2022-05-28 02:30:20 +02:00
"title": "some title",
"message": "this is a message",
"actions": `[{"id":"123","action":"view","label":"Open page","clear":true,"url":"https://ntfy.sh"},{"id":"456","action":"http","label":"Close door","clear":false,"url":"https://door.com/close","method":"PUT","headers":{"really":"yes"}}]`,
2023-07-08 21:14:35 +02:00
"content_type": "",
2022-05-28 02:30:20 +02:00
"encoding": "",
"attachment_name": "some file.jpg",
"attachment_type": "image/jpeg",
"attachment_size": "12345",
"attachment_expires": "98765543",
"attachment_url": "https://example.com/file.jpg",
},
},
}, fbm.APNS)
2022-02-01 14:47:01 +01:00
require.Equal(t, map[string]string{
"id": m.ID,
"time": fmt.Sprintf("%d", m.Time),
"event": "message",
"topic": "mytopic",
"priority": "4",
"tags": strings.Join(m.Tags, ","),
"click": "https://google.com",
2022-07-17 23:40:24 +02:00
"icon": "https://ntfy.sh/static/img/ntfy.png",
2022-02-01 14:47:01 +01:00
"title": "some title",
"message": "this is a message",
2022-05-28 02:30:20 +02:00
"actions": `[{"id":"123","action":"view","label":"Open page","clear":true,"url":"https://ntfy.sh"},{"id":"456","action":"http","label":"Close door","clear":false,"url":"https://door.com/close","method":"PUT","headers":{"really":"yes"}}]`,
2023-07-08 21:14:35 +02:00
"content_type": "",
2022-02-01 14:47:01 +01:00
"encoding": "",
"attachment_name": "some file.jpg",
"attachment_type": "image/jpeg",
"attachment_size": "12345",
"attachment_expires": "98765543",
"attachment_url": "https://example.com/file.jpg",
}, fbm.Data)
}
func TestToFirebaseMessage_Message_Normal_Not_Allowed(t *testing.T) {
m := newDefaultMessage("mytopic", "this is a message")
m.Priority = 5
fbm, err := toFirebaseMessage(m, &testAuther{Allow: false}) // Not allowed!
require.Nil(t, err)
require.Equal(t, "mytopic", fbm.Topic)
require.Equal(t, &messaging.AndroidConfig{
Priority: "high",
}, fbm.Android)
require.Equal(t, "", fbm.Data["message"])
require.Equal(t, "", fbm.Data["priority"])
require.Equal(t, map[string]string{
"id": m.ID,
"time": fmt.Sprintf("%d", m.Time),
"event": "poll_request",
"topic": "mytopic",
}, fbm.Data)
}
2022-05-28 02:30:20 +02:00
func TestToFirebaseMessage_PollRequest(t *testing.T) {
m := newPollRequestMessage("mytopic", "fOv6k1QbCzo6")
fbm, err := toFirebaseMessage(m, nil)
require.Nil(t, err)
require.Equal(t, "mytopic", fbm.Topic)
require.Nil(t, fbm.Android)
require.Equal(t, &messaging.APNSConfig{
Payload: &messaging.APNSPayload{
Aps: &messaging.Aps{
MutableContent: true,
Alert: &messaging.ApsAlert{
Title: "",
Body: "New message",
},
},
2022-10-01 22:31:48 +02:00
CustomData: map[string]any{
2022-05-28 02:30:20 +02:00
"id": m.ID,
"time": fmt.Sprintf("%d", m.Time),
"event": "poll_request",
"topic": "mytopic",
"message": "New message",
"poll_id": "fOv6k1QbCzo6",
},
},
}, fbm.APNS)
require.Equal(t, map[string]string{
"id": m.ID,
"time": fmt.Sprintf("%d", m.Time),
"event": "poll_request",
"topic": "mytopic",
"message": "New message",
"poll_id": "fOv6k1QbCzo6",
}, fbm.Data)
}
2022-02-01 14:47:01 +01:00
func TestMaybeTruncateFCMMessage(t *testing.T) {
origMessage := strings.Repeat("this is a long string", 300)
origFCMMessage := &messaging.Message{
Topic: "mytopic",
Data: map[string]string{
"id": "abcdefg",
"time": "1641324761",
"event": "message",
"topic": "mytopic",
"priority": "0",
"tags": "",
"title": "",
"message": origMessage,
},
Android: &messaging.AndroidConfig{
Priority: "high",
},
}
origMessageLength := len(origFCMMessage.Data["message"])
serializedOrigFCMMessage, _ := json.Marshal(origFCMMessage)
require.Greater(t, len(serializedOrigFCMMessage), fcmMessageLimit) // Pre-condition
truncatedFCMMessage := maybeTruncateFCMMessage(origFCMMessage)
truncatedMessageLength := len(truncatedFCMMessage.Data["message"])
serializedTruncatedFCMMessage, _ := json.Marshal(truncatedFCMMessage)
require.Equal(t, fcmMessageLimit, len(serializedTruncatedFCMMessage))
require.Equal(t, "1", truncatedFCMMessage.Data["truncated"])
require.NotEqual(t, origMessageLength, truncatedMessageLength)
}
func TestMaybeTruncateFCMMessage_NotTooLong(t *testing.T) {
origMessage := "not really a long string"
origFCMMessage := &messaging.Message{
Topic: "mytopic",
Data: map[string]string{
"id": "abcdefg",
"time": "1641324761",
"event": "message",
"topic": "mytopic",
"priority": "0",
"tags": "",
"title": "",
"message": origMessage,
},
}
origMessageLength := len(origFCMMessage.Data["message"])
serializedOrigFCMMessage, _ := json.Marshal(origFCMMessage)
require.LessOrEqual(t, len(serializedOrigFCMMessage), fcmMessageLimit) // Pre-condition
notTruncatedFCMMessage := maybeTruncateFCMMessage(origFCMMessage)
notTruncatedMessageLength := len(notTruncatedFCMMessage.Data["message"])
serializedNotTruncatedFCMMessage, _ := json.Marshal(notTruncatedFCMMessage)
require.Equal(t, origMessageLength, notTruncatedMessageLength)
require.Equal(t, len(serializedOrigFCMMessage), len(serializedNotTruncatedFCMMessage))
require.Equal(t, "", notTruncatedFCMMessage.Data["truncated"])
}
2022-06-01 05:16:44 +02:00
func TestToFirebaseSender_Abuse(t *testing.T) {
sender := &testFirebaseSender{allowed: 2}
client := newFirebaseClient(sender, &testAuther{})
2023-01-03 02:08:37 +01:00
visitor := newVisitor(newTestConfig(t), newMemTestCache(t), nil, netip.MustParseAddr("1.2.3.4"), nil)
2022-06-01 05:16:44 +02:00
require.Nil(t, client.Send(visitor, &message{Topic: "mytopic"}))
2022-06-01 05:55:05 +02:00
require.Equal(t, 1, len(sender.Messages()))
2022-06-01 05:16:44 +02:00
require.Nil(t, client.Send(visitor, &message{Topic: "mytopic"}))
2022-06-01 05:55:05 +02:00
require.Equal(t, 2, len(sender.Messages()))
2022-06-01 05:16:44 +02:00
require.Equal(t, errFirebaseQuotaExceeded, client.Send(visitor, &message{Topic: "mytopic"}))
2022-06-01 05:55:05 +02:00
require.Equal(t, 2, len(sender.Messages()))
2022-06-01 05:16:44 +02:00
sender.messages = make([]*messaging.Message, 0) // Reset to test that time limit is working
require.Equal(t, errFirebaseTemporarilyBanned, client.Send(visitor, &message{Topic: "mytopic"}))
2022-06-01 05:55:05 +02:00
require.Equal(t, 0, len(sender.Messages()))
2022-06-01 05:16:44 +02:00
}