Skip to content

Commit b8290cd

Browse files
gnpricechrisbobbe
authored andcommitted
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.
1 parent 2a7d145 commit b8290cd

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

lib/api/route/notifications.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
import '../core.dart';
3+
4+
// This endpoint is undocumented. Compare zulip-mobile:
5+
// https://github.com/zulip/zulip-mobile/blob/86d94fa89/src/api/notifications/savePushToken.js
6+
// and see the server implementation:
7+
// https://github.com/zulip/zulip/blob/34ceafadd/zproject/urls.py#L383
8+
// https://github.com/zulip/zulip/blob/34ceafadd/zerver/views/push_notifications.py#L47
9+
Future<void> registerFcmToken(ApiConnection connection, {
10+
required String token,
11+
}) {
12+
return connection.post('registerFcmToken', (_) {}, 'users/me/android_gcm_reg_id', {
13+
'token': RawParameter(token),
14+
});
15+
}
16+
17+
// This endpoint is undocumented. Compare zulip-mobile:
18+
// https://github.com/zulip/zulip-mobile/blob/86d94fa89/src/api/notifications/savePushToken.js
19+
// and see the server implementation:
20+
// https://github.com/zulip/zulip/blob/34ceafadd/zproject/urls.py#L378-L381
21+
// https://github.com/zulip/zulip/blob/34ceafadd/zerver/views/push_notifications.py#L34
22+
Future<void> registerApnsToken(ApiConnection connection, {
23+
required String token,
24+
String? appid,
25+
}) {
26+
return connection.post('registerApnsToken', (_) {}, 'users/me/apns_device_token', {
27+
'token': RawParameter(token),
28+
if (appid != null) 'appid': RawParameter(appid),
29+
});
30+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import 'package:checks/checks.dart';
2+
import 'package:http/http.dart' as http;
3+
import 'package:test/scaffolding.dart';
4+
import 'package:zulip/api/route/notifications.dart';
5+
6+
import '../../stdlib_checks.dart';
7+
import '../fake_api.dart';
8+
9+
void main() {
10+
group('registerFcmToken', () {
11+
Future<void> checkRegisterFcmToken(FakeApiConnection connection, {
12+
required String token,
13+
}) async {
14+
connection.prepare(json: {});
15+
await registerFcmToken(connection, token: token);
16+
check(connection.lastRequest).isA<http.Request>()
17+
..method.equals('POST')
18+
..url.path.equals('/api/v1/users/me/android_gcm_reg_id')
19+
..bodyFields.deepEquals({
20+
'token': token,
21+
});
22+
}
23+
24+
test('smoke', () {
25+
return FakeApiConnection.with_((connection) async {
26+
await checkRegisterFcmToken(connection, token: 'asdf');
27+
});
28+
});
29+
});
30+
31+
group('registerApnsToken', () {
32+
Future<void> checkRegisterApnsToken(FakeApiConnection connection, {
33+
required String token,
34+
required String? appid,
35+
}) async {
36+
connection.prepare(json: {});
37+
await registerApnsToken(connection, token: token, appid: appid);
38+
check(connection.lastRequest).isA<http.Request>()
39+
..method.equals('POST')
40+
..url.path.equals('/api/v1/users/me/apns_device_token')
41+
..bodyFields.deepEquals({
42+
'token': token,
43+
if (appid != null) 'appid': appid,
44+
});
45+
}
46+
47+
test('no appid', () {
48+
return FakeApiConnection.with_((connection) async {
49+
await checkRegisterApnsToken(connection, token: 'asdf', appid: null);
50+
});
51+
});
52+
53+
test('with appid', () {
54+
return FakeApiConnection.with_((connection) async {
55+
await checkRegisterApnsToken(connection, token: 'asdf', appid: 'qwer');
56+
});
57+
});
58+
});
59+
}

0 commit comments

Comments
 (0)