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);
}
final updateResult = await refreshServerData(domain);
return updateResult.andThenSuccess((sd) {
knownServers[sd.domainName] = sd;
return sd.isFediverse;
}).execErrorCast();
final serverData = await refreshServerData(domain);
knownServers[domain] = serverData;
return Result.ok(serverData.isFediverse);
}
static FutureResult<ServerData, ExecError> refreshServerData(
String domainName) async {
static Future<ServerData> refreshServerData(String domainName) async {
final uri = Uri.https(
domainName,
'/.well-known/nodeinfo',
@ -42,25 +39,24 @@ class FediverseServiceValidator {
return jsonDecode(page.data);
})
.andThenAsync(
(json) async => json is Map<String, dynamic>
? Result.ok(json)
: Result.error('Unknown response type for well-know/nodeinfo'),
(json) async =>
json is Map<String, dynamic> ? Result.ok(json) : Result.error(''),
)
.andThenSuccessAsync((json) async => json['links'] ?? [])
.andThenAsync(
(nodeInfos) async => nodeInfos.isNotEmpty
? Result.ok(nodeInfos.last)
: Result.error('Unknown response type for well-know/nodeinfo'),
: Result.error(''),
)
.andThenAsync((nodeInfo) async {
final rel = nodeInfo['rel']?.toString() ?? '';
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'] ?? '');
if (nodeInfoUrl == null) {
return Result.error('Unknown response type for well-know/nodeinfo');
return Result.error('');
}
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 {
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 {
final result = await FediverseServiceValidator.refreshServerData(domain);
expect(result.isSuccess, equals(true));
expect(result.value.isFediverse, equals(true));
expect(result.value.domainName, equals(domain));
expect(result.value.softwareName, equals(softwareName));
expect(result.isFediverse, equals(true));
expect(result.domainName, equals(domain));
expect(result.softwareName, equals(softwareName));
}