Skip to content

Commit

Permalink
api: Add routes registerFcmToken, registerApnsToken
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gnprice authored and chrisbobbe committed Oct 25, 2023
1 parent 2a7d145 commit b8290cd
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/api/route/notifications.dart
Original file line number Diff line number Diff line change
@@ -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<void> 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<void> 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),
});
}
59 changes: 59 additions & 0 deletions test/api/route/notifications_test.dart
Original file line number Diff line number Diff line change
@@ -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<void> checkRegisterFcmToken(FakeApiConnection connection, {
required String token,
}) async {
connection.prepare(json: {});
await registerFcmToken(connection, token: token);
check(connection.lastRequest).isA<http.Request>()
..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<void> checkRegisterApnsToken(FakeApiConnection connection, {
required String token,
required String? appid,
}) async {
connection.prepare(json: {});
await registerApnsToken(connection, token: token, appid: appid);
check(connection.lastRequest).isA<http.Request>()
..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');
});
});
});
}

0 comments on commit b8290cd

Please sign in to comment.