public void File_remove_removes_file_and_publishes_event()
        {
            // Arrange
            FakeFileStorage    fileStorage    = new FakeFileStorage();
            FakeFileRepository fileRepository = new FakeFileRepository();
            FakeEventPublisher eventPublisher = new FakeEventPublisher();
            FakeCurrentUser    currentUser    = new FakeCurrentUser();

            File file = new ExampleFileFactory().CreateFile();

            fileRepository.Save(file);
            fileStorage.SaveFile(file, Stream.Null);
            currentUser.Id = file.Owner.Id;

            var commandHandler = new DeleteFileCommandHandler(fileStorage, currentUser, fileRepository, eventPublisher);

            var command = new DeleteFileCommand("fileId");

            // Act
            commandHandler.Handle(command);

            // Assert
            Assert.IsNull(fileRepository.GetById("fileId"));
            Assert.False(fileStorage.Exists(file));

            File publishedEvent = eventPublisher.VerifyPublishedOnce <File>();

            Assert.AreEqual(file, publishedEvent);
        }
        public void When_file_unlocked_then_lock_owner_is_null_and_proper_event_published()
        {
            // Arrange
            FakeFileLockingService fileLockingService = new FakeFileLockingService();
            FakeFileRepository     fileRepository     = new FakeFileRepository();
            FakeEventPublisher     eventPublisher     = new FakeEventPublisher();
            FakeCurrentUser        currentUser        = new FakeCurrentUser();

            File file = new ExampleFileFactory().CreateFile();

            fileRepository.Save(file);
            currentUser.Id = "currentUserId";
            fileLockingService.Lock(file, new User(currentUser.Id, currentUser.Username));

            var commandHandler = new RemoveFileLockCommandHandler(
                fileLockingService, fileRepository, currentUser, eventPublisher);

            var command = new RemoveFileLockCommand("fileId");

            // Act
            commandHandler.Handle(command);

            // Assert
            Assert.IsFalse(fileLockingService.IsLocked(file));
            Assert.IsNull(fileLockingService.GetLockOwner(file));

            var publishedEvent = eventPublisher.VerifyPublishedOnce <FileLockChangedMessage>();

            Assert.IsFalse(publishedEvent.NewLock.IsLockPresent);
        }
        public void When_file_locked_by_its_owner_then_lock_owner_is_correct_and_proper_event_published()
        {
            // Arrange
            FakeFileLockingService fileLockingService = new FakeFileLockingService();
            FakeFileRepository     fileRepository     = new FakeFileRepository();
            FakeEventPublisher     eventPublisher     = new FakeEventPublisher();
            FakeCurrentUserSource  currentUserSource  = new FakeCurrentUserSource();

            File file = new ExampleFileFactory().CreateFile();

            fileRepository.Save(file);
            currentUserSource.CurrentUser = file.Owner;

            var commandHandler = new AcquireFileLockCommandHandler(
                fileLockingService, fileRepository, eventPublisher, currentUserSource);

            var command = new AcquireFileLockCommand("fileId");

            // Act
            commandHandler.Handle(command);

            // Assert
            Assert.AreEqual(file.Owner.Id, fileLockingService.GetLockOwner(file).Id);

            FileLockChangedMessage publishedEvent = eventPublisher.VerifyPublishedOnce <FileLockChangedMessage>();

            Assert.AreEqual("fileId", publishedEvent.FileId);
        }
        public void File_can_be_locked_by_authorized_user()
        {
            // Arrange
            FakeFileLockingService fileLockingService = new FakeFileLockingService();
            FakeFileRepository     fileRepository     = new FakeFileRepository();
            FakeEventPublisher     eventPublisher     = new FakeEventPublisher();
            FakeCurrentUserSource  currentUserSource  = new FakeCurrentUserSource();

            File file      = new ExampleFileFactory().CreateFile();
            User otherUser = new User("someRandomUserId", "someRandomUser");

            file.ShareWith(otherUser);

            fileRepository.Save(file);
            currentUserSource.CurrentUser = otherUser;

            var commandHandler = new AcquireFileLockCommandHandler(
                fileLockingService, fileRepository, eventPublisher, currentUserSource);

            var command = new AcquireFileLockCommand("fileId");

            // Act
            commandHandler.Handle(command);

            // Assert
            Assert.AreEqual(otherUser.Id, fileLockingService.GetLockOwner(file).Id);

            FileLockChangedMessage publishedEvent = eventPublisher.VerifyPublishedOnce <FileLockChangedMessage>();

            Assert.AreEqual("fileId", publishedEvent.FileId);
            Assert.AreEqual(otherUser.Id, publishedEvent.NewLock.LockOwner.Id);
        }
        public void File_edit_changes_filename_and_description_and_publishes_proper_event()
        {
            // Arrange
            FakeFileRepository fileRepository = new FakeFileRepository();
            FakeEventPublisher eventPublisher = new FakeEventPublisher();
            FakeCurrentUser    currentUser    = new FakeCurrentUser();

            File file = new ExampleFileFactory().CreateFile();

            fileRepository.Save(file);
            currentUser.Id = file.Owner.Id;

            var commandHandler = new EditFileCommandHandler(fileRepository, currentUser, eventPublisher);

            var command = new EditFileCommand("fileId", "newFileName", "newFileDescription");

            // Act
            commandHandler.Handle(command);

            // Assert
            Assert.AreEqual("newFileName", file.FileName);
            Assert.AreEqual("newFileDescription", file.Description);

            File publishedEvent = eventPublisher.VerifyPublishedOnce <File>();

            Assert.AreEqual(file, publishedEvent);
        }
        public void File_can_be_shared_and_allows_for_modifications_by_authorized_user()
        {
            // Arrange
            FakeFileRepository fileRepository = new FakeFileRepository();
            FakeUserRepository userRepository = new FakeUserRepository();
            FakeEventPublisher eventPublisher = new FakeEventPublisher();
            FakeCurrentUser    currentUser    = new FakeCurrentUser();

            File file      = new ExampleFileFactory().CreateFile();
            User shareWith = new User("shareWithUserId", "username");

            fileRepository.Save(file);
            userRepository.Save(shareWith);
            currentUser.Id = file.Owner.Id;

            var commandHandler = new ShareFileCommandHandler(
                currentUser, fileRepository, userRepository, eventPublisher);

            var command = new ShareFileCommand("fileId", shareWith.Id);

            // Act
            commandHandler.Handle(command);

            // Assert
            Assert.IsTrue(file.CanBeModifiedBy(shareWith));
            FileSharesChangedMessage publishedEvent = eventPublisher.VerifyPublishedOnce <FileSharesChangedMessage>();

            Assert.AreEqual(file, publishedEvent.File);
        }
Exemple #7
0
        public void File_cannot_be_unshared_with_its_owner()
        {
            // Arrange
            File file = new ExampleFileFactory().CreateFile();

            //Assert
            Assert.Throws <NotFoundException>(() => file.Unshare(file.Owner));
        }
Exemple #8
0
        public void File_cannot_be_shared_with_its_owner()
        {
            // Arrange
            File file = new ExampleFileFactory().CreateFile();

            //Assert
            Assert.Throws <PermissionException>(() => file.ShareWith(file.Owner));
        }
Exemple #9
0
        public void File_cannot_be_unshared_when_not_shared()
        {
            // Arrange
            File file           = new ExampleFileFactory().CreateFile();
            var  someRandomUser = new User("f10b5f27-5470-49aa-af83-754dfffa7c2c", "otherUser");

            //Assert
            Assert.Throws <NotFoundException>(() => file.Unshare(someRandomUser));
        }
Exemple #10
0
        public void File_IsOwnedBy_returns_true_for_owner()
        {
            // Arrange
            File file = new ExampleFileFactory().CreateFile();

            // Act
            bool isOwnedBy = file.IsOwnedBy(file.Owner);

            // Assert
            Assert.True(isOwnedBy);
        }
Exemple #11
0
        public void File_IsOwnedBy_returns_false_for_user_with_different_id_than_owner()
        {
            // Arrange
            File file     = new ExampleFileFactory().CreateFile();
            var  notOwner = new User("f10b5f27-5470-49aa-af83-754dfffa7c2c", "otherUser");

            // Act
            bool isOwnedBy = file.IsOwnedBy(notOwner);

            // Assert
            Assert.False(isOwnedBy);
        }
Exemple #12
0
        public void File_cannnot_be_modified_by_random_user()
        {
            // Arrange
            File file           = new ExampleFileFactory().CreateFile();
            var  someRandomUser = new User("f10b5f27-5470-49aa-af83-754dfffa7c2c", "otherUser");

            // Act
            bool canBeModifiedBySomeRandom = file.CanBeModifiedBy(someRandomUser);

            // Assert
            Assert.False(canBeModifiedBySomeRandom);
        }
Exemple #13
0
        public void When_content_changed_file_modification_date_changes()
        {
            // Arrange
            File     file         = new ExampleFileFactory().CreateFile();
            DateTime dateModified = file.DateCreated + TimeSpan.FromHours(1);

            // Act
            file.ContentChanged(dateModified);

            // Assert
            Assert.AreEqual(dateModified, file.DateModified);
        }
Exemple #14
0
        public void File_can_be_modified_by_owner()
        {
            // Arrange
            File file      = new ExampleFileFactory().CreateFile();
            User fileOwner = file.Owner;

            // Act
            bool canBeModifiedByOwner = file.CanBeModifiedBy(fileOwner);

            // Assert
            Assert.True(canBeModifiedByOwner);
        }
Exemple #15
0
        public void File_IsOwnedBy_returns_true_for_user_with_same_id_as_owner()
        {
            // Arrange
            File file      = new ExampleFileFactory().CreateFile();
            User fileOwner = file.Owner;
            var  user      = new User(fileOwner.Id, fileOwner.Username);

            // Act
            bool isOwnedBy = file.IsOwnedBy(user);

            // Assert
            Assert.True(isOwnedBy);
        }
Exemple #16
0
        public void When_edited_file_modification_date_changes()
        {
            // Arrange
            File         file           = new ExampleFileFactory().CreateFile();
            const string newFileName    = "otherName";
            const string newDescription = "otherDescription";
            DateTime     dateModified   = file.DateCreated + TimeSpan.FromHours(1);

            // Act
            file.Edit(newFileName, newDescription, dateModified);

            // Assert
            Assert.AreEqual(dateModified, file.DateModified);
        }
Exemple #17
0
        public void When_edited_file_name_and_description_change()
        {
            // Arrange
            File         file           = new ExampleFileFactory().CreateFile();
            const string newFileName    = "otherName";
            const string newDescription = "otherDescription";

            // Act
            file.Edit(newFileName, newDescription, file.DateCreated + TimeSpan.FromHours(1));

            // Assert
            Assert.AreEqual(newFileName, file.FileName);
            Assert.AreEqual(newDescription, file.Description);
        }
        public void File_can_only_be_edited_by_owner()
        {
            // Arrange
            FakeFileRepository fileRepository = new FakeFileRepository();
            FakeEventPublisher eventPublisher = new FakeEventPublisher();
            FakeCurrentUser    currentUser    = new FakeCurrentUser();

            File file = new ExampleFileFactory().CreateFile();

            fileRepository.Save(file);
            currentUser.Id = "otherUserId";

            var commandHandler = new EditFileCommandHandler(fileRepository, currentUser, eventPublisher);

            var command = new EditFileCommand("fileId", "newFileName", "newFileDescription");

            // Assert & Act
            Assert.Throws <PermissionException>(() => commandHandler.Handle(command));
        }
        public void File_can_be_removed_only_by_owner()
        {
            // Arrange
            FakeFileStorage    fileStorage    = new FakeFileStorage();
            FakeFileRepository fileRepository = new FakeFileRepository();
            FakeEventPublisher eventPublisher = new FakeEventPublisher();
            FakeCurrentUser    currentUser    = new FakeCurrentUser();

            File file = new ExampleFileFactory().CreateFile();

            fileRepository.Save(file);
            fileStorage.SaveFile(file, Stream.Null);
            currentUser.Id = "otherUserId";

            var commandHandler = new DeleteFileCommandHandler(fileStorage, currentUser, fileRepository, eventPublisher);

            var command = new DeleteFileCommand("fileId");

            // Assert & Act
            Assert.Throws <PermissionException>(() => commandHandler.Handle(command));
        }
        public void Not_locked_file_cannot_be_unlocked()
        {
            // Arrange
            FakeFileLockingService fileLockingService = new FakeFileLockingService();
            FakeFileRepository     fileRepository     = new FakeFileRepository();
            FakeEventPublisher     eventPublisher     = new FakeEventPublisher();
            FakeCurrentUser        currentUser        = new FakeCurrentUser();

            File file = new ExampleFileFactory().CreateFile();

            fileRepository.Save(file);
            currentUser.Id = "currentUserId";

            var commandHandler = new RemoveFileLockCommandHandler(
                fileLockingService, fileRepository, currentUser, eventPublisher);

            var command = new RemoveFileLockCommand("fileId");

            // Act
            Assert.Throws <NotFoundException>(() => commandHandler.Handle(command));
        }
        public void File_cannot_be_locked_by_not_authorized_user()
        {
            // Arrange
            FakeFileLockingService fileLockingService = new FakeFileLockingService();
            FakeFileRepository     fileRepository     = new FakeFileRepository();
            FakeEventPublisher     eventPublisher     = new FakeEventPublisher();
            FakeCurrentUserSource  currentUserSource  = new FakeCurrentUserSource();

            File file      = new ExampleFileFactory().CreateFile();
            User otherUser = new User("someRandomUserId", "someRandomUser");

            fileRepository.Save(file);
            currentUserSource.CurrentUser = otherUser;

            var commandHandler = new AcquireFileLockCommandHandler(
                fileLockingService, fileRepository, eventPublisher, currentUserSource);

            var command = new AcquireFileLockCommand("fileId");

            // Act
            Assert.Throws <PermissionException>(() => commandHandler.Handle(command));
        }
        public void File_cannot_be_unlocked_by_user_other_than_lock_owner()
        {
            // Arrange
            FakeFileLockingService fileLockingService = new FakeFileLockingService();
            FakeFileRepository     fileRepository     = new FakeFileRepository();
            FakeEventPublisher     eventPublisher     = new FakeEventPublisher();
            FakeCurrentUser        currentUser        = new FakeCurrentUser();

            File file = new ExampleFileFactory().CreateFile();

            fileRepository.Save(file);
            currentUser.Id = "currentUserId";
            fileLockingService.Lock(file, new User("someRandomUserId", "someRandomUser"));

            var commandHandler = new RemoveFileLockCommandHandler(
                fileLockingService, fileRepository, currentUser, eventPublisher);

            var command = new RemoveFileLockCommand("fileId");

            // Act
            Assert.Throws <PermissionException>(() => commandHandler.Handle(command));
        }
        public void File_cannot_be_shared_with_owner()
        {
            // Arrange
            FakeFileRepository fileRepository = new FakeFileRepository();
            FakeUserRepository userRepository = new FakeUserRepository();
            FakeEventPublisher eventPublisher = new FakeEventPublisher();
            FakeCurrentUser    currentUser    = new FakeCurrentUser();

            File file = new ExampleFileFactory().CreateFile();

            fileRepository.Save(file);
            userRepository.Save(file.Owner);
            currentUser.Id = file.Owner.Id;

            var commandHandler = new ShareFileCommandHandler(
                currentUser, fileRepository, userRepository, eventPublisher);

            var command = new ShareFileCommand("fileId", file.Owner.Id);

            // Act & Assert
            Assert.Throws <PermissionException>(() => commandHandler.Handle(command));
        }
        public void File_cannot_be_shared_with_non_existing_user()
        {
            // Arrange
            FakeFileRepository fileRepository = new FakeFileRepository();
            FakeUserRepository userRepository = new FakeUserRepository();
            FakeEventPublisher eventPublisher = new FakeEventPublisher();
            FakeCurrentUser    currentUser    = new FakeCurrentUser();

            File file      = new ExampleFileFactory().CreateFile();
            User shareWith = new User("shareWithUserId", "username");

            fileRepository.Save(file);
            currentUser.Id = file.Owner.Id;

            var commandHandler = new ShareFileCommandHandler(
                currentUser, fileRepository, userRepository, eventPublisher);

            var command = new ShareFileCommand("fileId", shareWith.Id);

            // Act & Assert
            Assert.Throws <NotFoundException>(() => commandHandler.Handle(command));
        }
        public void File_cannot_be_shared_by_user_which_has_access_but_is_not_owner()
        {
            // Arrange
            FakeFileRepository fileRepository = new FakeFileRepository();
            FakeUserRepository userRepository = new FakeUserRepository();
            FakeEventPublisher eventPublisher = new FakeEventPublisher();
            FakeCurrentUser    currentUser    = new FakeCurrentUser();

            File file      = new ExampleFileFactory().CreateFile();
            User shareWith = new User("shareWithUserId", "username");

            fileRepository.Save(file);
            userRepository.Save(shareWith);
            file.ShareWith(shareWith);
            currentUser.Id = shareWith.Id;

            var commandHandler = new ShareFileCommandHandler(
                currentUser, fileRepository, userRepository, eventPublisher);

            var command = new ShareFileCommand("fileId", shareWith.Id);

            // Act & Assert
            Assert.Throws <PermissionException>(() => commandHandler.Handle(command));
        }