A framework for building convergent cross-platform Nextcloud clients using Flutter.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
4.9 KiB

3 years ago
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'package:nextcloud/nextcloud.dart';
import 'package:process_run/cmd_run.dart';
import 'package:test/test.dart';
class DockerContainer {
DockerContainer({
required this.id,
required this.port,
});
final String id;
final int port;
3 years ago
Future runOccCommand(final List<String> args) async {
final result = await runExecutableArguments(
'docker',
[
'exec',
id,
3 years ago
'php',
'-f',
'occ',
...args,
],
stdout: stdout,
stderr: stderr,
);
if (result.exitCode != 0) {
throw Exception('Failed to run occ command');
}
}
void destroy() => unawaited(
runExecutableArguments(
'docker',
[
'kill',
id,
],
),
3 years ago
);
Future<String> collectLogs() async {
final serverLogs = (await runExecutableArguments(
3 years ago
'docker',
[
'logs',
id,
3 years ago
],
stdoutEncoding: utf8,
))
.stdout as String;
final nextcloudLogs = (await runExecutableArguments(
'docker',
[
'exec',
id,
3 years ago
'cat',
'data/nextcloud.log',
],
stdoutEncoding: utf8,
))
.stdout as String;
return '$serverLogs\n\n$nextcloudLogs';
3 years ago
}
}
class TestNextcloudClient extends NextcloudClient {
TestNextcloudClient(
super.baseURL, {
super.loginName,
super.username,
super.password,
super.language,
super.appType,
super.userAgentOverride,
super.cookieJar,
});
}
Future<TestNextcloudClient> getTestClient(
final DockerContainer container, {
final String? username = 'user1',
final String? password = 'user1',
final bool useAppPassword = false,
final AppType appType = AppType.unknown,
final String? userAgentOverride,
}) async {
// ignore: prefer_asserts_with_message
assert(!useAppPassword || (username != null && password != null));
3 years ago
var clientPassword = password;
if (useAppPassword) {
3 years ago
final inputStream = StreamController<List<int>>();
final process = runExecutableArguments(
'docker',
[
'exec',
'-i',
container.id,
'php',
3 years ago
'-f',
'occ',
'user:add-app-password',
username!,
3 years ago
],
stdin: inputStream.stream,
);
inputStream.add(utf8.encode(password!));
3 years ago
await inputStream.close();
final result = await process;
if (result.exitCode != 0) {
throw Exception('Failed to run generate app password command\n${result.stderr}\n${result.stdout}');
3 years ago
}
clientPassword = (result.stdout as String).split('\n')[1];
3 years ago
}
final client = TestNextcloudClient(
'http://localhost:${container.port}',
loginName: username,
username: username,
password: clientPassword,
appType: appType,
userAgentOverride: userAgentOverride,
cookieJar: CookieJar(),
);
3 years ago
while (true) {
// Test will timeout after 30s
try {
await client.core.getStatus();
break;
} catch (_) {}
}
3 years ago
return client;
}
3 years ago
Future<DockerContainer> getDockerContainer(final DockerImage image) async {
late ProcessResult result;
late int port;
while (true) {
port = randomPort();
result = await runExecutableArguments(
'docker',
[
'run',
'--rm',
'-d',
'--net',
'host',
image,
'php',
'-S',
'0.0.0.0:$port',
],
);
// 125 means the docker run command itself has failed which indicated the port is already used
if (result.exitCode != 125) {
break;
}
}
if (result.exitCode != 0) {
throw Exception('Failed to run docker container: ${result.stderr}');
}
3 years ago
return DockerContainer(
id: result.stdout.toString().replaceAll('\n', ''),
port: port,
);
}
typedef DockerImage = String;
Future<DockerImage> getDockerImage() async {
const dockerImageName = 'nextcloud-neon-dev';
3 years ago
final inputStream = StreamController<List<int>>();
final process = runExecutableArguments(
'docker',
[
'build',
'-t',
dockerImageName,
'-f',
'-',
'../../tool',
],
stdin: inputStream.stream,
);
inputStream.add(utf8.encode(File('../../tool/Dockerfile.dev').readAsStringSync()));
await inputStream.close();
final result = await process;
if (result.exitCode != 0) {
throw Exception('Failed to build docker image');
3 years ago
}
return dockerImageName;
3 years ago
}
class TestNextcloudUser {
TestNextcloudUser(
this.username,
this.password, {
3 years ago
this.displayName,
});
final String username;
final String password;
3 years ago
final String? displayName;
}
int randomPort() => 1024 + Random().nextInt(65535 - 1024);
void expectDateInReasonableTimeRange(final DateTime actual, final DateTime expected) {
const duration = Duration(seconds: 10);
expect(actual.isAfter(expected.subtract(duration)), isTrue);
expect(actual.isBefore(expected.add(duration)), isTrue);
}