From 88339df298016496d814e4df7d3abdf26e84447c Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 11 Apr 2022 19:26:58 +0800 Subject: ... --- .../IntegratedTests2/HttpClientTestExtensions.cs | 15 +++++-- .../IntegratedTests2/TimelineBookmarkTest.cs | 1 - .../IntegratedTests2/TimelineBookmarkTest2.cs | 51 ++++++++++++++++++++++ 3 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 BackEnd/Timeline.Tests/IntegratedTests2/TimelineBookmarkTest2.cs (limited to 'BackEnd/Timeline.Tests') diff --git a/BackEnd/Timeline.Tests/IntegratedTests2/HttpClientTestExtensions.cs b/BackEnd/Timeline.Tests/IntegratedTests2/HttpClientTestExtensions.cs index cd7daf3e..48496853 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests2/HttpClientTestExtensions.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests2/HttpClientTestExtensions.cs @@ -12,7 +12,7 @@ namespace Timeline.Tests.IntegratedTests2 public static class HttpClientTestExtensions { - public static async Task TestSendAsync(this HttpClient client, HttpMethod method, string url, HttpContent? body = null, HttpStatusCode expectedStatusCode = HttpStatusCode.OK, RequestSetupAsync? requestSetup = null) + public static async Task TestSendAsync(this HttpClient client, HttpMethod method, string url, HttpContent? body = null, HttpStatusCode? expectedStatusCode = null, RequestSetupAsync? requestSetup = null) { using var req = new HttpRequestMessage { @@ -23,7 +23,14 @@ namespace Timeline.Tests.IntegratedTests2 var task = requestSetup?.Invoke(req); if (task is not null) await task; var res = await client.SendAsync(req); - res.StatusCode.Should().Be(expectedStatusCode); + if (expectedStatusCode is null) + { + ((int)res.StatusCode).Should().BeGreaterThanOrEqualTo(200).And.BeLessThan(300); + } + else + { + res.StatusCode.Should().Be(expectedStatusCode.Value); + } return res; } @@ -34,13 +41,13 @@ namespace Timeline.Tests.IntegratedTests2 return body!; } - public static async Task TestJsonSendAsync(this HttpClient client, HttpMethod method, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.OK, RequestSetupAsync? requestSetup = null) + public static async Task TestJsonSendAsync(this HttpClient client, HttpMethod method, string url, object? jsonBody = null, HttpStatusCode? expectedStatusCode = null, RequestSetupAsync? requestSetup = null) { using JsonContent? reqContent = jsonBody is null ? null : JsonContent.Create(jsonBody, options: CommonJsonSerializeOptions.Options); await client.TestSendAsync(method, url, reqContent, expectedStatusCode, requestSetup); } - public static async Task TestJsonSendAsync(this HttpClient client, HttpMethod method, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.OK, RequestSetupAsync? requestSetup = null) + public static async Task TestJsonSendAsync(this HttpClient client, HttpMethod method, string url, object? jsonBody = null, HttpStatusCode? expectedStatusCode = null, RequestSetupAsync? requestSetup = null) { using JsonContent? reqContent = jsonBody == null ? null : JsonContent.Create(jsonBody, options: CommonJsonSerializeOptions.Options); var res = await client.TestSendAsync(method, url, reqContent, expectedStatusCode, requestSetup); diff --git a/BackEnd/Timeline.Tests/IntegratedTests2/TimelineBookmarkTest.cs b/BackEnd/Timeline.Tests/IntegratedTests2/TimelineBookmarkTest.cs index bac132ba..f708381f 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests2/TimelineBookmarkTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests2/TimelineBookmarkTest.cs @@ -23,7 +23,6 @@ namespace Timeline.Tests.IntegratedTests2 { Name = "hello" }, expectedStatusCode: HttpStatusCode.Created); - } [Fact] diff --git a/BackEnd/Timeline.Tests/IntegratedTests2/TimelineBookmarkTest2.cs b/BackEnd/Timeline.Tests/IntegratedTests2/TimelineBookmarkTest2.cs new file mode 100644 index 00000000..6bc93836 --- /dev/null +++ b/BackEnd/Timeline.Tests/IntegratedTests2/TimelineBookmarkTest2.cs @@ -0,0 +1,51 @@ +using System; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; +using FluentAssertions; +using Timeline.Models; +using Timeline.Models.Http; +using Xunit; +using Xunit.Abstractions; + +namespace Timeline.Tests.IntegratedTests2 +{ + public class TimelineBookmarkTest2 : IntegratedTestBase + { + public TimelineBookmarkTest2(ITestOutputHelper testOutput) : base(testOutput) + { + } + + protected override async Task OnInitializeAsync() + { + using var client = CreateClientAsUser(); + await client.TestJsonSendAsync(HttpMethod.Post, "v2/timelines", new HttpTimelineCreateRequest + { + Name = "hello" + }, expectedStatusCode: HttpStatusCode.Created); + + await client.TestJsonSendAsync(HttpMethod.Post, "v2/users/user/bookmarks", new HttpTimelineBookmarkCreateRequest + { + TimelineOwner = "user", + TimelineName = "hello" + }, expectedStatusCode: HttpStatusCode.Created); + } + + [Fact] + public async Task ChangeVisibilityShouldWork() + { + using var client = CreateClientAsUser(); + var a = await client.TestJsonSendAsync(HttpMethod.Get, "v2/users/user/bookmarks/visibility", expectedStatusCode: HttpStatusCode.OK); + a.Visibility.Should().Be(TimelineVisibility.Private); + + await client.TestJsonSendAsync(HttpMethod.Put, "v2/users/user/bookmarks/visibility", new HttpTimelineBookmarkVisibility { Visibility = TimelineVisibility.Register }, expectedStatusCode: HttpStatusCode.NoContent); + var b = await client.TestJsonSendAsync(HttpMethod.Get, "v2/users/user/bookmarks/visibility", expectedStatusCode: HttpStatusCode.OK); + b.Visibility.Should().Be(TimelineVisibility.Register); + + await client.TestJsonSendAsync(HttpMethod.Put, "v2/users/user/bookmarks/visibility", new HttpTimelineBookmarkVisibility { Visibility = TimelineVisibility.Public }, expectedStatusCode: HttpStatusCode.NoContent); + var c = await client.TestJsonSendAsync(HttpMethod.Get, "v2/users/user/bookmarks/visibility", expectedStatusCode: HttpStatusCode.OK); + c.Visibility.Should().Be(TimelineVisibility.Public); + } + } +} + -- cgit v1.2.3