import 'package:nc_photos/entity/file.dart'; import 'package:nc_photos/entity/webdav_response_parser.dart'; import 'package:test/test.dart'; import 'package:xml/xml.dart'; void main() { group("WebdavFileParser", () { group("parseFiles", () { test("file", _files); test("file w/ 404 properties", _files404props); test("file w/ metadata", _filesMetadata); test("file w/ is-archived", _filesIsArchived); test("file w/ override-date-time", _filesOverrideDateTime); test("multiple files", _filesMultiple); test("directory", _filesDir); test("nextcloud hosted in subdir", _filesServerHostedInSubdir); }); }); } void _files() { final xml = XmlDocument.parse(""" /remote.php/dav/files/admin/Nextcloud%20intro.mp4 Fri, 01 Jan 2021 02:03:04 GMT "1324f58d4d5c8d81bed6e4ed9d5ea862" video/mp4 123 3963036 false HTTP/1.1 200 OK """); final results = WebdavResponseParser().parseFiles(xml); expect(results, [ File( path: "remote.php/dav/files/admin/Nextcloud intro.mp4", contentLength: 3963036, contentType: "video/mp4", etag: "1324f58d4d5c8d81bed6e4ed9d5ea862", lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4), hasPreview: false, fileId: 123, isCollection: false, ), ]); } void _files404props() { final xml = XmlDocument.parse(""" /remote.php/dav/files/admin/Nextcloud%20intro.mp4 Fri, 01 Jan 2021 02:03:04 GMT "1324f58d4d5c8d81bed6e4ed9d5ea862" video/mp4 123 3963036 false HTTP/1.1 200 OK HTTP/1.1 404 Not Found """); final results = WebdavResponseParser().parseFiles(xml); expect(results, [ File( path: "remote.php/dav/files/admin/Nextcloud intro.mp4", contentLength: 3963036, contentType: "video/mp4", etag: "1324f58d4d5c8d81bed6e4ed9d5ea862", lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4), hasPreview: false, fileId: 123, isCollection: false, ), ]); } void _filesMetadata() { final xml = XmlDocument.parse(""" /remote.php/dav/files/admin/Photos/Nextcloud%20community.jpg Fri, 01 Jan 2021 02:03:04 GMT "8950e39a034e369237d9285e2d815a50" image/jpeg 123 797325 true {"version":2,"lastUpdated":"2021-01-02T03:04:05.678Z","fileEtag":"8950e39a034e369237d9285e2d815a50","imageWidth":3000,"imageHeight":2000} HTTP/1.1 200 OK """); final results = WebdavResponseParser().parseFiles(xml); expect(results, [ File( path: "remote.php/dav/files/admin/Photos/Nextcloud community.jpg", contentLength: 797325, contentType: "image/jpeg", etag: "8950e39a034e369237d9285e2d815a50", lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4), hasPreview: true, fileId: 123, isCollection: false, metadata: Metadata( lastUpdated: DateTime.utc(2021, 1, 2, 3, 4, 5, 678), fileEtag: "8950e39a034e369237d9285e2d815a50", imageWidth: 3000, imageHeight: 2000, ), ), ]); } void _filesIsArchived() { final xml = XmlDocument.parse(""" /remote.php/dav/files/admin/Photos/Nextcloud%20community.jpg Fri, 01 Jan 2021 02:03:04 GMT "8950e39a034e369237d9285e2d815a50" image/jpeg 797325 true true HTTP/1.1 200 OK """); final results = WebdavResponseParser().parseFiles(xml); expect(results, [ File( path: "remote.php/dav/files/admin/Photos/Nextcloud community.jpg", contentLength: 797325, contentType: "image/jpeg", etag: "8950e39a034e369237d9285e2d815a50", lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4), hasPreview: true, isCollection: false, isArchived: true, ), ]); } void _filesOverrideDateTime() { final xml = XmlDocument.parse(""" /remote.php/dav/files/admin/Photos/Nextcloud%20community.jpg Fri, 01 Jan 2021 02:03:04 GMT "8950e39a034e369237d9285e2d815a50" image/jpeg 797325 true 2021-01-02T03:04:05.000Z HTTP/1.1 200 OK """); final results = WebdavResponseParser().parseFiles(xml); expect(results, [ File( path: "remote.php/dav/files/admin/Photos/Nextcloud community.jpg", contentLength: 797325, contentType: "image/jpeg", etag: "8950e39a034e369237d9285e2d815a50", lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4), hasPreview: true, isCollection: false, overrideDateTime: DateTime.utc(2021, 1, 2, 3, 4, 5), ), ]); } void _filesMultiple() { final xml = XmlDocument.parse(""" /remote.php/dav/files/admin/Nextcloud%20intro.mp4 Fri, 01 Jan 2021 02:03:04 GMT "1324f58d4d5c8d81bed6e4ed9d5ea862" video/mp4 123 3963036 false HTTP/1.1 200 OK /remote.php/dav/files/admin/Nextcloud.png Sat, 02 Jan 2021 03:04:05 GMT "48689d5b17c449d9db492ffe8f7ab8a6" image/png 124 50598 true {"version":2,"lastUpdated":"2021-01-02T03:04:05.678000Z","fileEtag":"48689d5b17c449d9db492ffe8f7ab8a6","imageWidth":500,"imageHeight":500} HTTP/1.1 200 OK """); final results = WebdavResponseParser().parseFiles(xml); expect(results, [ File( path: "remote.php/dav/files/admin/Nextcloud intro.mp4", contentLength: 3963036, contentType: "video/mp4", etag: "1324f58d4d5c8d81bed6e4ed9d5ea862", lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4), hasPreview: false, fileId: 123, isCollection: false, ), File( path: "remote.php/dav/files/admin/Nextcloud.png", contentLength: 50598, contentType: "image/png", etag: "48689d5b17c449d9db492ffe8f7ab8a6", lastModified: DateTime.utc(2021, 1, 2, 3, 4, 5), hasPreview: true, fileId: 124, isCollection: false, metadata: Metadata( fileEtag: "48689d5b17c449d9db492ffe8f7ab8a6", imageWidth: 500, imageHeight: 500, lastUpdated: DateTime.utc(2021, 1, 2, 3, 4, 5, 678), ), ), ]); } void _filesDir() { final xml = XmlDocument.parse(""" /remote.php/dav/files/admin/Photos/ Fri, 01 Jan 2021 02:03:04 GMT "123456789abcd" 123 false HTTP/1.1 200 OK HTTP/1.1 404 Not Found """); final results = WebdavResponseParser().parseFiles(xml); expect(results, [ File( path: "remote.php/dav/files/admin/Photos", etag: "123456789abcd", lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4), isCollection: true, hasPreview: false, fileId: 123, ), ]); } void _filesServerHostedInSubdir() { final xml = XmlDocument.parse(""" /nextcloud/remote.php/dav/files/admin/Nextcloud%20intro.mp4 Fri, 01 Jan 2021 02:03:04 GMT "1324f58d4d5c8d81bed6e4ed9d5ea862" video/mp4 123 3963036 false HTTP/1.1 200 OK """); final results = WebdavResponseParser().parseFiles(xml); expect(results, [ File( path: "remote.php/dav/files/admin/Nextcloud intro.mp4", contentLength: 3963036, contentType: "video/mp4", etag: "1324f58d4d5c8d81bed6e4ed9d5ea862", lastModified: DateTime.utc(2021, 1, 1, 2, 3, 4), hasPreview: false, fileId: 123, isCollection: false, ), ]); }