Skip to content

Commit d3cc000

Browse files
wesbillmanBrain
andcommitted
Fix mobile identity inventory and onboarding retries
Co-authored-by: Brain <1a02c72794dcd0f07058a353bc3a81f4028b8c77c92c87fce6d5c8b85970a20b@wes.communities.public.builderlabstaging.xyz> Signed-off-by: Wes <[email protected]>
1 parent e48fe73 commit d3cc000

9 files changed

Lines changed: 162 additions & 44 deletions

‎mobile/lib/features/channels/deep_link_dispatcher.dart‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,20 @@ class _DeepLinkDispatcherState extends ConsumerState<DeepLinkDispatcher> {
106106
}
107107
} else if (link is InviteDeepLink) {
108108
_maybeDispatchInvite(link);
109+
} else if (ref.read(pendingDeepLinkProvider) == link) {
110+
// No fallback owns this link. It must not block later entry attempts.
111+
ref.read(pendingDeepLinkProvider.notifier).consume();
109112
}
110113
} catch (_) {
111114
if (mounted) {
115+
if (ref.read(pendingDeepLinkProvider) == link) {
116+
ref.read(pendingDeepLinkProvider.notifier).consume();
117+
}
112118
ScaffoldMessenger.maybeOf(context)?.showSnackBar(
113119
const SnackBar(
114-
content: Text('Could not open setup. Please try again.'),
120+
content: Text(
121+
'Could not open setup. Re-open the link to try again.',
122+
),
115123
),
116124
);
117125
}

‎mobile/lib/shared/hosted/account_identity_store.dart‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,9 @@ class AccountIdentityStore {
205205
try {
206206
return (await _storage.readAll()).keys.where(_isIdentityKey).toSet();
207207
} catch (_) {
208-
return {storageKey, _key, if (pubkey != null) forPairing(pubkey)._key};
208+
// A partial inventory cannot establish absence or authorize generation.
209+
if (pubkey == null) throw AccountIdentityFailure.unreadable;
210+
return {storageKey, _key, forPairing(pubkey)._key};
209211
}
210212
}
211213

‎mobile/lib/shared/hosted/hosted_join_policy.dart‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ Future<Map<String, dynamic>> readJoinResponse(
2828
final Map<String, dynamic> data;
2929
try {
3030
response = await boundedHttpResponse(client, request);
31+
if (response.statusCode == 429 ||
32+
(response.statusCode >= 500 && response.statusCode < 600)) {
33+
throw AccountFailure.unavailable;
34+
}
3135
data = jsonDecode(utf8.decode(response.bodyBytes)) as Map<String, dynamic>;
3236
} on FormatException {
3337
throw AccountFailure.invalidResponse;

‎mobile/test/features/channels/deep_link_dispatcher_test.dart‎

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,49 @@ import 'package:hooks_riverpod/hooks_riverpod.dart';
1515
import '../../shared/community/community_storage_test.dart';
1616

1717
void main() {
18-
testWidgets('hosted handler drains queued links without legacy generation', (
19-
tester,
20-
) async {
21-
const first = AddCommunityDeepLink(
22-
relayUrl: 'wss://one.communities.buzz.xyz',
23-
name: 'One',
24-
);
25-
const second = InviteDeepLink(
26-
relayUrl: 'wss://two.communities.buzz.xyz',
27-
code: 'invite',
28-
);
29-
final pending = _QueuedPendingDeepLinkNotifier([first, second]);
30-
final handled = <BuzzDeepLink>[];
31-
await tester.pumpWidget(
32-
ProviderScope(
33-
overrides: [
34-
pendingDeepLinkProvider.overrideWith(() => pending),
35-
inviteKeyGeneratorProvider.overrideWithValue(
36-
() => throw StateError('must not generate'),
37-
),
38-
],
39-
child: MaterialApp(
40-
home: DeepLinkDispatcher(
41-
dispatchMessageLinks: false,
42-
onboardingHandler: (_, link) async {
43-
handled.add(link);
44-
return true;
45-
},
46-
child: const Scaffold(body: SizedBox()),
18+
for (final outcome in ['handled', 'declined', 'failed']) {
19+
testWidgets('hosted handler drains queued links after $outcome setup', (
20+
tester,
21+
) async {
22+
const first = AddCommunityDeepLink(
23+
relayUrl: 'wss://one.communities.buzz.xyz',
24+
name: 'One',
25+
);
26+
const second = InviteDeepLink(
27+
relayUrl: 'wss://two.communities.buzz.xyz',
28+
code: 'invite',
29+
);
30+
final pending = _QueuedPendingDeepLinkNotifier([first, second]);
31+
final handled = <BuzzDeepLink>[];
32+
await tester.pumpWidget(
33+
ProviderScope(
34+
overrides: [
35+
pendingDeepLinkProvider.overrideWith(() => pending),
36+
inviteKeyGeneratorProvider.overrideWithValue(
37+
() => throw StateError('must not generate'),
38+
),
39+
],
40+
child: MaterialApp(
41+
home: DeepLinkDispatcher(
42+
dispatchMessageLinks: false,
43+
onboardingHandler: (_, link) async {
44+
handled.add(link);
45+
if (link == first) {
46+
if (outcome == 'declined') return false;
47+
if (outcome == 'failed') throw StateError('setup failed');
48+
}
49+
return true;
50+
},
51+
child: const Scaffold(body: SizedBox()),
52+
),
4753
),
4854
),
49-
),
50-
);
51-
await tester.pumpAndSettle();
52-
expect(handled, [first, second]);
53-
expect(pending.consumeCalls, 2);
54-
});
55+
);
56+
await tester.pumpAndSettle();
57+
expect(handled, [first, second]);
58+
expect(pending.consumeCalls, 2);
59+
});
60+
}
5561
testWidgets('queues deep links in arrival order until acknowledged', (
5662
tester,
5763
) async {

‎mobile/test/features/onboarding/hosted_app_gate_environment_test.dart‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:buzz/features/channels/deep_link_dispatcher.dart';
2+
import 'package:buzz/features/onboarding/self_hosted_entry_sheet.dart';
23
import 'package:buzz/hosted_app_gate.dart';
34
import 'package:buzz/shared/hosted/hosted_flow_provider.dart';
45
import 'package:buzz/shared/deeplink/deep_link.dart';
@@ -71,6 +72,46 @@ void main() {
7172
if (!HostedEnvironment.requiresAuth(Uri.parse(staging.relayUrl).host)) {
7273
expect(await pending, isFalse);
7374
expect(flow.started, isNull);
75+
if (!HostedEnvironment.enabled) {
76+
final container = ProviderScope.containerOf(context);
77+
final queue = container.read(pendingDeepLinkProvider.notifier);
78+
final bind = Uri(
79+
scheme: 'buzz',
80+
host: 'nostr-bind',
81+
queryParameters: {
82+
'challenge_id': '12345678-1234-1234-1234-123456789012',
83+
'nonce': 'a' * 43,
84+
'verification_code': '123456',
85+
'audience': 'buzz:nostr-identity',
86+
'action': 'bind_nostr_identity',
87+
'protocol': 'buzz-nostr-identity',
88+
'version': '1',
89+
'origin': HostedEnvironment.origin.toString(),
90+
'expires_at': '2099-01-01T00:00:00Z',
91+
'return': 'browser_fragment_v1',
92+
'callback_url': HostedEnvironment.origin
93+
.resolve('/buzz')
94+
.toString(),
95+
},
96+
);
97+
expect(parseBuzzDeepLink(bind), isA<NostrBindDeepLink>());
98+
queue.open(bind);
99+
queue.open(
100+
Uri.parse(
101+
'buzz://add-community?relay=wss%3A%2F%2Fcommunity.example.org&name=Community',
102+
),
103+
);
104+
await tester.pumpAndSettle();
105+
expect(find.byType(SelfHostedEntrySheet), findsOneWidget);
106+
expect(
107+
container.read(pendingDeepLinkProvider),
108+
isA<AddCommunityDeepLink>(),
109+
);
110+
Navigator.of(tester.element(find.byType(SelfHostedEntrySheet))).pop();
111+
await tester.pumpAndSettle();
112+
expect(container.read(pendingDeepLinkProvider), isNull);
113+
expect(flow.started, isNull);
114+
}
74115
return;
75116
}
76117
if (HostedEnvironment.origin.host == 'app.builderlabstaging.xyz') {

‎mobile/test/features/onboarding/hosted_journeys_test.dart‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,9 +731,19 @@ void main() {
731731
f.offerProfile = false;
732732
// The first dispatched attempt ends; retry owns the pending consent.
733733
// A new incoming link must now invalidate that consent, not wait on it.
734-
f.policyFailures = 1;
734+
f.policyResponses.addAll([
735+
http.Response('<html>Unavailable</html>', 502),
736+
http.Response('', 503),
737+
http.Response('{"error":"proxy_busy"}', 503),
738+
http.Response('', 429),
739+
]);
735740
await f.mount(tester);
736741
await f.link(tester, 'one');
742+
while (f.policyResponses.isNotEmpty) {
743+
expect(find.text('Try again').hitTestable(), findsOneWidget);
744+
expect(await f.storage.loadAll(), isEmpty);
745+
await f.tap(tester, 'Try again');
746+
}
737747
await f.tap(tester, 'Try again');
738748
expect(find.text('Accept and join').hitTestable(), findsOneWidget);
739749
await f.link(tester, 'two');

‎mobile/test/helpers/hosted_journey_fixture.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class HostedJourneyFixture {
5555
late ProviderContainer container;
5656
String? bound;
5757
int starterFailures = 0;
58-
int policyFailures = 0;
58+
final policyResponses = <http.Response>[];
5959
int browserStarts = 0;
6060
int secondRelayFailures = 0;
6161
DateTime assertionNow = DateTime.now();
@@ -242,7 +242,7 @@ class HostedJourneyFixture {
242242
if (missingList) return http.Response('{}', 404);
243243
result = {'communities': rows};
244244
} else if (path == '/api/join-policy') {
245-
if (policyFailures-- > 0) throw AccountFailure.unavailable;
245+
if (policyResponses.isNotEmpty) return policyResponses.removeAt(0);
246246
result = {
247247
'policy': {
248248
'version': request.url.host,

‎mobile/test/shared/hosted/account_identity_store_test.dart‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ void main() {
6868
final scoped = legacy.forSubject('b');
6969
final other = nostr.Keys('2'.padLeft(64, '0'));
7070
await scoped.create(other.nsec);
71-
expect((await scoped.identities()).map((item) => item.pubkey).toSet(), {
72-
key.public,
73-
other.public,
74-
});
71+
await expectLater(
72+
scoped.identities(),
73+
throwsA(AccountIdentityFailure.unreadable),
74+
);
75+
expect((await scoped.read())!.pubkey, other.public);
7576
expect(
7677
await scoped.withSavedIdentity(key.public, (value) async => value),
7778
key.nsec,

‎mobile/test/shared/hosted/device_identity_reconciliation_test.dart‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,36 @@ void main() {
165165
);
166166
expect(await secure.readAll(), data);
167167
}
168+
// Another account's scoped key must remain a choice, even when an
169+
// unrelated Android entry prevents bulk enumeration. Never mint a key
170+
// from that incomplete inventory; retry after storage recovers.
171+
FlutterSecureStorage.setMockInitialValues({});
172+
final storage = _InventoryStorage();
173+
store = AccountIdentityStore(storage: storage, subject: 'fixture');
174+
await store.forSubject('other-account').create(first.nsec);
175+
service = HostedIdentityService(account: account, store: store);
176+
final before = await secure.readAll();
177+
expect(
178+
(await service.resolve()).state,
179+
HostedIdentityState.needsSelection,
180+
);
181+
storage.failEnumeration = true;
182+
await expectLater(
183+
service.resolve(),
184+
throwsA(AccountIdentityFailure.unreadable),
185+
);
186+
expect(await secure.readAll(), before);
187+
expect(await store.read(), isNull);
188+
storage.failEnumeration = false;
189+
expect((await service.resolve()).choices.single.pubkey, first.public);
190+
expect(
191+
(await service.resolve(selectedPubkey: first.public)).pubkey,
192+
first.public,
193+
);
194+
expect(
195+
(await store.forSubject('other-account').read())!.pubkey,
196+
first.public,
197+
);
168198
},
169199
);
170200

@@ -226,3 +256,19 @@ void main() {
226256
expect(await store.read(), isNull);
227257
});
228258
}
259+
260+
class _InventoryStorage extends FlutterSecureStorage {
261+
bool failEnumeration = false;
262+
@override
263+
Future<Map<String, String>> readAll({
264+
AppleOptions? iOptions,
265+
AndroidOptions? aOptions,
266+
LinuxOptions? lOptions,
267+
WebOptions? webOptions,
268+
AppleOptions? mOptions,
269+
WindowsOptions? wOptions,
270+
}) async {
271+
if (failEnumeration) throw StateError('unrelated entry cannot decrypt');
272+
return super.readAll();
273+
}
274+
}

0 commit comments

Comments
 (0)