2021-07-23 17:56:33 +00:00
|
|
|
package notify
|
2017-07-25 08:41:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/appleboy/gorush/config"
|
2021-07-13 08:32:39 +00:00
|
|
|
"github.com/appleboy/gorush/core"
|
|
|
|
|
|
|
|
"github.com/appleboy/go-fcm"
|
2017-07-25 08:41:30 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2017-07-25 13:05:27 +00:00
|
|
|
func TestMissingAndroidAPIKey(t *testing.T) {
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg, _ := config.LoadConf()
|
2017-07-25 13:05:27 +00:00
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg.Android.Enabled = true
|
|
|
|
cfg.Android.APIKey = ""
|
2017-07-25 13:05:27 +00:00
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
err := CheckPushConf(cfg)
|
2017-07-25 13:05:27 +00:00
|
|
|
|
|
|
|
assert.Error(t, err)
|
2022-12-20 14:20:24 +00:00
|
|
|
assert.Equal(t, "missing android api key", err.Error())
|
2017-07-25 13:05:27 +00:00
|
|
|
}
|
2021-01-23 01:39:06 +00:00
|
|
|
|
2017-07-25 08:41:30 +00:00
|
|
|
func TestMissingKeyForInitFCMClient(t *testing.T) {
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg, _ := config.LoadConf()
|
|
|
|
cfg.Android.APIKey = ""
|
|
|
|
client, err := InitFCMClient(cfg, "")
|
2017-07-25 08:41:30 +00:00
|
|
|
|
|
|
|
assert.Nil(t, client)
|
|
|
|
assert.Error(t, err)
|
2022-12-20 14:20:24 +00:00
|
|
|
assert.Equal(t, "missing android api key", err.Error())
|
2017-07-25 08:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushToAndroidWrongToken(t *testing.T) {
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg, _ := config.LoadConf()
|
2017-07-25 08:41:30 +00:00
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg.Android.Enabled = true
|
|
|
|
cfg.Android.APIKey = os.Getenv("ANDROID_API_KEY")
|
2017-07-25 08:41:30 +00:00
|
|
|
|
2021-08-02 06:07:30 +00:00
|
|
|
req := &PushNotification{
|
2017-07-25 08:41:30 +00:00
|
|
|
Tokens: []string{"aaaaaa", "bbbbb"},
|
2021-07-13 08:32:39 +00:00
|
|
|
Platform: core.PlatFormAndroid,
|
2017-07-25 08:41:30 +00:00
|
|
|
Message: "Welcome",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Android Success count: 0, Failure count: 2
|
2021-08-03 06:44:00 +00:00
|
|
|
resp, err := PushToAndroid(req, cfg)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, resp.Logs, 2)
|
2017-07-25 08:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushToAndroidRightTokenForJSONLog(t *testing.T) {
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg, _ := config.LoadConf()
|
2017-07-25 08:41:30 +00:00
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg.Android.Enabled = true
|
|
|
|
cfg.Android.APIKey = os.Getenv("ANDROID_API_KEY")
|
2017-07-25 08:41:30 +00:00
|
|
|
// log for json
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg.Log.Format = "json"
|
2017-07-25 08:41:30 +00:00
|
|
|
|
|
|
|
androidToken := os.Getenv("ANDROID_TEST_TOKEN")
|
|
|
|
|
2021-08-02 06:07:30 +00:00
|
|
|
req := &PushNotification{
|
2017-07-25 08:41:30 +00:00
|
|
|
Tokens: []string{androidToken},
|
2021-07-13 08:32:39 +00:00
|
|
|
Platform: core.PlatFormAndroid,
|
2017-07-25 08:41:30 +00:00
|
|
|
Message: "Welcome",
|
|
|
|
}
|
|
|
|
|
2021-08-03 06:44:00 +00:00
|
|
|
resp, err := PushToAndroid(req, cfg)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, resp.Logs, 0)
|
2017-07-25 08:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushToAndroidRightTokenForStringLog(t *testing.T) {
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg, _ := config.LoadConf()
|
2017-07-25 08:41:30 +00:00
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg.Android.Enabled = true
|
|
|
|
cfg.Android.APIKey = os.Getenv("ANDROID_API_KEY")
|
2017-07-25 08:41:30 +00:00
|
|
|
|
|
|
|
androidToken := os.Getenv("ANDROID_TEST_TOKEN")
|
|
|
|
|
2021-08-02 06:07:30 +00:00
|
|
|
req := &PushNotification{
|
2017-07-25 08:41:30 +00:00
|
|
|
Tokens: []string{androidToken},
|
2021-07-13 08:32:39 +00:00
|
|
|
Platform: core.PlatFormAndroid,
|
2017-07-25 08:41:30 +00:00
|
|
|
Message: "Welcome",
|
|
|
|
}
|
|
|
|
|
2021-08-03 06:44:00 +00:00
|
|
|
resp, err := PushToAndroid(req, cfg)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, resp.Logs, 0)
|
2017-07-25 08:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOverwriteAndroidAPIKey(t *testing.T) {
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg, _ := config.LoadConf()
|
2017-07-25 08:41:30 +00:00
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg.Core.Sync = true
|
|
|
|
cfg.Android.Enabled = true
|
|
|
|
cfg.Android.APIKey = os.Getenv("ANDROID_API_KEY")
|
2017-07-25 08:41:30 +00:00
|
|
|
|
|
|
|
androidToken := os.Getenv("ANDROID_TEST_TOKEN")
|
|
|
|
|
2021-08-02 06:07:30 +00:00
|
|
|
req := &PushNotification{
|
2017-07-25 08:41:30 +00:00
|
|
|
Tokens: []string{androidToken, "bbbbb"},
|
2021-07-13 08:32:39 +00:00
|
|
|
Platform: core.PlatFormAndroid,
|
2017-07-25 08:41:30 +00:00
|
|
|
Message: "Welcome",
|
|
|
|
// overwrite android api key
|
|
|
|
APIKey: "1234",
|
|
|
|
}
|
|
|
|
|
|
|
|
// FCM server error: 401 error: 401 Unauthorized (Wrong API Key)
|
2021-08-01 11:48:57 +00:00
|
|
|
resp, err := PushToAndroid(req, cfg)
|
2020-11-08 00:56:11 +00:00
|
|
|
|
2021-08-01 09:12:16 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Len(t, resp.Logs, 2)
|
2017-07-25 08:41:30 +00:00
|
|
|
}
|
2017-07-25 13:05:27 +00:00
|
|
|
|
|
|
|
func TestFCMMessage(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// the message must specify at least one registration ID
|
2021-08-02 06:07:30 +00:00
|
|
|
req := &PushNotification{
|
2017-07-25 13:05:27 +00:00
|
|
|
Message: "Test",
|
|
|
|
Tokens: []string{},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = CheckMessage(req)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// the token must not be empty
|
2021-08-02 06:07:30 +00:00
|
|
|
req = &PushNotification{
|
2017-07-25 13:05:27 +00:00
|
|
|
Message: "Test",
|
|
|
|
Tokens: []string{""},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = CheckMessage(req)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2017-10-24 09:00:08 +00:00
|
|
|
// ignore check token length if send topic message
|
2021-08-02 06:07:30 +00:00
|
|
|
req = &PushNotification{
|
2017-10-24 09:00:08 +00:00
|
|
|
Message: "Test",
|
2021-07-13 08:32:39 +00:00
|
|
|
Platform: core.PlatFormAndroid,
|
2017-10-24 09:00:08 +00:00
|
|
|
To: "/topics/foo-bar",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = CheckMessage(req)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// "condition": "'dogs' in topics || 'cats' in topics",
|
2021-08-02 06:07:30 +00:00
|
|
|
req = &PushNotification{
|
2017-10-24 09:00:08 +00:00
|
|
|
Message: "Test",
|
2021-07-13 08:32:39 +00:00
|
|
|
Platform: core.PlatFormAndroid,
|
2017-10-24 09:00:08 +00:00
|
|
|
Condition: "'dogs' in topics || 'cats' in topics",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = CheckMessage(req)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-07-25 13:05:27 +00:00
|
|
|
// the message may specify at most 1000 registration IDs
|
2021-08-02 06:07:30 +00:00
|
|
|
req = &PushNotification{
|
2017-07-25 13:05:27 +00:00
|
|
|
Message: "Test",
|
2021-07-13 08:32:39 +00:00
|
|
|
Platform: core.PlatFormAndroid,
|
2017-07-25 13:05:27 +00:00
|
|
|
Tokens: make([]string, 1001),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = CheckMessage(req)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// the message's TimeToLive field must be an integer
|
|
|
|
// between 0 and 2419200 (4 weeks)
|
|
|
|
timeToLive := uint(2419201)
|
2021-08-02 06:07:30 +00:00
|
|
|
req = &PushNotification{
|
2017-07-25 13:05:27 +00:00
|
|
|
Message: "Test",
|
2021-07-13 08:32:39 +00:00
|
|
|
Platform: core.PlatFormAndroid,
|
2017-07-25 13:05:27 +00:00
|
|
|
Tokens: []string{"XXXXXXXXX"},
|
|
|
|
TimeToLive: &timeToLive,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = CheckMessage(req)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// Pass
|
|
|
|
timeToLive = uint(86400)
|
2021-08-02 06:07:30 +00:00
|
|
|
req = &PushNotification{
|
2017-07-25 13:05:27 +00:00
|
|
|
Message: "Test",
|
2021-07-13 08:32:39 +00:00
|
|
|
Platform: core.PlatFormAndroid,
|
2017-07-25 13:05:27 +00:00
|
|
|
Tokens: []string{"XXXXXXXXX"},
|
|
|
|
TimeToLive: &timeToLive,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = CheckMessage(req)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckAndroidMessage(t *testing.T) {
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg, _ := config.LoadConf()
|
2017-07-25 13:05:27 +00:00
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
cfg.Android.Enabled = true
|
|
|
|
cfg.Android.APIKey = os.Getenv("ANDROID_API_KEY")
|
2017-07-25 13:05:27 +00:00
|
|
|
|
|
|
|
timeToLive := uint(2419201)
|
2021-08-02 06:07:30 +00:00
|
|
|
req := &PushNotification{
|
2017-07-25 13:05:27 +00:00
|
|
|
Tokens: []string{"aaaaaa", "bbbbb"},
|
2021-07-13 08:32:39 +00:00
|
|
|
Platform: core.PlatFormAndroid,
|
2017-07-25 13:05:27 +00:00
|
|
|
Message: "Welcome",
|
|
|
|
TimeToLive: &timeToLive,
|
|
|
|
}
|
|
|
|
|
2021-08-03 06:44:00 +00:00
|
|
|
// the message's TimeToLive field must be an integer between 0 and 2419200 (4 weeks)
|
|
|
|
resp, err := PushToAndroid(req, cfg)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Nil(t, resp)
|
2017-07-25 13:05:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAndroidNotificationStructure(t *testing.T) {
|
|
|
|
test := "test"
|
|
|
|
timeToLive := uint(100)
|
2021-08-02 06:07:30 +00:00
|
|
|
req := &PushNotification{
|
2017-07-25 13:05:27 +00:00
|
|
|
Tokens: []string{"a", "b"},
|
|
|
|
Message: "Welcome",
|
|
|
|
To: test,
|
2022-12-20 14:03:29 +00:00
|
|
|
Priority: HIGH,
|
2017-07-25 13:05:27 +00:00
|
|
|
CollapseKey: "1",
|
|
|
|
ContentAvailable: true,
|
|
|
|
DelayWhileIdle: true,
|
|
|
|
TimeToLive: &timeToLive,
|
|
|
|
RestrictedPackageName: test,
|
|
|
|
DryRun: true,
|
|
|
|
Title: test,
|
|
|
|
Sound: test,
|
|
|
|
Data: D{
|
|
|
|
"a": "1",
|
|
|
|
"b": 2,
|
|
|
|
},
|
2020-01-20 15:14:07 +00:00
|
|
|
Notification: &fcm.Notification{
|
2017-07-25 13:05:27 +00:00
|
|
|
Color: test,
|
|
|
|
Tag: test,
|
2020-01-20 15:14:07 +00:00
|
|
|
Body: "",
|
2017-07-25 13:05:27 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
notification := GetAndroidNotification(req)
|
|
|
|
|
|
|
|
assert.Equal(t, test, notification.To)
|
2022-12-20 14:03:29 +00:00
|
|
|
assert.Equal(t, HIGH, notification.Priority)
|
2017-07-25 13:05:27 +00:00
|
|
|
assert.Equal(t, "1", notification.CollapseKey)
|
|
|
|
assert.True(t, notification.ContentAvailable)
|
|
|
|
assert.True(t, notification.DelayWhileIdle)
|
|
|
|
assert.Equal(t, uint(100), *notification.TimeToLive)
|
|
|
|
assert.Equal(t, test, notification.RestrictedPackageName)
|
|
|
|
assert.True(t, notification.DryRun)
|
|
|
|
assert.Equal(t, test, notification.Notification.Title)
|
|
|
|
assert.Equal(t, test, notification.Notification.Sound)
|
|
|
|
assert.Equal(t, test, notification.Notification.Color)
|
|
|
|
assert.Equal(t, test, notification.Notification.Tag)
|
|
|
|
assert.Equal(t, "Welcome", notification.Notification.Body)
|
|
|
|
assert.Equal(t, "1", notification.Data["a"])
|
|
|
|
assert.Equal(t, 2, notification.Data["b"])
|
|
|
|
|
|
|
|
// test empty body
|
2021-08-02 06:07:30 +00:00
|
|
|
req = &PushNotification{
|
2017-07-25 13:05:27 +00:00
|
|
|
Tokens: []string{"a", "b"},
|
|
|
|
To: test,
|
2020-01-20 15:14:07 +00:00
|
|
|
Notification: &fcm.Notification{
|
|
|
|
Body: "",
|
|
|
|
},
|
2017-07-25 13:05:27 +00:00
|
|
|
}
|
|
|
|
notification = GetAndroidNotification(req)
|
|
|
|
|
|
|
|
assert.Equal(t, test, notification.To)
|
|
|
|
assert.Equal(t, "", notification.Notification.Body)
|
|
|
|
}
|