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.
267 lines
8.4 KiB
267 lines
8.4 KiB
2 years ago
|
import 'dart:convert';
|
||
|
import 'dart:io';
|
||
|
import 'dart:typed_data';
|
||
|
|
||
|
import 'package:nextcloud/nextcloud.dart';
|
||
|
import 'package:test/test.dart';
|
||
|
|
||
|
import 'helper.dart';
|
||
|
|
||
|
Future main() async {
|
||
2 years ago
|
await run(await getDockerImage());
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
Future run(final DockerImage image) async {
|
||
2 years ago
|
group('webdav', () {
|
||
2 years ago
|
late DockerContainer container;
|
||
2 years ago
|
late TestNextcloudClient client;
|
||
2 years ago
|
setUp(() async {
|
||
|
container = await getDockerContainer(image);
|
||
|
client = await getTestClient(container);
|
||
|
});
|
||
|
tearDown(() => container.destroy());
|
||
2 years ago
|
|
||
|
test('Fail without username', () async {
|
||
2 years ago
|
client = await getTestClient(
|
||
|
container,
|
||
|
username: null,
|
||
|
);
|
||
2 years ago
|
expect(() => client.webdav, throwsException);
|
||
|
});
|
||
|
|
||
|
test('Get status', () async {
|
||
2 years ago
|
final status = await client.webdav.status();
|
||
2 years ago
|
expect(status.capabilities, containsAll(['1', '3', 'access-control']));
|
||
|
expect(status.searchCapabilities, hasLength(0));
|
||
|
});
|
||
|
|
||
|
test('List directory', () async {
|
||
2 years ago
|
final files = await client.webdav.ls(
|
||
2 years ago
|
'/',
|
||
|
props: {
|
||
|
WebDavProps.ncHasPreview.name,
|
||
|
WebDavProps.davContentType.name,
|
||
|
WebDavProps.davLastModified.name,
|
||
|
WebDavProps.ocSize.name,
|
||
|
},
|
||
|
);
|
||
|
expect(files, hasLength(7));
|
||
|
final file = files.singleWhere((final f) => f.name == 'Nextcloud.png');
|
||
|
expect(file.hasPreview, isTrue);
|
||
|
expect(file.mimeType, 'image/png');
|
||
2 years ago
|
expectDateInReasonableTimeRange(file.lastModified!, DateTime.now());
|
||
2 years ago
|
expect(file.size!, 50598);
|
||
|
});
|
||
|
|
||
|
test('Create directory', () async {
|
||
2 years ago
|
final response = await client.webdav.mkdir('test');
|
||
2 years ago
|
expect(response.statusCode, equals(201));
|
||
|
});
|
||
|
|
||
2 years ago
|
test('Create directory recursively', () async {
|
||
|
final response = await client.webdav.mkdirs('test/bla');
|
||
|
expect(response!.statusCode, equals(201));
|
||
|
|
||
|
final files = await client.webdav.ls('/test');
|
||
|
expect(files, hasLength(1));
|
||
|
expect(files[0].path, '/test/bla/');
|
||
|
});
|
||
|
|
||
2 years ago
|
test('Upload files', () async {
|
||
|
final pngBytes = File('test/files/test.png').readAsBytesSync();
|
||
|
final txtBytes = File('test/files/test.txt').readAsBytesSync();
|
||
|
|
||
2 years ago
|
var response = await client.webdav.upload(pngBytes, 'test.png');
|
||
2 years ago
|
expect(response.statusCode, equals(201));
|
||
|
|
||
2 years ago
|
response = await client.webdav.upload(txtBytes, 'test.txt');
|
||
2 years ago
|
expect(response.statusCode, equals(201));
|
||
|
|
||
2 years ago
|
final files = await client.webdav.ls(
|
||
2 years ago
|
'/',
|
||
|
props: {
|
||
|
WebDavProps.ocSize.name,
|
||
|
},
|
||
|
);
|
||
|
expect(files, hasLength(9));
|
||
|
final pngFile = files.singleWhere((final f) => f.name == 'test.png');
|
||
|
final txtFile = files.singleWhere((final f) => f.name == 'test.txt');
|
||
|
expect(pngFile.size, pngBytes.lengthInBytes);
|
||
|
expect(txtFile.size, txtBytes.lengthInBytes);
|
||
|
});
|
||
|
|
||
|
test('Copy file', () async {
|
||
2 years ago
|
final response = await client.webdav.copy(
|
||
2 years ago
|
'Nextcloud.png',
|
||
|
'test.png',
|
||
|
);
|
||
|
expect(response.statusCode, 201);
|
||
2 years ago
|
final files = await client.webdav.ls('/');
|
||
2 years ago
|
expect(files.where((final f) => f.name == 'Nextcloud.png'), hasLength(1));
|
||
|
expect(files.where((final f) => f.name == 'test.png'), hasLength(1));
|
||
|
});
|
||
|
|
||
|
test('Copy file (overwrite fail)', () async {
|
||
2 years ago
|
await client.webdav.upload(Uint8List.fromList(utf8.encode('1')), '1.txt');
|
||
|
await client.webdav.upload(Uint8List.fromList(utf8.encode('2')), '2.txt');
|
||
2 years ago
|
|
||
|
expect(
|
||
2 years ago
|
() => client.webdav.copy('1.txt', '2.txt'),
|
||
2 years ago
|
throwsA(predicate((final e) => (e! as ApiException).statusCode == 412)),
|
||
2 years ago
|
);
|
||
|
});
|
||
|
|
||
|
test('Copy file (overwrite success)', () async {
|
||
2 years ago
|
await client.webdav.upload(Uint8List.fromList(utf8.encode('1')), '1.txt');
|
||
|
await client.webdav.upload(Uint8List.fromList(utf8.encode('2')), '2.txt');
|
||
2 years ago
|
|
||
2 years ago
|
final response = await client.webdav.copy(
|
||
2 years ago
|
'1.txt',
|
||
|
'2.txt',
|
||
|
overwrite: true,
|
||
|
);
|
||
|
expect(response.statusCode, 204);
|
||
|
});
|
||
|
|
||
|
test('Move file', () async {
|
||
2 years ago
|
final response = await client.webdav.move(
|
||
2 years ago
|
'Nextcloud.png',
|
||
|
'test.png',
|
||
|
);
|
||
|
expect(response.statusCode, 201);
|
||
2 years ago
|
final files = await client.webdav.ls('/');
|
||
2 years ago
|
expect(files.where((final f) => f.name == 'Nextcloud.png'), hasLength(0));
|
||
|
expect(files.where((final f) => f.name == 'test.png'), hasLength(1));
|
||
|
});
|
||
|
|
||
|
test('Move file (overwrite fail)', () async {
|
||
2 years ago
|
await client.webdav.upload(Uint8List.fromList(utf8.encode('1')), '1.txt');
|
||
|
await client.webdav.upload(Uint8List.fromList(utf8.encode('2')), '2.txt');
|
||
2 years ago
|
|
||
|
expect(
|
||
2 years ago
|
() => client.webdav.move('1.txt', '2.txt'),
|
||
2 years ago
|
throwsA(predicate((final e) => (e! as ApiException).statusCode == 412)),
|
||
2 years ago
|
);
|
||
|
});
|
||
|
|
||
|
test('Move file (overwrite success)', () async {
|
||
2 years ago
|
await client.webdav.upload(Uint8List.fromList(utf8.encode('1')), '1.txt');
|
||
|
await client.webdav.upload(Uint8List.fromList(utf8.encode('2')), '2.txt');
|
||
2 years ago
|
|
||
2 years ago
|
final response = await client.webdav.move(
|
||
2 years ago
|
'1.txt',
|
||
|
'2.txt',
|
||
|
overwrite: true,
|
||
|
);
|
||
|
expect(response.statusCode, 204);
|
||
|
});
|
||
|
|
||
|
test('Get file props', () async {
|
||
2 years ago
|
final file = await client.webdav.getProps(
|
||
2 years ago
|
'Nextcloud.png',
|
||
|
props: {
|
||
|
WebDavProps.ncHasPreview.name,
|
||
|
WebDavProps.davContentType.name,
|
||
|
WebDavProps.davLastModified.name,
|
||
|
WebDavProps.ocSize.name,
|
||
|
},
|
||
|
);
|
||
|
expect(file.hasPreview, isTrue);
|
||
|
expect(file.mimeType, 'image/png');
|
||
2 years ago
|
expectDateInReasonableTimeRange(file.lastModified!, DateTime.now());
|
||
2 years ago
|
expect(file.size!, 50598);
|
||
|
});
|
||
|
|
||
|
test('Get directory props', () async {
|
||
|
final data = Uint8List.fromList(utf8.encode('test'));
|
||
2 years ago
|
await client.webdav.mkdir('test');
|
||
|
await client.webdav.upload(data, 'test/test.txt');
|
||
2 years ago
|
|
||
2 years ago
|
final file = await client.webdav.getProps(
|
||
2 years ago
|
'test',
|
||
|
props: {
|
||
|
WebDavProps.davResourceType.name,
|
||
|
WebDavProps.davContentType.name,
|
||
|
WebDavProps.davLastModified.name,
|
||
|
WebDavProps.ocSize.name,
|
||
|
},
|
||
|
);
|
||
|
expect(file.isDirectory, isTrue);
|
||
|
expect(file.name, 'test');
|
||
|
expect(file.mimeType, null);
|
||
2 years ago
|
expectDateInReasonableTimeRange(file.lastModified!, DateTime.now());
|
||
2 years ago
|
expect(file.size!, data.lengthInBytes);
|
||
|
});
|
||
|
|
||
|
test('Filter files', () async {
|
||
2 years ago
|
final response = await client.webdav.upload(Uint8List.fromList(utf8.encode('test')), 'test.txt');
|
||
2 years ago
|
final id = response.headers['oc-fileid']!.first;
|
||
2 years ago
|
await client.webdav.updateProps('test.txt', {WebDavProps.ocFavorite.name: '1'});
|
||
2 years ago
|
|
||
2 years ago
|
final files = await client.webdav.filter(
|
||
2 years ago
|
'/',
|
||
|
{
|
||
|
WebDavProps.ocFavorite.name: '1',
|
||
|
},
|
||
|
props: {
|
||
|
WebDavProps.ocId.name,
|
||
|
WebDavProps.ocFavorite.name,
|
||
|
},
|
||
|
);
|
||
|
expect(files, hasLength(1));
|
||
|
final file = files.singleWhere((final e) => e.name == 'test.txt');
|
||
|
expect(file.id, id);
|
||
|
expect(file.favorite, isTrue);
|
||
|
});
|
||
|
|
||
|
test('Set properties', () async {
|
||
|
final createdDate = DateTime.utc(1971, 2);
|
||
|
final createdEpoch = createdDate.millisecondsSinceEpoch / 1000;
|
||
|
final uploadTime = DateTime.now();
|
||
|
|
||
2 years ago
|
await client.webdav.upload(Uint8List.fromList(utf8.encode('test')), 'test.txt');
|
||
2 years ago
|
|
||
2 years ago
|
final updated = await client.webdav.updateProps('test.txt', {
|
||
2 years ago
|
WebDavProps.ocFavorite.name: '1',
|
||
|
WebDavProps.ncCreationTime.name: '$createdEpoch',
|
||
|
});
|
||
|
expect(updated, isTrue);
|
||
|
|
||
2 years ago
|
final file = await client.webdav.getProps(
|
||
2 years ago
|
'test.txt',
|
||
|
props: {
|
||
|
WebDavProps.ocFavorite.name,
|
||
|
WebDavProps.ncCreationTime.name,
|
||
|
WebDavProps.ncUploadTime.name,
|
||
|
},
|
||
|
);
|
||
|
expect(file.favorite, isTrue);
|
||
|
expect(file.createdDate!.isAtSameMomentAs(createdDate), isTrue);
|
||
2 years ago
|
expectDateInReasonableTimeRange(file.uploadedDate!, uploadTime);
|
||
2 years ago
|
});
|
||
|
|
||
|
test('Set custom properties', () async {
|
||
2 years ago
|
client.webdav.registerNamespace('http://example.com/ns', 'test');
|
||
2 years ago
|
|
||
2 years ago
|
await client.webdav.upload(Uint8List.fromList(utf8.encode('test')), 'test.txt');
|
||
2 years ago
|
|
||
2 years ago
|
final updated = await client.webdav.updateProps('test.txt', {
|
||
2 years ago
|
'test:custom': 'test-custom-prop-value',
|
||
|
});
|
||
|
expect(updated, isTrue);
|
||
|
|
||
2 years ago
|
final file = await client.webdav.getProps(
|
||
2 years ago
|
'test.txt',
|
||
|
props: {
|
||
|
'test:custom',
|
||
|
},
|
||
|
);
|
||
|
|
||
|
expect(
|
||
|
file.getProp('test:custom')!.text,
|
||
|
'test-custom-prop-value',
|
||
|
);
|
||
|
});
|
||
|
});
|
||
|
}
|