Skip to content

Commit

Permalink
refactor: update FCM environment variables in Makefile and tests (#769)
Browse files Browse the repository at this point in the history
* refactor: update FCM environment variables in Makefile and tests

- Replace `ANDROID_API_KEY` with `FCM_CREDENTIAL` in Makefile
- Replace `ANDROID_TEST_TOKEN` with `FCM_TEST_TOKEN` in Makefile
- Update environment variable `ANDROID_API_KEY` to `FCM_CREDENTIAL` in notification FCM tests
- Update environment variable `ANDROID_TEST_TOKEN` to `FCM_TEST_TOKEN` in notification FCM tests
- Update environment variable `ANDROID_TEST_TOKEN` to `FCM_TEST_TOKEN` in server tests

Signed-off-by: Bo-Yi Wu <[email protected]>

* refactor: refactor FCM integration and update related tests

- Replace Android API key and test token with FCM credentials in GitHub Actions workflow
- Refactor platform case handling in `CheckMessage` function
- Remove test cases for empty token and excessive tokens in FCM message tests
- Rename `To` field to `Topic` in FCM message tests

Signed-off-by: Bo-Yi Wu <[email protected]>

* test: refactor notification tests and API integration

- Disable Android configuration in Huawei notification tests

Signed-off-by: Bo-Yi Wu <[email protected]>

* test: refactor notification system for improved reliability

- Remove the `TestCheckAndroidMessage` function from the FCM notification tests

Signed-off-by: Bo-Yi Wu <[email protected]>

* test: refactor and optimize device group notification tests

- Change assertion in `TestSyncModeForDeviceGroupNotification` to expect 0 logs instead of 1

Signed-off-by: Bo-Yi Wu <[email protected]>

* test: improve test coverage and API integration

- Change assertion to check for zero logs instead of one in `TestSyncModeForTopicNotification`

Signed-off-by: Bo-Yi Wu <[email protected]>

---------

Signed-off-by: Bo-Yi Wu <[email protected]>
  • Loading branch information
appleboy authored Jun 8, 2024
1 parent a20e5be commit d1603ec
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 60 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ jobs:

- name: testing
env:
ANDROID_API_KEY: ${{ secrets.ANDROID_API_KEY }}
ANDROID_TEST_TOKEN: ${{ secrets.ANDROID_TEST_TOKEN }}
FCM_CREDENTIAL: ${{ secrets.FCM_CREDENTIAL }}
FCM_TEST_TOKEN: ${{ secrets.FCM_TEST_TOKEN }}
run: make test

- name: Upload coverage to Codecov
Expand Down
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ COMMIT ?= $(shell git rev-parse --short HEAD)
all: build

init:
ifeq ($(ANDROID_API_KEY),)
@echo "Missing ANDROID_API_KEY Parameter"
ifeq ($(FCM_CREDENTIAL),)
@echo "Missing FCM_CREDENTIAL Parameter"
@exit 1
endif
ifeq ($(ANDROID_TEST_TOKEN),)
@echo "Missing ANDROID_TEST_TOKEN Parameter"
ifeq ($(FCM_TEST_TOKEN),)
@echo "Missing FCM_TEST_TOKEN Parameter"
@exit 1
endif
@echo "Already set ANDROID_API_KEY and ANDROID_TEST_TOKEN globale variable."
@echo "Already set FCM_CREDENTIAL and endif global variable."

vet:
$(GO) vet ./...
Expand Down
7 changes: 4 additions & 3 deletions notify/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,15 @@ func CheckMessage(req *PushNotification) error {
logx.LogAccess.Debug(msg)
return errors.New(msg)
}
case core.PlatFormAndroid:
case core.PlatFormHuawei:
default:
case
core.PlatFormAndroid,
core.PlatFormHuawei:
if len(req.Tokens) > 500 {
msg = "tokens must not contain more than 500 elements"
logx.LogAccess.Debug(msg)
return errors.New(msg)
}
default:
}

return nil
Expand Down
50 changes: 7 additions & 43 deletions notify/notification_fcm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestPushToAndroidWrongToken(t *testing.T) {
cfg, _ := config.LoadConf()

cfg.Android.Enabled = true
cfg.Android.Credential = os.Getenv("ANDROID_API_KEY")
cfg.Android.Credential = os.Getenv("FCM_CREDENTIAL")

req := &PushNotification{
Tokens: []string{"aaaaaa", "bbbbb"},
Expand All @@ -56,11 +56,11 @@ func TestPushToAndroidRightTokenForJSONLog(t *testing.T) {
cfg, _ := config.LoadConf()

cfg.Android.Enabled = true
cfg.Android.Credential = os.Getenv("ANDROID_API_KEY")
cfg.Android.Credential = os.Getenv("FCM_CREDENTIAL")
// log for json
cfg.Log.Format = "json"

androidToken := os.Getenv("ANDROID_TEST_TOKEN")
androidToken := os.Getenv("FCM_TEST_TOKEN")

req := &PushNotification{
Tokens: []string{androidToken},
Expand All @@ -77,9 +77,9 @@ func TestPushToAndroidRightTokenForStringLog(t *testing.T) {
cfg, _ := config.LoadConf()

cfg.Android.Enabled = true
cfg.Android.Credential = os.Getenv("ANDROID_API_KEY")
cfg.Android.Credential = os.Getenv("FCM_CREDENTIAL")

androidToken := os.Getenv("ANDROID_TEST_TOKEN")
androidToken := os.Getenv("FCM_TEST_TOKEN")

req := &PushNotification{
Tokens: []string{androidToken},
Expand All @@ -104,20 +104,11 @@ func TestFCMMessage(t *testing.T) {
err = CheckMessage(req)
assert.Error(t, err)

// the token must not be empty
req = &PushNotification{
Message: "Test",
Tokens: []string{""},
}

err = CheckMessage(req)
assert.Error(t, err)

// ignore check token length if send topic message
req = &PushNotification{
Message: "Test",
Platform: core.PlatFormAndroid,
To: "/topics/foo-bar",
Topic: "/topics/foo-bar",
}

err = CheckMessage(req)
Expand All @@ -137,16 +128,7 @@ func TestFCMMessage(t *testing.T) {
req = &PushNotification{
Message: "Test",
Platform: core.PlatFormAndroid,
Tokens: make([]string, 1001),
}

err = CheckMessage(req)
assert.Error(t, err)

req = &PushNotification{
Message: "Test",
Platform: core.PlatFormAndroid,
Tokens: []string{"XXXXXXXXX"},
Tokens: make([]string, 501),
}

err = CheckMessage(req)
Expand All @@ -163,24 +145,6 @@ func TestFCMMessage(t *testing.T) {
assert.NoError(t, err)
}

func TestCheckAndroidMessage(t *testing.T) {
cfg, _ := config.LoadConf()

cfg.Android.Enabled = true
cfg.Android.Credential = os.Getenv("ANDROID_API_KEY")

req := &PushNotification{
Tokens: []string{"aaaaaa", "bbbbb"},
Platform: core.PlatFormAndroid,
Message: "Welcome",
}

// 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)
}

func TestAndroidNotificationStructure(t *testing.T) {
test := "test"
req := &PushNotification{
Expand Down
2 changes: 2 additions & 0 deletions notify/notification_hms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
func TestMissingHuaweiAppSecret(t *testing.T) {
cfg, _ := config.LoadConf()

cfg.Android.Enabled = false
cfg.Huawei.Enabled = true
cfg.Huawei.AppSecret = ""

Expand All @@ -23,6 +24,7 @@ func TestMissingHuaweiAppSecret(t *testing.T) {
func TestMissingHuaweiAppID(t *testing.T) {
cfg, _ := config.LoadConf()

cfg.Android.Enabled = false
cfg.Huawei.Enabled = true
cfg.Huawei.AppID = ""

Expand Down
15 changes: 8 additions & 7 deletions router/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func TestSuccessPushHandler(t *testing.T) {
cfg.Android.Enabled = true
cfg.Android.Credential = os.Getenv("FCM_CREDENTIAL")

androidToken := os.Getenv("ANDROID_TEST_TOKEN")
androidToken := os.Getenv("FCM_TEST_TOKEN")

r := gofight.New()

Expand Down Expand Up @@ -480,7 +480,7 @@ func TestSenMultipleNotifications(t *testing.T) {
cfg.Android.Enabled = true
cfg.Android.Credential = os.Getenv("FCM_CREDENTIAL")

androidToken := os.Getenv("ANDROID_TEST_TOKEN")
androidToken := os.Getenv("FCM_TEST_TOKEN")

req := notify.RequestPush{
Notifications: []notify.PushNotification{
Expand Down Expand Up @@ -516,7 +516,7 @@ func TestDisabledAndroidNotifications(t *testing.T) {
cfg.Android.Enabled = false
cfg.Android.Credential = os.Getenv("FCM_CREDENTIAL")

androidToken := os.Getenv("ANDROID_TEST_TOKEN")
androidToken := os.Getenv("FCM_TEST_TOKEN")

req := notify.RequestPush{
Notifications: []notify.PushNotification{
Expand Down Expand Up @@ -555,7 +555,7 @@ func TestSyncModeForNotifications(t *testing.T) {
// enable sync mode
cfg.Core.Sync = true

androidToken := os.Getenv("ANDROID_TEST_TOKEN")
androidToken := os.Getenv("FCM_TEST_TOKEN")

req := notify.RequestPush{
Notifications: []notify.PushNotification{
Expand Down Expand Up @@ -621,7 +621,7 @@ func TestSyncModeForTopicNotification(t *testing.T) {

count, logs := handleNotification(ctx, cfg, req, q)
assert.Equal(t, 2, count)
assert.Equal(t, 1, len(logs))
assert.Equal(t, 0, len(logs))
}

func TestSyncModeForDeviceGroupNotification(t *testing.T) {
Expand All @@ -646,9 +646,10 @@ func TestSyncModeForDeviceGroupNotification(t *testing.T) {
},
}

// success
count, logs := handleNotification(ctx, cfg, req, q)
assert.Equal(t, 1, count)
assert.Equal(t, 1, len(logs))
assert.Equal(t, 0, len(logs))
}

func TestDisabledIosNotifications(t *testing.T) {
Expand All @@ -663,7 +664,7 @@ func TestDisabledIosNotifications(t *testing.T) {
cfg.Android.Enabled = true
cfg.Android.Credential = os.Getenv("FCM_CREDENTIAL")

androidToken := os.Getenv("ANDROID_TEST_TOKEN")
androidToken := os.Getenv("FCM_TEST_TOKEN")

req := notify.RequestPush{
Notifications: []notify.PushNotification{
Expand Down

0 comments on commit d1603ec

Please sign in to comment.