|
| 1 | +import 'package:checks/checks.dart'; |
| 2 | +import 'package:flutter/foundation.dart'; |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | +import 'package:http/http.dart' as http; |
| 5 | +import 'package:zulip/api/exception.dart'; |
| 6 | +import 'package:zulip/model/actions.dart'; |
| 7 | +import 'package:zulip/model/store.dart'; |
| 8 | +import 'package:zulip/notifications/receive.dart'; |
| 9 | + |
| 10 | +import '../api/fake_api.dart'; |
| 11 | +import '../example_data.dart' as eg; |
| 12 | +import '../fake_async.dart'; |
| 13 | +import '../model/binding.dart'; |
| 14 | +import '../model/store_checks.dart'; |
| 15 | +import '../model/test_store.dart'; |
| 16 | +import '../stdlib_checks.dart'; |
| 17 | +import 'store_test.dart'; |
| 18 | + |
| 19 | +void main() { |
| 20 | + TestZulipBinding.ensureInitialized(); |
| 21 | + |
| 22 | + late PerAccountStore store; |
| 23 | + late FakeApiConnection connection; |
| 24 | + |
| 25 | + Future<void> prepare({String? ackedPushToken = '123'}) async { |
| 26 | + addTearDown(testBinding.reset); |
| 27 | + final selfAccount = eg.selfAccount.copyWith(ackedPushToken: Value(ackedPushToken)); |
| 28 | + await testBinding.globalStore.add(selfAccount, eg.initialSnapshot()); |
| 29 | + store = await testBinding.globalStore.perAccount(selfAccount.id); |
| 30 | + connection = store.connection as FakeApiConnection; |
| 31 | + } |
| 32 | + |
| 33 | + /// Creates and caches a new [FakeApiConnection] in [TestGlobalStore]. |
| 34 | + /// |
| 35 | + /// In live code, [unregisterToken] makes a new [ApiConnection] for the |
| 36 | + /// unregister-token request instead of reusing the store's connection. |
| 37 | + /// To enable callers to prepare responses for that request, this function |
| 38 | + /// creates a new [FakeApiConnection] and caches it in [TestGlobalStore] |
| 39 | + /// for [unregisterToken] to pick up. |
| 40 | + /// |
| 41 | + /// Call this instead of just turning on |
| 42 | + /// [TestGlobalStore.useCachedApiConnections] so that [unregisterToken] |
| 43 | + /// doesn't try to call `close` twice on the same connection instance, |
| 44 | + /// which isn't allowed. (Once by the unregister-token code |
| 45 | + /// and once as part of removing the account.) |
| 46 | + FakeApiConnection separateConnection() { |
| 47 | + testBinding.globalStore |
| 48 | + ..clearCachedApiConnections() |
| 49 | + ..useCachedApiConnections = true; |
| 50 | + return testBinding.globalStore |
| 51 | + .apiConnectionFromAccount(eg.selfAccount) as FakeApiConnection; |
| 52 | + } |
| 53 | + |
| 54 | + String unregisterApiPathForPlatform(TargetPlatform platform) { |
| 55 | + return switch (platform) { |
| 56 | + TargetPlatform.android => '/api/v1/users/me/android_gcm_reg_id', |
| 57 | + TargetPlatform.iOS => '/api/v1/users/me/apns_device_token', |
| 58 | + _ => throw Error(), |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + void checkSingleUnregisterRequest( |
| 63 | + FakeApiConnection connection, { |
| 64 | + String? expectedToken, |
| 65 | + }) { |
| 66 | + final subject = check(connection.takeRequests()).single.isA<http.Request>() |
| 67 | + ..method.equals('DELETE') |
| 68 | + ..url.path.equals(unregisterApiPathForPlatform(defaultTargetPlatform)); |
| 69 | + if (expectedToken != null) { |
| 70 | + subject.bodyFields.deepEquals({'token': expectedToken}); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + group('logOutAccount', () { |
| 75 | + test('smoke', () => awaitFakeAsync((async) async { |
| 76 | + await prepare(); |
| 77 | + check(testBinding.globalStore).accountIds.single.equals(eg.selfAccount.id); |
| 78 | + const unregisterDelay = Duration(seconds: 5); |
| 79 | + assert(unregisterDelay > TestGlobalStore.removeAccountDuration); |
| 80 | + final newConnection = separateConnection() |
| 81 | + ..prepare(delay: unregisterDelay, json: {'msg': '', 'result': 'success'}); |
| 82 | + |
| 83 | + final future = logOutAccount(testBinding.globalStore, eg.selfAccount.id); |
| 84 | + // Unregister-token request and account removal dispatched together |
| 85 | + checkSingleUnregisterRequest(newConnection); |
| 86 | + check(testBinding.globalStore.takeDoRemoveAccountCalls()) |
| 87 | + .single.equals(eg.selfAccount.id); |
| 88 | + |
| 89 | + async.elapse(TestGlobalStore.removeAccountDuration); |
| 90 | + await future; |
| 91 | + // Account removal not blocked on unregister-token response |
| 92 | + check(testBinding.globalStore).accountIds.isEmpty(); |
| 93 | + check(connection.isOpen).isFalse(); |
| 94 | + check(newConnection.isOpen).isTrue(); // still busy with unregister-token |
| 95 | + |
| 96 | + async.elapse(unregisterDelay - TestGlobalStore.removeAccountDuration); |
| 97 | + check(newConnection.isOpen).isFalse(); |
| 98 | + })); |
| 99 | + |
| 100 | + test('unregister request has an error', () => awaitFakeAsync((async) async { |
| 101 | + await prepare(); |
| 102 | + check(testBinding.globalStore).accountIds.single.equals(eg.selfAccount.id); |
| 103 | + const unregisterDelay = Duration(seconds: 5); |
| 104 | + assert(unregisterDelay > TestGlobalStore.removeAccountDuration); |
| 105 | + final exception = ZulipApiException( |
| 106 | + httpStatus: 401, |
| 107 | + code: 'UNAUTHORIZED', |
| 108 | + data: {"result": "error", "msg": "Invalid API key", "code": "UNAUTHORIZED"}, |
| 109 | + routeName: 'removeEtcEtcToken', |
| 110 | + message: 'Invalid API key', |
| 111 | + ); |
| 112 | + final newConnection = separateConnection() |
| 113 | + ..prepare(delay: unregisterDelay, exception: exception); |
| 114 | + |
| 115 | + final future = logOutAccount(testBinding.globalStore, eg.selfAccount.id); |
| 116 | + // Unregister-token request and account removal dispatched together |
| 117 | + checkSingleUnregisterRequest(newConnection); |
| 118 | + check(testBinding.globalStore.takeDoRemoveAccountCalls()) |
| 119 | + .single.equals(eg.selfAccount.id); |
| 120 | + |
| 121 | + async.elapse(TestGlobalStore.removeAccountDuration); |
| 122 | + await future; |
| 123 | + // Account removal not blocked on unregister-token response |
| 124 | + check(testBinding.globalStore).accountIds.isEmpty(); |
| 125 | + check(connection.isOpen).isFalse(); |
| 126 | + check(newConnection.isOpen).isTrue(); // for the unregister-token request |
| 127 | + |
| 128 | + async.elapse(unregisterDelay - TestGlobalStore.removeAccountDuration); |
| 129 | + check(newConnection.isOpen).isFalse(); |
| 130 | + })); |
| 131 | + }); |
| 132 | + |
| 133 | + group('unregisterToken', () { |
| 134 | + testAndroidIos('smoke, happy path', () => awaitFakeAsync((async) async { |
| 135 | + await prepare(ackedPushToken: '123'); |
| 136 | + |
| 137 | + final newConnection = separateConnection() |
| 138 | + ..prepare(json: {'msg': '', 'result': 'success'}); |
| 139 | + final future = unregisterToken(testBinding.globalStore, eg.selfAccount.id); |
| 140 | + async.elapse(Duration.zero); |
| 141 | + await future; |
| 142 | + checkSingleUnregisterRequest(newConnection, expectedToken: '123'); |
| 143 | + check(newConnection.isOpen).isFalse(); |
| 144 | + })); |
| 145 | + |
| 146 | + test('fallback to current token if acked is missing', () => awaitFakeAsync((async) async { |
| 147 | + await prepare(ackedPushToken: null); |
| 148 | + NotificationService.instance.token = ValueNotifier('asdf'); |
| 149 | + |
| 150 | + final newConnection = separateConnection() |
| 151 | + ..prepare(json: {'msg': '', 'result': 'success'}); |
| 152 | + final future = unregisterToken(testBinding.globalStore, eg.selfAccount.id); |
| 153 | + async.elapse(Duration.zero); |
| 154 | + await future; |
| 155 | + checkSingleUnregisterRequest(newConnection, expectedToken: 'asdf'); |
| 156 | + check(newConnection.isOpen).isFalse(); |
| 157 | + })); |
| 158 | + |
| 159 | + test('no error if acked token and current token both missing', () => awaitFakeAsync((async) async { |
| 160 | + await prepare(ackedPushToken: null); |
| 161 | + NotificationService.instance.token = ValueNotifier(null); |
| 162 | + |
| 163 | + final newConnection = separateConnection(); |
| 164 | + final future = unregisterToken(testBinding.globalStore, eg.selfAccount.id); |
| 165 | + async.flushTimers(); |
| 166 | + await future; |
| 167 | + check(newConnection.takeRequests()).isEmpty(); |
| 168 | + })); |
| 169 | + |
| 170 | + test('connection closed if request errors', () => awaitFakeAsync((async) async { |
| 171 | + await prepare(ackedPushToken: '123'); |
| 172 | + |
| 173 | + final newConnection = separateConnection() |
| 174 | + ..prepare(exception: ZulipApiException( |
| 175 | + httpStatus: 401, |
| 176 | + code: 'UNAUTHORIZED', |
| 177 | + data: {"result": "error", "msg": "Invalid API key", "code": "UNAUTHORIZED"}, |
| 178 | + routeName: 'removeEtcEtcToken', |
| 179 | + message: 'Invalid API key', |
| 180 | + )); |
| 181 | + final future = unregisterToken(testBinding.globalStore, eg.selfAccount.id); |
| 182 | + async.elapse(Duration.zero); |
| 183 | + await future; |
| 184 | + checkSingleUnregisterRequest(newConnection, expectedToken: '123'); |
| 185 | + check(newConnection.isOpen).isFalse(); |
| 186 | + })); |
| 187 | + }); |
| 188 | +} |
0 commit comments