From b8290cd115d5c7ccd4740f2df2dd8fd24db90e18 Mon Sep 17 00:00:00 2001 From: Greg Price Date: Mon, 16 Oct 2023 17:14:14 -0700 Subject: [PATCH] api: Add routes registerFcmToken, registerApnsToken This code is so trivial that I'm torn on whether the tests are actually useful; they feel a lot like just repeating the implementation. But they were easy to write. --- lib/api/route/notifications.dart | 30 +++++++++++++ test/api/route/notifications_test.dart | 59 ++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 lib/api/route/notifications.dart create mode 100644 test/api/route/notifications_test.dart diff --git a/lib/api/route/notifications.dart b/lib/api/route/notifications.dart new file mode 100644 index 0000000000..754de97178 --- /dev/null +++ b/lib/api/route/notifications.dart @@ -0,0 +1,30 @@ + +import '../core.dart'; + +// This endpoint is undocumented. Compare zulip-mobile: +// https://github.com/zulip/zulip-mobile/blob/86d94fa89/src/api/notifications/savePushToken.js +// and see the server implementation: +// https://github.com/zulip/zulip/blob/34ceafadd/zproject/urls.py#L383 +// https://github.com/zulip/zulip/blob/34ceafadd/zerver/views/push_notifications.py#L47 +Future registerFcmToken(ApiConnection connection, { + required String token, +}) { + return connection.post('registerFcmToken', (_) {}, 'users/me/android_gcm_reg_id', { + 'token': RawParameter(token), + }); +} + +// This endpoint is undocumented. Compare zulip-mobile: +// https://github.com/zulip/zulip-mobile/blob/86d94fa89/src/api/notifications/savePushToken.js +// and see the server implementation: +// https://github.com/zulip/zulip/blob/34ceafadd/zproject/urls.py#L378-L381 +// https://github.com/zulip/zulip/blob/34ceafadd/zerver/views/push_notifications.py#L34 +Future registerApnsToken(ApiConnection connection, { + required String token, + String? appid, +}) { + return connection.post('registerApnsToken', (_) {}, 'users/me/apns_device_token', { + 'token': RawParameter(token), + if (appid != null) 'appid': RawParameter(appid), + }); +} diff --git a/test/api/route/notifications_test.dart b/test/api/route/notifications_test.dart new file mode 100644 index 0000000000..7601bd5a9d --- /dev/null +++ b/test/api/route/notifications_test.dart @@ -0,0 +1,59 @@ +import 'package:checks/checks.dart'; +import 'package:http/http.dart' as http; +import 'package:test/scaffolding.dart'; +import 'package:zulip/api/route/notifications.dart'; + +import '../../stdlib_checks.dart'; +import '../fake_api.dart'; + +void main() { + group('registerFcmToken', () { + Future checkRegisterFcmToken(FakeApiConnection connection, { + required String token, + }) async { + connection.prepare(json: {}); + await registerFcmToken(connection, token: token); + check(connection.lastRequest).isA() + ..method.equals('POST') + ..url.path.equals('/api/v1/users/me/android_gcm_reg_id') + ..bodyFields.deepEquals({ + 'token': token, + }); + } + + test('smoke', () { + return FakeApiConnection.with_((connection) async { + await checkRegisterFcmToken(connection, token: 'asdf'); + }); + }); + }); + + group('registerApnsToken', () { + Future checkRegisterApnsToken(FakeApiConnection connection, { + required String token, + required String? appid, + }) async { + connection.prepare(json: {}); + await registerApnsToken(connection, token: token, appid: appid); + check(connection.lastRequest).isA() + ..method.equals('POST') + ..url.path.equals('/api/v1/users/me/apns_device_token') + ..bodyFields.deepEquals({ + 'token': token, + if (appid != null) 'appid': appid, + }); + } + + test('no appid', () { + return FakeApiConnection.with_((connection) async { + await checkRegisterApnsToken(connection, token: 'asdf', appid: null); + }); + }); + + test('with appid', () { + return FakeApiConnection.with_((connection) async { + await checkRegisterApnsToken(connection, token: 'asdf', appid: 'qwer'); + }); + }); + }); +}