Browse Source

refactor(nextcloud): Migrate to nextcloud_test package

Signed-off-by: jld3103 <jld3103yt@gmail.com>
pull/1185/head
jld3103 1 year ago
parent
commit
2e6e7b6975
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 4
      packages/nextcloud/pubspec.yaml
  2. 4
      packages/nextcloud/pubspec_overrides.yaml
  3. 7
      packages/nextcloud/test/core_test.dart
  4. 7
      packages/nextcloud/test/dashboard_test.dart
  5. 196
      packages/nextcloud/test/helper.dart
  6. 7
      packages/nextcloud/test/news_test.dart
  7. 7
      packages/nextcloud/test/notes_test.dart
  8. 11
      packages/nextcloud/test/notifications_test.dart
  9. 7
      packages/nextcloud/test/provisioning_api_test.dart
  10. 7
      packages/nextcloud/test/settings_test.dart
  11. 9
      packages/nextcloud/test/spreed_test.dart
  12. 7
      packages/nextcloud/test/uppush_test.dart
  13. 7
      packages/nextcloud/test/user_status_test.dart
  14. 7
      packages/nextcloud/test/webdav_test.dart

4
packages/nextcloud/pubspec.yaml

@ -37,6 +37,10 @@ dev_dependencies:
git: git:
url: https://github.com/nextcloud/neon url: https://github.com/nextcloud/neon
path: packages/neon_lints path: packages/neon_lints
nextcloud_test:
git:
url: https://github.com/nextcloud/neon
path: packages/nextcloud_test
path: ^1.8.3 path: ^1.8.3
process_run: ^0.13.3 process_run: ^0.13.3
test: ^1.24.9 test: ^1.24.9

4
packages/nextcloud/pubspec_overrides.yaml

@ -1,4 +1,4 @@
# melos_managed_dependency_overrides: dynamite,dynamite_runtime,neon_lints # melos_managed_dependency_overrides: dynamite,dynamite_runtime,neon_lints,nextcloud_test
dependency_overrides: dependency_overrides:
dynamite: dynamite:
path: ../dynamite/dynamite path: ../dynamite/dynamite
@ -6,3 +6,5 @@ dependency_overrides:
path: ../dynamite/dynamite_runtime path: ../dynamite/dynamite_runtime
neon_lints: neon_lints:
path: ../neon_lints path: ../neon_lints
nextcloud_test:
path: ../nextcloud_test

7
packages/nextcloud/test/core_test.dart

@ -1,9 +1,8 @@
import 'package:nextcloud/core.dart' as core; import 'package:nextcloud/core.dart' as core;
import 'package:nextcloud/nextcloud.dart'; import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud_test/nextcloud_test.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'helper.dart';
void main() { void main() {
group( group(
'core', 'core',
@ -11,8 +10,8 @@ void main() {
late DockerContainer container; late DockerContainer container;
late NextcloudClient client; late NextcloudClient client;
setUp(() async { setUp(() async {
container = await getDockerContainer(); container = await DockerContainer.create();
client = await getTestClient(container); client = await TestNextcloudClient.create(container);
}); });
tearDown(() => container.destroy()); tearDown(() => container.destroy());

7
packages/nextcloud/test/dashboard_test.dart

@ -1,9 +1,8 @@
import 'package:nextcloud/dashboard.dart'; import 'package:nextcloud/dashboard.dart';
import 'package:nextcloud/nextcloud.dart'; import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud_test/nextcloud_test.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'helper.dart';
void main() { void main() {
group( group(
'dashboard', 'dashboard',
@ -11,8 +10,8 @@ void main() {
late DockerContainer container; late DockerContainer container;
late NextcloudClient client; late NextcloudClient client;
setUp(() async { setUp(() async {
container = await getDockerContainer(); container = await DockerContainer.create();
client = await getTestClient(container); client = await TestNextcloudClient.create(container);
}); });
tearDown(() => container.destroy()); tearDown(() => container.destroy());

196
packages/nextcloud/test/helper.dart

@ -1,196 +0,0 @@
import 'dart:async';
import 'dart:convert';
import 'dart:math';
import 'package:nextcloud/core.dart' as core;
import 'package:nextcloud/nextcloud.dart';
import 'package:process_run/cmd_run.dart';
import 'package:test/test.dart';
import 'package:universal_io/io.dart';
const retryCount = 3;
const timeout = Timeout(Duration(seconds: 30));
class DockerContainer {
DockerContainer({
required this.id,
required this.port,
});
final String id;
final int port;
Future<void> runOccCommand(final List<String> args) async {
final result = await runExecutableArguments(
'docker',
[
'exec',
id,
'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,
],
),
);
Future<String> serverLogs() async => (await runExecutableArguments(
'docker',
[
'logs',
id,
],
stdoutEncoding: utf8,
))
.stdout as String;
Future<String> nextcloudLogs() async => (await runExecutableArguments(
'docker',
[
'exec',
id,
'cat',
'data/nextcloud.log',
],
stdoutEncoding: utf8,
))
.stdout as String;
Future<String> allLogs() async => '${await serverLogs()}\n\n${await nextcloudLogs()}';
}
Future<NextcloudClient> getTestClient(
final DockerContainer container, {
final String? username = 'user1',
final AppType appType = AppType.unknown,
final String? userAgentOverride,
}) async {
String? appPassword;
if (username != null) {
final inputStream = StreamController<List<int>>();
final process = runExecutableArguments(
'docker',
[
'exec',
'-i',
container.id,
'php',
'-f',
'occ',
'user:add-app-password',
username,
],
stdin: inputStream.stream,
);
inputStream.add(utf8.encode(username));
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}');
}
appPassword = (result.stdout as String).split('\n')[1];
}
final client = NextcloudClient(
Uri(
scheme: 'http',
host: 'localhost',
port: container.port,
),
loginName: username,
password: username,
appPassword: appPassword,
appType: appType,
userAgentOverride: userAgentOverride,
cookieJar: CookieJar(),
);
var i = 0;
while (true) {
try {
await client.core.getStatus();
break;
} catch (error) {
if (error is HttpException || error is DynamiteApiException) {
i++;
await Future<void>.delayed(const Duration(milliseconds: 100));
if (i >= 300) {
throw TimeoutException('Failed to get the status of the Server. $error');
}
} else {
rethrow;
}
}
}
return client;
}
Future<DockerContainer> getDockerContainer() async {
const dockerImageName = 'ghcr.io/nextcloud/neon/dev';
var result = await runExecutableArguments(
'docker',
[
'images',
'-q',
dockerImageName,
],
);
if (result.exitCode != 0) {
throw Exception('Querying docker image failed: ${result.stderr}');
}
if (result.stdout.toString().isEmpty) {
throw Exception('Missing docker image $dockerImageName. Please build it using ./tool/build-dev-container.sh');
}
late int port;
while (true) {
port = randomPort();
result = await runExecutableArguments(
'docker',
[
'run',
'--rm',
'-d',
'--add-host',
'host.docker.internal:host-gateway',
'-p',
'$port:80',
dockerImageName,
],
);
// 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}');
}
return DockerContainer(
id: result.stdout.toString().replaceAll('\n', ''),
port: port,
);
}
int randomPort() => 1024 + Random().nextInt(65535 - 1024);

7
packages/nextcloud/test/news_test.dart

@ -2,10 +2,9 @@ import 'dart:async';
import 'package:nextcloud/news.dart' as news; import 'package:nextcloud/news.dart' as news;
import 'package:nextcloud/nextcloud.dart'; import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud_test/nextcloud_test.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'helper.dart';
void main() { void main() {
group( group(
'news', 'news',
@ -13,8 +12,8 @@ void main() {
late DockerContainer container; late DockerContainer container;
late NextcloudClient client; late NextcloudClient client;
setUp(() async { setUp(() async {
container = await getDockerContainer(); container = await DockerContainer.create();
client = await getTestClient(container); client = await TestNextcloudClient.create(container);
}); });
tearDown(() => container.destroy()); tearDown(() => container.destroy());

7
packages/nextcloud/test/notes_test.dart

@ -1,10 +1,9 @@
import 'package:nextcloud/core.dart' as core; import 'package:nextcloud/core.dart' as core;
import 'package:nextcloud/nextcloud.dart'; import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud/notes.dart' as notes; import 'package:nextcloud/notes.dart' as notes;
import 'package:nextcloud_test/nextcloud_test.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'helper.dart';
void main() { void main() {
group( group(
'notes', 'notes',
@ -12,8 +11,8 @@ void main() {
late DockerContainer container; late DockerContainer container;
late NextcloudClient client; late NextcloudClient client;
setUp(() async { setUp(() async {
container = await getDockerContainer(); container = await DockerContainer.create();
client = await getTestClient(container); client = await TestNextcloudClient.create(container);
}); });
tearDown(() => container.destroy()); tearDown(() => container.destroy());

11
packages/nextcloud/test/notifications_test.dart

@ -2,17 +2,16 @@ import 'dart:async';
import 'package:nextcloud/nextcloud.dart'; import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud/notifications.dart'; import 'package:nextcloud/notifications.dart';
import 'package:nextcloud_test/nextcloud_test.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'helper.dart';
void main() { void main() {
group('notifications', () { group('notifications', () {
late DockerContainer container; late DockerContainer container;
late NextcloudClient client; late NextcloudClient client;
setUp(() async { setUp(() async {
container = await getDockerContainer(); container = await DockerContainer.create();
client = await getTestClient( client = await TestNextcloudClient.create(
container, container,
username: 'admin', username: 'admin',
); );
@ -110,8 +109,8 @@ void main() {
late DockerContainer container; late DockerContainer container;
late NextcloudClient client; late NextcloudClient client;
setUp(() async { setUp(() async {
container = await getDockerContainer(); container = await DockerContainer.create();
client = await getTestClient( client = await TestNextcloudClient.create(
container, container,
username: 'admin', username: 'admin',
); );

7
packages/nextcloud/test/provisioning_api_test.dart

@ -1,9 +1,8 @@
import 'package:nextcloud/nextcloud.dart'; import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud/provisioning_api.dart'; import 'package:nextcloud/provisioning_api.dart';
import 'package:nextcloud_test/nextcloud_test.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'helper.dart';
void main() { void main() {
group( group(
'provisioning_api', 'provisioning_api',
@ -11,8 +10,8 @@ void main() {
late DockerContainer container; late DockerContainer container;
late NextcloudClient client; late NextcloudClient client;
setUp(() async { setUp(() async {
container = await getDockerContainer(); container = await DockerContainer.create();
client = await getTestClient( client = await TestNextcloudClient.create(
container, container,
username: 'admin', username: 'admin',
); );

7
packages/nextcloud/test/settings_test.dart

@ -2,10 +2,9 @@ import 'dart:convert';
import 'package:nextcloud/nextcloud.dart'; import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud/settings.dart'; import 'package:nextcloud/settings.dart';
import 'package:nextcloud_test/nextcloud_test.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'helper.dart';
void main() { void main() {
group( group(
'settings', 'settings',
@ -13,8 +12,8 @@ void main() {
late DockerContainer container; late DockerContainer container;
late NextcloudClient client; late NextcloudClient client;
setUp(() async { setUp(() async {
container = await getDockerContainer(); container = await DockerContainer.create();
client = await getTestClient( client = await TestNextcloudClient.create(
container, container,
username: 'admin', username: 'admin',
); );

9
packages/nextcloud/test/spreed_test.dart

@ -5,10 +5,9 @@ import 'package:built_value/json_object.dart';
import 'package:nextcloud/core.dart' as core; import 'package:nextcloud/core.dart' as core;
import 'package:nextcloud/nextcloud.dart'; import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud/spreed.dart' as spreed; import 'package:nextcloud/spreed.dart' as spreed;
import 'package:nextcloud_test/nextcloud_test.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'helper.dart';
void main() { void main() {
group( group(
'spreed', 'spreed',
@ -16,8 +15,8 @@ void main() {
late DockerContainer container; late DockerContainer container;
late NextcloudClient client1; late NextcloudClient client1;
setUp(() async { setUp(() async {
container = await getDockerContainer(); container = await DockerContainer.create();
client1 = await getTestClient(container); client1 = await TestNextcloudClient.create(container);
}); });
tearDown(() => container.destroy()); tearDown(() => container.destroy());
@ -360,7 +359,7 @@ void main() {
final room1 = (await client1.spreed.room.joinRoom(token: room.token)).body.ocs.data; final room1 = (await client1.spreed.room.joinRoom(token: room.token)).body.ocs.data;
await client1.spreed.call.joinCall(token: room.token); await client1.spreed.call.joinCall(token: room.token);
final client2 = await getTestClient( final client2 = await TestNextcloudClient.create(
container, container,
username: 'user2', username: 'user2',
); );

7
packages/nextcloud/test/uppush_test.dart

@ -1,9 +1,8 @@
import 'package:nextcloud/nextcloud.dart'; import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud/uppush.dart'; import 'package:nextcloud/uppush.dart';
import 'package:nextcloud_test/nextcloud_test.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'helper.dart';
void main() { void main() {
group( group(
'uppush', 'uppush',
@ -11,8 +10,8 @@ void main() {
late DockerContainer container; late DockerContainer container;
late NextcloudClient client; late NextcloudClient client;
setUp(() async { setUp(() async {
container = await getDockerContainer(); container = await DockerContainer.create();
client = await getTestClient( client = await TestNextcloudClient.create(
container, container,
username: 'admin', username: 'admin',
); );

7
packages/nextcloud/test/user_status_test.dart

@ -1,9 +1,8 @@
import 'package:nextcloud/nextcloud.dart'; import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud/user_status.dart' as user_status; import 'package:nextcloud/user_status.dart' as user_status;
import 'package:nextcloud_test/nextcloud_test.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'helper.dart';
void main() { void main() {
group( group(
'user_status', 'user_status',
@ -11,8 +10,8 @@ void main() {
late DockerContainer container; late DockerContainer container;
late NextcloudClient client; late NextcloudClient client;
setUp(() async { setUp(() async {
container = await getDockerContainer(); container = await DockerContainer.create();
client = await getTestClient(container); client = await TestNextcloudClient.create(container);
}); });
tearDown(() => container.destroy()); tearDown(() => container.destroy());

7
packages/nextcloud/test/webdav_test.dart

@ -3,11 +3,10 @@ import 'dart:math';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:nextcloud/nextcloud.dart'; import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud_test/nextcloud_test.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'package:universal_io/io.dart'; import 'package:universal_io/io.dart';
import 'helper.dart';
void main() { void main() {
group('constructUri', () { group('constructUri', () {
for (final values in [ for (final values in [
@ -142,8 +141,8 @@ void main() {
late NextcloudClient client; late NextcloudClient client;
setUp(() async { setUp(() async {
container = await getDockerContainer(); container = await DockerContainer.create();
client = await getTestClient(container); client = await TestNextcloudClient.create(container);
}); });
tearDown(() => container.destroy()); tearDown(() => container.destroy());

Loading…
Cancel
Save