mirror of
https://github.com/block/buzz.git
synced 2026-11-28 04:01:45 +00:00
Follow-ups from the review of #9811 (Android push enrollment): the review items that were safe to leave for after merge. - **A timed-out gateway call is now aborted.** The 15 s timeout used to abandon the POST while it kept running, so a retry could race the original request at the gateway. The gateway client now sends through `boundedHttpResponse`, which aborts the request at the deadline (and caps the reply size). - **An Android gateway reply the app can't read is reported to diagnostics,** as #9806 does for iOS. Malformed replies (no JSON object, missing fields, or an oversized reply with the expected status) throw `AndroidPushGatewayResponseInvalid`, which `enrollBuzzPush` reports under the push gateway operation. Refusals and transport failures stay unreported, and a refusal keeps its status even when its body is over the cap. - **The delegation sweep after enrollment keeps the enrolling relay while it is still wanted.** It now sweeps with the desired relays plus the enrolling origin, but only while `stillWanted()` holds, so an opt-out that landed during the delegation still sees that delegation revoked. - **Tests for two paths that had none:** an opt-out that lands while the gateway answers the delegation ends the attempt quietly (no lease, delegation revoked, no failure retry), and an opted-out iOS community enrolls nothing. Two other items from the review, both Codex findings on an earlier head of #9811 (reusing a revoked grant after a lost re-delegation; deleting a key after a lost renewal), were already fixed and tested before #9811 merged, so nothing changes here for them. ### Validation Added tests: - [the enrolling relay keeps its delegation before the caller lists it](157d6d4759/mobile/test/shared/push/android_push_authority_test.dart): the sweep keeps a still-wanted relay that the desired list doesn't show yet, and revokes a delegation whose community opted out while it was in flight. - [turning push off while it delegates ends the attempt quietly](157d6d4759/mobile/test/features/settings/android_notifications_test.dart): production bootstrap, opt-out while the delegation response is held. - [only a gateway reply the app cannot read is reported](157d6d4759/mobile/test/shared/push/push_bridge_android_test.dart) and [a gateway call that times out is aborted, not abandoned](157d6d4759/mobile/test/shared/push/push_bridge_android_test.dart). - [a community that opted out enrolls nothing](157d6d4759/mobile/test/shared/push/push_bridge_test.dart) (iOS). - [an unavailable gateway keeps the journal and a refusal clears it](157d6d4759/mobile/test/shared/push/android_push_authority_test.dart): now also with refusal bodies over the 64 KiB cap. Each test was checked to fail with its guard removed. No device or full-app run: every change is on the Android push gateway path (which needs a gateway-configured Android build) or is a test, and the behavior changed is timeout, malformed-reply and race handling that the tests above drive directly against the production authority and bootstrap. ### Regressions - iOS is unchanged apart from the new test; the abort and diagnostics changes are Android-only code. - `boundedHttpResponse` gains an optional `timeout` that defaults to its old 30 s, so existing callers behave as before. - Gateway replies are capped at 64 KiB. Gateway responses are small JSON objects, so an oversized reply with the expected status is itself a defect and is reported. An oversized refusal is handled as that refusal (a 503 stays retryable, a 403 discards the pending enrollment). --------- Signed-off-by: Tom Brow <tomb@block.xyz> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
531 lines
17 KiB
Dart
531 lines
17 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:buzz/shared/community/community.dart';
|
|
import 'package:buzz/shared/deeplink/deep_link.dart';
|
|
import 'package:buzz/shared/diagnostics/diagnostic_reporter.dart';
|
|
import 'package:buzz/shared/hosted/hosted_environment.dart';
|
|
import 'package:buzz/shared/hosted/hosted_relay_auth.dart';
|
|
import 'package:buzz/shared/push/push_bridge.dart';
|
|
import 'package:buzz/shared/relay/relay_provider.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
const _channel = MethodChannel('buzz/push');
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
setUp(() {
|
|
apnsDeviceToken.value = null;
|
|
apnsRegistrationError.value = null;
|
|
pushEndpointGrants.value = const [];
|
|
pushEndpointGrantError.value = null;
|
|
pushCommunitySnapshotError.value = null;
|
|
pendingPushNotificationLink.value = null;
|
|
installBuzzPushMethodHandler();
|
|
});
|
|
|
|
tearDown(() {
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(_channel, null);
|
|
debugDefaultTargetPlatformOverride = null;
|
|
});
|
|
|
|
test('captures APNs token success and clears the previous error', () async {
|
|
apnsRegistrationError.value = 'old error';
|
|
await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.handlePlatformMessage(
|
|
_channel.name,
|
|
_channel.codec.encodeMethodCall(
|
|
const MethodCall('apnsTokenChanged', {'token': '01ab'}),
|
|
),
|
|
(_) {},
|
|
);
|
|
expect(apnsDeviceToken.value, '01ab');
|
|
expect(apnsRegistrationError.value, isNull);
|
|
});
|
|
|
|
test(
|
|
'starts native permission and APNs registration without a result gate',
|
|
() async {
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(_channel, (call) async {
|
|
expect(call.method, 'startRegistration');
|
|
return null;
|
|
});
|
|
|
|
await startBuzzPushRegistration();
|
|
},
|
|
);
|
|
|
|
test('purges notifications delivered before age restriction', () async {
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(_channel, (call) async {
|
|
expect(call.method, 'purgeAgeRestrictedNotifications');
|
|
expect(call.arguments, isNull);
|
|
return null;
|
|
});
|
|
|
|
await purgeAgeRestrictedBuzzNotifications();
|
|
});
|
|
|
|
test('reads and exposes persisted endpoint grants on iOS', () async {
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
|
final messenger =
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
|
|
messenger.setMockMethodCallHandler(_channel, (call) async {
|
|
expect(call.method, 'endpointGrants');
|
|
expect(call.arguments, {'gatewayUrl': Env.pushGatewayUrl});
|
|
return [_grantMap('opaque-grant')];
|
|
});
|
|
|
|
final grants = await readBuzzPushEndpointGrants();
|
|
|
|
expect(grants, hasLength(1));
|
|
expect(grants.single.relayOrigin, 'wss://relay.example');
|
|
expect(grants.single.relayPubkey, 'a' * 64);
|
|
expect(grants.single.installationId, 'c' * 32);
|
|
expect(grants.single.endpointGrant, 'opaque-grant');
|
|
expect(grants.single.endpointHash, 'b' * 64);
|
|
expect(grants.single.endpointEpoch, 1);
|
|
expect(grants.single.generation, 1);
|
|
expect(grants.single.expiresAt, 1752624000);
|
|
expect(pushEndpointGrants.value.single.endpointGrant, 'opaque-grant');
|
|
expect(pushEndpointGrantError.value, isNull);
|
|
});
|
|
|
|
test(
|
|
'debug enrollment carries the configured relay and gateway URLs',
|
|
() async {
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
|
final methods = <String>[];
|
|
final enrollmentArguments = <Object?>[];
|
|
final snapshotArguments = <Object?>[];
|
|
final messenger =
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
|
|
messenger.setMockMethodCallHandler(_channel, (call) async {
|
|
methods.add(call.method);
|
|
if (call.method == 'enrollPush') {
|
|
enrollmentArguments.add(call.arguments);
|
|
return _grantMap('new-grant');
|
|
}
|
|
if (call.method == 'endpointGrants') {
|
|
return [_grantMap('new-grant')];
|
|
}
|
|
if (call.method == 'syncPushSnapshot') {
|
|
snapshotArguments.add(call.arguments);
|
|
return null;
|
|
}
|
|
fail('Unexpected method ${call.method}');
|
|
});
|
|
|
|
final firstGrant = await enrollBuzzPush(
|
|
'wss://relay.example/',
|
|
'https://gateway-one.example/',
|
|
);
|
|
final secondGrant = await enrollBuzzPush(
|
|
'wss://relay.example/',
|
|
'https://gateway-two.example/',
|
|
communitiesForSnapshotRefresh: [
|
|
Community(
|
|
id: 'community-id',
|
|
name: 'Community',
|
|
relayUrl: 'wss://relay.example/',
|
|
pubkey: 'd' * 64,
|
|
pushNotificationsEnabled: true,
|
|
addedAt: DateTime.fromMillisecondsSinceEpoch(0),
|
|
),
|
|
],
|
|
);
|
|
|
|
expect(firstGrant!.endpointGrant, 'new-grant');
|
|
expect(secondGrant!.endpointGrant, 'new-grant');
|
|
expect(enrollmentArguments, [
|
|
{
|
|
'relayUrl': 'wss://relay.example/',
|
|
'gatewayUrl': 'https://gateway-one.example/',
|
|
},
|
|
{
|
|
'relayUrl': 'wss://relay.example/',
|
|
'gatewayUrl': 'https://gateway-two.example/',
|
|
},
|
|
]);
|
|
expect(methods, [
|
|
'enrollPush',
|
|
'endpointGrants',
|
|
'enrollPush',
|
|
'endpointGrants',
|
|
'syncPushSnapshot',
|
|
]);
|
|
expect(snapshotArguments, [
|
|
{
|
|
'section': 'communities',
|
|
'communities': [
|
|
{
|
|
'id': 'community-id',
|
|
'name': 'Community',
|
|
'relayUrl': 'wss://relay.example/',
|
|
'pubkey': 'd' * 64,
|
|
'policies': <Object?>[],
|
|
},
|
|
],
|
|
'signingKeys': <String, String>{},
|
|
},
|
|
]);
|
|
expect(pushEndpointGrants.value.single.endpointGrant, 'new-grant');
|
|
},
|
|
);
|
|
|
|
test('a community that opted out enrolls nothing', () async {
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
|
final methods = <String>[];
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(_channel, (call) async {
|
|
methods.add(call.method);
|
|
return null;
|
|
});
|
|
|
|
final grant = await enrollBuzzPush(
|
|
'wss://relay.example/',
|
|
'https://gateway.example/',
|
|
stillWanted: () => false,
|
|
);
|
|
|
|
expect(grant, isNull);
|
|
expect(methods, isEmpty);
|
|
});
|
|
|
|
test('a gateway reply the app cannot read is reported and still fails '
|
|
'enrollment', () async {
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(_channel, (call) async {
|
|
throw PlatformException(code: 'gateway_response_invalid');
|
|
});
|
|
final reporter = _RecordingDiagnostics();
|
|
|
|
await expectLater(
|
|
enrollBuzzPush(
|
|
'wss://relay.example/',
|
|
'https://gateway.example/',
|
|
diagnostics: reporter,
|
|
),
|
|
throwsA(isA<PlatformException>()),
|
|
);
|
|
|
|
expect(reporter.operations, [DiagnosticOperation.pushGatewayResponse]);
|
|
});
|
|
|
|
test('routine enrollment failures are not reported', () async {
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
|
final reporter = _RecordingDiagnostics();
|
|
|
|
for (final code in [
|
|
'dev_enrollment_failed',
|
|
'missing_apns_token',
|
|
'enrollment_in_progress',
|
|
]) {
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(_channel, (call) async {
|
|
throw PlatformException(code: code);
|
|
});
|
|
await expectLater(
|
|
enrollBuzzPush(
|
|
'wss://relay.example/',
|
|
'https://gateway.example/',
|
|
diagnostics: reporter,
|
|
),
|
|
throwsA(isA<PlatformException>()),
|
|
);
|
|
}
|
|
|
|
expect(reporter.operations, isEmpty);
|
|
});
|
|
|
|
test('shares and clears the hosted session with the extension', () async {
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
|
final shared = <Object?>[];
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(_channel, (call) async {
|
|
expect(call.method, 'syncPushSnapshot');
|
|
shared.add(call.arguments);
|
|
return null;
|
|
});
|
|
|
|
for (final credential in ['session-credential', null]) {
|
|
await shareHostedSessionWithNotifications(
|
|
origin: Uri.parse('https://app.example'),
|
|
credential: credential,
|
|
relayDomains: const ['communities.example'],
|
|
communityDomain: 'communities.example',
|
|
);
|
|
}
|
|
|
|
expect(shared, [
|
|
for (final credential in ['session-credential', null])
|
|
{
|
|
'section': 'hostedSession',
|
|
'origin': 'https://app.example',
|
|
'credential': credential,
|
|
'relayDomains': ['communities.example'],
|
|
'communityDomain': 'communities.example',
|
|
},
|
|
]);
|
|
});
|
|
|
|
test(
|
|
'a build without hosted relay auth clears a shared hosted session',
|
|
() async {
|
|
expect(HostedRelayAuth.configured && HostedEnvironment.enabled, isFalse);
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
|
final shared = <Object?>[];
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(_channel, (call) async {
|
|
shared.add((call.arguments as Map)['credential']);
|
|
return null;
|
|
});
|
|
final container = ProviderContainer();
|
|
addTearDown(container.dispose);
|
|
|
|
// A copy an earlier flag-on build shared is removed instead of kept.
|
|
await container.read(builderlabAccountProvider).onSessionChanged!('live');
|
|
expect(shared, [null]);
|
|
},
|
|
);
|
|
|
|
test('exposes APNs registration failure', () async {
|
|
await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.handlePlatformMessage(
|
|
_channel.name,
|
|
_channel.codec.encodeMethodCall(
|
|
const MethodCall('apnsRegistrationFailed', {'message': 'denied'}),
|
|
),
|
|
(_) {},
|
|
);
|
|
expect(apnsRegistrationError.value, 'denied');
|
|
});
|
|
|
|
test('routes a warm notification response with opaque IDs', () async {
|
|
final response = Completer<ByteData?>();
|
|
await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.handlePlatformMessage(
|
|
_channel.name,
|
|
_channel.codec.encodeMethodCall(
|
|
MethodCall('notificationOpened', {
|
|
'eventId': 'MESSAGE-ID',
|
|
'communityId': 'community-id',
|
|
'channelId': 'CHANNEL/GENERAL',
|
|
}),
|
|
),
|
|
response.complete,
|
|
);
|
|
|
|
final envelope = await response.future;
|
|
expect(envelope, isNotNull);
|
|
expect(_channel.codec.decodeEnvelope(envelope!), 'handled');
|
|
expect(
|
|
pendingPushNotificationLink.value,
|
|
MessageDeepLink(
|
|
communityId: 'community-id',
|
|
channelId: 'CHANNEL/GENERAL',
|
|
messageId: 'MESSAGE-ID',
|
|
),
|
|
);
|
|
});
|
|
|
|
test(
|
|
'preserves the exact reply and containing thread on a warm tap',
|
|
() async {
|
|
final response = Completer<ByteData?>();
|
|
await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.handlePlatformMessage(
|
|
_channel.name,
|
|
_channel.codec.encodeMethodCall(
|
|
const MethodCall('notificationOpened', {
|
|
'eventId': 'older-reply',
|
|
'communityId': 'community-id',
|
|
'channelId': 'channel-id',
|
|
'threadRootId': 'direct-parent',
|
|
}),
|
|
),
|
|
response.complete,
|
|
);
|
|
expect(
|
|
_channel.codec.decodeEnvelope((await response.future)!),
|
|
'handled',
|
|
);
|
|
expect(
|
|
pendingPushNotificationLink.value,
|
|
const MessageDeepLink(
|
|
communityId: 'community-id',
|
|
channelId: 'channel-id',
|
|
messageId: 'older-reply',
|
|
threadRootId: 'direct-parent',
|
|
),
|
|
);
|
|
},
|
|
);
|
|
|
|
for (final invalidThread in ['', 42]) {
|
|
test('rejects malformed thread ID $invalidThread', () async {
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(
|
|
_channel,
|
|
(_) async => {
|
|
'eventId': 'reply',
|
|
'communityId': 'community',
|
|
'channelId': 'channel',
|
|
'threadRootId': invalidThread,
|
|
},
|
|
);
|
|
await syncPendingBuzzPushNotificationResponse();
|
|
expect(pendingPushNotificationLink.value, isNull);
|
|
});
|
|
}
|
|
|
|
test('rejects a notification response with an empty channel ID', () async {
|
|
final response = Completer<ByteData?>();
|
|
await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.handlePlatformMessage(
|
|
_channel.name,
|
|
_channel.codec.encodeMethodCall(
|
|
MethodCall('notificationOpened', {
|
|
'eventId': 'message-id',
|
|
'communityId': 'community-id',
|
|
'channelId': '',
|
|
}),
|
|
),
|
|
response.complete,
|
|
);
|
|
|
|
final envelope = await response.future;
|
|
expect(envelope, isNotNull);
|
|
expect(_channel.codec.decodeEnvelope(envelope!), 'ignored');
|
|
expect(pendingPushNotificationLink.value, isNull);
|
|
});
|
|
|
|
test(
|
|
'rejects a notification response with a non-string message ID',
|
|
() async {
|
|
final response = Completer<ByteData?>();
|
|
await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.handlePlatformMessage(
|
|
_channel.name,
|
|
_channel.codec.encodeMethodCall(
|
|
MethodCall('notificationOpened', {
|
|
'eventId': 42,
|
|
'communityId': 'community-id',
|
|
'channelId': 'channel-id',
|
|
}),
|
|
),
|
|
response.complete,
|
|
);
|
|
|
|
final envelope = await response.future;
|
|
expect(envelope, isNotNull);
|
|
expect(_channel.codec.decodeEnvelope(envelope!), 'ignored');
|
|
expect(pendingPushNotificationLink.value, isNull);
|
|
},
|
|
);
|
|
|
|
test('rejects a notification response with an absent message ID', () async {
|
|
final response = Completer<ByteData?>();
|
|
await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.handlePlatformMessage(
|
|
_channel.name,
|
|
_channel.codec.encodeMethodCall(
|
|
const MethodCall('notificationOpened', {
|
|
'communityId': 'community-id',
|
|
'channelId': 'channel-id',
|
|
}),
|
|
),
|
|
response.complete,
|
|
);
|
|
|
|
final envelope = await response.future;
|
|
expect(envelope, isNotNull);
|
|
expect(_channel.codec.decodeEnvelope(envelope!), 'ignored');
|
|
expect(pendingPushNotificationLink.value, isNull);
|
|
});
|
|
|
|
test('pulls a cold-start notification response from native iOS', () async {
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(_channel, (call) async {
|
|
expect(call.method, 'takePendingNotificationResponse');
|
|
return {
|
|
'eventId': 'b' * 64,
|
|
'threadRootId': 'a' * 64,
|
|
'communityId': 'community-id',
|
|
'channelId': '123e4567-e89b-42d3-a456-426614174000',
|
|
};
|
|
});
|
|
|
|
await syncPendingBuzzPushNotificationResponse();
|
|
|
|
expect(
|
|
pendingPushNotificationLink.value,
|
|
MessageDeepLink(
|
|
communityId: 'community-id',
|
|
channelId: '123e4567-e89b-42d3-a456-426614174000',
|
|
messageId: 'b' * 64,
|
|
threadRootId: 'a' * 64,
|
|
),
|
|
);
|
|
});
|
|
|
|
test(
|
|
'pulls a cold-start notification response from native Android',
|
|
() async {
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.android;
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(_channel, (call) async {
|
|
expect(call.method, 'takePendingNotificationResponse');
|
|
return {
|
|
'eventId': 'b' * 64,
|
|
'communityId': 'community-id',
|
|
'channelId': '123e4567-e89b-42d3-a456-426614174000',
|
|
};
|
|
});
|
|
|
|
await syncPendingBuzzPushNotificationResponse();
|
|
|
|
expect(
|
|
pendingPushNotificationLink.value,
|
|
MessageDeepLink(
|
|
communityId: 'community-id',
|
|
channelId: '123e4567-e89b-42d3-a456-426614174000',
|
|
messageId: 'b' * 64,
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
Map<String, Object> _grantMap(String endpointGrant) => {
|
|
'relayOrigin': 'wss://relay.example',
|
|
'relayPubkey': 'a' * 64,
|
|
'installationId': 'c' * 32,
|
|
'endpointGrant': endpointGrant,
|
|
'endpointHash': 'b' * 64,
|
|
'endpointEpoch': 1,
|
|
'generation': 1,
|
|
'expiresAt': 1752624000,
|
|
};
|
|
|
|
class _RecordingDiagnostics extends NoopDiagnosticReporter {
|
|
final operations = <DiagnosticOperation>[];
|
|
|
|
@override
|
|
void reportError(
|
|
Object error,
|
|
StackTrace? stackTrace, {
|
|
required DiagnosticOperation operation,
|
|
}) => operations.add(operation);
|
|
}
|