forked from appleboy/gorush
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver_test.go
689 lines (559 loc) · 19 KB
/
server_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
package router
import (
"context"
"crypto/tls"
"io"
"log"
"net/http"
"os"
"runtime"
"testing"
"time"
"github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/core"
"github.com/appleboy/gorush/logx"
"github.com/appleboy/gorush/notify"
"github.com/appleboy/gorush/status"
"github.com/appleboy/gofight/v2"
"github.com/buger/jsonparser"
"github.com/gin-gonic/gin"
"github.com/golang-queue/queue"
qcore "github.com/golang-queue/queue/core"
"github.com/stretchr/testify/assert"
)
var (
goVersion = runtime.Version()
q *queue.Queue
testKeyPath = "../certificate/certificate-valid.pem"
)
func TestMain(m *testing.M) {
cfg := initTest()
if err := status.InitAppStatus(cfg); err != nil {
log.Fatal(err)
}
cfg.Android.Enabled = true
cfg.Android.Credential = os.Getenv("FCM_CREDENTIAL")
if _, err := notify.InitFCMClient(cfg); err != nil {
log.Fatal(err)
}
q = queue.NewPool(
int(cfg.Core.WorkerNum),
queue.WithFn(func(ctx context.Context, msg qcore.QueuedMessage) error {
_, err := notify.SendNotification(ctx, msg, cfg)
return err
}),
queue.WithLogger(logx.QueueLogger()),
)
code := m.Run()
defer func() {
q.Release()
os.Exit(code)
}()
}
func initTest() *config.ConfYaml {
cfg, _ := config.LoadConf()
cfg.Core.Mode = "test"
return cfg
}
// testRequest is testing url string if server is running
func testRequest(t *testing.T, url string) {
tr := &http.Transport{
//nolint:gosec
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Timeout: time.Second * 10,
Transport: tr,
}
req, _ := http.NewRequestWithContext(
context.Background(),
http.MethodGet,
url,
nil,
)
resp, err := client.Do(req)
defer func() {
if err := resp.Body.Close(); err != nil {
log.Println("close body err:", err)
}
}()
assert.NoError(t, err)
_, ioerr := io.ReadAll(resp.Body)
assert.NoError(t, ioerr)
assert.Equal(t, "200 OK", resp.Status, "should get a 200")
}
func TestPrintGoRushVersion(t *testing.T) {
SetVersion("3.0.0")
SetCommit("abcdefg")
ver := GetVersion()
PrintGoRushVersion()
assert.Equal(t, "3.0.0", ver)
}
func TestRunNormalServer(t *testing.T) {
cfg := initTest()
gin.SetMode(gin.TestMode)
ctx, cancel := context.WithCancel(context.Background())
go func() {
assert.NoError(t, RunHTTPServer(ctx, cfg, q))
}()
defer func() {
// close the server
cancel()
}()
// have to wait for the goroutine to start and run the server
// otherwise the main thread will complete
time.Sleep(5 * time.Millisecond)
testRequest(t, "http://localhost:8088/api/stat/go")
}
func TestRunTLSServer(t *testing.T) {
cfg := initTest()
cfg.Core.SSL = true
cfg.Core.Port = "8087"
cfg.Core.CertPath = "../certificate/localhost.cert"
cfg.Core.KeyPath = "../certificate/localhost.key"
ctx, cancel := context.WithCancel(context.Background())
go func() {
assert.NoError(t, RunHTTPServer(ctx, cfg, q))
}()
defer func() {
// close the server
cancel()
}()
// have to wait for the goroutine to start and run the server
// otherwise the main thread will complete
time.Sleep(5 * time.Millisecond)
testRequest(t, "https://localhost:8087/api/stat/go")
}
func TestRunTLSBase64Server(t *testing.T) {
//nolint
cert := `LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUMrekNDQWVPZ0F3SUJBZ0lKQUxiWkVEdlVRckZLTUEwR0NTc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25wK2dQYkIrTDdZbDhsdEFtVFNPSmdWWjBoY1QwRHhBMzYxWngrMk11NThHQmw0T2JsbmNobXdFMXZqMQpLY1F5UHJFUXhkb1VUeWlzd0dmcXZyczhKOWltdmIrejkvVTZUMUtBQjhXaTNXVmlYelByNE1zaWFhUlhnNjQyCkZJZHhBb0dBWjcvNzM1ZGtoSmN5T2ZzK0xLc0xyNjhKU3N0b29yWE9ZdmRNdTErSkdhOWlMdWhuSEVjTVZXQzgKSXVpaHpQZmxvWnRNYkdZa1pKbjhsM0JlR2Q4aG1mRnRnVGdaR1BvVlJldGZ0MkxERkxuUHhwMnNFSDVPRkxzUQpSK0sva0FPdWw4ZVN0V3VNWE9GQTlwTXpHa0dFZ0lGSk1KT3lhSk9OM2tlZFFJOGRlQ009Ci0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==`
cfg := initTest()
cfg.Core.SSL = true
cfg.Core.Port = "8089"
cfg.Core.CertPath = ""
cfg.Core.KeyPath = ""
cfg.Core.CertBase64 = cert
cfg.Core.KeyBase64 = key
ctx, cancel := context.WithCancel(context.Background())
go func() {
assert.NoError(t, RunHTTPServer(ctx, cfg, q))
}()
defer func() {
// close the server
cancel()
}()
// have to wait for the goroutine to start and run the server
// otherwise the main thread will complete
time.Sleep(5 * time.Millisecond)
testRequest(t, "https://localhost:8089/api/stat/go")
}
func TestRunAutoTLSServer(t *testing.T) {
cfg := initTest()
cfg.Core.AutoTLS.Enabled = true
ctx, cancel := context.WithCancel(context.Background())
go func() {
assert.NoError(t, RunHTTPServer(ctx, cfg, q))
}()
defer func() {
// close the server
cancel()
}()
// have to wait for the goroutine to start and run the server
// otherwise the main thread will complete
time.Sleep(5 * time.Millisecond)
}
func TestLoadTLSCertError(t *testing.T) {
cfg := initTest()
cfg.Core.SSL = true
cfg.Core.Port = "8087"
cfg.Core.CertPath = "../config/config.yml"
cfg.Core.KeyPath = "../config/config.yml"
assert.Error(t, RunHTTPServer(context.Background(), cfg, q))
}
func TestMissingTLSCertcfgg(t *testing.T) {
cfg := initTest()
cfg.Core.SSL = true
cfg.Core.Port = "8087"
cfg.Core.CertPath = ""
cfg.Core.KeyPath = ""
cfg.Core.CertBase64 = ""
cfg.Core.KeyBase64 = ""
err := RunHTTPServer(context.Background(), cfg, q)
assert.Error(t, RunHTTPServer(context.Background(), cfg, q))
assert.Equal(t, "missing https cert config", err.Error())
}
func TestRootHandler(t *testing.T) {
cfg := initTest()
r := gofight.New()
// log for json
cfg.Log.Format = "json"
r.GET("/").
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
data := r.Body.Bytes()
value, _ := jsonparser.GetString(data, "text")
assert.Equal(t, "Welcome to notification server.", value)
assert.Equal(t, http.StatusOK, r.Code)
})
}
func TestAPIStatusGoHandler(t *testing.T) {
cfg := initTest()
r := gofight.New()
r.GET("/api/stat/go").
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
data := r.Body.Bytes()
value, _ := jsonparser.GetString(data, "go_version")
assert.Equal(t, goVersion, value)
assert.Equal(t, http.StatusOK, r.Code)
})
}
func TestAPIStatusAppHandler(t *testing.T) {
cfg := initTest()
r := gofight.New()
appVersion := "v1.0.0"
SetVersion(appVersion)
r.GET("/api/stat/app").
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
data := r.Body.Bytes()
value, _ := jsonparser.GetString(data, "version")
assert.Equal(t, appVersion, value)
assert.Equal(t, http.StatusOK, r.Code)
})
}
func TestAPIConfigHandler(t *testing.T) {
cfg := initTest()
r := gofight.New()
r.GET("/api/config").
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusCreated, r.Code)
})
}
func TestMissingNotificationsParameter(t *testing.T) {
cfg := initTest()
r := gofight.New()
// missing notifications parameter.
r.POST("/api/push").
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusBadRequest, r.Code)
})
}
func TestEmptyNotifications(t *testing.T) {
cfg := initTest()
r := gofight.New()
// notifications is empty.
r.POST("/api/push").
SetJSON(gofight.D{
"notifications": []notify.PushNotification{},
}).
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusBadRequest, r.Code)
})
}
func TestMutableContent(t *testing.T) {
cfg := initTest()
r := gofight.New()
// notifications is empty.
r.POST("/api/push").
SetJSON(gofight.D{
"notifications": []gofight.D{
{
"tokens": []string{"aaaaa", "bbbbb"},
"platform": core.PlatFormAndroid,
"message": "Welcome From API",
"mutable_content": 1,
"topic": "test",
"badge": 1,
"alert": gofight.D{
"title": "title",
"body": "body",
},
},
},
}).
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
// json: cannot unmarshal number into Go struct field notify.PushNotification.mutable_content of type bool
assert.Equal(t, http.StatusBadRequest, r.Code)
})
}
func TestOutOfRangeMaxNotifications(t *testing.T) {
cfg := initTest()
cfg.Core.MaxNotification = int64(1)
r := gofight.New()
// notifications is empty.
r.POST("/api/push").
SetJSON(gofight.D{
"notifications": []gofight.D{
{
"tokens": []string{"aaaaa", "bbbbb"},
"platform": core.PlatFormAndroid,
"message": "Welcome API From Android",
},
{
"tokens": []string{"aaaaa", "bbbbb"},
"platform": core.PlatFormAndroid,
"message": "Welcome API From Android",
},
},
}).
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusBadRequest, r.Code)
})
}
func TestSuccessPushHandler(t *testing.T) {
t.Skip()
cfg := initTest()
cfg.Android.Enabled = true
cfg.Android.Credential = os.Getenv("FCM_CREDENTIAL")
androidToken := os.Getenv("FCM_TEST_TOKEN")
r := gofight.New()
r.POST("/api/push").
SetJSON(gofight.D{
"notifications": []gofight.D{
{
"tokens": []string{androidToken, "bbbbb"},
"platform": core.PlatFormAndroid,
"message": "Welcome Android",
},
},
}).
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
})
}
func TestSysStatsHandler(t *testing.T) {
cfg := initTest()
r := gofight.New()
r.GET("/sys/stats").
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
})
}
func TestMetricsHandler(t *testing.T) {
cfg := initTest()
r := gofight.New()
r.GET("/metrics").
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
})
}
func TestGETHeartbeatHandler(t *testing.T) {
cfg := initTest()
r := gofight.New()
r.GET("/healthz").
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
})
}
func TestHEADHeartbeatHandler(t *testing.T) {
cfg := initTest()
r := gofight.New()
r.HEAD("/healthz").
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
})
}
func TestVersionHandler(t *testing.T) {
SetVersion("3.0.0")
cfg := initTest()
r := gofight.New()
r.GET("/version").
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
data := r.Body.Bytes()
value, _ := jsonparser.GetString(data, "version")
assert.Equal(t, "3.0.0", value)
})
}
func TestDisabledHTTPServer(t *testing.T) {
cfg := initTest()
cfg.Core.Enabled = false
err := RunHTTPServer(context.Background(), cfg, q)
cfg.Core.Enabled = true
assert.Nil(t, err)
}
func TestSenMultipleNotifications(t *testing.T) {
ctx := context.Background()
cfg := initTest()
cfg.Ios.Enabled = true
cfg.Ios.KeyPath = testKeyPath
err := notify.InitAPNSClient(cfg)
assert.Nil(t, err)
cfg.Android.Enabled = true
cfg.Android.Credential = os.Getenv("FCM_CREDENTIAL")
androidToken := os.Getenv("FCM_TEST_TOKEN")
req := notify.RequestPush{
Notifications: []notify.PushNotification{
// ios
{
Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"},
Platform: core.PlatFormIos,
Message: "Welcome iOS",
},
// android
{
Tokens: []string{androidToken, "bbbbb"},
Platform: core.PlatFormAndroid,
Message: "Welcome Android",
},
},
}
count, logs := handleNotification(ctx, cfg, req, q)
assert.Equal(t, 3, count)
assert.Equal(t, 0, len(logs))
}
func TestDisabledAndroidNotifications(t *testing.T) {
ctx := context.Background()
cfg := initTest()
cfg.Ios.Enabled = true
cfg.Ios.KeyPath = testKeyPath
err := notify.InitAPNSClient(cfg)
assert.Nil(t, err)
cfg.Android.Enabled = false
cfg.Android.Credential = os.Getenv("FCM_CREDENTIAL")
androidToken := os.Getenv("FCM_TEST_TOKEN")
req := notify.RequestPush{
Notifications: []notify.PushNotification{
// ios
{
Tokens: []string{"11aa01229f15f0f0c5209d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"},
Platform: core.PlatFormIos,
Message: "Welcome iOS",
},
// android
{
Tokens: []string{androidToken, "bbbbb"},
Platform: core.PlatFormAndroid,
Message: "Welcome Android",
},
},
}
count, logs := handleNotification(ctx, cfg, req, q)
assert.Equal(t, 1, count)
assert.Equal(t, 0, len(logs))
}
func TestSyncModeForNotifications(t *testing.T) {
ctx := context.Background()
cfg := initTest()
cfg.Ios.Enabled = true
cfg.Ios.KeyPath = testKeyPath
err := notify.InitAPNSClient(cfg)
assert.Nil(t, err)
cfg.Android.Enabled = true
cfg.Android.Credential = os.Getenv("FCM_CREDENTIAL")
// enable sync mode
cfg.Core.Sync = true
androidToken := os.Getenv("FCM_TEST_TOKEN")
req := notify.RequestPush{
Notifications: []notify.PushNotification{
// ios
{
Tokens: []string{
"11aa01229f15f0f0c12029d8c111d1ae1f2365f14cebc4af26cd6d76b7919ef7",
},
Platform: core.PlatFormIos,
Message: "Welcome iOS Sync",
},
// android
{
Tokens: []string{androidToken, "bbbbb"},
Platform: core.PlatFormAndroid,
Message: "Welcome Android Sync",
},
},
}
count, logs := handleNotification(ctx, cfg, req, q)
assert.Equal(t, 3, count)
assert.Equal(t, 2, len(logs))
}
func TestSyncModeForTopicNotification(t *testing.T) {
ctx := context.Background()
cfg := initTest()
cfg.Android.Enabled = true
cfg.Android.Credential = os.Getenv("FCM_CREDENTIAL")
cfg.Log.HideToken = false
// enable sync mode
cfg.Core.Sync = true
req := notify.RequestPush{
Notifications: []notify.PushNotification{
// android
{
// error:InvalidParameters
// Check that the provided parameters have the right name and type.
Topic: "/topics/foo-bar@@@##",
Platform: core.PlatFormAndroid,
Message: "This is a Firebase Cloud Messaging Topic Message!",
},
// android
{
// success
Topic: "/topics/foo-bar",
Platform: core.PlatFormAndroid,
Message: "This is a Firebase Cloud Messaging Topic Message!",
},
// android
{
// success
Condition: "'dogs' in topics || 'cats' in topics",
Platform: core.PlatFormAndroid,
Message: "This is a Firebase Cloud Messaging Topic Message!",
},
},
}
count, logs := handleNotification(ctx, cfg, req, q)
assert.Equal(t, 2, count)
assert.Equal(t, 0, len(logs))
}
func TestSyncModeForDeviceGroupNotification(t *testing.T) {
ctx := context.Background()
cfg := initTest()
cfg.Android.Enabled = true
cfg.Android.Credential = os.Getenv("FCM_CREDENTIAL")
cfg.Log.HideToken = false
// enable sync mode
cfg.Core.Sync = true
req := notify.RequestPush{
Notifications: []notify.PushNotification{
// android
{
Topic: "aUniqueKey",
Platform: core.PlatFormAndroid,
Message: "This is a Firebase Cloud Messaging Device Group Message!",
},
},
}
// success
count, logs := handleNotification(ctx, cfg, req, q)
assert.Equal(t, 1, count)
assert.Equal(t, 0, len(logs))
}
func TestDisabledIosNotifications(t *testing.T) {
ctx := context.Background()
cfg := initTest()
cfg.Ios.Enabled = false
cfg.Ios.KeyPath = testKeyPath
err := notify.InitAPNSClient(cfg)
assert.Nil(t, err)
cfg.Android.Enabled = true
cfg.Android.Credential = os.Getenv("FCM_CREDENTIAL")
androidToken := os.Getenv("FCM_TEST_TOKEN")
req := notify.RequestPush{
Notifications: []notify.PushNotification{
// ios
{
Tokens: []string{"11aa01229f15f0f0c52021d8cf3cd0ae1f2365fe4cebc4af26cd6d76b7919ef7"},
Platform: core.PlatFormIos,
Message: "Welcome iOS platform",
},
// android
{
Tokens: []string{androidToken, androidToken + "_"},
Platform: core.PlatFormAndroid,
Message: "Welcome Android platform",
},
},
}
count, logs := handleNotification(ctx, cfg, req, q)
assert.Equal(t, 2, count)
assert.Equal(t, 0, len(logs))
}