forked from zulip/zulip-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); | ||
} |