nc-photos/test/use_case/add_to_album_test.dart

298 lines
8.9 KiB
Dart
Raw Normal View History

2021-11-11 18:04:17 +01:00
import 'package:event_bus/event_bus.dart';
import 'package:kiwi/kiwi.dart';
2021-11-12 22:13:02 +01:00
import 'package:nc_photos/ci_string.dart';
2021-11-11 18:04:17 +01:00
import 'package:nc_photos/entity/album.dart';
import 'package:nc_photos/entity/album/cover_provider.dart';
import 'package:nc_photos/entity/album/item.dart';
import 'package:nc_photos/entity/album/provider.dart';
import 'package:nc_photos/entity/album/sort_provider.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/or_null.dart';
import 'package:nc_photos/pref.dart';
import 'package:nc_photos/use_case/add_to_album.dart';
import 'package:test/test.dart';
import '../mock_type.dart';
2021-11-23 08:45:10 +01:00
import '../test_util.dart' as util;
2021-11-11 18:04:17 +01:00
void main() {
KiwiContainer().registerInstance<EventBus>(MockEventBus());
group("AddToAlbum", () {
test("file", _addFile);
2021-11-24 09:36:16 +01:00
test("ignore existing file", _addExistingFile);
2021-11-11 18:04:17 +01:00
group("shared album (owned)", () {
test("file", _addFileToSharedAlbumOwned);
2021-11-19 18:03:53 +01:00
test("file owned by user", _addFileOwnedByUserToSharedAlbumOwned);
2021-11-11 18:04:17 +01:00
});
group("shared album (not owned)", () {
test("file", _addFileToMultiuserSharedAlbumNotOwned);
});
});
}
/// Add a [File] to an [Album]
///
/// Expect: file added to album
Future<void> _addFile() async {
2021-11-23 08:45:10 +01:00
final account = util.buildAccount();
2021-11-11 18:04:17 +01:00
final pref = Pref.scoped(PrefMemoryProvider());
2021-11-23 08:45:10 +01:00
final file = (util.FilesBuilder(initialFileId: 1)..addJpeg("admin/test1.jpg"))
2021-11-23 08:39:35 +01:00
.build()[0];
2021-11-23 08:45:10 +01:00
final album = util.AlbumBuilder().build();
2021-11-23 08:39:35 +01:00
final albumFile = album.albumFile!;
2021-11-11 18:04:17 +01:00
final appDb = MockAppDb();
2021-12-02 07:04:11 +01:00
await util.fillAppDb(appDb, account, [file]);
2021-11-23 08:39:35 +01:00
final albumRepo = MockAlbumMemoryRepo([album]);
2021-11-11 18:04:17 +01:00
final shareRepo = MockShareRepo();
await AddToAlbum(albumRepo, shareRepo, appDb, pref)(
account,
albumRepo.findAlbumByPath(albumFile.path),
[
AlbumFileItem(
2021-11-12 22:13:02 +01:00
addedBy: "admin".toCi(),
2021-11-11 18:04:17 +01:00
addedAt: DateTime.utc(2020, 1, 2, 3, 4, 5),
file: file,
),
],
);
expect(
albumRepo.albums
.map((e) => e.copyWith(
// we need to set a known value to lastUpdated
lastUpdated: OrNull(DateTime.utc(2020, 1, 2, 3, 4, 5)),
))
.toList(),
[
Album(
lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5),
name: "test",
provider: AlbumStaticProvider(
items: [
AlbumFileItem(
2021-11-12 22:13:02 +01:00
addedBy: "admin".toCi(),
2021-11-11 18:04:17 +01:00
addedAt: DateTime.utc(2020, 1, 2, 3, 4, 5),
file: file,
),
],
latestItemTime: DateTime.utc(2020, 1, 2, 3, 4, 5),
),
coverProvider: AlbumAutoCoverProvider(
coverFile: file,
),
sortProvider: const AlbumNullSortProvider(),
albumFile: albumFile,
),
],
);
}
2021-11-24 09:36:16 +01:00
/// Add a [File], already included in the [Album], to an [Album]
///
/// Expect: file not added to album
Future<void> _addExistingFile() async {
final account = util.buildAccount();
final pref = Pref.scoped(PrefMemoryProvider());
final files =
(util.FilesBuilder(initialFileId: 1)..addJpeg("admin/test1.jpg")).build();
final album = (util.AlbumBuilder()..addFileItem(files[0])).build();
final oldFile = files[0];
final newFile = files[0].copyWith();
final albumFile = album.albumFile!;
final appDb = MockAppDb();
2021-12-02 07:04:11 +01:00
await util.fillAppDb(appDb, account, files);
2021-11-24 09:36:16 +01:00
final albumRepo = MockAlbumMemoryRepo([album]);
final shareRepo = MockShareRepo();
await AddToAlbum(albumRepo, shareRepo, appDb, pref)(
account,
albumRepo.findAlbumByPath(albumFile.path),
[
AlbumFileItem(
addedBy: "admin".toCi(),
addedAt: DateTime.utc(2020, 1, 2, 3, 4, 5),
file: newFile,
),
],
);
expect(
albumRepo.albums
.map((e) => e.copyWith(
// we need to set a known value to lastUpdated
lastUpdated: OrNull(DateTime.utc(2020, 1, 2, 3, 4, 5)),
))
.toList(),
[
Album(
lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5),
name: "test",
provider: AlbumStaticProvider(
items: [
AlbumFileItem(
addedBy: "admin".toCi(),
addedAt: DateTime.utc(2020, 1, 2, 3, 4, 5),
file: files[0],
),
],
latestItemTime: DateTime.utc(2020, 1, 2, 3, 4, 5),
),
coverProvider: AlbumAutoCoverProvider(coverFile: files[0]),
sortProvider: const AlbumNullSortProvider(),
albumFile: albumFile,
),
],
);
// when there's a conflict, it's guaranteed that the original file in the
// album is kept and the incoming file dropped
expect(
identical(
AlbumStaticProvider.of(albumRepo.albums[0])
.items
.whereType<AlbumFileItem>()
.first
.file,
oldFile),
true);
expect(
identical(
AlbumStaticProvider.of(albumRepo.albums[0])
.items
.whereType<AlbumFileItem>()
.first
.file,
newFile),
false);
}
2021-11-11 18:04:17 +01:00
/// Add a file to a shared album (admin -> user1)
///
/// Expect: a new share (admin -> user1) is created for the file
Future<void> _addFileToSharedAlbumOwned() async {
2021-11-23 08:45:10 +01:00
final account = util.buildAccount();
2021-11-11 18:04:17 +01:00
final pref = Pref.scoped(PrefMemoryProvider({
"isLabEnableSharedAlbum": true,
}));
2021-11-23 08:45:10 +01:00
final file = (util.FilesBuilder(initialFileId: 1)..addJpeg("admin/test1.jpg"))
2021-11-23 08:39:35 +01:00
.build()[0];
2021-11-23 08:45:10 +01:00
final album = (util.AlbumBuilder()..addShare("user1")).build();
2021-11-23 08:39:35 +01:00
final albumFile = album.albumFile!;
2021-11-11 18:04:17 +01:00
final appDb = MockAppDb();
2021-12-02 07:04:11 +01:00
await util.fillAppDb(appDb, account, [file]);
2021-11-23 08:39:35 +01:00
final albumRepo = MockAlbumMemoryRepo([album]);
2021-11-11 18:04:17 +01:00
final shareRepo = MockShareMemoryRepo([
2021-11-23 08:45:10 +01:00
util.buildShare(id: "0", file: albumFile, shareWith: "user1"),
2021-11-11 18:04:17 +01:00
]);
await AddToAlbum(albumRepo, shareRepo, appDb, pref)(
account,
albumRepo.findAlbumByPath(albumFile.path),
[
AlbumFileItem(
2021-11-12 22:13:02 +01:00
addedBy: "admin".toCi(),
2021-11-11 18:04:17 +01:00
addedAt: DateTime.utc(2020, 1, 2, 3, 4, 5),
file: file,
),
],
);
expect(
shareRepo.shares,
[
2021-11-23 08:45:10 +01:00
util.buildShare(id: "0", file: albumFile, shareWith: "user1"),
util.buildShare(id: "1", file: file, shareWith: "user1"),
2021-11-11 18:04:17 +01:00
],
);
}
2021-11-19 18:03:53 +01:00
/// Add a file owned by user (user1) to a shared album (admin -> user1)
///
/// Expect: no shares created
Future<void> _addFileOwnedByUserToSharedAlbumOwned() async {
2021-11-23 08:45:10 +01:00
final account = util.buildAccount();
2021-11-19 18:03:53 +01:00
final pref = Pref.scoped(PrefMemoryProvider({
"isLabEnableSharedAlbum": true,
}));
2021-11-23 08:45:10 +01:00
final file = (util.FilesBuilder(initialFileId: 1)
2021-11-23 08:39:35 +01:00
..addJpeg("admin/test1.jpg", ownerId: "user1"))
.build()[0];
2021-11-23 08:45:10 +01:00
final album = (util.AlbumBuilder()..addShare("user1")).build();
2021-11-23 08:39:35 +01:00
final albumFile = album.albumFile!;
2021-11-19 18:03:53 +01:00
final appDb = MockAppDb();
2021-12-02 07:04:11 +01:00
await util.fillAppDb(appDb, account, [file]);
2021-11-23 08:39:35 +01:00
final albumRepo = MockAlbumMemoryRepo([album]);
2021-11-19 18:03:53 +01:00
final shareRepo = MockShareMemoryRepo([
2021-11-23 08:45:10 +01:00
util.buildShare(id: "0", file: albumFile, shareWith: "user1"),
2021-11-19 18:03:53 +01:00
]);
await AddToAlbum(albumRepo, shareRepo, appDb, pref)(
account,
albumRepo.findAlbumByPath(albumFile.path),
[
AlbumFileItem(
addedBy: "admin".toCi(),
addedAt: DateTime.utc(2020, 1, 2, 3, 4, 5),
file: file,
),
],
);
expect(
shareRepo.shares,
[
2021-11-23 08:45:10 +01:00
util.buildShare(id: "0", file: albumFile, shareWith: "user1"),
2021-11-19 18:03:53 +01:00
],
);
}
2021-11-11 18:04:17 +01:00
/// Add a file to a shared album (user1 -> admin, user2)
///
/// Expect: a new share (admin -> user1, user2) is created for the file
Future<void> _addFileToMultiuserSharedAlbumNotOwned() async {
// doesn't work right now, skipped
2021-11-23 08:45:10 +01:00
final account = util.buildAccount();
2021-11-11 18:04:17 +01:00
final pref = Pref.scoped(PrefMemoryProvider({
"isLabEnableSharedAlbum": true,
}));
2021-11-23 08:45:10 +01:00
final file = (util.FilesBuilder(initialFileId: 1)..addJpeg("admin/test1.jpg"))
2021-11-23 08:39:35 +01:00
.build()[0];
2021-11-23 08:45:10 +01:00
final album = (util.AlbumBuilder(ownerId: "user1")
2021-11-23 08:39:35 +01:00
..addShare("admin")
..addShare("user2"))
.build();
final albumFile = album.albumFile!;
2021-11-11 18:04:17 +01:00
final appDb = MockAppDb();
2021-12-02 07:04:11 +01:00
await util.fillAppDb(appDb, account, [file]);
2021-11-23 08:39:35 +01:00
final albumRepo = MockAlbumMemoryRepo([album]);
2021-11-11 18:04:17 +01:00
final shareRepo = MockShareMemoryRepo([
2021-11-23 08:45:10 +01:00
util.buildShare(
2021-12-02 07:04:11 +01:00
id: "0", file: albumFile, uidOwner: "user1", shareWith: "admin"),
2021-11-23 08:45:10 +01:00
util.buildShare(
2021-12-02 07:04:11 +01:00
id: "1", file: albumFile, uidOwner: "user1", shareWith: "user2"),
2021-11-11 18:04:17 +01:00
]);
await AddToAlbum(albumRepo, shareRepo, appDb, pref)(
account,
albumRepo.findAlbumByPath(albumFile.path),
[
AlbumFileItem(
2021-11-12 22:13:02 +01:00
addedBy: "admin".toCi(),
2021-11-11 18:04:17 +01:00
addedAt: DateTime.utc(2020, 1, 2, 3, 4, 5),
file: file,
),
],
);
expect(
shareRepo.shares,
[
2021-11-23 08:45:10 +01:00
util.buildShare(
2021-11-11 18:04:17 +01:00
id: "0", uidOwner: "user1", file: albumFile, shareWith: "admin"),
2021-11-23 08:45:10 +01:00
util.buildShare(
2021-11-11 18:04:17 +01:00
id: "1", uidOwner: "user1", file: albumFile, shareWith: "user2"),
// the order for these two shares are actually NOT guaranteed
2021-11-23 08:45:10 +01:00
util.buildShare(id: "2", file: file, shareWith: "user2"),
util.buildShare(id: "3", file: file, shareWith: "user1"),
2021-11-11 18:04:17 +01:00
],
);
}