public async Task ExecuteAsync(ApproveActionRequest request)
        {
            if (string.IsNullOrEmpty(request.Context) || string.IsNullOrEmpty(request.Nonce))
            {
                throw new CommandValidationException("Context and nonce are required");
            }


            await _cacheItemRepository.UpdateAsync(request.Context, item =>
            {
                if (item.Nonce != request.Nonce)
                {
                    throw new ArgumentException(
                        $"Can not find any item with context '{request.Context}' and nonce '{request.Nonce}'");
                }

                if (item.Status != CacheItemStatus.WaitingForApproval)
                {
                    throw new ArgumentException(
                        $"Incorrect status={item.Status.ToString()} for approval '{request.Context}'");
                }

                item.Status = request.IsApproved ? CacheItemStatus.Approved : CacheItemStatus.Declined;
            });
        }
Пример #2
0
        public async Task ExecuteAsync_Success(Mock <ICacheItemRepository> cacheItemRepository,
                                               ApproveActionRequest request)
        {
            var command = new ApproveActionCommand(cacheItemRepository.Object);
            await command.ExecuteAsync(request);

            cacheItemRepository.Verify(x => x.UpdateAsync(request.Context, It.IsAny <Action <CacheItem> >(), null),
                                       Times.Once);
        }
Пример #3
0
        public async Task ExecuteAsync_Fail_EmptyNonce(Mock <ICacheItemRepository> cacheItemRepository,
                                                       ApproveActionRequest request)
        {
            const string errorMessage = "Context and nonce are required";

            request.Nonce = null;
            var command = new ApproveActionCommand(cacheItemRepository.Object);
            await command.Invoking(x => x.ExecuteAsync(request)).Should().ThrowAsync <CommandValidationException>()
            .WithMessage(errorMessage);

            request.Nonce = string.Empty;
            await command.Invoking(x => x.ExecuteAsync(request)).Should().ThrowAsync <CommandValidationException>()
            .WithMessage(errorMessage);
        }