90 lines
2.7 KiB
Dart
90 lines
2.7 KiB
Dart
|
|
import 'dart:convert';
|
||
|
|
|
||
|
|
import 'package:http/http.dart' as http;
|
||
|
|
import 'package:http/testing.dart';
|
||
|
|
import 'package:swarm/swarm.dart';
|
||
|
|
import 'package:test/test.dart';
|
||
|
|
|
||
|
|
const _pubkey =
|
||
|
|
'7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e';
|
||
|
|
|
||
|
|
void main() {
|
||
|
|
group('nip05MatchesPubkey (pure)', () {
|
||
|
|
test('true when names maps the name to the pubkey', () {
|
||
|
|
final body = jsonEncode({
|
||
|
|
'names': {'alice': _pubkey},
|
||
|
|
});
|
||
|
|
expect(nip05MatchesPubkey(body, 'alice', _pubkey), isTrue);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('case-insensitive on the pubkey', () {
|
||
|
|
final body = jsonEncode({
|
||
|
|
'names': {'alice': _pubkey.toUpperCase()},
|
||
|
|
});
|
||
|
|
expect(nip05MatchesPubkey(body, 'alice', _pubkey), isTrue);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('false on name mismatch, missing name, or junk', () {
|
||
|
|
final body = jsonEncode({
|
||
|
|
'names': {'alice': _pubkey},
|
||
|
|
});
|
||
|
|
expect(nip05MatchesPubkey(body, 'bob', _pubkey), isFalse);
|
||
|
|
expect(
|
||
|
|
nip05MatchesPubkey(jsonEncode({'names': {}}), 'alice', _pubkey),
|
||
|
|
isFalse,
|
||
|
|
);
|
||
|
|
expect(nip05MatchesPubkey('not json', 'alice', _pubkey), isFalse);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
group('verifyNip05 (mocked HTTP)', () {
|
||
|
|
test('verifies a well-formed match and queries the right URL', () async {
|
||
|
|
late Uri requested;
|
||
|
|
final client = MockClient((req) async {
|
||
|
|
requested = req.url;
|
||
|
|
return http.Response(
|
||
|
|
jsonEncode({
|
||
|
|
'names': {'alice': _pubkey},
|
||
|
|
}),
|
||
|
|
200,
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
final ok = await verifyNip05('alice@example.com', _pubkey,
|
||
|
|
client: client);
|
||
|
|
expect(ok, isTrue);
|
||
|
|
expect(requested.host, equals('example.com'));
|
||
|
|
expect(requested.path, equals('/.well-known/nostr.json'));
|
||
|
|
expect(requested.queryParameters['name'], equals('alice'));
|
||
|
|
});
|
||
|
|
|
||
|
|
test('false on name mismatch', () async {
|
||
|
|
final client = MockClient((req) async => http.Response(
|
||
|
|
jsonEncode({
|
||
|
|
'names': {'alice': 'deadbeef'},
|
||
|
|
}),
|
||
|
|
200,
|
||
|
|
));
|
||
|
|
expect(
|
||
|
|
await verifyNip05('alice@example.com', _pubkey, client: client),
|
||
|
|
isFalse,
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('false on non-200', () async {
|
||
|
|
final client = MockClient((req) async => http.Response('', 404));
|
||
|
|
expect(
|
||
|
|
await verifyNip05('alice@example.com', _pubkey, client: client),
|
||
|
|
isFalse,
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('false on malformed identifier', () async {
|
||
|
|
final client = MockClient((req) async => http.Response('', 200));
|
||
|
|
expect(await verifyNip05('no-at-sign', _pubkey, client: client), isFalse);
|
||
|
|
expect(await verifyNip05('@example.com', _pubkey, client: client),
|
||
|
|
isFalse);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|