diff --git a/gorush/notification.go b/gorush/notification.go index 65e1040..7fa21c4 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -120,12 +120,12 @@ func pushNotification(notification RequestPushNotification) bool { if !PushConf.Ios.Enabled { return false } - go pushNotificationIos(notification) + go PushToIOS(notification) case PlatFormAndroid: if !PushConf.Android.Enabled { return false } - go pushNotificationAndroid(notification) + go PushToAndroid(notification) } return true @@ -222,7 +222,7 @@ func GetIOSNotification(req RequestPushNotification) *apns.Notification { return notification } -func pushNotificationIos(req RequestPushNotification) bool { +func PushToIOS(req RequestPushNotification) bool { notification := GetIOSNotification(req) @@ -299,7 +299,7 @@ func GetAndroidNotification(req RequestPushNotification) gcm.HttpMessage { return notification } -func pushNotificationAndroid(req RequestPushNotification) bool { +func PushToAndroid(req RequestPushNotification) bool { notification := GetAndroidNotification(req) diff --git a/gorush/notification_test.go b/gorush/notification_test.go index f2f854f..48e1846 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -7,6 +7,7 @@ import ( "github.com/stretchr/testify/assert" "log" "testing" + "os" ) func TestDisabledAndroidIosConf(t *testing.T) { @@ -203,3 +204,72 @@ func TestAndroidNotificationStructure(t *testing.T) { assert.Equal(t, test, notification.Notification.Title) assert.Equal(t, "Welcome", notification.Notification.Body) } + +func TestPushToIOS(t *testing.T) { + PushConf = BuildDefaultPushConf() + + PushConf.Ios.Enabled = true + PushConf.Ios.PemKeyPath = "../certificate/certificate-valid.pem" + InitAPNSClient() + + req := RequestPushNotification{ + Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"}, + Platform: 1, + Message: "Welcome", + } + + success := PushToIOS(req) + assert.False(t, success) +} + +func TestPushToAndroidWrongAPIKey(t *testing.T) { + PushConf = BuildDefaultPushConf() + + PushConf.Android.Enabled = true + PushConf.Android.ApiKey = os.Getenv("ANDROID_API_KEY") + "a" + + + req := RequestPushNotification{ + Tokens: []string{"aaaaaa", "bbbbb"}, + Platform: 2, + Message: "Welcome", + } + + success := PushToAndroid(req) + assert.False(t, success) +} + +func TestPushToAndroidWrongToken(t *testing.T) { + PushConf = BuildDefaultPushConf() + + PushConf.Android.Enabled = true + PushConf.Android.ApiKey = os.Getenv("ANDROID_API_KEY") + + + req := RequestPushNotification{ + Tokens: []string{"aaaaaa", "bbbbb"}, + Platform: 2, + Message: "Welcome", + } + + success := PushToAndroid(req) + assert.True(t, success) +} + +func TestPushToAndroidRightToken(t *testing.T) { + PushConf = BuildDefaultPushConf() + + PushConf.Android.Enabled = true + PushConf.Android.ApiKey = os.Getenv("ANDROID_API_KEY") + + android_token := os.Getenv("ANDROID_TEST_TOKEN") + + req := RequestPushNotification{ + Tokens: []string{android_token, "bbbbb"}, + Platform: 2, + Message: "Welcome", + } + + success := PushToAndroid(req) + assert.True(t, success) +}