2021-07-13 18:16:09 +00:00
|
|
|
package router
|
2016-03-31 16:35:53 +00:00
|
|
|
|
|
|
|
import (
|
2020-02-04 09:10:12 +00:00
|
|
|
"context"
|
2020-01-13 15:05:27 +00:00
|
|
|
"crypto/tls"
|
2022-12-17 14:59:18 +00:00
|
|
|
"io"
|
2020-01-13 15:05:27 +00:00
|
|
|
"log"
|
2016-03-31 16:35:53 +00:00
|
|
|
"net/http"
|
2016-04-01 16:37:58 +00:00
|
|
|
"os"
|
2016-04-01 02:35:04 +00:00
|
|
|
"runtime"
|
2016-04-01 03:01:54 +00:00
|
|
|
"testing"
|
2016-04-01 16:37:58 +00:00
|
|
|
"time"
|
2017-01-01 05:46:30 +00:00
|
|
|
|
|
|
|
"github.com/appleboy/gorush/config"
|
2021-07-13 08:32:39 +00:00
|
|
|
"github.com/appleboy/gorush/core"
|
2021-08-11 03:13:29 +00:00
|
|
|
"github.com/appleboy/gorush/logx"
|
2021-07-23 17:56:33 +00:00
|
|
|
"github.com/appleboy/gorush/notify"
|
2021-07-13 18:16:09 +00:00
|
|
|
"github.com/appleboy/gorush/status"
|
2019-06-10 17:49:33 +00:00
|
|
|
|
|
|
|
"github.com/appleboy/gofight/v2"
|
2017-01-01 05:46:30 +00:00
|
|
|
"github.com/buger/jsonparser"
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-08-15 08:12:08 +00:00
|
|
|
"github.com/golang-queue/queue"
|
2022-05-05 05:56:28 +00:00
|
|
|
qcore "github.com/golang-queue/queue/core"
|
2017-01-01 05:46:30 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-03-31 16:35:53 +00:00
|
|
|
)
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
var (
|
2022-12-20 14:03:29 +00:00
|
|
|
goVersion = runtime.Version()
|
|
|
|
q *queue.Queue
|
|
|
|
testKeyPath = "../certificate/certificate-valid.pem"
|
2021-07-16 04:10:34 +00:00
|
|
|
)
|
2016-04-01 02:35:04 +00:00
|
|
|
|
2021-07-13 18:16:09 +00:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
cfg := initTest()
|
|
|
|
if err := status.InitAppStatus(cfg); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-08-01 11:48:57 +00:00
|
|
|
cfg.Android.Enabled = true
|
|
|
|
cfg.Android.APIKey = os.Getenv("ANDROID_API_KEY")
|
|
|
|
|
|
|
|
if _, err := notify.InitFCMClient(cfg, ""); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-08-22 14:04:50 +00:00
|
|
|
q = queue.NewPool(
|
|
|
|
int(cfg.Core.WorkerNum),
|
2022-05-05 05:56:28 +00:00
|
|
|
queue.WithFn(func(ctx context.Context, msg qcore.QueuedMessage) error {
|
2021-08-01 11:48:57 +00:00
|
|
|
_, err := notify.SendNotification(msg, cfg)
|
|
|
|
return err
|
2021-07-24 14:33:44 +00:00
|
|
|
}),
|
2021-08-11 03:13:29 +00:00
|
|
|
queue.WithLogger(logx.QueueLogger()),
|
2021-07-23 17:29:47 +00:00
|
|
|
)
|
2021-08-04 01:00:04 +00:00
|
|
|
|
|
|
|
code := m.Run()
|
2021-07-16 08:30:01 +00:00
|
|
|
defer func() {
|
2021-08-22 14:04:50 +00:00
|
|
|
q.Release()
|
2021-08-04 01:00:04 +00:00
|
|
|
os.Exit(code)
|
2021-07-16 08:30:01 +00:00
|
|
|
}()
|
2021-07-13 18:16:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 06:07:30 +00:00
|
|
|
func initTest() *config.ConfYaml {
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg, _ := config.LoadConf()
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg.Core.Mode = "test"
|
|
|
|
return cfg
|
2016-03-31 16:35:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 15:05:27 +00:00
|
|
|
// testRequest is testing url string if server is running
|
|
|
|
func testRequest(t *testing.T, url string) {
|
|
|
|
tr := &http.Transport{
|
2022-12-18 02:06:43 +00:00
|
|
|
//nolint:gosec
|
2020-01-13 15:05:27 +00:00
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
|
|
|
client := &http.Client{
|
|
|
|
Timeout: time.Second * 10,
|
|
|
|
Transport: tr,
|
|
|
|
}
|
2022-12-19 08:43:05 +00:00
|
|
|
req, _ := http.NewRequestWithContext(
|
|
|
|
context.Background(),
|
|
|
|
http.MethodGet,
|
|
|
|
url,
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
resp, err := client.Do(req)
|
2020-01-13 15:05:27 +00:00
|
|
|
defer func() {
|
|
|
|
if err := resp.Body.Close(); err != nil {
|
|
|
|
log.Println("close body err:", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-12-17 14:59:18 +00:00
|
|
|
_, ioerr := io.ReadAll(resp.Body)
|
2020-01-13 15:05:27 +00:00
|
|
|
assert.NoError(t, ioerr)
|
|
|
|
assert.Equal(t, "200 OK", resp.Status, "should get a 200")
|
|
|
|
}
|
|
|
|
|
2016-04-13 07:22:04 +00:00
|
|
|
func TestPrintGoRushVersion(t *testing.T) {
|
2016-05-19 14:45:42 +00:00
|
|
|
SetVersion("3.0.0")
|
2023-02-27 08:27:56 +00:00
|
|
|
SetCommit("abcdefg")
|
2016-05-19 14:45:42 +00:00
|
|
|
ver := GetVersion()
|
2016-04-13 07:22:04 +00:00
|
|
|
PrintGoRushVersion()
|
2016-05-19 14:45:42 +00:00
|
|
|
|
|
|
|
assert.Equal(t, "3.0.0", ver)
|
2016-04-01 14:15:10 +00:00
|
|
|
}
|
|
|
|
|
2016-04-01 16:37:58 +00:00
|
|
|
func TestRunNormalServer(t *testing.T) {
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg := initTest()
|
2016-04-01 16:37:58 +00:00
|
|
|
|
2016-04-03 13:05:32 +00:00
|
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
|
2021-07-11 12:04:46 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2016-04-01 16:37:58 +00:00
|
|
|
go func() {
|
2021-07-16 04:10:34 +00:00
|
|
|
assert.NoError(t, RunHTTPServer(ctx, cfg, q))
|
2021-07-11 12:04:46 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
// close the server
|
|
|
|
cancel()
|
2016-04-01 16:37:58 +00:00
|
|
|
}()
|
|
|
|
// have to wait for the goroutine to start and run the server
|
|
|
|
// otherwise the main thread will complete
|
|
|
|
time.Sleep(5 * time.Millisecond)
|
|
|
|
|
2020-01-13 15:05:27 +00:00
|
|
|
testRequest(t, "http://localhost:8088/api/stat/go")
|
2016-04-01 16:37:58 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 15:49:52 +00:00
|
|
|
func TestRunTLSServer(t *testing.T) {
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg := initTest()
|
2016-04-04 15:49:52 +00:00
|
|
|
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg.Core.SSL = true
|
|
|
|
cfg.Core.Port = "8087"
|
|
|
|
cfg.Core.CertPath = "../certificate/localhost.cert"
|
|
|
|
cfg.Core.KeyPath = "../certificate/localhost.key"
|
2016-04-04 15:49:52 +00:00
|
|
|
|
2021-07-11 12:04:46 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2016-04-04 15:49:52 +00:00
|
|
|
go func() {
|
2021-07-16 04:10:34 +00:00
|
|
|
assert.NoError(t, RunHTTPServer(ctx, cfg, q))
|
2021-07-11 12:04:46 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
// close the server
|
|
|
|
cancel()
|
2016-04-04 15:49:52 +00:00
|
|
|
}()
|
|
|
|
// have to wait for the goroutine to start and run the server
|
|
|
|
// otherwise the main thread will complete
|
|
|
|
time.Sleep(5 * time.Millisecond)
|
|
|
|
|
2020-01-13 15:05:27 +00:00
|
|
|
testRequest(t, "https://localhost:8087/api/stat/go")
|
2016-04-04 15:49:52 +00:00
|
|
|
}
|
2016-04-01 16:37:58 +00:00
|
|
|
|
2018-02-18 09:43:34 +00:00
|
|
|
func TestRunTLSBase64Server(t *testing.T) {
|
2022-12-18 02:06:43 +00:00
|
|
|
//nolint
|
2021-01-23 01:39:06 +00:00
|
|
|
cert := `LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUMrekNDQWVPZ0F3SUJBZ0lKQUxiWkVEdlVRckZLTUEwR0NTcUdTSWIzRFFFQkJRVUFNQlF4RWpBUUJnTlYKQkFNTUNXeHZZMkZzYUc5emREQWVGdzB4TmpBek1qZ3dNek13TkRGYUZ3MHlOakF6TWpZd016TXdOREZhTUJReApFakFRQmdOVkJBTU1DV3h2WTJGc2FHOXpkRENDQVNJd0RRWUpLb1pJaHZjTkFRRUJCUUFEZ2dFUEFEQ0NBUW9DCmdnRUJBTWoxK3hnNGpWTHpWbkI1ajduMXVsMzBXRUU0QkN6Y05GeGc1QU9CNUg1cSt3amUwWVlpVkZnNlBReXYKR0NpcHFJUlhWUmRWUTFoSFNldW5ZR0tlOGxxM1NiMVg4UFVKMTJ2OXVSYnBTOURLMU93cWs4cnNQRHU2c1ZUTApxS0tnSDFaOHlhenphUzBBYlh1QTVlOWdPL1J6aWpibnBFUCtxdU00ZHVlaU1QVkVKeUxxK0VvSVFZK01NOE1QCjhkWnpMNFhabDd3TDRVc0NON3JQY082VzN0bG5UMGlPM2g5Yy9ZbTJoRmh6K0tOSjlLUlJDdnRQR1pFU2lndEsKYkhzWEgwOTlXRG84di9XcDUvZXZCdy8rSkQwb3B4bUNmSElCQUxIdDl2NTNSdnZzRFoxdDMzUnB1NUM4em5FWQpZMkF5N05neGhxanFvV0pxQTQ4bEplQTBjbHNDQXdFQUFhTlFNRTR3SFFZRFZSME9CQllFRkMwYlRVMVhvZmVoCk5LSWVsYXNoSXNxS2lkRFlNQjhHQTFVZEl3UVlNQmFBRkMwYlRVMVhvZmVoTktJZWxhc2hJc3FLaWREWU1Bd0cKQTFVZEV3UUZNQU1CQWY4d0RRWUpLb1pJaHZjTkFRRUZCUUFEZ2dFQkFBaUpMOElNVHdOWDlYcVFXWURGZ2tHNApBbnJWd1FocmVBcUM5clN4RENqcXFuTUhQSEd6Y0NlRE1MQU1vaDBrT3kyMG5vd1VHTnRDWjB1QnZuWDJxMWJOCmcxanQrR0JjTEpEUjNMTDRDcE5PbG0zWWhPeWN1TmZXTXhUQTdCWGttblNyWkQvN0toQXJzQkVZOGF1bHh3S0oKSFJnTmxJd2Uxb0ZEMVlkWDFCUzVwcDR0MjVCNlZxNEEzRk1NVWtWb1dFNjg4bkUxNjhodlFnd2pySGtnSGh3ZQplTjhsR0UyRGhGcmFYbldtRE1kd2FIRDNIUkZHaHlwcElGTitmN0JxYldYOWdNK1QyWVJUZk9iSVhMV2JxSkxECjNNay9Oa3hxVmNnNGVZNTR3SjF1ZkNVR0FZQUlhWTZmUXFpTlV6OG5od0szdDQ1TkJWVDl5L3VKWHFuVEx5WT0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=`
|
2022-12-18 02:06:43 +00:00
|
|
|
//nolint
|
2021-01-23 01:39:06 +00:00
|
|
|
key := `LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlFb2dJQkFBS0NBUUVBeVBYN0dEaU5Vdk5XY0htUHVmVzZYZlJZUVRnRUxOdzBYR0RrQTRIa2ZtcjdDTjdSCmhpSlVXRG85REs4WUtLbW9oRmRWRjFWRFdFZEo2NmRnWXA3eVdyZEp2VmZ3OVFuWGEvMjVGdWxMME1yVTdDcVQKeXV3OE83cXhWTXVvb3FBZlZuekpyUE5wTFFCdGU0RGw3MkE3OUhPS051ZWtRLzZxNHpoMjU2SXc5VVFuSXVyNApTZ2hCajR3end3L3gxbk12aGRtWHZBdmhTd0kzdXM5dzdwYmUyV2RQU0k3ZUgxejlpYmFFV0hQNG8wbjBwRkVLCiswOFprUktLQzBwc2V4Y2ZUMzFZT2p5Lzlhbm45NjhIRC80a1BTaW5HWUo4Y2dFQXNlMzIvbmRHKyt3Tm5XM2YKZEdtN2tMek9jUmhqWURMczJER0dxT3FoWW1vRGp5VWw0RFJ5V3dJREFRQUJBb0lCQUdUS3FzTjlLYlNmQTQycQpDcUkwVXVMb3VKTU5hMXFzbno1dUFpNllLV2dXZEE0QTQ0bXBFakNtRlJTVmhVSnZ4V3VLK2N5WUlRelh4SVdECkQxNm5aZHFGNzJBZUNXWjlKeVNzdnZaMDBHZktNM3kzNWlSeTA4c0pXZ096bWNMbkdKQ2lTZXlLc1FlM0hUSkMKZGhEWGJYcXZzSFRWUFpnMDFMVGVEeFVpVGZmVThOTUtxUjJBZWNRMnNURHdYRWhBblR5QXRuemwvWGFCZ0Z6dQpVNkc3RnpHTTV5OWJ4a2ZRVmt2eStERUprSEdOT2p6d2NWZkJ5eVZsNjEwaXhtRzF2bXhWajlQYldtSVBzVVY4CnlTbWpodkRRYk9mb3hXMGg5dlRsVHFHdFFjQnc5NjJvc25ERE1XRkNkTTdsek8wVDdSUm5QVkdJUnBDSk9LaHEKa2VxSEt3RUNnWUVBOHd3SS9pWnVnaG9UWFRORzlMblFRL1dBdHNxTzgwRWpNVFVoZW81STFrT3ptVXowOXB5aAppQXNVRG9OMC8yNnRaNVdOamxueVp1N2R2VGMveDNkVFpwbU5ub284Z2NWYlFORUNEUnpxZnVROVBQWG0xU041CjZwZUJxQXZCdjc4aGpWMDVhWHpQRy9WQmJlaWc3bDI5OUVhckVBK2Evb0gzS3JnRG9xVnFFMEVDZ1lFQTA2dkEKWUptZ2c0ZlpSdWNBWW9hWXNMejlaOXJDRmpUZTFQQlRtVUprYk9SOHZGSUhIVFRFV2kvU3V4WEwwd0RTZW9FMgo3QlFtODZnQ0M3L0tnUmRyem9CcVo1cVM5TXYyZHNMZ1k2MzVWU2dqamZaa1ZMaUgxVlJScFNRT2JZbmZveXNnCmdhdGNIU0tNRXhkNFNMUUJ5QXVJbVhQK0w1YXlEQmNFSmZicVNwc0NnWUI3OElzMWIwdXpOTERqT2g3WTlWaHIKRDJxUHpFT1JjSW9Oc2RaY3RPb1h1WGFBbW1uZ3lJYm01UjlaTjFnV1djNDdvRndMVjNyeFdxWGdzNmZtZzhjWAo3djMwOXZGY0M5UTQvVnhhYTRCNUxOSzluM2dUQUlCUFRPdGxVbmwrMm15MXRmQnRCcVJtMFc2SUtiVEhXUzVnCnZ4akVtL0NpRUl5R1VFZ3FUTWdIQVFLQmdCS3VYZFFvdXRuZzYzUXVmd0l6RHRiS1Z6TUxRNFhpTktobWJYcGgKT2F2Q25wK2dQYkIrTDdZbDhsdEFtVFNPSmdWWjBoY1QwRHhBMzYxWngrMk11NThHQmw0T2JsbmNobXdFMXZqMQpLY1F5UHJFUXhkb1VUeWlzd0dmcXZyczhKOWltdmIrejkvVTZUMUtBQjhXaTNXVmlYelByNE1zaWFhUlhnNjQyCkZJZHhBb0dBWjcvNzM1ZGtoSmN5T2ZzK0xLc0xyNjhKU3N0b29yWE9ZdmRNdTErSkdhOWlMdWhuSEVjTVZXQzgKSXVpaHpQZmxvWnRNYkdZa1pKbjhsM0JlR2Q4aG1mRnRnVGdaR1BvVlJldGZ0MkxERkxuUHhwMnNFSDVPRkxzUQpSK0sva0FPdWw4ZVN0V3VNWE9GQTlwTXpHa0dFZ0lGSk1KT3lhSk9OM2tlZFFJOGRlQ009Ci0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==`
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg := initTest()
|
2018-02-18 09:43:34 +00:00
|
|
|
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg.Core.SSL = true
|
|
|
|
cfg.Core.Port = "8089"
|
|
|
|
cfg.Core.CertPath = ""
|
|
|
|
cfg.Core.KeyPath = ""
|
|
|
|
cfg.Core.CertBase64 = cert
|
|
|
|
cfg.Core.KeyBase64 = key
|
2018-02-18 09:43:34 +00:00
|
|
|
|
2021-07-11 12:04:46 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2018-02-18 09:43:34 +00:00
|
|
|
go func() {
|
2021-07-16 04:10:34 +00:00
|
|
|
assert.NoError(t, RunHTTPServer(ctx, cfg, q))
|
2021-07-11 12:04:46 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
// close the server
|
|
|
|
cancel()
|
2018-02-18 09:43:34 +00:00
|
|
|
}()
|
|
|
|
// have to wait for the goroutine to start and run the server
|
|
|
|
// otherwise the main thread will complete
|
|
|
|
time.Sleep(5 * time.Millisecond)
|
|
|
|
|
2020-01-13 15:05:27 +00:00
|
|
|
testRequest(t, "https://localhost:8089/api/stat/go")
|
2018-02-18 09:43:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-05 04:03:43 +00:00
|
|
|
func TestRunAutoTLSServer(t *testing.T) {
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg := initTest()
|
|
|
|
cfg.Core.AutoTLS.Enabled = true
|
2021-07-11 12:04:46 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2017-04-05 04:03:43 +00:00
|
|
|
go func() {
|
2021-07-16 04:10:34 +00:00
|
|
|
assert.NoError(t, RunHTTPServer(ctx, cfg, q))
|
2021-07-11 12:04:46 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
// close the server
|
|
|
|
cancel()
|
2017-04-05 04:03:43 +00:00
|
|
|
}()
|
|
|
|
// have to wait for the goroutine to start and run the server
|
|
|
|
// otherwise the main thread will complete
|
|
|
|
time.Sleep(5 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2017-01-01 07:47:54 +00:00
|
|
|
func TestLoadTLSCertError(t *testing.T) {
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg := initTest()
|
2017-01-01 07:47:54 +00:00
|
|
|
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg.Core.SSL = true
|
|
|
|
cfg.Core.Port = "8087"
|
|
|
|
cfg.Core.CertPath = "../config/config.yml"
|
|
|
|
cfg.Core.KeyPath = "../config/config.yml"
|
2017-01-01 07:47:54 +00:00
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
assert.Error(t, RunHTTPServer(context.Background(), cfg, q))
|
2017-01-01 07:47:54 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 18:16:09 +00:00
|
|
|
func TestMissingTLSCertcfgg(t *testing.T) {
|
|
|
|
cfg := initTest()
|
2018-02-18 09:43:34 +00:00
|
|
|
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg.Core.SSL = true
|
|
|
|
cfg.Core.Port = "8087"
|
|
|
|
cfg.Core.CertPath = ""
|
|
|
|
cfg.Core.KeyPath = ""
|
|
|
|
cfg.Core.CertBase64 = ""
|
|
|
|
cfg.Core.KeyBase64 = ""
|
2018-02-18 09:43:34 +00:00
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
err := RunHTTPServer(context.Background(), cfg, q)
|
|
|
|
assert.Error(t, RunHTTPServer(context.Background(), cfg, q))
|
2018-02-18 09:43:34 +00:00
|
|
|
assert.Equal(t, "missing https cert config", err.Error())
|
|
|
|
}
|
|
|
|
|
2016-04-01 01:53:10 +00:00
|
|
|
func TestRootHandler(t *testing.T) {
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg := initTest()
|
2016-03-31 16:35:53 +00:00
|
|
|
|
|
|
|
r := gofight.New()
|
|
|
|
|
2016-04-07 06:42:38 +00:00
|
|
|
// log for json
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg.Log.Format = "json"
|
2016-04-07 06:42:38 +00:00
|
|
|
|
2016-03-31 16:35:53 +00:00
|
|
|
r.GET("/").
|
2021-07-16 04:10:34 +00:00
|
|
|
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2019-10-17 15:49:21 +00:00
|
|
|
data := r.Body.Bytes()
|
2016-03-31 16:35:53 +00:00
|
|
|
|
|
|
|
value, _ := jsonparser.GetString(data, "text")
|
|
|
|
|
|
|
|
assert.Equal(t, "Welcome to notification server.", value)
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
}
|
2016-04-01 02:35:04 +00:00
|
|
|
|
2016-04-15 02:24:39 +00:00
|
|
|
func TestAPIStatusGoHandler(t *testing.T) {
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg := initTest()
|
2016-04-01 02:35:04 +00:00
|
|
|
|
|
|
|
r := gofight.New()
|
|
|
|
|
2016-04-15 02:24:39 +00:00
|
|
|
r.GET("/api/stat/go").
|
2021-07-16 04:10:34 +00:00
|
|
|
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2019-10-17 15:49:21 +00:00
|
|
|
data := r.Body.Bytes()
|
2016-04-01 02:35:04 +00:00
|
|
|
|
2016-04-13 07:22:04 +00:00
|
|
|
value, _ := jsonparser.GetString(data, "go_version")
|
2016-04-01 02:35:04 +00:00
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
assert.Equal(t, goVersion, value)
|
2016-04-01 02:35:04 +00:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-15 02:24:39 +00:00
|
|
|
func TestAPIStatusAppHandler(t *testing.T) {
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg := initTest()
|
2016-04-15 02:24:39 +00:00
|
|
|
|
|
|
|
r := gofight.New()
|
|
|
|
|
2016-09-30 02:49:52 +00:00
|
|
|
appVersion := "v1.0.0"
|
|
|
|
SetVersion(appVersion)
|
|
|
|
|
2016-04-15 02:24:39 +00:00
|
|
|
r.GET("/api/stat/app").
|
2021-07-16 04:10:34 +00:00
|
|
|
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2019-10-17 15:49:21 +00:00
|
|
|
data := r.Body.Bytes()
|
2016-09-30 02:49:52 +00:00
|
|
|
|
|
|
|
value, _ := jsonparser.GetString(data, "version")
|
|
|
|
|
|
|
|
assert.Equal(t, appVersion, value)
|
2016-04-15 02:24:39 +00:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-15 07:05:23 +00:00
|
|
|
func TestAPIConfigHandler(t *testing.T) {
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg := initTest()
|
2016-04-15 07:05:23 +00:00
|
|
|
|
|
|
|
r := gofight.New()
|
|
|
|
|
|
|
|
r.GET("/api/config").
|
2021-07-16 04:10:34 +00:00
|
|
|
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2016-04-15 07:05:23 +00:00
|
|
|
assert.Equal(t, http.StatusCreated, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-10 04:02:44 +00:00
|
|
|
func TestMissingNotificationsParameter(t *testing.T) {
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg := initTest()
|
2016-04-01 02:35:04 +00:00
|
|
|
|
|
|
|
r := gofight.New()
|
|
|
|
|
2016-04-10 04:02:44 +00:00
|
|
|
// missing notifications parameter.
|
2016-04-01 02:35:04 +00:00
|
|
|
r.POST("/api/push").
|
2021-07-16 04:10:34 +00:00
|
|
|
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2016-04-01 02:35:04 +00:00
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
|
|
|
}
|
2016-04-01 03:58:34 +00:00
|
|
|
|
2016-04-10 04:02:44 +00:00
|
|
|
func TestEmptyNotifications(t *testing.T) {
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg := initTest()
|
2016-04-01 15:06:29 +00:00
|
|
|
|
2016-04-01 09:14:23 +00:00
|
|
|
r := gofight.New()
|
|
|
|
|
2016-04-10 04:02:44 +00:00
|
|
|
// notifications is empty.
|
2016-04-01 09:14:23 +00:00
|
|
|
r.POST("/api/push").
|
|
|
|
SetJSON(gofight.D{
|
2021-07-23 17:56:33 +00:00
|
|
|
"notifications": []notify.PushNotification{},
|
2016-04-01 03:58:34 +00:00
|
|
|
}).
|
2021-07-16 04:10:34 +00:00
|
|
|
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2016-04-10 04:02:44 +00:00
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
2016-04-01 14:15:10 +00:00
|
|
|
})
|
|
|
|
}
|
2016-04-09 02:26:55 +00:00
|
|
|
|
2018-05-22 02:19:13 +00:00
|
|
|
func TestMutableContent(t *testing.T) {
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg := initTest()
|
2018-05-22 02:19:13 +00:00
|
|
|
|
|
|
|
r := gofight.New()
|
|
|
|
|
|
|
|
// notifications is empty.
|
|
|
|
r.POST("/api/push").
|
|
|
|
SetJSON(gofight.D{
|
|
|
|
"notifications": []gofight.D{
|
|
|
|
{
|
|
|
|
"tokens": []string{"aaaaa", "bbbbb"},
|
2021-07-13 08:32:39 +00:00
|
|
|
"platform": core.PlatFormAndroid,
|
2021-07-16 08:30:01 +00:00
|
|
|
"message": "Welcome From API",
|
2018-08-15 03:47:15 +00:00
|
|
|
"mutable_content": 1,
|
2018-05-22 02:19:13 +00:00
|
|
|
"topic": "test",
|
|
|
|
"badge": 1,
|
|
|
|
"alert": gofight.D{
|
|
|
|
"title": "title",
|
|
|
|
"body": "body",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}).
|
2021-07-16 04:10:34 +00:00
|
|
|
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2021-07-23 17:56:33 +00:00
|
|
|
// json: cannot unmarshal number into Go struct field notify.PushNotification.mutable_content of type bool
|
2018-05-22 02:19:13 +00:00
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-10 05:04:32 +00:00
|
|
|
func TestOutOfRangeMaxNotifications(t *testing.T) {
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg := initTest()
|
2016-04-10 05:04:32 +00:00
|
|
|
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg.Core.MaxNotification = int64(1)
|
2016-04-10 05:04:32 +00:00
|
|
|
|
|
|
|
r := gofight.New()
|
|
|
|
|
|
|
|
// notifications is empty.
|
|
|
|
r.POST("/api/push").
|
|
|
|
SetJSON(gofight.D{
|
|
|
|
"notifications": []gofight.D{
|
2016-04-15 10:06:16 +00:00
|
|
|
{
|
2016-04-10 05:04:32 +00:00
|
|
|
"tokens": []string{"aaaaa", "bbbbb"},
|
2021-07-13 08:32:39 +00:00
|
|
|
"platform": core.PlatFormAndroid,
|
2021-07-16 08:30:01 +00:00
|
|
|
"message": "Welcome API From Android",
|
2016-04-10 05:04:32 +00:00
|
|
|
},
|
2016-04-15 10:06:16 +00:00
|
|
|
{
|
2016-04-10 05:04:32 +00:00
|
|
|
"tokens": []string{"aaaaa", "bbbbb"},
|
2021-07-13 08:32:39 +00:00
|
|
|
"platform": core.PlatFormAndroid,
|
2021-07-16 08:30:01 +00:00
|
|
|
"message": "Welcome API From Android",
|
2016-04-10 05:04:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}).
|
2021-07-16 04:10:34 +00:00
|
|
|
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2016-04-10 05:04:32 +00:00
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-10 04:02:44 +00:00
|
|
|
func TestSuccessPushHandler(t *testing.T) {
|
2019-10-17 15:19:25 +00:00
|
|
|
t.Skip()
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg := initTest()
|
2016-04-09 02:26:55 +00:00
|
|
|
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg.Android.Enabled = true
|
|
|
|
cfg.Android.APIKey = os.Getenv("ANDROID_API_KEY")
|
2016-04-09 02:26:55 +00:00
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
androidToken := os.Getenv("ANDROID_TEST_TOKEN")
|
2016-04-09 02:26:55 +00:00
|
|
|
|
|
|
|
r := gofight.New()
|
|
|
|
|
|
|
|
r.POST("/api/push").
|
|
|
|
SetJSON(gofight.D{
|
2016-04-10 04:02:44 +00:00
|
|
|
"notifications": []gofight.D{
|
2016-04-15 10:06:16 +00:00
|
|
|
{
|
2016-04-13 06:59:28 +00:00
|
|
|
"tokens": []string{androidToken, "bbbbb"},
|
2021-07-13 08:32:39 +00:00
|
|
|
"platform": core.PlatFormAndroid,
|
2021-07-16 08:30:01 +00:00
|
|
|
"message": "Welcome Android",
|
2016-04-10 04:02:44 +00:00
|
|
|
},
|
|
|
|
},
|
2016-04-09 02:26:55 +00:00
|
|
|
}).
|
2021-07-16 04:10:34 +00:00
|
|
|
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2016-04-09 02:26:55 +00:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
}
|
2016-06-26 04:21:32 +00:00
|
|
|
|
|
|
|
func TestSysStatsHandler(t *testing.T) {
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg := initTest()
|
2016-06-26 04:21:32 +00:00
|
|
|
|
|
|
|
r := gofight.New()
|
|
|
|
|
|
|
|
r.GET("/sys/stats").
|
2021-07-16 04:10:34 +00:00
|
|
|
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2016-06-26 04:21:32 +00:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
}
|
2017-01-19 08:56:30 +00:00
|
|
|
|
|
|
|
func TestMetricsHandler(t *testing.T) {
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg := initTest()
|
2017-01-19 08:56:30 +00:00
|
|
|
|
|
|
|
r := gofight.New()
|
|
|
|
|
|
|
|
r.GET("/metrics").
|
2021-07-16 04:10:34 +00:00
|
|
|
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2017-01-19 08:56:30 +00:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
}
|
2017-07-27 03:19:36 +00:00
|
|
|
|
2020-06-23 05:58:40 +00:00
|
|
|
func TestGETHeartbeatHandler(t *testing.T) {
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg := initTest()
|
2017-09-26 03:10:51 +00:00
|
|
|
|
|
|
|
r := gofight.New()
|
|
|
|
|
|
|
|
r.GET("/healthz").
|
2021-07-16 04:10:34 +00:00
|
|
|
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2017-09-26 03:10:51 +00:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
}
|
2017-09-26 03:25:41 +00:00
|
|
|
|
2020-06-23 05:58:40 +00:00
|
|
|
func TestHEADHeartbeatHandler(t *testing.T) {
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg := initTest()
|
2020-06-23 05:58:40 +00:00
|
|
|
|
|
|
|
r := gofight.New()
|
|
|
|
|
|
|
|
r.HEAD("/healthz").
|
2021-07-16 04:10:34 +00:00
|
|
|
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2020-06-23 05:58:40 +00:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-26 03:25:41 +00:00
|
|
|
func TestVersionHandler(t *testing.T) {
|
|
|
|
SetVersion("3.0.0")
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg := initTest()
|
2017-09-26 03:25:41 +00:00
|
|
|
|
|
|
|
r := gofight.New()
|
|
|
|
|
|
|
|
r.GET("/version").
|
2021-07-16 04:10:34 +00:00
|
|
|
Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2017-09-26 03:25:41 +00:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2019-10-17 15:49:21 +00:00
|
|
|
data := r.Body.Bytes()
|
2017-09-26 03:25:41 +00:00
|
|
|
|
|
|
|
value, _ := jsonparser.GetString(data, "version")
|
|
|
|
|
|
|
|
assert.Equal(t, "3.0.0", value)
|
|
|
|
})
|
|
|
|
}
|
2021-01-23 01:39:06 +00:00
|
|
|
|
2017-07-27 03:19:36 +00:00
|
|
|
func TestDisabledHTTPServer(t *testing.T) {
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg := initTest()
|
|
|
|
cfg.Core.Enabled = false
|
2021-07-16 04:10:34 +00:00
|
|
|
err := RunHTTPServer(context.Background(), cfg, q)
|
2021-07-13 18:16:09 +00:00
|
|
|
cfg.Core.Enabled = true
|
2017-07-27 03:19:36 +00:00
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
2021-07-16 04:10:34 +00:00
|
|
|
|
|
|
|
func TestSenMultipleNotifications(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
cfg := initTest()
|
|
|
|
|
|
|
|
cfg.Ios.Enabled = true
|
2022-12-20 14:03:29 +00:00
|
|
|
cfg.Ios.KeyPath = testKeyPath
|
2021-07-23 17:56:33 +00:00
|
|
|
err := notify.InitAPNSClient(cfg)
|
2021-07-16 04:10:34 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
cfg.Android.Enabled = true
|
|
|
|
cfg.Android.APIKey = os.Getenv("ANDROID_API_KEY")
|
|
|
|
|
|
|
|
androidToken := os.Getenv("ANDROID_TEST_TOKEN")
|
|
|
|
|
2021-07-23 17:56:33 +00:00
|
|
|
req := notify.RequestPush{
|
|
|
|
Notifications: []notify.PushNotification{
|
2021-07-16 04:10:34 +00:00
|
|
|
// ios
|
|
|
|
{
|
|
|
|
Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"},
|
|
|
|
Platform: core.PlatFormIos,
|
2021-07-16 08:30:01 +00:00
|
|
|
Message: "Welcome iOS",
|
2021-07-16 04:10:34 +00:00
|
|
|
},
|
|
|
|
// android
|
|
|
|
{
|
|
|
|
Tokens: []string{androidToken, "bbbbb"},
|
|
|
|
Platform: core.PlatFormAndroid,
|
2021-07-16 08:30:01 +00:00
|
|
|
Message: "Welcome Android",
|
2021-07-16 04:10:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2022-12-20 14:03:29 +00:00
|
|
|
cfg.Ios.KeyPath = testKeyPath
|
2021-07-23 17:56:33 +00:00
|
|
|
err := notify.InitAPNSClient(cfg)
|
2021-07-16 04:10:34 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
cfg.Android.Enabled = false
|
|
|
|
cfg.Android.APIKey = os.Getenv("ANDROID_API_KEY")
|
|
|
|
|
|
|
|
androidToken := os.Getenv("ANDROID_TEST_TOKEN")
|
|
|
|
|
2021-07-23 17:56:33 +00:00
|
|
|
req := notify.RequestPush{
|
|
|
|
Notifications: []notify.PushNotification{
|
2021-07-16 04:10:34 +00:00
|
|
|
// ios
|
|
|
|
{
|
2021-07-16 08:30:01 +00:00
|
|
|
Tokens: []string{"11aa01229f15f0f0c5209d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"},
|
2021-07-16 04:10:34 +00:00
|
|
|
Platform: core.PlatFormIos,
|
2021-07-16 08:30:01 +00:00
|
|
|
Message: "Welcome iOS",
|
2021-07-16 04:10:34 +00:00
|
|
|
},
|
|
|
|
// android
|
|
|
|
{
|
|
|
|
Tokens: []string{androidToken, "bbbbb"},
|
|
|
|
Platform: core.PlatFormAndroid,
|
2021-07-16 08:30:01 +00:00
|
|
|
Message: "Welcome Android",
|
2021-07-16 04:10:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2022-12-20 14:03:29 +00:00
|
|
|
cfg.Ios.KeyPath = testKeyPath
|
2021-07-23 17:56:33 +00:00
|
|
|
err := notify.InitAPNSClient(cfg)
|
2021-07-16 04:10:34 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
cfg.Android.Enabled = true
|
|
|
|
cfg.Android.APIKey = os.Getenv("ANDROID_API_KEY")
|
|
|
|
|
|
|
|
// enable sync mode
|
|
|
|
cfg.Core.Sync = true
|
|
|
|
|
|
|
|
androidToken := os.Getenv("ANDROID_TEST_TOKEN")
|
|
|
|
|
2021-07-23 17:56:33 +00:00
|
|
|
req := notify.RequestPush{
|
|
|
|
Notifications: []notify.PushNotification{
|
2021-07-16 04:10:34 +00:00
|
|
|
// ios
|
|
|
|
{
|
2021-07-16 08:30:01 +00:00
|
|
|
Tokens: []string{
|
|
|
|
"11aa01229f15f0f0c12029d8c111d1ae1f2365f14cebc4af26cd6d76b7919ef7",
|
|
|
|
},
|
2021-07-16 04:10:34 +00:00
|
|
|
Platform: core.PlatFormIos,
|
2021-07-16 08:30:01 +00:00
|
|
|
Message: "Welcome iOS Sync",
|
2021-07-16 04:10:34 +00:00
|
|
|
},
|
|
|
|
// android
|
|
|
|
{
|
|
|
|
Tokens: []string{androidToken, "bbbbb"},
|
|
|
|
Platform: core.PlatFormAndroid,
|
2021-07-16 08:30:01 +00:00
|
|
|
Message: "Welcome Android Sync",
|
2021-07-16 04:10:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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.APIKey = os.Getenv("ANDROID_API_KEY")
|
|
|
|
cfg.Log.HideToken = false
|
|
|
|
|
|
|
|
// enable sync mode
|
|
|
|
cfg.Core.Sync = true
|
|
|
|
|
2021-07-23 17:56:33 +00:00
|
|
|
req := notify.RequestPush{
|
|
|
|
Notifications: []notify.PushNotification{
|
2021-07-16 04:10:34 +00:00
|
|
|
// android
|
|
|
|
{
|
|
|
|
// error:InvalidParameters
|
|
|
|
// Check that the provided parameters have the right name and type.
|
|
|
|
To: "/topics/foo-bar@@@##",
|
|
|
|
Platform: core.PlatFormAndroid,
|
|
|
|
Message: "This is a Firebase Cloud Messaging Topic Message!",
|
|
|
|
},
|
|
|
|
// android
|
|
|
|
{
|
|
|
|
// success
|
|
|
|
To: "/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, 1, len(logs))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSyncModeForDeviceGroupNotification(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
cfg := initTest()
|
|
|
|
|
|
|
|
cfg.Android.Enabled = true
|
|
|
|
cfg.Android.APIKey = os.Getenv("ANDROID_API_KEY")
|
|
|
|
cfg.Log.HideToken = false
|
|
|
|
|
|
|
|
// enable sync mode
|
|
|
|
cfg.Core.Sync = true
|
|
|
|
|
2021-07-23 17:56:33 +00:00
|
|
|
req := notify.RequestPush{
|
|
|
|
Notifications: []notify.PushNotification{
|
2021-07-16 04:10:34 +00:00
|
|
|
// android
|
|
|
|
{
|
|
|
|
To: "aUniqueKey",
|
|
|
|
Platform: core.PlatFormAndroid,
|
|
|
|
Message: "This is a Firebase Cloud Messaging Device Group Message!",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
count, logs := handleNotification(ctx, cfg, req, q)
|
|
|
|
assert.Equal(t, 1, count)
|
|
|
|
assert.Equal(t, 1, len(logs))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDisabledIosNotifications(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
cfg := initTest()
|
|
|
|
|
|
|
|
cfg.Ios.Enabled = false
|
2022-12-20 14:03:29 +00:00
|
|
|
cfg.Ios.KeyPath = testKeyPath
|
2021-07-23 17:56:33 +00:00
|
|
|
err := notify.InitAPNSClient(cfg)
|
2021-07-16 04:10:34 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
cfg.Android.Enabled = true
|
|
|
|
cfg.Android.APIKey = os.Getenv("ANDROID_API_KEY")
|
|
|
|
|
|
|
|
androidToken := os.Getenv("ANDROID_TEST_TOKEN")
|
|
|
|
|
2021-07-23 17:56:33 +00:00
|
|
|
req := notify.RequestPush{
|
|
|
|
Notifications: []notify.PushNotification{
|
2021-07-16 04:10:34 +00:00
|
|
|
// ios
|
|
|
|
{
|
2021-07-16 08:30:01 +00:00
|
|
|
Tokens: []string{"11aa01229f15f0f0c52021d8cf3cd0ae1f2365fe4cebc4af26cd6d76b7919ef7"},
|
2021-07-16 04:10:34 +00:00
|
|
|
Platform: core.PlatFormIos,
|
2021-07-16 08:30:01 +00:00
|
|
|
Message: "Welcome iOS platform",
|
2021-07-16 04:10:34 +00:00
|
|
|
},
|
|
|
|
// android
|
|
|
|
{
|
|
|
|
Tokens: []string{androidToken, androidToken + "_"},
|
|
|
|
Platform: core.PlatFormAndroid,
|
2021-07-16 08:30:01 +00:00
|
|
|
Message: "Welcome Android platform",
|
2021-07-16 04:10:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
count, logs := handleNotification(ctx, cfg, req, q)
|
|
|
|
assert.Equal(t, 2, count)
|
|
|
|
assert.Equal(t, 0, len(logs))
|
|
|
|
}
|