Skip to content

Commit 6996370

Browse files
authored
Merge pull request #19 from chemidy/dev
Dev : add tests for fcm
2 parents 6b166b5 + 8e58396 commit 6996370

File tree

3 files changed

+130
-6
lines changed

3 files changed

+130
-6
lines changed

app_test.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@ package admin_test
33
import (
44
"context"
55
"io/ioutil"
6+
"testing"
67

78
admin "github.com/acoshift/go-firebase-admin"
9+
"github.com/stretchr/testify/assert"
810
"gopkg.in/yaml.v2"
911
)
1012

1113
type config struct {
12-
ProjectID string `yaml:"projectId"`
13-
ServiceAccount []byte `yaml:"serviceAccount"`
14-
DatabaseURL string `yaml:"databaseURL"`
15-
APIKey string `yaml:"apiKey"`
14+
ProjectID string `yaml:"projectId"`
15+
ServiceAccount []byte `yaml:"serviceAccount"`
16+
DatabaseURL string `yaml:"databaseURL"`
17+
DatabaseAuthVariableOverride interface{} `yaml:"DatabaseAuthVariableOverride"`
18+
APIKey string `yaml:"apiKey"`
1619
}
1720

1821
func initApp() *admin.App {
@@ -21,6 +24,39 @@ func initApp() *admin.App {
2124
var c config
2225
yaml.Unmarshal(bs, &c)
2326

27+
// if service account is in separate file service_account.json
28+
if len(c.ServiceAccount) <= 0 {
29+
serviceAccount, _ := ioutil.ReadFile("private/service_account.json")
30+
c.ServiceAccount = serviceAccount
31+
}
32+
2433
app, _ := admin.InitializeApp(context.Background(), admin.AppOptions(c))
2534
return app
2635
}
36+
37+
func TestAuth(t *testing.T) {
38+
39+
app := initApp()
40+
firAuth := app.Auth()
41+
42+
assert.NotNil(t, app)
43+
assert.NotNil(t, firAuth)
44+
}
45+
46+
func TestDatabase(t *testing.T) {
47+
48+
app := initApp()
49+
firDatabase := app.Database()
50+
51+
assert.NotNil(t, app)
52+
assert.NotNil(t, firDatabase)
53+
}
54+
55+
func TestFCM(t *testing.T) {
56+
57+
app := initApp()
58+
firFCM := app.FCM()
59+
60+
assert.NotNil(t, app)
61+
assert.NotNil(t, firFCM)
62+
}

fcm.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type FCM struct {
1717
client *http.Client
1818
}
1919

20-
const (
20+
var (
2121
fcmSendEndpoint = "https://fcm.googleapis.com/fcm/send"
2222
fcmTopicAddEndpoint = "https://iid.googleapis.com/iid/v1:batchAdd"
2323
fcmTopicRemoveEndpoint = "https://iid.googleapis.com/iid/v1:batchRemove"
@@ -30,6 +30,21 @@ func newFCM(app *App) *FCM {
3030
}
3131
}
3232

33+
// NewFcmSendEndpoint set fcmSendEndpoint URL
34+
func (fcm *FCM) NewFcmSendEndpoint(endpoint string) {
35+
fcmSendEndpoint = endpoint
36+
}
37+
38+
// NewFcmTopicAddEndpoint set fcmTopicAddEndpoint URL
39+
func (fcm *FCM) NewFcmTopicAddEndpoint(endpoint string) {
40+
fcmTopicAddEndpoint = endpoint
41+
}
42+
43+
// NewFcmTopicRemoveEndpoint set fcmTopicRemoveEndpoint URL
44+
func (fcm *FCM) NewFcmTopicRemoveEndpoint(endpoint string) {
45+
fcmTopicRemoveEndpoint = endpoint
46+
}
47+
3348
// SendToDevice Send Message to individual device
3449
// see https://firebase.google.com/docs/cloud-messaging/admin/send-messages#send_to_individual_devices
3550
func (fcm *FCM) SendToDevice(ctx context.Context, registrationToken string, payload Message) (*Response, error) {
@@ -235,5 +250,5 @@ func normalizeTopicName(topic string) (result string) {
235250
if strings.HasPrefix(topic, "/topics/") {
236251
return topic
237252
}
238-
return fmt.Sprint("/topic/", topic)
253+
return fmt.Sprint("/topics/", topic)
239254
}

fcm_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package admin_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"net/http/httptest"
8+
"testing"
9+
10+
admin "github.com/acoshift/go-firebase-admin"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestSendToDevices(t *testing.T) {
15+
16+
t.Run("send=success", func(t *testing.T) {
17+
// generate Response
18+
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
19+
20+
rw.WriteHeader(http.StatusOK)
21+
rw.Header().Set("Content-Type", "application/json")
22+
fmt.Fprint(rw, `{"success": 1,"failure": 0,"results": [{"message_id":"118218","registration_id": "abcd118218","error": ""}]}`)
23+
}))
24+
defer srv.Close()
25+
26+
app := initApp()
27+
firFCM := app.FCM()
28+
firFCM.NewFcmSendEndpoint(srv.URL)
29+
30+
assert.NotNil(t, app)
31+
assert.NotNil(t, firFCM)
32+
33+
response, err := firFCM.SendToDevice(context.Background(), "mydevicetoken",
34+
admin.Message{Notification: admin.Notification{
35+
Title: "Hello go firebase admin",
36+
Body: "My little Big Notification",
37+
Color: "#ffcc33"},
38+
})
39+
40+
assert.Nil(t, err)
41+
assert.NotNil(t, response)
42+
assert.Equal(t, 1, response.Success)
43+
assert.Equal(t, 0, response.Failure)
44+
})
45+
46+
t.Run("send=failure", func(t *testing.T) {
47+
// generate Response
48+
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
49+
50+
rw.WriteHeader(http.StatusBadRequest)
51+
}))
52+
defer srv.Close()
53+
54+
app := initApp()
55+
firFCM := app.FCM()
56+
firFCM.NewFcmSendEndpoint(srv.URL)
57+
58+
assert.NotNil(t, app)
59+
assert.NotNil(t, firFCM)
60+
61+
response, err := firFCM.SendToDevice(context.Background(), "mydevicetoken",
62+
admin.Message{Notification: admin.Notification{
63+
Title: "Hello go firebase admin",
64+
Body: "My little Big Notification",
65+
Color: "#ffcc33"},
66+
})
67+
68+
assert.NotNil(t, err)
69+
assert.Nil(t, response)
70+
71+
})
72+
73+
}

0 commit comments

Comments
 (0)