Example #1
0
        public async Task Handle_AllCardsExistAndBelongToSameBoard_ReordersCards()
        {
            var dbName            = $"{nameof(ReorderCardsCommandHandlerTests)}_{nameof(Handle_AllCardsExistAndBelongToSameBoard_ReordersCards)}";
            var context           = TestApplicationDbContext.Create(dbName);
            var cancellationToken = new CancellationToken();

            var boardFactory = new BoardFactory();
            var board        = boardFactory.Create("board-title");
            var card1        = Card.Create("card1-title");
            var card2        = Card.Create("card2-title");

            board.AddCard(card1);
            board.AddCard(card2);
            context.Boards.Add(board);
            await context.SaveChangesAsync(cancellationToken);

            var handler = new ReorderCardsCommandHandler(context, new CardService());
            var request = new ReorderCardsCommand {
                CardIds = new List <int> {
                    2, 1
                }
            };

            await handler.Handle(request, cancellationToken);

            var card1FromDb = await context.Cards.FirstOrDefaultAsync(c => c.Id == 1, cancellationToken);

            var card2FromDb = await context.Cards.FirstOrDefaultAsync(c => c.Id == 2, cancellationToken);

            Assert.NotNull(card1FromDb);
            Assert.NotNull(card2FromDb);
            Assert.Equal(0, card2FromDb.Order);
            Assert.Equal(1, card1FromDb.Order);
        }
Example #2
0
        public async Task Handle_AnyCardNotBelongsToSameBoard_ThrowsInvalidOperationApplicationException()
        {
            var dbName            = $"{nameof(ReorderCardsCommandHandlerTests)}_{nameof(Handle_AnyCardNotBelongsToSameBoard_ThrowsInvalidOperationApplicationException)}";
            var context           = TestApplicationDbContext.Create(dbName);
            var cancellationToken = new CancellationToken();

            var boardFactory = new BoardFactory();
            var board1       = boardFactory.Create("board1-title");
            var board2       = boardFactory.Create("board2-title");
            var card1        = Card.Create("card1-title");
            var card2        = Card.Create("card2-title");

            board1.AddCard(card1);
            board2.AddCard(card2);
            context.Boards.AddRange(board1, board2);
            await context.SaveChangesAsync(cancellationToken);

            var handler = new ReorderCardsCommandHandler(context, new CardService());
            var request = new ReorderCardsCommand {
                CardIds = new List <int> {
                    1, 2
                }
            };

            Task <Unit> action() => handler.Handle(request, cancellationToken);

            await Assert.ThrowsAsync <InvalidOperationApplicationException>(action);
        }
Example #3
0
        public async Task Authorise_FirstCardId_CallsHasPermissionForCard_CanEditCard()
        {
            var permission        = BoardUserPermission.CAN_EDIT_CARD;
            var expectedResult    = true;
            var cancellationToken = new CancellationToken();

            _currentUserPermissionService
            .Setup(s => s.HasPermissionForCard(
                       It.IsAny <int>(),
                       It.Is <BoardUserPermission>(p => p == permission),
                       cancellationToken))
            .ReturnsAsync(expectedResult);

            var authorizer = new ReorderCardsAuthorizer(_currentUserPermissionService.Object);
            var request    = new ReorderCardsCommand {
                CardIds = new List <int> {
                    1
                }
            };
            var result = await authorizer.Authorise(request, cancellationToken);

            Assert.Equal(expectedResult, result);
            _currentUserPermissionService
            .Verify(s => s.HasPermissionForCard(It.IsAny <int>(), It.Is <BoardUserPermission>(p => p == permission), cancellationToken), Times.Once);
        }
Example #4
0
        public async Task CardIdsEmpty_ReturnsTrue()
        {
            var authorizer = new ReorderCardsAuthorizer(_currentUserPermissionService.Object);
            var request    = new ReorderCardsCommand {
                CardIds = new List <int>()
            };
            var cancellationToken = new CancellationToken();

            var result = await authorizer.Authorise(request, cancellationToken);

            Assert.True(result);
        }
Example #5
0
        public async Task Handle_AnyCardIdNotExists_ThrowsNotFoundApplicationException()
        {
            var dbName = $"{nameof(ReorderCardsCommandHandlerTests)}_{nameof(Handle_AnyCardIdNotExists_ThrowsNotFoundApplicationException)}";

            using var context = TestApplicationDbContext.Create(dbName);
            var cancellationToken = new CancellationToken();

            var card = Card.Create("card-title");

            context.Cards.Add(card);
            await context.SaveChangesAsync(cancellationToken);

            var handler = new ReorderCardsCommandHandler(context, new CardService());
            var request = new ReorderCardsCommand {
                CardIds = new List <int> {
                    1, 2
                }
            };

            Task <Unit> action() => handler.Handle(request, cancellationToken);

            await Assert.ThrowsAsync <NotFoundApplicationException>(action);
        }
Example #6
0
 public async Task <Unit> RedorderCards(ReorderCardsCommand command) => await _mediator.Send(command);