nc-photos/test/use_case/add_to_album_test.dart

242 lines
7.7 KiB
Dart
Raw Normal View History

2021-11-11 18:04:17 +01:00
import 'package:event_bus/event_bus.dart';
import 'package:idb_shim/idb.dart';
import 'package:kiwi/kiwi.dart';
import 'package:nc_photos/app_db.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);
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();
await appDb.use((db) async {
final transaction = db.transaction(AppDb.fileDbStoreName, idbModeReadWrite);
final store = transaction.objectStore(AppDb.fileDbStoreName);
await store.put(AppDbFileDbEntry.fromFile(account, file).toJson(),
AppDbFileDbEntry.toPrimaryKey(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,
),
],
);
}
/// 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();
await appDb.use((db) async {
final transaction = db.transaction(AppDb.fileDbStoreName, idbModeReadWrite);
final store = transaction.objectStore(AppDb.fileDbStoreName);
await store.put(AppDbFileDbEntry.fromFile(account, file).toJson(),
AppDbFileDbEntry.toPrimaryKey(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();
await appDb.use((db) async {
final transaction = db.transaction(AppDb.fileDbStoreName, idbModeReadWrite);
final store = transaction.objectStore(AppDb.fileDbStoreName);
await store.put(AppDbFileDbEntry.fromFile(account, file).toJson(),
AppDbFileDbEntry.toPrimaryKey(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();
await appDb.use((db) async {
final transaction = db.transaction(AppDb.fileDbStoreName, idbModeReadWrite);
final store = transaction.objectStore(AppDb.fileDbStoreName);
await store.put(AppDbFileDbEntry.fromFile(account, file).toJson(),
AppDbFileDbEntry.toPrimaryKey(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-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"),
]);
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
],
);
}