Ejemplo n.º 1
0
func TestConnector_GetErrorMessageFromFCM(t *testing.T) {
	_, finish := testutil.NewMockCtrl(t)
	defer finish()

	a := assert.New(t)
	fcm, mocks := testFCM(t, true)

	err := fcm.Start()
	a.NoError(err)

	var route *router.Route

	mocks.router.EXPECT().Subscribe(gomock.Any()).Do(func(r *router.Route) (*router.Route, error) {
		a.Equal("/topic", string(r.Path))
		a.Equal("user01", r.Get("user_id"))
		a.Equal("device01", r.Get(deviceTokenKey))
		route = r
		return r, nil
	})

	// put a dummy FCM message with minimum information
	postSubscription(t, fcm, "user01", "device01", "topic")
	time.Sleep(100 * time.Millisecond)
	a.NoError(err)
	a.NotNil(route)

	// expect the route unsubscribed
	mocks.router.EXPECT().Unsubscribe(gomock.Any()).Do(func(route *router.Route) {
		a.Equal("/topic", string(route.Path))
		a.Equal("device01", route.Get(deviceTokenKey))
	})

	// expect the route subscribe with the new canonicalID from replaceSubscriptionWithCanonicalID
	mocks.router.EXPECT().Subscribe(gomock.Any()).Do(func(route *router.Route) {
		a.Equal("/topic", string(route.Path))
		a.Equal("user01", route.Get("user_id"))
		appid := route.Get(deviceTokenKey)
		a.Equal("fcmCanonicalID", appid)
	})
	// mocks.store.EXPECT().MaxMessageID(gomock.Any()).Return(uint64(4), nil)

	response := new(gcm.Response)
	err = json.Unmarshal([]byte(ErrorFCMResponse), response)
	a.NoError(err)
	mocks.gcmSender.EXPECT().Send(gomock.Any()).Return(response, nil)

	// send the message into the subscription route channel
	route.Deliver(&protocol.Message{
		ID:   uint64(4),
		Path: "/topic",
		Body: []byte("{id:id}"),
	})

	// wait before closing the FCM connector
	time.Sleep(100 * time.Millisecond)

	err = fcm.Stop()
	a.NoError(err)
}
Ejemplo n.º 2
0
func TestFCMFormatMessage(t *testing.T) {
	_, finish := testutil.NewMockCtrl(t)
	defer finish()

	a := assert.New(t)

	var subRoute *router.Route

	fcm, mocks := testFCM(t, false)
	fcm.Start()
	defer fcm.Stop()
	time.Sleep(50 * time.Millisecond)

	mocks.router.EXPECT().Subscribe(gomock.Any()).Do(func(route *router.Route) (*router.Route, error) {
		subRoute = route
		return route, nil
	})

	postSubscription(t, fcm, "user01", "device01", "topic")
	time.Sleep(100 * time.Millisecond)

	// send a fully formated GCM message
	m := &protocol.Message{
		Path: "/topic",
		ID:   1,
		Body: []byte(fullFCMMessage),
	}

	if !a.NotNil(subRoute) {
		return
	}

	doneC := make(chan bool)

	mocks.gcmSender.EXPECT().Send(gomock.Any()).Do(func(m *gcm.Message) (*gcm.Response, error) {
		a.NotNil(m.Notification)
		a.Equal("TEST", m.Notification.Title)
		a.Equal("notification body", m.Notification.Body)
		a.Equal("ic_notification_test_icon", m.Notification.Icon)
		a.Equal("estimated_arrival", m.Notification.ClickAction)

		a.NotNil(m.Data)
		if a.Contains(m.Data, "field1") {
			a.Equal("value1", m.Data["field1"])
		}
		if a.Contains(m.Data, "field2") {
			a.Equal("value2", m.Data["field2"])
		}

		doneC <- true
		return nil, nil
	}).Return(&gcm.Response{}, nil)

	subRoute.Deliver(m)
	select {
	case <-doneC:
	case <-time.After(100 * time.Millisecond):
		a.Fail("Message not received by FCM")
	}

	m = &protocol.Message{
		Path: "/topic",
		ID:   1,
		Body: []byte(`plain body`),
	}

	mocks.gcmSender.EXPECT().Send(gomock.Any()).Do(func(m *gcm.Message) (*gcm.Response, error) {
		a.Nil(m.Notification)

		a.NotNil(m.Data)
		a.Contains(m.Data, "message")

		doneC <- true
		return nil, nil
	}).Return(&gcm.Response{}, nil)

	subRoute.Deliver(m)
	select {
	case <-doneC:
	case <-time.After(100 * time.Millisecond):
		a.Fail("Message not received by FCM")
	}
}