Add test for non-fediverse response

codemagic-setup
Hank Grabowski 2023-03-21 21:23:25 -04:00
rodzic c5a64a8691
commit bf7b97a112
2 zmienionych plików z 20 dodań i 18 usunięć

Wyświetl plik

@ -24,15 +24,12 @@ class FediverseServiceValidator {
return Result.ok(data.isFediverse); return Result.ok(data.isFediverse);
} }
final updateResult = await refreshServerData(domain); final serverData = await refreshServerData(domain);
return updateResult.andThenSuccess((sd) { knownServers[domain] = serverData;
knownServers[sd.domainName] = sd; return Result.ok(serverData.isFediverse);
return sd.isFediverse;
}).execErrorCast();
} }
static FutureResult<ServerData, ExecError> refreshServerData( static Future<ServerData> refreshServerData(String domainName) async {
String domainName) async {
final uri = Uri.https( final uri = Uri.https(
domainName, domainName,
'/.well-known/nodeinfo', '/.well-known/nodeinfo',
@ -42,25 +39,24 @@ class FediverseServiceValidator {
return jsonDecode(page.data); return jsonDecode(page.data);
}) })
.andThenAsync( .andThenAsync(
(json) async => json is Map<String, dynamic> (json) async =>
? Result.ok(json) json is Map<String, dynamic> ? Result.ok(json) : Result.error(''),
: Result.error('Unknown response type for well-know/nodeinfo'),
) )
.andThenSuccessAsync((json) async => json['links'] ?? []) .andThenSuccessAsync((json) async => json['links'] ?? [])
.andThenAsync( .andThenAsync(
(nodeInfos) async => nodeInfos.isNotEmpty (nodeInfos) async => nodeInfos.isNotEmpty
? Result.ok(nodeInfos.last) ? Result.ok(nodeInfos.last)
: Result.error('Unknown response type for well-know/nodeinfo'), : Result.error(''),
) )
.andThenAsync((nodeInfo) async { .andThenAsync((nodeInfo) async {
final rel = nodeInfo['rel']?.toString() ?? ''; final rel = nodeInfo['rel']?.toString() ?? '';
if (!rel.startsWith('http://nodeinfo.diaspora.software/ns/schema/')) { if (!rel.startsWith('http://nodeinfo.diaspora.software/ns/schema/')) {
return Result.error('Unknown response type for well-know/nodeinfo'); return Result.error('');
} }
final nodeInfoUrl = Uri.tryParse(nodeInfo['href'] ?? ''); final nodeInfoUrl = Uri.tryParse(nodeInfo['href'] ?? '');
if (nodeInfoUrl == null) { if (nodeInfoUrl == null) {
return Result.error('Unknown response type for well-know/nodeinfo'); return Result.error('');
} }
return await getUrl(nodeInfoUrl); return await getUrl(nodeInfoUrl);
}) })
@ -81,6 +77,7 @@ class FediverseServiceValidator {
); );
}); });
return result.execErrorCast(); return result.getValueOrElse(
() => ServerData(domainName: domainName, isFediverse: false));
} }
} }

Wyświetl plik

@ -45,12 +45,17 @@ void main() {
test('Test against Pleroma Server', () async { test('Test against Pleroma Server', () async {
await testDomain('stereophonic.space', 'pleroma'); await testDomain('stereophonic.space', 'pleroma');
}); });
test('Test against non-fediverse server', () async {
final result =
await FediverseServiceValidator.refreshServerData('myportal.social');
expect(result.isFediverse, equals(false));
});
} }
Future<void> testDomain(String domain, String softwareName) async { Future<void> testDomain(String domain, String softwareName) async {
final result = await FediverseServiceValidator.refreshServerData(domain); final result = await FediverseServiceValidator.refreshServerData(domain);
expect(result.isSuccess, equals(true)); expect(result.isFediverse, equals(true));
expect(result.value.isFediverse, equals(true)); expect(result.domainName, equals(domain));
expect(result.value.domainName, equals(domain)); expect(result.softwareName, equals(softwareName));
expect(result.value.softwareName, equals(softwareName));
} }