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_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);
        }
        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 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 File_add_throws_when_owner_now_found()
        {
            // Arrange
            FakeFileStorage         fileStorage         = new FakeFileStorage();
            FakeFileRepository      fileRepository      = new FakeFileRepository();
            FakeEventPublisher      eventPublisher      = new FakeEventPublisher();
            FakeCurrentUserSource   currentUserSource   = new FakeCurrentUserSource();
            FakePostCommitRegistrar postCommitRegistrar = new FakePostCommitRegistrar();

            currentUserSource.CurrentUser = null;

            var commandHandler = new AddFileCommandHandler(
                fileRepository, fileStorage, eventPublisher, postCommitRegistrar, currentUserSource);

            var contentBuffer = Encoding.UTF8.GetBytes("testContent");

            using (Stream fileContent = new MemoryStream(contentBuffer))
            {
                var command = new AddFileCommand("filename", "desc", "text/plain", fileContent);

                // Assert & Act
                Assert.Throws <NotFoundException>(() =>
                {
                    commandHandler.Handle(command);
                    postCommitRegistrar.ExecuteActions();
                });
            }
        }
        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_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_add_adds_file_and_publishes_event()
        {
            // Arrange
            FakeFileStorage         fileStorage         = new FakeFileStorage();
            FakeFileRepository      fileRepository      = new FakeFileRepository();
            FakeEventPublisher      eventPublisher      = new FakeEventPublisher();
            FakeCurrentUserSource   currentUserSource   = new FakeCurrentUserSource();
            FakePostCommitRegistrar postCommitRegistrar = new FakePostCommitRegistrar();

            currentUserSource.CurrentUser = new User("userId", "username");

            var commandHandler = new AddFileCommandHandler(
                fileRepository, fileStorage, eventPublisher, postCommitRegistrar, currentUserSource);

            var contentBuffer = Encoding.UTF8.GetBytes("testContent");

            using (Stream fileContent = new MemoryStream(contentBuffer))
            {
                var command = new AddFileCommand("filename", "desc", "text/plain", fileContent);

                // Act
                commandHandler.Handle(command);
                postCommitRegistrar.ExecuteActions();

                // Assert
                File addedFile = fileRepository.GetByName("filename");

                Assert.IsNotNull(addedFile);
                Assert.AreEqual("filename", addedFile.FileName);
                Assert.AreEqual("desc", addedFile.Description);
                Assert.AreEqual("userId", addedFile.Owner.Id);

                Stream fileStream = fileStorage.ReadFile(addedFile).Result;
                Assert.IsNotNull(fileStream);

                using (var reader = new StreamReader(fileStream))
                    Assert.AreEqual("testContent", reader.ReadToEnd());

                File publishedEvent = eventPublisher.VerifyPublishedOnce <File>();
                Assert.AreEqual(addedFile, publishedEvent);
            }
        }
        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_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 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_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_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_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));
        }