mirror of
https://github.com/block/buzz.git
synced 2026-11-01 15:01:12 +00:00
Signed-off-by: npub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@sprout-oss.stage.blox.sqprod.co> Signed-off-by: npub1jmc9dt2lyvzu3h0kxlwxt5zg4fxp9476awyxw6gwxn72g6cw7exqs64whm <96f056ad5f2305c8ddf637dc65d048aa4c12d7daeb8867690e34fca46b0ef64c@sprout-oss.stage.blox.sqprod.co> Signed-off-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1jmc9dt2lyvzu3h0kxlwxt5zg4fxp9476awyxw6gwxn72g6cw7exqs64whm <96f056ad5f2305c8ddf637dc65d048aa4c12d7daeb8867690e34fca46b0ef64c@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co>
259 lines
8.5 KiB
Dart
259 lines
8.5 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: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'}));
|
|
expect(capturedRequest!.headers['Authorization'], startsWith('Nostr '));
|
|
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('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('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),
|
|
);
|
|
}
|
|
}
|