nc-photos/test/use_case/unshare_album_with_user_test.dart

136 lines
5.1 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/use_case/unshare_album_with_user.dart';
import 'package:test/test.dart';
import '../mock_type.dart';
import '../test_util.dart' as test_util;
void main() {
KiwiContainer().registerInstance<EventBus>(MockEventBus());
group("UnshareAlbumWithUser", () {
test("w/o file", _unshareWithoutFile);
test("w/ file", _unshareWithFile);
test("w/ file not owned", _unshareWithFileNotOwned);
});
}
/// Unshare an empty album with a user (user1)
///
2021-11-22 10:37:28 +01:00
/// Expect: user (admin -> user1) removed from album's shares list;
2021-11-11 18:04:17 +01:00
/// share (admin -> user1) for the album json deleted
Future<void> _unshareWithoutFile() async {
final account = test_util.buildAccount();
2021-11-23 08:39:35 +01:00
final album = (test_util.AlbumBuilder()
..addShare("user1")
..addShare("user2"))
.build();
final albumFile = album.albumFile!;
final albumRepo = MockAlbumMemoryRepo([album]);
2021-11-11 18:04:17 +01:00
final fileRepo = MockFileMemoryRepo([albumFile]);
final shareRepo = MockShareMemoryRepo([
test_util.buildShare(id: "0", file: albumFile, shareWith: "user1"),
test_util.buildShare(id: "1", file: albumFile, shareWith: "user2"),
]);
await UnshareAlbumWithUser(shareRepo, fileRepo, albumRepo)(
2021-11-12 22:13:02 +01:00
account, albumRepo.findAlbumByPath(albumFile.path), "user1".toCi());
2021-11-11 18:04:17 +01:00
expect(albumRepo.findAlbumByPath(albumFile.path).shares,
2021-11-23 08:39:35 +01:00
[test_util.buildAlbumShare(userId: "user2")]);
2021-11-11 18:04:17 +01:00
expect(
shareRepo.shares,
[test_util.buildShare(id: "1", file: albumFile, shareWith: "user2")],
);
}
/// Unshare an album with a user (user1)
///
2021-11-22 10:37:28 +01:00
/// Expect: user (admin -> user1) removed from album's shares list;
2021-11-11 18:04:17 +01:00
/// share (admin -> user1) for the album json deleted;
/// shares (admin -> user1) for the album files deleted
Future<void> _unshareWithFile() async {
final account = test_util.buildAccount();
2021-11-23 08:39:35 +01:00
final files = (test_util.FilesBuilder(initialFileId: 1)
..addJpeg("admin/test1.jpg"))
.build();
final album = (test_util.AlbumBuilder()
..addFileItem(files[0])
..addShare("user1")
..addShare("user2"))
.build();
final file1 = files[0];
final albumFile = album.albumFile!;
final albumRepo = MockAlbumMemoryRepo([album]);
2021-11-11 18:04:17 +01:00
final fileRepo = MockFileMemoryRepo([albumFile, file1]);
final shareRepo = MockShareMemoryRepo([
test_util.buildShare(id: "0", file: albumFile, shareWith: "user1"),
test_util.buildShare(id: "1", file: albumFile, shareWith: "user2"),
test_util.buildShare(id: "2", file: file1, shareWith: "user1"),
test_util.buildShare(id: "3", file: file1, shareWith: "user2"),
]);
await UnshareAlbumWithUser(shareRepo, fileRepo, albumRepo)(
2021-11-12 22:13:02 +01:00
account, albumRepo.findAlbumByPath(albumFile.path), "user1".toCi());
2021-11-11 18:04:17 +01:00
expect(albumRepo.findAlbumByPath(albumFile.path).shares,
2021-11-23 08:39:35 +01:00
[test_util.buildAlbumShare(userId: "user2")]);
2021-11-11 18:04:17 +01:00
expect(
shareRepo.shares,
[
test_util.buildShare(id: "1", file: albumFile, shareWith: "user2"),
test_util.buildShare(id: "3", file: file1, shareWith: "user2"),
],
);
}
/// Unshare an album with a user (user1), where some files are not owned by us
/// (admin)
///
2021-11-22 10:37:28 +01:00
/// Expect: user (admin -> user1) removed from album's shares list;
2021-11-11 18:04:17 +01:00
/// share (admin -> user1) for the album json deleted;
/// shares (admin -> user1) for the owned album files deleted;
2021-11-22 10:37:28 +01:00
/// shares (user2 -> user1) created by other unchanged
2021-11-11 18:04:17 +01:00
Future<void> _unshareWithFileNotOwned() async {
final account = test_util.buildAccount();
2021-11-23 08:39:35 +01:00
final files = (test_util.FilesBuilder(initialFileId: 1)
..addJpeg("admin/test1.jpg")
..addJpeg("user2/test2.jpg", ownerId: "user2"))
.build();
final album = (test_util.AlbumBuilder()
..addFileItem(files[0])
..addFileItem(files[1], addedBy: "user2")
..addShare("user1")
..addShare("user2"))
.build();
final albumFile = album.albumFile!;
final albumRepo = MockAlbumMemoryRepo([album]);
final fileRepo = MockFileMemoryRepo([albumFile, files[0]]);
2021-11-11 18:04:17 +01:00
final shareRepo = MockShareMemoryRepo([
test_util.buildShare(id: "0", file: albumFile, shareWith: "user1"),
test_util.buildShare(id: "1", file: albumFile, shareWith: "user2"),
2021-11-23 08:39:35 +01:00
test_util.buildShare(id: "2", file: files[0], shareWith: "user1"),
test_util.buildShare(id: "3", file: files[0], shareWith: "user2"),
2021-11-11 18:04:17 +01:00
test_util.buildShare(
2021-11-23 08:39:35 +01:00
id: "4", uidOwner: "user2", file: files[1], shareWith: "admin"),
2021-11-11 18:04:17 +01:00
test_util.buildShare(
2021-11-23 08:39:35 +01:00
id: "5", uidOwner: "user2", file: files[1], shareWith: "user1"),
2021-11-11 18:04:17 +01:00
]);
await UnshareAlbumWithUser(shareRepo, fileRepo, albumRepo)(
2021-11-12 22:13:02 +01:00
account, albumRepo.findAlbumByPath(albumFile.path), "user1".toCi());
2021-11-11 18:04:17 +01:00
expect(albumRepo.findAlbumByPath(albumFile.path).shares,
2021-11-23 08:39:35 +01:00
[test_util.buildAlbumShare(userId: "user2")]);
2021-11-11 18:04:17 +01:00
expect(
shareRepo.shares,
[
test_util.buildShare(id: "1", file: albumFile, shareWith: "user2"),
2021-11-23 08:39:35 +01:00
test_util.buildShare(id: "3", file: files[0], shareWith: "user2"),
2021-11-11 18:04:17 +01:00
test_util.buildShare(
2021-11-23 08:39:35 +01:00
id: "4", uidOwner: "user2", file: files[1], shareWith: "admin"),
2021-11-11 18:04:17 +01:00
test_util.buildShare(
2021-11-23 08:39:35 +01:00
id: "5", uidOwner: "user2", file: files[1], shareWith: "user1"),
2021-11-11 18:04:17 +01:00
],
);
}