Files
buzz/mobile/test/features/settings/android_notifications_test.dart
Tom Brow d4c0729a86 fix(mobile): Android push follow-ups from enrollment review (#9892)
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>
2026-11-23 16:25:19 -08:00

539 lines
20 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'package:buzz/features/settings/settings_page.dart';
import 'package:buzz/shared/auth/auth_provider.dart';
import 'package:buzz/shared/community/community.dart';
import 'package:buzz/shared/community/community_provider.dart';
import 'package:buzz/shared/community/community_storage.dart';
import 'package:buzz/shared/push/dev_push_lease.dart';
import 'package:buzz/shared/push/push_bootstrap.dart';
import 'package:buzz/shared/push/push_lease_revocation_outbox.dart';
import 'package:buzz/shared/crypto/nip44.dart';
import 'package:buzz/shared/relay/nostr_models.dart';
import 'package:buzz/shared/push/android_push_authority.dart';
import 'package:buzz/shared/push/push_bridge.dart';
import 'package:buzz/shared/push/push_relay_capability_provider.dart';
import 'package:buzz/shared/push/push_subscription.dart';
import 'package:buzz/shared/relay/app_lifecycle_provider.dart';
import 'package:buzz/shared/relay/relay_provider.dart';
import 'package:buzz/shared/relay/relay_session.dart';
import 'package:buzz/shared/theme/theme.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:nostr/nostr.dart' as nostr;
import 'package:shared_preferences/shared_preferences.dart';
import '../../helpers/android_push_fakes.dart';
import '../../shared/community/community_storage_test.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
setUpAll(() async {
final fonts = FontLoader('Inter')
..addFont(rootBundle.load('assets/fonts/InterVariable.ttf'));
await fonts.load();
});
for (final attests in [true, false]) {
testWidgets(
attests
? 'an attesting Android phone enrolls and publishes its lease'
: 'an Android phone that cannot attest keeps working and says so',
(tester) async {
tester.view.physicalSize = const Size(375, 1800);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
debugDefaultTargetPlatformOverride = TargetPlatform.android;
addTearDown(() => debugDefaultTargetPlatformOverride = null);
PackageInfo.setMockInitialValues(
appName: 'Buzz',
packageName: 'buzz',
version: '1.0.0',
buildNumber: '1',
buildSignature: '',
);
SharedPreferences.setMockInitialValues({});
final prefs = await SharedPreferences.getInstance();
final storage = CommunityStorage(secure: FakeSecureStorage());
final subscriptions = [
BuzzPushSubscription(
filter: BuzzPushFilter(kinds: const [9], pTags: ['a' * 64]),
notificationClass: 'default',
),
];
final session = _Connected();
var advertised = _descriptor;
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
final gateway = FakePushGateway(nowSeconds: () => now);
debugAndroidPushAuthority = AndroidPushAuthority(
keystore: FakeAndroidKeystore()..attests = attests,
gateway: fakeGatewayClient(gateway),
store: _MemoryStore(),
sleep: (_) async {},
);
addTearDown(() {
debugAndroidPushAuthority = null;
fcmDeviceToken.value = null;
pushUnavailableReason.value = null;
pushEndpointGrants.value = const [];
});
fcmDeviceToken.value = 'test-fcm-token';
const nativePush = MethodChannel('buzz/push_registration');
final messenger =
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
messenger.setMockMethodCallHandler(
nativePush,
(call) async => switch (call.method) {
'startRegistration' || 'refreshToken' || 'syncPushSnapshot' => null,
_ => throw StateError('Unexpected native call ${call.method}'),
},
);
addTearDown(() => messenger.setMockMethodCallHandler(nativePush, null));
final community =
Community.create(
name: 'Team',
relayUrl: 'https://relay.example',
).copyWith(
nsec: nostr.Nip19.encode(
prefix: nostr.Nip19Prefix.nsec,
data: '1' * 64,
),
pushNotificationsEnabled: true,
pushLeaseInstallationId: 'd' * 32,
pushSubscriptionState: BuzzPushLeaseSubscriptionState.desired(
desired: subscriptions,
).withAccepted(subscriptions: subscriptions, generation: 7),
);
await storage.save(community);
await storage.saveActiveId(community.id);
await tester.pumpWidget(
ProviderScope(
overrides: [
savedPrefsProvider.overrideWithValue(prefs),
authProvider.overrideWith(_Auth.new),
communityStorageProvider.overrideWithValue(storage),
communitySnapshotWriterProvider.overrideWithValue(
(communities) async {},
),
buzzPushLeaseRevocationStorageProvider.overrideWithValue(
BuzzPushLeaseRevocationStorage(secure: FakeSecureStorage()),
),
relaySessionProvider.overrideWith(() => session),
myPubkeyProvider.overrideWithValue(nostr.Keys('1' * 64).public),
buzzPushDescriptorFetcherProvider.overrideWithValue(
(_) async => advertised,
),
appLifecycleProvider.overrideWith(_Lifecycle.new),
],
child: BuzzPushBootstrap(
child: MaterialApp(
theme: AppTheme.light(),
home: SettingsPage(
profileHeader: const SizedBox.shrink(),
identityRecoveryPageBuilder: (_) => const SizedBox.shrink(),
),
),
),
),
);
await tester.pumpAndSettle();
// Notification settings exist on Android, and the app stays usable
// either way.
expect(
find.byKey(const ValueKey('push-notifications-setting')),
findsOneWidget,
);
expect(tester.widget<Switch>(find.byType(Switch)).value, isTrue);
expect(tester.takeException(), isNull);
if (attests) {
expect(find.byKey(const ValueKey('push-unavailable')), findsNothing);
expect(gateway.installations, hasLength(1));
expect(session.leases.last['active'], isTrue);
expect(session.leases.last['endpoint'], isNotEmpty);
// The relay reconnects advertising a new executor key: the old key's
// delegation is revoked while push stays on, and the lease carries a
// grant for the new key.
final oldExecutor = _descriptor.executorPubkey;
final newExecutor = nostr.Keys('3' * 64).public;
advertised = _descriptorFor(newExecutor);
session.executorSecret = '3' * 64;
final installationBefore = gateway.installations.values.single;
session.setStatus(SessionStatus.reconnecting);
await tester.pumpAndSettle();
session.setStatus(SessionStatus.connected);
await tester.pumpAndSettle();
expect(installationBefore.revokedDelegations, {'$oldExecutor/1'});
expect(installationBefore.delegations, contains(newExecutor));
expect(session.leases.last['endpoint'], contains(newExecutor));
// Turning push off keeps the installation: the community remains.
// Removing that community afterwards is what revokes it.
final installation = gateway.installations.values.single;
await tester.tap(find.byType(Switch));
await tester.pumpAndSettle();
expect(installation.revokedDelegations, isNotEmpty);
expect(installation.revoked, isFalse);
final container = ProviderScope.containerOf(
tester.element(find.byType(SettingsPage)),
);
await container
.read(communityListProvider.notifier)
.removeCommunity(community.id);
await tester.pumpAndSettle();
expect(installation.revoked, isTrue);
} else {
expect(
find.byKey(const ValueKey('push-unavailable')),
findsOneWidget,
);
expect(find.text('Push is unavailable on this phone'), findsOne);
expect(gateway.installations, isEmpty);
expect(session.leases.where((l) => l['active'] == true), isEmpty);
}
await tester.pumpWidget(const SizedBox.shrink());
await tester.pumpAndSettle();
// Foundation checks its debug variables before teardowns run.
debugDefaultTargetPlatformOverride = null;
},
);
}
// Mounts only the push bootstrap over a community with the given relay URL
// and opt-in, recording the native registration calls.
Future<List<String>> pumpBootstrap(
WidgetTester tester, {
required TargetPlatform platform,
required String relayUrl,
required bool enabled,
_Connected? session,
bool advertisesPush = true,
FakePushGateway? gateway,
Future<void>? descriptorGate,
List<BuzzPushSubscription>? subscriptions,
}) async {
debugDefaultTargetPlatformOverride = platform;
addTearDown(() => debugDefaultTargetPlatformOverride = null);
SharedPreferences.setMockInitialValues({});
final prefs = await SharedPreferences.getInstance();
final storage = CommunityStorage(secure: FakeSecureStorage());
final pushGateway =
gateway ??
FakePushGateway(
nowSeconds: () => DateTime.now().millisecondsSinceEpoch ~/ 1000,
);
debugAndroidPushAuthority = AndroidPushAuthority(
keystore: FakeAndroidKeystore(),
gateway: fakeGatewayClient(pushGateway),
store: _MemoryStore(),
sleep: (_) async {},
);
addTearDown(() => debugAndroidPushAuthority = null);
var descriptorFetches = 0;
final calls = <String>[];
final messenger =
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
for (final channel in const [
MethodChannel('buzz/push_registration'),
MethodChannel('buzz/push'),
]) {
messenger.setMockMethodCallHandler(channel, (call) async {
calls.add(call.method);
return null;
});
addTearDown(() => messenger.setMockMethodCallHandler(channel, null));
}
final community = Community.create(name: 'Team', relayUrl: relayUrl)
.copyWith(
nsec: nostr.Nip19.encode(
prefix: nostr.Nip19Prefix.nsec,
data: '1' * 64,
),
pushNotificationsEnabled: enabled,
pushSubscriptionState: subscriptions == null
? null
: BuzzPushLeaseSubscriptionState.desired(desired: subscriptions),
);
await storage.save(community);
await storage.saveActiveId(community.id);
await tester.pumpWidget(
ProviderScope(
overrides: [
savedPrefsProvider.overrideWithValue(prefs),
authProvider.overrideWith(_Auth.new),
communityStorageProvider.overrideWithValue(storage),
communitySnapshotWriterProvider.overrideWithValue(
(communities) async {},
),
buzzPushLeaseRevocationStorageProvider.overrideWithValue(
BuzzPushLeaseRevocationStorage(secure: FakeSecureStorage()),
),
relaySessionProvider.overrideWith(() => session ?? _Connected()),
myPubkeyProvider.overrideWithValue(nostr.Keys('1' * 64).public),
buzzPushDescriptorFetcherProvider.overrideWithValue((_) async {
// Discovery reads the descriptor first, which sets push up; the
// fetch that publication makes next is the one held back.
if (++descriptorFetches > 1) await descriptorGate;
if (!advertisesPush) throw const BuzzPushNotAdvertised();
return _descriptor;
}),
appLifecycleProvider.overrideWith(_Lifecycle.new),
],
child: const BuzzPushBootstrap(child: SizedBox()),
),
);
await tester.pumpAndSettle();
return calls;
}
Future<void> unmount(WidgetTester tester) async {
await tester.pumpWidget(const SizedBox.shrink());
await tester.pumpAndSettle();
// Foundation checks its debug variables before teardowns run.
debugDefaultTargetPlatformOverride = null;
}
for (final platform in [TargetPlatform.android, TargetPlatform.iOS]) {
testWidgets('a relay URL with a path does not break push on $platform', (
tester,
) async {
await pumpBootstrap(
tester,
platform: platform,
relayUrl: 'https://relay.example/team',
enabled: true,
);
expect(tester.takeException(), isNull);
await unmount(tester);
});
}
for (final (name, enabled, advertisesPush) in [
('refreshes the FCM token', true, true),
('fetches no token while opted out', false, true),
('fetches no token for a relay without push', true, false),
]) {
testWidgets('resuming the app $name', (tester) async {
fcmDeviceToken.value = 'test-fcm-token';
addTearDown(() => fcmDeviceToken.value = null);
final calls = await pumpBootstrap(
tester,
platform: TargetPlatform.android,
relayUrl: 'https://relay.example',
enabled: enabled,
advertisesPush: advertisesPush,
);
calls.clear();
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
await tester.pumpAndSettle();
expect(calls.contains('refreshToken'), enabled && advertisesPush);
await unmount(tester);
});
}
testWidgets('turning push off while its enrollment waits publishes nothing', (
tester,
) async {
fcmDeviceToken.value = 'test-fcm-token';
addTearDown(() => fcmDeviceToken.value = null);
final gateway = FakePushGateway(
nowSeconds: () => DateTime.now().millisecondsSinceEpoch ~/ 1000,
);
final session = _Connected();
final descriptorGate = Completer<void>();
await pumpBootstrap(
tester,
platform: TargetPlatform.android,
relayUrl: 'https://relay.example',
enabled: true,
session: session,
gateway: gateway,
descriptorGate: descriptorGate.future,
subscriptions: [
BuzzPushSubscription(
filter: BuzzPushFilter(kinds: const [9], pTags: ['a' * 64]),
notificationClass: 'default',
),
],
);
final container = ProviderScope.containerOf(
tester.element(find.byType(BuzzPushBootstrap)),
);
final notifier = container.read(communityListProvider.notifier);
await notifier.setPushNotificationsEnabled(
container.read(communityListProvider).value!.single.id,
false,
);
// The descriptor arrives after the opt-out: nothing is enrolled, delegated
// or published for a community that no longer wants push.
descriptorGate.complete();
await tester.pumpAndSettle();
expect(gateway.requests, isEmpty);
expect(session.leases.where((lease) => lease['active'] == true), isEmpty);
await unmount(tester);
});
testWidgets('turning push off while it delegates ends the attempt quietly', (
tester,
) async {
fcmDeviceToken.value = 'test-fcm-token';
addTearDown(() => fcmDeviceToken.value = null);
final failures = <String>[];
final print = debugPrint;
debugPrint = (message, {wrapWidth}) {
if (message != null && message.contains('bootstrap failed')) {
failures.add(message);
}
};
final gateway = FakePushGateway(
nowSeconds: () => DateTime.now().millisecondsSinceEpoch ~/ 1000,
);
final delegated = Completer<void>();
gateway.holdResponseFor['/v1/delegations'] = delegated.future;
final session = _Connected();
final descriptorGate = Completer<void>();
await pumpBootstrap(
tester,
platform: TargetPlatform.android,
relayUrl: 'https://relay.example',
enabled: true,
session: session,
gateway: gateway,
descriptorGate: descriptorGate.future,
subscriptions: [
BuzzPushSubscription(
filter: BuzzPushFilter(kinds: const [9], pTags: ['a' * 64]),
notificationClass: 'default',
),
],
);
descriptorGate.complete();
await tester.pump();
await tester.pump();
expect(gateway.bodiesFor('/v1/delegations'), hasLength(1));
// The gateway accepted the delegation; the opt-out lands before the phone
// hears back.
final container = ProviderScope.containerOf(
tester.element(find.byType(BuzzPushBootstrap)),
);
await container
.read(communityListProvider.notifier)
.setPushNotificationsEnabled(
container.read(communityListProvider).value!.single.id,
false,
);
delegated.complete();
await tester.pumpAndSettle();
// No lease is published, the new delegation is revoked, and the attempt
// ends as cancelled rather than failing into a retry.
expect(session.leases.where((lease) => lease['active'] == true), isEmpty);
expect(gateway.installations.values.single.revokedDelegations, isNotEmpty);
// Foundation checks its debug variables before teardowns run.
debugPrint = print;
expect(failures, isEmpty);
await unmount(tester);
});
testWidgets('resuming while the relay reconnects refreshes the FCM token', (
tester,
) async {
fcmDeviceToken.value = 'test-fcm-token';
addTearDown(() => fcmDeviceToken.value = null);
final session = _Connected();
final calls = await pumpBootstrap(
tester,
platform: TargetPlatform.android,
relayUrl: 'https://relay.example',
enabled: true,
session: session,
);
session.setStatus(SessionStatus.reconnecting);
await tester.pumpAndSettle();
calls.clear();
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
await tester.pumpAndSettle();
expect(calls, contains('refreshToken'));
await unmount(tester);
});
}
class _MemoryStore implements AndroidPushStateStore {
String? value;
@override
Future<String?> read() async => value;
@override
Future<void> write(String? next) async => value = next;
}
class _Lifecycle extends AppLifecycleNotifier {
@override
AppLifecycleState build() => AppLifecycleState.resumed;
}
class _Auth extends AuthNotifier {
@override
Future<AuthState> build() async =>
const AuthState(status: AuthStatus.unauthenticated);
}
class _Connected extends RelaySessionNotifier {
final leases = <Map<String, dynamic>>[];
String executorSecret = '2' * 64;
@override
Future<NostrEvent> publish(
NostrEvent event, {
Duration timeout = const Duration(seconds: 8),
Future<bool> Function()? mayPublish,
}) async {
expect(event.kind, buzzPushLeaseKind);
final plaintext = nip44Decrypt(
getConversationKey(executorSecret, event.pubkey),
event.content,
);
leases.add(jsonDecode(plaintext) as Map<String, dynamic>);
return event;
}
void setStatus(SessionStatus status) => state = SessionState(status: status);
@override
SessionState build() => const SessionState(status: SessionStatus.connected);
}
final _descriptor = _descriptorFor(nostr.Keys('2' * 64).public);
BuzzPushLeaseDescriptor _descriptorFor(String executorPubkey) =>
BuzzPushLeaseDescriptor(
origin: 'wss://relay.example',
executorKeyId: 'relay-v1',
executorPubkey: executorPubkey,
transport: 'apns',
maxLeaseTtlSeconds: 3600,
maxContentLength: 4096,
maxPlaintextLength: 4096,
maxEndpointLength: 2048,
maxStringLength: 512,
);