From f93ac5c1b21f9bee827c51955bd831ce0d322aaf Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 10 Jul 2020 16:10:17 +0800 Subject: Add unit tests. --- Timeline.Tests/IntegratedTests/TimelineTest.cs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'Timeline.Tests/IntegratedTests/TimelineTest.cs') diff --git a/Timeline.Tests/IntegratedTests/TimelineTest.cs b/Timeline.Tests/IntegratedTests/TimelineTest.cs index 4f21d8d1..64619864 100644 --- a/Timeline.Tests/IntegratedTests/TimelineTest.cs +++ b/Timeline.Tests/IntegratedTests/TimelineTest.cs @@ -942,6 +942,7 @@ namespace Timeline.Tests.IntegratedTests body.Should().NotBeNull(); body.Content.Should().BeEquivalentTo(TimelineHelper.TextPostContent(mockContent)); body.Author.Should().BeEquivalentTo(UserInfos[1]); + body.Deleted.Should().BeFalse(); createRes = body; } { @@ -963,6 +964,7 @@ namespace Timeline.Tests.IntegratedTests body.Content.Should().BeEquivalentTo(TimelineHelper.TextPostContent(mockContent2)); body.Author.Should().BeEquivalentTo(UserInfos[1]); body.Time.Should().BeCloseTo(mockTime2, 1000); + body.Deleted.Should().BeFalse(); createRes2 = body; } { -- cgit v1.2.3 From 94f64bd98a267360a9e941b86c56c614605067f2 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 10 Jul 2020 20:18:08 +0800 Subject: Add http api and integrated tests. --- Timeline.Tests/IntegratedTests/TimelineTest.cs | 34 ++++++++++++++++++++++++++ Timeline/Controllers/TimelineController.cs | 8 ++---- 2 files changed, 36 insertions(+), 6 deletions(-) (limited to 'Timeline.Tests/IntegratedTests/TimelineTest.cs') diff --git a/Timeline.Tests/IntegratedTests/TimelineTest.cs b/Timeline.Tests/IntegratedTests/TimelineTest.cs index 64619864..4ba8800e 100644 --- a/Timeline.Tests/IntegratedTests/TimelineTest.cs +++ b/Timeline.Tests/IntegratedTests/TimelineTest.cs @@ -1255,5 +1255,39 @@ namespace Timeline.Tests.IntegratedTests .And.Subject.Select(p => p.Content.Text).Should().Equal(postContentList.Skip(1)); } } + + [Theory] + [MemberData(nameof(TimelineUrlGeneratorData))] + public async Task PostList_IncludeDeleted(TimelineUrlGenerator urlGenerator) + { + using var client = await CreateClientAsUser(); + + var postContentList = new List { "a", "b", "c", "d" }; + var posts = new List(); + + foreach (var content in postContentList) + { + var res = await client.PostAsJsonAsync(urlGenerator(1, "posts"), + new TimelinePostCreateRequest { Content = new TimelinePostCreateRequestContent { Text = content, Type = TimelinePostContentTypes.Text } }); + posts.Add(res.Should().HaveStatusCode(200) + .And.HaveJsonBody().Which); + } + + foreach (var id in new long[] { posts[0].Id, posts[2].Id }) + { + var res = await client.DeleteAsync(urlGenerator(1, $"posts/{id}")); + res.Should().BeDelete(true); + } + + { + var res = await client.GetAsync(urlGenerator(1, "posts", new Dictionary { ["includeDeleted"] = "true" })); + posts = res.Should().HaveStatusCode(200) + .And.HaveJsonBody>() + .Which; + posts.Should().HaveCount(4); + posts.Select(p => p.Deleted).Should().Equal(true, false, true, false); + posts.Select(p => p.Content == null).Should().Equal(true, false, true, false); + } + } } } diff --git a/Timeline/Controllers/TimelineController.cs b/Timeline/Controllers/TimelineController.cs index b8cc608b..2330698f 100644 --- a/Timeline/Controllers/TimelineController.cs +++ b/Timeline/Controllers/TimelineController.cs @@ -102,18 +102,14 @@ namespace Timeline.Controllers } [HttpGet("timelines/{name}/posts")] - public async Task>> PostListGet([FromRoute][GeneralTimelineName] string name, [FromQuery] DateTime? modifiedSince) + public async Task>> PostListGet([FromRoute][GeneralTimelineName] string name, [FromQuery] DateTime? modifiedSince, [FromQuery] bool? includeDeleted) { if (!this.IsAdministrator() && !await _service.HasReadPermission(name, this.GetOptionalUserId())) { return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); } - List posts; - if (modifiedSince == null) - posts = await _service.GetPosts(name); - else - posts = await _service.GetPosts(name, modifiedSince.Value); + List posts = await _service.GetPosts(name, modifiedSince, includeDeleted ?? false); var result = _mapper.Map>(posts); return result; -- cgit v1.2.3 From 8036808ae8b7865d7d17b232c94b6a04ad197bb1 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 10 Jul 2020 20:29:02 +0800 Subject: Add another integrated test. --- Timeline.Tests/IntegratedTests/TimelineTest.cs | 57 ++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 7 deletions(-) (limited to 'Timeline.Tests/IntegratedTests/TimelineTest.cs') diff --git a/Timeline.Tests/IntegratedTests/TimelineTest.cs b/Timeline.Tests/IntegratedTests/TimelineTest.cs index 4ba8800e..ba335bd6 100644 --- a/Timeline.Tests/IntegratedTests/TimelineTest.cs +++ b/Timeline.Tests/IntegratedTests/TimelineTest.cs @@ -1231,28 +1231,31 @@ namespace Timeline.Tests.IntegratedTests { using var client = await CreateClientAsUser(); - DateTime testPoint = new DateTime(); var postContentList = new List { "a", "b", "c", "d" }; + var posts = new List(); - foreach (var (content, index) in postContentList.Select((v, i) => (v, i))) + foreach (var content in postContentList) { var res = await client.PostAsJsonAsync(generator(1, "posts"), new TimelinePostCreateRequest { Content = new TimelinePostCreateRequestContent { Text = content, Type = TimelinePostContentTypes.Text } }); var post = res.Should().HaveStatusCode(200) .And.HaveJsonBody().Which; - if (index == 1) - testPoint = post.LastUpdated; + posts.Add(post); await Task.Delay(1000); } { + var res = await client.DeleteAsync(generator(1, $"posts/{posts[2].Id}")); + res.Should().BeDelete(true); + } + { var res = await client.GetAsync(generator(1, "posts", - new Dictionary { { "modifiedSince", testPoint.ToString("s", CultureInfo.InvariantCulture) } })); + new Dictionary { { "modifiedSince", posts[1].LastUpdated.ToString("s", CultureInfo.InvariantCulture) } })); res.Should().HaveStatusCode(200) .And.HaveJsonBody>() - .Which.Should().HaveCount(3) - .And.Subject.Select(p => p.Content.Text).Should().Equal(postContentList.Skip(1)); + .Which.Should().HaveCount(2) + .And.Subject.Select(p => p.Content.Text).Should().Equal("b", "d"); } } @@ -1289,5 +1292,45 @@ namespace Timeline.Tests.IntegratedTests posts.Select(p => p.Content == null).Should().Equal(true, false, true, false); } } + + [Theory] + [MemberData(nameof(TimelineUrlGeneratorData))] + public async Task Post_ModifiedSince_And_IncludeDeleted(TimelineUrlGenerator urlGenerator) + { + using var client = await CreateClientAsUser(); + + var postContentList = new List { "a", "b", "c", "d" }; + var posts = new List(); + + foreach (var (content, index) in postContentList.Select((v, i) => (v, i))) + { + var res = await client.PostAsJsonAsync(urlGenerator(1, "posts"), + new TimelinePostCreateRequest { Content = new TimelinePostCreateRequestContent { Text = content, Type = TimelinePostContentTypes.Text } }); + var post = res.Should().HaveStatusCode(200) + .And.HaveJsonBody().Which; + posts.Add(post); + await Task.Delay(1000); + } + + { + var res = await client.DeleteAsync(urlGenerator(1, $"posts/{posts[2].Id}")); + res.Should().BeDelete(true); + } + + { + + var res = await client.GetAsync(urlGenerator(1, "posts", + new Dictionary { + { "modifiedSince", posts[1].LastUpdated.ToString("s", CultureInfo.InvariantCulture) }, + { "includeDeleted", "true" } + })); + posts = res.Should().HaveStatusCode(200) + .And.HaveJsonBody>() + .Which; + posts.Should().HaveCount(3); + posts.Select(p => p.Deleted).Should().Equal(false, true, false); + posts.Select(p => p.Content == null).Should().Equal(false, true, false); + } + } } } -- cgit v1.2.3