nc-photos/test/use_case/share_album_with_user_test.dart

173 lines
5.3 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-24 09:36:16 +01:00
import 'package:nc_photos/or_null.dart';
2021-11-11 18:04:17 +01:00
import 'package:nc_photos/use_case/share_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("ShareAlbumWithUser", () {
test("w/o file", _shareWithoutFile);
test("w/ file", _shareWithFile);
2021-11-19 18:03:53 +01:00
test("w/ file owned by user", _shareWithFileOwnedByUser);
2021-11-11 18:04:17 +01:00
test("shared album", _shareSharedAlbum);
});
}
/// Share an empty album with a user (user1)
///
/// Expect: share (admin -> user1) added to album's shares list;
/// a new share (admin -> user1) is created for the album json
Future<void> _shareWithoutFile() async {
2021-11-23 08:45:10 +01:00
final account = util.buildAccount();
final album = util.AlbumBuilder().build();
2021-11-23 08:39:35 +01:00
final albumFile = album.albumFile!;
final albumRepo = MockAlbumMemoryRepo([album]);
2021-11-11 18:04:17 +01:00
final shareRepo = MockShareMemoryRepo();
await ShareAlbumWithUser(shareRepo, albumRepo)(
account,
albumRepo.findAlbumByPath(albumFile.path),
2021-11-23 08:45:10 +01:00
util.buildSharee(shareWith: "user1".toCi()),
2021-11-11 18:04:17 +01:00
);
expect(
2021-11-24 09:36:16 +01:00
albumRepo
.findAlbumByPath(albumFile.path)
.shares
?.map((s) => s.copyWith(
// we need to set a known value to sharedAt
sharedAt: OrNull(DateTime.utc(2020, 1, 2, 3, 4, 5)),
))
.toList(),
2021-11-23 08:45:10 +01:00
[util.buildAlbumShare(userId: "user1")],
2021-11-11 18:04:17 +01:00
);
expect(
shareRepo.shares,
2021-11-23 08:45:10 +01:00
[util.buildShare(id: "0", file: albumFile, shareWith: "user1")],
2021-11-11 18:04:17 +01:00
);
}
/// Share an album with files to a user (user1)
///
/// Expect: share (admin -> user1) added to album's shares list;
/// new shares (admin -> user1) are created for the album json and the album
/// files
Future<void> _shareWithFile() 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()..addFileItem(files[0])).build();
2021-11-23 08:39:35 +01:00
final file1 = files[0];
final albumFile = album.albumFile!;
final albumRepo = MockAlbumMemoryRepo([album]);
2021-11-11 18:04:17 +01:00
final shareRepo = MockShareMemoryRepo();
await ShareAlbumWithUser(shareRepo, albumRepo)(
account,
albumRepo.findAlbumByPath(albumFile.path),
2021-11-23 08:45:10 +01:00
util.buildSharee(shareWith: "user1".toCi()),
2021-11-11 18:04:17 +01:00
);
expect(
2021-11-24 09:36:16 +01:00
albumRepo
.findAlbumByPath(albumFile.path)
.shares
?.map((s) => s.copyWith(
// we need to set a known value to sharedAt
sharedAt: OrNull(DateTime.utc(2020, 1, 2, 3, 4, 5)),
))
.toList(),
2021-11-23 08:45:10 +01:00
[util.buildAlbumShare(userId: "user1")],
2021-11-11 18:04:17 +01:00
);
expect(
shareRepo.shares,
[
2021-11-23 08:45:10 +01:00
util.buildShare(id: "0", file: albumFile, shareWith: "user1"),
util.buildShare(id: "1", file: file1, shareWith: "user1"),
2021-11-11 18:04:17 +01:00
],
);
}
2021-11-19 18:03:53 +01:00
/// Share an album with files owned by user (user1) to that user (user1)
///
/// Expect: share (admin -> user1) added to album's shares list;
/// new shares (admin -> user1) are created for the album json
Future<void> _shareWithFileOwnedByUser() 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", ownerId: "user1"))
.build();
2021-11-23 08:45:10 +01:00
final album = (util.AlbumBuilder()..addFileItem(files[0])).build();
2021-11-23 08:39:35 +01:00
final albumFile = album.albumFile!;
final albumRepo = MockAlbumMemoryRepo([album]);
2021-11-19 18:03:53 +01:00
final shareRepo = MockShareMemoryRepo();
await ShareAlbumWithUser(shareRepo, albumRepo)(
account,
albumRepo.findAlbumByPath(albumFile.path),
2021-11-23 08:45:10 +01:00
util.buildSharee(shareWith: "user1".toCi()),
2021-11-19 18:03:53 +01:00
);
expect(
2021-11-24 09:36:16 +01:00
albumRepo
.findAlbumByPath(albumFile.path)
.shares
?.map((s) => s.copyWith(
// we need to set a known value to sharedAt
sharedAt: OrNull(DateTime.utc(2020, 1, 2, 3, 4, 5)),
))
.toList(),
2021-11-23 08:45:10 +01:00
[util.buildAlbumShare(userId: "user1")],
2021-11-19 18:03:53 +01:00
);
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
/// Share a shared album (admin -> user1) with a user (user2)
///
/// Expect: share (admin -> user2) added to album's shares list;
/// a new share (admin -> user2) is created for the album json
Future<void> _shareSharedAlbum() async {
2021-11-23 08:45:10 +01:00
final account = util.buildAccount();
final album = (util.AlbumBuilder()..addShare("user1")).build();
2021-11-23 08:39:35 +01:00
final albumFile = album.albumFile!;
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 ShareAlbumWithUser(shareRepo, albumRepo)(
account,
albumRepo.findAlbumByPath(albumFile.path),
2021-11-23 08:45:10 +01:00
util.buildSharee(shareWith: "user2".toCi()),
2021-11-11 18:04:17 +01:00
);
expect(
2021-11-24 09:36:16 +01:00
albumRepo
.findAlbumByPath(albumFile.path)
.shares
?.map((s) => s.copyWith(
// we need to set a known value to sharedAt
sharedAt: OrNull(DateTime.utc(2020, 1, 2, 3, 4, 5)),
))
.toList(),
2021-11-11 18:04:17 +01:00
[
2021-11-23 08:45:10 +01:00
util.buildAlbumShare(userId: "user1"),
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: "0", file: albumFile, shareWith: "user1"),
util.buildShare(id: "1", file: albumFile, shareWith: "user2")
2021-11-11 18:04:17 +01:00
],
);
}