Skip to content

Commit 8e58396

Browse files
committed
update tests
1 parent 9679bc6 commit 8e58396

File tree

2 files changed

+85
-33
lines changed

2 files changed

+85
-33
lines changed

app_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ 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

@@ -31,3 +33,30 @@ func initApp() *admin.App {
3133
app, _ := admin.InitializeApp(context.Background(), admin.AppOptions(c))
3234
return app
3335
}
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_test.go

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,61 @@ import (
1313

1414
func TestSendToDevices(t *testing.T) {
1515

16-
// generate Response
17-
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
18-
19-
rw.WriteHeader(http.StatusOK)
20-
rw.Header().Set("Content-Type", "application/json")
21-
fmt.Fprint(rw, `{
22-
"success": 1,
23-
"failure": 0,
24-
"results": [{
25-
"message_id":"118218",
26-
"registration_id": "abcd118218",
27-
"error": ""
28-
}]
29-
}`)
30-
}))
31-
defer srv.Close()
32-
33-
app := initApp()
34-
firFCM := app.FCM()
35-
firFCM.NewFcmSendEndpoint(srv.URL)
36-
37-
assert.NotNil(t, firFCM)
38-
39-
response, err := firFCM.SendToDevice(context.Background(), "mydevicetoken",
40-
admin.Message{Notification: admin.Notification{
41-
Title: "Hello go firebase admin",
42-
Body: "My little Big Notification",
43-
Color: "#ffcc33"},
44-
})
45-
46-
assert.Nil(t, err, fmt.Sprintf("unexpected error: %v", err))
47-
assert.Equal(t, 1, response.Success, fmt.Sprintf("expected success: %v", err))
48-
assert.Equal(t, 0, response.Failure, fmt.Sprintf("Not expected failure: %v", err))
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+
})
4972

5073
}

0 commit comments

Comments
 (0)