public async Task Update_WithSuccessfulUpdate_PublishesNotification()
        {
            _mockImagePostService.Setup(s => s.Update(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <IEnumerable <Tag> >())).ReturnsAsync(new ImagePost());
            _mockImagePostService.Setup(s => s.IsAuthor(It.IsAny <int>(), It.IsAny <string>())).ReturnsAsync(true);
            UpdateImagePostRequest updateImagePostRequest = new UpdateImagePostRequest()
            {
                Tags = new List <string>()
            };

            await _controller.Update(It.IsAny <int>(), updateImagePostRequest);

            _mockNotificationService.Verify(s => s.Publish(It.IsAny <ImagePostUpdatedNotification>()), Times.Once);
        }
Пример #2
0
        public async Task <ActionResult <ImagePostResponse> > Update(int id, [FromBody] UpdateImagePostRequest imagePostRequest)
        {
            _logger.LogInformation("Received image post update request.");

            // Get the user making the request.
            User user = _currentUserService.GetCurrentUser(HttpContext);

            _logger.LogInformation("Requesting user email: {0}", user.Email);
            _logger.LogInformation("Image post id: {0}", id);

            // Check if user owns the post.
            _logger.LogInformation("Verifying user owns image post.");
            bool userOwnsPost = await _imagePostService.IsAuthor(id, user.Email);

            if (!userOwnsPost)
            {
                _logger.LogError("User '{0}' does not own image post with id {1}.", user.Email, id);
                return(Forbid());
            }

            try
            {
                // Update database image post.
                _logger.LogInformation("Updating image post with id {0}.", id);
                ImagePost updatedImagePost = await _imagePostService.Update(id,
                                                                            imagePostRequest.Description,
                                                                            imagePostRequest.Tags.Select(t => new Tag()
                {
                    Content = t
                }));

                updatedImagePost.User = user;

                ImagePostResponse imagePostResponse = _mapper.Map <ImagePostResponse>(updatedImagePost);

                // Publish event.
                _logger.LogInformation("Publishing image post updated notification.");
                await _notificationService.Publish(new ImagePostUpdatedNotification(imagePostResponse));

                _logger.LogInformation("Successfully updated image post with id {0}.", id);

                return(Ok(imagePostResponse));
            }
            catch (EntityNotFoundException)
            {
                _logger.LogError("Image post with id {0} does not exist.", id);
                return(NotFound());
            }
        }
        public async Task Update_WithNonExistingImagePost_ReturnsNotFound()
        {
            _mockImagePostService.Setup(s => s.Update(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <IEnumerable <Tag> >())).ThrowsAsync(new EntityNotFoundException());
            _mockImagePostService.Setup(s => s.IsAuthor(It.IsAny <int>(), It.IsAny <string>())).ReturnsAsync(true);
            Type expectedType = typeof(NotFoundResult);
            UpdateImagePostRequest updateImagePostRequest = new UpdateImagePostRequest()
            {
                Tags = new List <string>()
            };

            ActionResult <ImagePostResponse> actual = await _controller.Update(It.IsAny <int>(), updateImagePostRequest);

            ActionResult actualResult = actual.Result;

            Assert.IsAssignableFrom(expectedType, actualResult);
        }