-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_test.go
163 lines (146 loc) · 3.55 KB
/
message_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package fcm
import (
"testing"
)
func TestValidate(t *testing.T) {
t.Run("extract message id", func(t *testing.T) {
msg := &Message{
Name: "projects/*/messages/{message_id}",
}
expected := "{message_id}"
result := msg.MessageID()
if expected != result {
t.Fatalf("expected: %v got: %v", expected, result)
}
})
t.Run("valid with token", func(t *testing.T) {
msg := &Message{
Topic: "test",
}
err := msg.Validate()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
})
t.Run("invalid message", func(t *testing.T) {
var msg *Message
err := msg.Validate()
if err == nil {
t.Fatalf("expected <%v> error, but got <nil>", ErrInvalidMessage)
}
})
t.Run("invalid target", func(t *testing.T) {
msg := &Message{
Data: map[string]string{
"message": "This is a Firebase Cloud Messaging Topic Message!",
},
}
err := msg.Validate()
if err == nil {
t.Fatalf("expected <%v> error, but got nil", ErrInvalidTarget)
}
})
t.Run("invalid TTL", func(t *testing.T) {
timeToLive := "5"
msg := &Message{
Topic: "test",
Android: &AndroidConfig{
TTL: timeToLive,
},
}
err := msg.Validate()
if err == nil {
t.Fatalf("expected <%v> error, but got nil", ErrInvalidTimeToLive)
}
})
t.Run("valid target with condition", func(t *testing.T) {
msg := &Message{
Condition: "'dogs' in topics || 'cats' in topics",
}
err := msg.Validate()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
})
t.Run("valid target with topic", func(t *testing.T) {
msg := &Message{
Topic: "cats",
}
err := msg.Validate()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
})
t.Run("valid target with token", func(t *testing.T) {
msg := &Message{
Token: "12345678",
}
err := msg.Validate()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
})
t.Run("invalid condition", func(t *testing.T) {
msg := &Message{
Condition: "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics || 'TopicD' in topics )",
}
err := msg.Validate()
if err == nil {
t.Fatalf("expected <%v> error, but got nil", ErrInvalidTarget)
}
})
t.Run("invalid union with topic and condition", func(t *testing.T) {
msg := &Message{
Topic: "cats",
Condition: "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics || 'TopicD' in topics )",
}
err := msg.Validate()
if err == nil {
t.Fatalf("expected <%v> error, but got nil", ErrInvalidTarget)
}
})
t.Run("invalid union with token and condition", func(t *testing.T) {
msg := &Message{
Token: "12345678",
Condition: "'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics || 'TopicD' in topics )",
}
err := msg.Validate()
if err == nil {
t.Fatalf("expected <%v> error, but got nil", ErrInvalidTarget)
}
})
t.Run("invalid union with token and topic", func(t *testing.T) {
msg := &Message{
Token: "12345678",
Topic: "cats",
}
err := msg.Validate()
if err == nil {
t.Fatalf("expected <%v> error, but got nil", ErrInvalidTarget)
}
})
t.Run("invalid apns priority", func(t *testing.T) {
payload := &ApnsPayload{
Aps: &ApsDictionary{
ContentAvailable: int(ApnsContentAvailable),
},
}
payloadMap, err := payload.ToMap()
if err != nil {
t.Error(err)
}
msg := &Message{
Token: "12345678",
Apns: &ApnsConfig{
Headers: &ApnsHeaders{
Priority: string(ApnsHighPriority),
},
Payload: payloadMap,
},
}
err = msg.Validate()
if err == nil {
t.Fatalf("expected <%v> error, but got nil", ErrInvalidApnsPriority)
}
})
}