public async Task GivenArticleIsFavourited_ShouldUnfavouriteArticle() { // Arrange var user = new UserProfile(Guid.NewGuid().ToString(), "*****@*****.**", "username"); var article = new Article("test title", "test", "test", new DateTime(1111, 1, 1), user); user.FavouriteArticle(article); Context.Add(user); await Context.SaveChangesAsync(); var currentUser = Mock.Of <ICurrentUserService>(s => s.UserId == user.Id); var command = new UnfavoriteArticleCommand { Slug = article.Slug }; var sut = new UnfavoriteArticleCommand.Handler(Context, currentUser); // Act await sut.Handle(command, CancellationToken.None); var existingFavouritedArticle = await Context.FavouritedArticles .SingleOrDefaultAsync(); // Assert existingFavouritedArticle.Should().BeNull(); }
public async Task GivenValidRequest_WhenTheArticleDoesNotExist_ThrowsApiExpceptionForNotFound() { // Arrange var unfavoriteCommand = new UnfavoriteArticleCommand("a-girl-does-not-have-a-name"); // Act var handler = new UnfavoriteArticleCommandHandler(CurrentUserContext, Context, Mapper); var response = await Should.ThrowAsync <ConduitApiException>(async() => { await handler.Handle(unfavoriteCommand, CancellationToken.None); }); // Assert response.ShouldNotBeNull(); response.ShouldBeOfType <ConduitApiException>(); response.StatusCode.ShouldBe(HttpStatusCode.NotFound); }
public async Task GivenArticleIsNotFavourited_ShouldThrowEntityNotFoundException() { // Arrange var currentUserMock = new Mock <ICurrentUserService>(); var command = new UnfavoriteArticleCommand() { Slug = "some slug" }; var sut = new UnfavoriteArticleCommand.Handler(Context, currentUserMock.Object); // Act var action = new Func <Task <Unit> >(() => sut.Handle(command, CancellationToken.None)); // Assert await action.Should().ThrowAsync <EntityNotFoundException <FavouritedArticle> >(); }
public async Task GivenValidRequest_WhenTheArticleExistsAndTheUserHasNotFavorited_ReturnsArticleViewModelWithArticleUnfavorited() { // Arrange var unfavoriteCommand = new UnfavoriteArticleCommand("why-beer-is-gods-gift-to-the-world"); var existingFavorite = Context.Favorites.Where(f => f.Article.Slug == "why-beer-is-gods-gift-to-the-world").ToList(); existingFavorite.ShouldNotBeNull(); existingFavorite.ShouldNotContain(f => f.User.UserName == "test.user"); Context.Articles.FirstOrDefault(a => a.Slug == "why-beer-is-gods-gift-to-the-world")?.FavoritesCount.ShouldBe(0); // Act var handler = new UnfavoriteArticleCommandHandler(CurrentUserContext, Context, Mapper); var response = await handler.Handle(unfavoriteCommand, CancellationToken.None); // Assert response.ShouldNotBeNull(); response.ShouldBeOfType <ArticleViewModel>(); response.Article.ShouldNotBeNull(); response.Article.ShouldBeOfType <ArticleDto>(); response.Article.Favorited.ShouldBeFalse(); Context.Articles.FirstOrDefault(a => a.Slug == "why-beer-is-gods-gift-to-the-world")?.FavoritesCount.ShouldBe(0); Context.Articles.FirstOrDefault(a => a.Slug == "why-beer-is-gods-gift-to-the-world")?.Favorites.ShouldNotContain(f => f.User.UserName == "test.user"); }
public async Task <ArticleDetailWrapper> UnfavoriteArticle([FromRoute] UnfavoriteArticleCommand command) { return(await _mediator.Send(command)); }