mirror of
https://github.com/block/buzz.git
synced 2026-11-01 23:11:58 +00:00
## Summary - add database-backed v2 invite links with optional maximum-use limits and atomic final-slot redemption - preserve v1 invite compatibility while adding exhausted/expired/invalid client handling across desktop, web, and mobile - emit structured claim-outcome logs with community, invite ID, outcome, maximum uses, and post-claim count ## Verification - `cargo fmt --all -- --check` - `cargo test -p buzz-db` (85 passed, 134 Postgres-dependent ignored) - `cargo clippy -p buzz-db --all-targets -- -D warnings` - desktop `npm run typecheck` - push hook: desktop checks/tests, desktop Tauri tests, Rust tests, and branch-skew passed - Postgres integration tests were previously reviewed green at the pre-rebase tree; local rerun on this session was unavailable because Postgres/Docker were not running - mobile push-hook check could not start because Flutter is unavailable locally --------- Signed-off-by: Kalvin Chau <kalvin@block.xyz> Signed-off-by: npub1dccv64krpcpse5cmkzfeh998cftungyatw3djt8jwdw6g43f7fyqzzmrf7 <6e30cd56c30e030cd31bb0939b94a7c257c9a09d5ba2d92cf2735da45629f248@buzz.block.builderlab.xyz> Co-authored-by: npub1c4alndp82zyt9veaklm5d965quss79vlhk9awv7qu5erwhmf42qqlvc25c <c57bf9b4275088b2b33db7f746975407210f159fbd8bd733c0e532375f69aa80@buzz.block.builderlab.xyz> Co-authored-by: npub122y0pqkertljmedu303rl0aqrj3w8pvu43t6jxm6875lzg6f2pwqegc3xc <5288f082d91aff2de5bc8be23fbfa01ca2e3859cac57a91b7a3fa9f12349505c@buzz.block.builderlab.xyz> Co-authored-by: npub1dccv64krpcpse5cmkzfeh998cftungyatw3djt8jwdw6g43f7fyqzzmrf7 <6e30cd56c30e030cd31bb0939b94a7c257c9a09d5ba2d92cf2735da45629f248@buzz.block.builderlab.xyz>
337 lines
11 KiB
Dart
337 lines
11 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:http/testing.dart' as http_testing;
|
|
import 'package:nostr/nostr.dart' as nostr;
|
|
import 'package:pointycastle/digests/sha256.dart';
|
|
|
|
import 'package:buzz/features/invites/invite_join_provider.dart';
|
|
import 'package:buzz/shared/auth/auth.dart';
|
|
import 'package:buzz/shared/deeplink/deep_link.dart';
|
|
|
|
import '../../shared/community/community_storage_test.dart';
|
|
|
|
void main() {
|
|
for (final existingRelayUrl in [
|
|
'wss://relay.example.com',
|
|
'https://relay.example.com',
|
|
]) {
|
|
test(
|
|
'same-relay invite switches existing $existingRelayUrl before keygen or claim',
|
|
() async {
|
|
var generatedKeys = 0;
|
|
var claimRequests = 0;
|
|
final storage = CommunityStorage(secure: FakeSecureStorage());
|
|
final existing = Community(
|
|
id: 'existing-id',
|
|
name: 'Existing',
|
|
relayUrl: existingRelayUrl,
|
|
pubkey: 'old-pubkey',
|
|
nsec: 'old-nsec',
|
|
addedAt: DateTime.utc(2026),
|
|
);
|
|
await storage.save(existing);
|
|
final auth = _RecordingAuthNotifier();
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
communityStorageProvider.overrideWithValue(storage),
|
|
authProvider.overrideWith(() => auth),
|
|
inviteKeyGeneratorProvider.overrideWithValue(() {
|
|
generatedKeys++;
|
|
return nostr.Keys.generate();
|
|
}),
|
|
inviteJoinHttpClientProvider.overrideWithValue(
|
|
http_testing.MockClient((request) async {
|
|
claimRequests++;
|
|
return http.Response('{}', 500);
|
|
}),
|
|
),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
await container.read(communityListProvider.future);
|
|
|
|
await container
|
|
.read(inviteJoinProvider.notifier)
|
|
.prepare(
|
|
const InviteDeepLink(
|
|
relayUrl: 'wss://relay.example.com',
|
|
code: 'code',
|
|
),
|
|
);
|
|
|
|
final state = container.read(inviteJoinProvider);
|
|
final stored = (await storage.loadAll()).single;
|
|
expect(state.status, InviteJoinStatus.switchedExisting);
|
|
expect(await storage.loadActiveId(), existing.id);
|
|
expect(stored.relayUrl, existingRelayUrl);
|
|
expect(stored.pubkey, 'old-pubkey');
|
|
expect(stored.nsec, 'old-nsec');
|
|
expect(generatedKeys, 0);
|
|
expect(claimRequests, 0);
|
|
expect(auth.authenticatedCommunities, isEmpty);
|
|
},
|
|
);
|
|
}
|
|
|
|
test(
|
|
'claim posts with freshly-generated key and stores joined community',
|
|
() async {
|
|
final keys = nostr.Keys.generate();
|
|
http.Request? capturedRequest;
|
|
final storage = CommunityStorage(secure: FakeSecureStorage());
|
|
final auth = _RecordingAuthNotifier();
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
communityStorageProvider.overrideWithValue(storage),
|
|
authProvider.overrideWith(() => auth),
|
|
inviteKeyGeneratorProvider.overrideWithValue(() => keys),
|
|
inviteJoinHttpClientProvider.overrideWithValue(
|
|
http_testing.MockClient((request) async {
|
|
capturedRequest = request;
|
|
return http.Response(
|
|
jsonEncode({
|
|
'status': 'joined',
|
|
'community_id': 'community-id',
|
|
'host': 'relay.example.com',
|
|
'role': 'member',
|
|
}),
|
|
200,
|
|
);
|
|
}),
|
|
),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
|
|
await container
|
|
.read(inviteJoinProvider.notifier)
|
|
.prepare(
|
|
const InviteDeepLink(
|
|
relayUrl: 'wss://relay.example.com',
|
|
code: 'code',
|
|
),
|
|
);
|
|
expect(
|
|
container.read(inviteJoinProvider).status,
|
|
InviteJoinStatus.confirming,
|
|
);
|
|
|
|
await container.read(inviteJoinProvider.notifier).confirmJoin();
|
|
|
|
final state = container.read(inviteJoinProvider);
|
|
expect(state.status, InviteJoinStatus.success);
|
|
expect(capturedRequest, isNotNull);
|
|
expect(
|
|
capturedRequest!.url.toString(),
|
|
'https://relay.example.com/api/invites/claim',
|
|
);
|
|
expect(capturedRequest!.body, jsonEncode({'code': 'code'}));
|
|
final authHeader = capturedRequest!.headers['Authorization'];
|
|
expect(authHeader, startsWith('Nostr '));
|
|
expect(capturedRequest!.followRedirects, isFalse);
|
|
final encoded = authHeader!.substring('Nostr '.length);
|
|
final authEvent =
|
|
jsonDecode(
|
|
utf8.decode(base64Url.decode(base64Url.normalize(encoded))),
|
|
)
|
|
as Map<String, dynamic>;
|
|
final tags = (authEvent['tags'] as List<dynamic>)
|
|
.map((tag) => (tag as List<dynamic>).cast<String>())
|
|
.toList();
|
|
final payloadHash = SHA256Digest()
|
|
.process(capturedRequest!.bodyBytes)
|
|
.map((byte) => byte.toRadixString(16).padLeft(2, '0'))
|
|
.join();
|
|
expect(authEvent['kind'], 27235);
|
|
expect(tags, contains(equals(['u', capturedRequest!.url.toString()])));
|
|
expect(tags, contains(equals(['method', 'POST'])));
|
|
expect(tags, contains(equals(['payload', payloadHash])));
|
|
expect(auth.authenticatedCommunities, hasLength(1));
|
|
expect(
|
|
auth.authenticatedCommunities.single.relayUrl,
|
|
'wss://relay.example.com',
|
|
);
|
|
expect(auth.authenticatedCommunities.single.pubkey, keys.public);
|
|
expect(auth.authenticatedCommunities.single.nsec, keys.nsec);
|
|
},
|
|
);
|
|
|
|
test('prepare rejects an unsafe relay before showing confirmation', () async {
|
|
final container = ProviderContainer();
|
|
addTearDown(container.dispose);
|
|
|
|
await expectLater(
|
|
container
|
|
.read(inviteJoinProvider.notifier)
|
|
.prepare(
|
|
const InviteDeepLink(relayUrl: 'wss://127.0.0.1', code: 'code'),
|
|
),
|
|
throwsFormatException,
|
|
);
|
|
expect(container.read(inviteJoinProvider).status, InviteJoinStatus.idle);
|
|
});
|
|
|
|
test('join_policy_required requires a fresh link and cannot retry', () async {
|
|
final keys = nostr.Keys.generate();
|
|
var attempts = 0;
|
|
final storage = CommunityStorage(secure: FakeSecureStorage());
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
communityStorageProvider.overrideWithValue(storage),
|
|
inviteKeyGeneratorProvider.overrideWithValue(() => keys),
|
|
inviteJoinHttpClientProvider.overrideWithValue(
|
|
http_testing.MockClient((request) async {
|
|
attempts++;
|
|
return http.Response(
|
|
jsonEncode({'error': 'join_policy_required'}),
|
|
403,
|
|
);
|
|
}),
|
|
),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
|
|
await container
|
|
.read(inviteJoinProvider.notifier)
|
|
.prepare(
|
|
const InviteDeepLink(
|
|
relayUrl: 'wss://relay.example.com',
|
|
code: 'code',
|
|
policyReceipt: 'expired.receipt',
|
|
),
|
|
);
|
|
await container.read(inviteJoinProvider.notifier).confirmJoin();
|
|
|
|
final state = container.read(inviteJoinProvider);
|
|
expect(state.status, InviteJoinStatus.error);
|
|
expect(state.requiresFreshInvite, isTrue);
|
|
expect(
|
|
state.errorMessage,
|
|
'This invite approval has expired. Re-open the invite link to try again.',
|
|
);
|
|
|
|
await container.read(inviteJoinProvider.notifier).confirmJoin();
|
|
expect(attempts, 1);
|
|
});
|
|
|
|
test('invite_exhausted requires a fresh invite and cannot retry', () async {
|
|
final keys = nostr.Keys.generate();
|
|
var attempts = 0;
|
|
final storage = CommunityStorage(secure: FakeSecureStorage());
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
communityStorageProvider.overrideWithValue(storage),
|
|
inviteKeyGeneratorProvider.overrideWithValue(() => keys),
|
|
inviteJoinHttpClientProvider.overrideWithValue(
|
|
http_testing.MockClient((request) async {
|
|
attempts++;
|
|
return http.Response(
|
|
jsonEncode({'error': 'invite_exhausted'}),
|
|
403,
|
|
);
|
|
}),
|
|
),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
|
|
await container
|
|
.read(inviteJoinProvider.notifier)
|
|
.prepare(
|
|
const InviteDeepLink(
|
|
relayUrl: 'wss://relay.example.com',
|
|
code: 'v2.exhausted-secret',
|
|
),
|
|
);
|
|
await container.read(inviteJoinProvider.notifier).confirmJoin();
|
|
|
|
final state = container.read(inviteJoinProvider);
|
|
expect(state.status, InviteJoinStatus.error);
|
|
expect(state.requiresFreshInvite, isTrue);
|
|
expect(
|
|
state.errorMessage,
|
|
'This invite has reached its use limit. Ask for a new invite.',
|
|
);
|
|
|
|
await container.read(inviteJoinProvider.notifier).confirmJoin();
|
|
expect(attempts, 1);
|
|
});
|
|
|
|
test('failed claim can be retried and preserves policy receipt', () async {
|
|
final keys = nostr.Keys.generate();
|
|
var attempts = 0;
|
|
final bodies = <String>[];
|
|
final storage = CommunityStorage(secure: FakeSecureStorage());
|
|
final auth = _RecordingAuthNotifier();
|
|
final container = ProviderContainer(
|
|
overrides: [
|
|
communityStorageProvider.overrideWithValue(storage),
|
|
authProvider.overrideWith(() => auth),
|
|
inviteKeyGeneratorProvider.overrideWithValue(() => keys),
|
|
inviteJoinHttpClientProvider.overrideWithValue(
|
|
http_testing.MockClient((request) async {
|
|
attempts++;
|
|
bodies.add(request.body);
|
|
if (attempts == 1) {
|
|
return http.Response(jsonEncode({'error': 'temporary'}), 503);
|
|
}
|
|
return http.Response(
|
|
jsonEncode({
|
|
'status': 'joined',
|
|
'host': 'relay.example.com',
|
|
'role': 'member',
|
|
}),
|
|
200,
|
|
);
|
|
}),
|
|
),
|
|
],
|
|
);
|
|
addTearDown(container.dispose);
|
|
|
|
await container
|
|
.read(inviteJoinProvider.notifier)
|
|
.prepare(
|
|
const InviteDeepLink(
|
|
relayUrl: 'wss://relay.example.com',
|
|
code: 'code',
|
|
policyReceipt: 'receipt.value',
|
|
),
|
|
);
|
|
await container.read(inviteJoinProvider.notifier).confirmJoin();
|
|
expect(container.read(inviteJoinProvider).status, InviteJoinStatus.error);
|
|
|
|
await container.read(inviteJoinProvider.notifier).confirmJoin();
|
|
|
|
expect(container.read(inviteJoinProvider).status, InviteJoinStatus.success);
|
|
expect(attempts, 2);
|
|
expect(
|
|
bodies,
|
|
everyElement(
|
|
jsonEncode({'code': 'code', 'policy_receipt': 'receipt.value'}),
|
|
),
|
|
);
|
|
expect(auth.authenticatedCommunities, hasLength(1));
|
|
});
|
|
}
|
|
|
|
class _RecordingAuthNotifier extends AuthNotifier {
|
|
final List<Community> authenticatedCommunities = [];
|
|
|
|
@override
|
|
Future<AuthState> build() async =>
|
|
const AuthState(status: AuthStatus.unauthenticated);
|
|
|
|
@override
|
|
Future<void> authenticateWithCommunity(Community community) async {
|
|
authenticatedCommunities.add(community);
|
|
state = AsyncData(
|
|
AuthState(status: AuthStatus.authenticated, community: community),
|
|
);
|
|
}
|
|
}
|