public async Task TestHandle_WithAuthor_ShouldFilterCorrectly() { // Arrange var author = new UserProfile(Guid.NewGuid().ToString(), "email", "username"); var otherAuthor = new UserProfile(Guid.NewGuid().ToString(), "otherAuthorEmail", "otherAuthorUsername"); var articles = new List <Article> { new Article("first title", "first description", "first body", new DateTime(1, 2, 3), author), new Article("second title", "second description", "second body", new DateTime(2, 2, 3), otherAuthor), }; Context.Articles.AddRange(articles); await Context.SaveChangesAsync(); var expected = new ArticleListDto { Articles = new List <ArticleDto> { Mapper.Map <ArticleDto>(articles[0]) } }; var query = new ListArticlesQuery { Author = author.Username }; var currentUserMock = new Mock <ICurrentUserService>(); var sut = new ListArticlesQuery.Handler(Context, Mapper, currentUserMock.Object); // Act var result = await sut.Handle(query, CancellationToken.None); // Assert result.Should().BeEquivalentTo(expected); }
public async Task TestHandle_WithAuthenticatedUser_ShouldSetAuthorFollowedCorrectly() { // Arrange var user = new UserProfile(Guid.NewGuid().ToString(), "currentUser", "currentUser"); var followedAuthor = new UserProfile(Guid.NewGuid().ToString(), "email", "username"); var notFollowedAuthor = new UserProfile(Guid.NewGuid().ToString(), "otherAuthorEmail", "otherAuthorUsername"); Context.UserFollowers.Add(new UserFollower(followedAuthor, user)); var articles = new List <Article> { new Article("first title", "first description", "first body", new DateTime(1, 2, 3), followedAuthor), new Article("second title", "second description", "second body", new DateTime(2, 2, 3), notFollowedAuthor), }; Context.Articles.AddRange(articles); await Context.SaveChangesAsync(); var query = new ListArticlesQuery(); var currentUser = Mock.Of <ICurrentUserService>(s => s.UserId == user.Id && s.IsAuthenticated == true); var sut = new ListArticlesQuery.Handler(Context, Mapper, currentUser); // Act var result = await sut.Handle(query, CancellationToken.None); var articleDtos = result.Articles.ToList(); // Assert articleDtos[0].Author.Following.Should().BeFalse(); articleDtos[1].Author.Following.Should().BeTrue(); }