nc-photos/test/use_case/unshare_album_with_user_test.dart

135 lines
5 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';
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("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 {
2021-11-23 08:45:10 +01:00
final account = util.buildAccount();
final album = (util.AlbumBuilder()
2021-11-23 08:39:35 +01:00
..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([
2021-11-23 08:45:10 +01:00
util.buildShare(id: "0", file: albumFile, shareWith: "user1"),
util.buildShare(id: "1", file: albumFile, shareWith: "user2"),
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:45:10 +01:00
[util.buildAlbumShare(userId: "user2")]);
2021-11-11 18:04:17 +01:00
expect(
shareRepo.shares,
2021-11-23 08:45:10 +01:00
[util.buildShare(id: "1", file: albumFile, shareWith: "user2")],
2021-11-11 18:04:17 +01:00
);
}
/// 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 {
2021-11-23 08:45:10 +01:00
final account = util.buildAccount();
final files =
(util.FilesBuilder(initialFileId: 1)..addJpeg("admin/test1.jpg")).build();
final album = (util.AlbumBuilder()
2021-11-23 08:39:35 +01:00
..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([
2021-11-23 08:45:10 +01:00
util.buildShare(id: "0", file: albumFile, shareWith: "user1"),
util.buildShare(id: "1", file: albumFile, shareWith: "user2"),
util.buildShare(id: "2", file: file1, shareWith: "user1"),
util.buildShare(id: "3", file: file1, shareWith: "user2"),
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:45:10 +01:00
[util.buildAlbumShare(userId: "user2")]);
2021-11-11 18:04:17 +01:00
expect(
shareRepo.shares,
[
2021-11-23 08:45:10 +01:00
util.buildShare(id: "1", file: albumFile, shareWith: "user2"),
util.buildShare(id: "3", file: file1, shareWith: "user2"),
2021-11-11 18:04:17 +01:00
],
);
}
/// 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 {
2021-11-23 08:45:10 +01:00
final account = util.buildAccount();
final files = (util.FilesBuilder(initialFileId: 1)
2021-11-23 08:39:35 +01:00
..addJpeg("admin/test1.jpg")
..addJpeg("user2/test2.jpg", ownerId: "user2"))
.build();
2021-11-23 08:45:10 +01:00
final album = (util.AlbumBuilder()
2021-11-23 08:39:35 +01:00
..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([
2021-11-23 08:45:10 +01:00
util.buildShare(id: "0", file: albumFile, shareWith: "user1"),
util.buildShare(id: "1", file: albumFile, shareWith: "user2"),
util.buildShare(id: "2", file: files[0], shareWith: "user1"),
util.buildShare(id: "3", file: files[0], shareWith: "user2"),
util.buildShare(
2021-11-23 08:39:35 +01:00
id: "4", uidOwner: "user2", file: files[1], shareWith: "admin"),
2021-11-23 08:45:10 +01:00
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:45:10 +01:00
[util.buildAlbumShare(userId: "user2")]);
2021-11-11 18:04:17 +01:00
expect(
shareRepo.shares,
[
2021-11-23 08:45:10 +01:00
util.buildShare(id: "1", file: albumFile, shareWith: "user2"),
util.buildShare(id: "3", file: files[0], shareWith: "user2"),
util.buildShare(
2021-11-23 08:39:35 +01:00
id: "4", uidOwner: "user2", file: files[1], shareWith: "admin"),
2021-11-23 08:45:10 +01:00
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
],
);
}