From f5ccd0d9855f82e14c6b765eee6a04b22c50dc8a Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 15 Nov 2020 01:44:48 +0800 Subject: ... --- .../IntegratedTests/HttpClientTestExtensions.cs | 192 +++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs (limited to 'BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs') diff --git a/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs b/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs new file mode 100644 index 00000000..18334622 --- /dev/null +++ b/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs @@ -0,0 +1,192 @@ +using FluentAssertions; +using System; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Net.Http.Json; +using System.Threading.Tasks; +using Timeline.Models.Http; + +namespace Timeline.Tests.IntegratedTests +{ + public delegate void HeaderSetup(HttpRequestHeaders requestHeaders, HttpContentHeaders? contentHeaders); + + public static class HttpClientTestExtensions + { + public static async Task TestSendAsync(this HttpClient client, HttpMethod method, string url, HttpContent? body = null, HttpStatusCode expectedStatusCode = HttpStatusCode.OK, HeaderSetup? headerSetup = null) + { + using var req = new HttpRequestMessage + { + Method = method, + RequestUri = new Uri(url, UriKind.Relative), + Content = body + }; + headerSetup?.Invoke(req.Headers, body?.Headers); + var res = await client.SendAsync(req); + res.StatusCode.Should().Be(expectedStatusCode); + return res; + } + + public static async Task AssertJsonBodyAsync(HttpResponseMessage response) + { + var body = await response.Content.ReadFromJsonAsync(CommonJsonSerializeOptions.Options); + body.Should().NotBeNull($"Body is not json format of type {typeof(T).FullName}"); + return body!; + } + + public static async Task TestJsonSendAsync(this HttpClient client, HttpMethod method, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.OK, HeaderSetup? headerSetup = null) + { + using JsonContent? reqContent = jsonBody == null ? null : JsonContent.Create(jsonBody, options: CommonJsonSerializeOptions.Options); + await client.TestSendAsync(method, url, reqContent, expectedStatusCode, headerSetup); + } + + public static async Task TestJsonSendAsync(this HttpClient client, HttpMethod method, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.OK, HeaderSetup? headerSetup = null) + { + using JsonContent? reqContent = jsonBody == null ? null : JsonContent.Create(jsonBody, options: CommonJsonSerializeOptions.Options); + var res = await client.TestSendAsync(method, url, reqContent, expectedStatusCode, headerSetup); + var resBody = await AssertJsonBodyAsync(res); + return resBody; + } + + public static async Task TestGetAsync(this HttpClient client, string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) + { + await client.TestJsonSendAsync(HttpMethod.Get, url, expectedStatusCode: expectedStatusCode); + } + + public static async Task TestGetAsync(this HttpClient client, string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) + { + return await client.TestJsonSendAsync(HttpMethod.Get, url, expectedStatusCode: expectedStatusCode); + } + + public static async Task TestPostAsync(this HttpClient client, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) + { + await client.TestJsonSendAsync(HttpMethod.Post, url, jsonBody, expectedStatusCode: expectedStatusCode); + } + + public static async Task TestPostAsync(this HttpClient client, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) + { + return await client.TestJsonSendAsync(HttpMethod.Post, url, jsonBody, expectedStatusCode: expectedStatusCode); + } + + public static async Task TestPutAsync(this HttpClient client, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) + { + await client.TestJsonSendAsync(HttpMethod.Put, url, jsonBody, expectedStatusCode: expectedStatusCode); + } + + public static async Task TestPutAsync(this HttpClient client, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) + { + return await client.TestJsonSendAsync(HttpMethod.Put, url, jsonBody, expectedStatusCode: expectedStatusCode); + } + + public static async Task TestPatchAsync(this HttpClient client, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) + { + return await client.TestJsonSendAsync(HttpMethod.Patch, url, jsonBody, expectedStatusCode: expectedStatusCode); + } + + public static async Task TestDeleteAsync(this HttpClient client, string url, bool? delete = null, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) + { + var body = await client.TestJsonSendAsync(HttpMethod.Delete, url, expectedStatusCode: expectedStatusCode); + if (delete.HasValue) + body.Data.Delete.Should().Be(delete.Value); + } + + public static async Task TestSendAssertErrorAsync(this HttpClient client, HttpMethod method, string url, HttpContent? body = null, HttpStatusCode expectedStatusCode = HttpStatusCode.BadRequest, int? errorCode = null, HeaderSetup? headerSetup = null) + { + var res = await client.TestSendAsync(method, url, body, expectedStatusCode, headerSetup); + if (errorCode.HasValue) + { + var resBody = await AssertJsonBodyAsync(res); + resBody.Code.Should().Be(errorCode.Value); + } + } + + public static async Task TestJsonSendAssertErrorAsync(this HttpClient client, HttpMethod method, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.BadRequest, int? errorCode = null, HeaderSetup? headerSetup = null) + { + using JsonContent? reqContent = jsonBody == null ? null : JsonContent.Create(jsonBody, options: CommonJsonSerializeOptions.Options); + await client.TestSendAssertErrorAsync(method, url, reqContent, expectedStatusCode, errorCode, headerSetup); + } + + public static async Task TestPostAssertErrorAsync(this HttpClient client, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.BadRequest, int? errorCode = null, HeaderSetup? headerSetup = null) + { + await client.TestJsonSendAssertErrorAsync(HttpMethod.Post, url, jsonBody, expectedStatusCode, errorCode, headerSetup); + } + + public static async Task TestPutAssertErrorAsync(this HttpClient client, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.BadRequest, int? errorCode = null, HeaderSetup? headerSetup = null) + { + await client.TestJsonSendAssertErrorAsync(HttpMethod.Put, url, jsonBody, expectedStatusCode, errorCode, headerSetup); + } + + public static async Task TestDeleteAssertErrorAsync(this HttpClient client, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.BadRequest, int? errorCode = null, HeaderSetup? headerSetup = null) + { + await client.TestJsonSendAssertErrorAsync(HttpMethod.Delete, url, jsonBody, expectedStatusCode, errorCode, headerSetup); + } + + public static async Task TestJsonSendAssertInvalidModelAsync(this HttpClient client, HttpMethod method, string url, object? jsonBody = null) + { + await client.TestJsonSendAssertErrorAsync(method, url, jsonBody, expectedStatusCode: HttpStatusCode.BadRequest, errorCode: ErrorCodes.Common.InvalidModel); + } + + public static async Task TestGetAssertInvalidModelAsync(this HttpClient client, string url) + { + await client.TestJsonSendAssertInvalidModelAsync(HttpMethod.Get, url); + } + + public static async Task TestPostAssertInvalidModelAsync(this HttpClient client, string url, object? jsonBody = null) + { + await client.TestJsonSendAssertInvalidModelAsync(HttpMethod.Post, url, jsonBody); + } + + public static async Task TestDeleteAssertInvalidModelAsync(this HttpClient client, string url, object? jsonBody = null) + { + await client.TestJsonSendAssertInvalidModelAsync(HttpMethod.Delete, url, jsonBody); + } + + public static async Task TestJsonSendAssertUnauthorizedAsync(this HttpClient client, HttpMethod method, string url, object? jsonBody = null, int? errorCode = null, HeaderSetup? headerSetup = null) + { + await client.TestJsonSendAssertErrorAsync(method, url, jsonBody, HttpStatusCode.Unauthorized, errorCode, headerSetup); + } + + public static async Task TestGetAssertUnauthorizedAsync(this HttpClient client, string url, object? jsonBody = null, int? errorCode = null, HeaderSetup? headerSetup = null) + { + await client.TestJsonSendAssertUnauthorizedAsync(HttpMethod.Get, url, jsonBody, errorCode, headerSetup); + } + + public static async Task TestPostAssertUnauthorizedAsync(this HttpClient client, string url, object? jsonBody = null, int? errorCode = null, HeaderSetup? headerSetup = null) + { + await client.TestJsonSendAssertUnauthorizedAsync(HttpMethod.Post, url, jsonBody, errorCode, headerSetup); + } + + public static async Task TestDeleteAssertUnauthorizedAsync(this HttpClient client, string url, object? jsonBody = null, int? errorCode = null, HeaderSetup? headerSetup = null) + { + await client.TestJsonSendAssertUnauthorizedAsync(HttpMethod.Delete, url, jsonBody, errorCode, headerSetup); + } + + public static async Task TestJsonSendAssertForbiddenAsync(this HttpClient client, HttpMethod method, string url, object? jsonBody = null, int? errorCode = null, HeaderSetup? headerSetup = null) + { + await client.TestJsonSendAssertErrorAsync(method, url, jsonBody, HttpStatusCode.Forbidden, errorCode, headerSetup); + } + + public static async Task TestGetAssertForbiddenAsync(this HttpClient client, string url, object? jsonBody = null, int? errorCode = null, HeaderSetup? headerSetup = null) + { + await client.TestJsonSendAssertForbiddenAsync(HttpMethod.Get, url, jsonBody, errorCode, headerSetup); + } + + public static async Task TestPostAssertForbiddenAsync(this HttpClient client, string url, object? jsonBody = null, int? errorCode = null, HeaderSetup? headerSetup = null) + { + await client.TestJsonSendAssertForbiddenAsync(HttpMethod.Post, url, jsonBody, errorCode, headerSetup); + } + + public static async Task TestDeleteAssertForbiddenAsync(this HttpClient client, string url, object? jsonBody = null, int? errorCode = null, HeaderSetup? headerSetup = null) + { + await client.TestJsonSendAssertForbiddenAsync(HttpMethod.Delete, url, jsonBody, errorCode, headerSetup); + } + + public static async Task TestPutByteArrayAsync(this HttpClient client, string url, byte[] body, string mimeType, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) + { + using var content = new ByteArrayContent(body); + content.Headers.ContentLength = body.Length; + content.Headers.ContentType = new MediaTypeHeaderValue(mimeType); + return await client.TestSendAsync(HttpMethod.Put, url, content); + } + } +} -- cgit v1.2.3 From 67fff3b0883ffaf2a2b314252412e5476413bbf0 Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 15 Nov 2020 02:28:10 +0800 Subject: ... --- .../IntegratedTests/HttpClientTestExtensions.cs | 13 +- .../Timeline.Tests/IntegratedTests/TimelineTest.cs | 485 +++++++-------------- 2 files changed, 167 insertions(+), 331 deletions(-) (limited to 'BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs') diff --git a/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs b/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs index 18334622..5091a7f7 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs @@ -48,14 +48,14 @@ namespace Timeline.Tests.IntegratedTests return resBody; } - public static async Task TestGetAsync(this HttpClient client, string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) + public static async Task TestGetAsync(this HttpClient client, string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK, HeaderSetup? headerSetup = null) { - await client.TestJsonSendAsync(HttpMethod.Get, url, expectedStatusCode: expectedStatusCode); + await client.TestJsonSendAsync(HttpMethod.Get, url, expectedStatusCode: expectedStatusCode, headerSetup: headerSetup); } - public static async Task TestGetAsync(this HttpClient client, string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) + public static async Task TestGetAsync(this HttpClient client, string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK, HeaderSetup? headerSetup = null) { - return await client.TestJsonSendAsync(HttpMethod.Get, url, expectedStatusCode: expectedStatusCode); + return await client.TestJsonSendAsync(HttpMethod.Get, url, expectedStatusCode: expectedStatusCode, headerSetup: headerSetup); } public static async Task TestPostAsync(this HttpClient client, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) @@ -106,6 +106,11 @@ namespace Timeline.Tests.IntegratedTests await client.TestSendAssertErrorAsync(method, url, reqContent, expectedStatusCode, errorCode, headerSetup); } + public static async Task TestGetAssertErrorAsync(this HttpClient client, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.BadRequest, int? errorCode = null, HeaderSetup? headerSetup = null) + { + await client.TestJsonSendAssertErrorAsync(HttpMethod.Get, url, jsonBody, expectedStatusCode, errorCode, headerSetup); + } + public static async Task TestPostAssertErrorAsync(this HttpClient client, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.BadRequest, int? errorCode = null, HeaderSetup? headerSetup = null) { await client.TestJsonSendAssertErrorAsync(HttpMethod.Post, url, jsonBody, expectedStatusCode, errorCode, headerSetup); diff --git a/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs index fbcdb3fc..44a5584e 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs @@ -7,6 +7,7 @@ using System; using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Net; using System.Net.Http; using System.Net.Http.Json; using System.Threading.Tasks; @@ -513,95 +514,63 @@ namespace Timeline.Tests.IntegratedTests [MemberData(nameof(TimelineNameGeneratorTestData))] public async Task TextPost_Should_Work(TimelineNameGenerator generator) { + using var client = await CreateClientAsUser(); + { - using var client = await CreateClientAsUser(); - { - var res = await client.GetAsync(generator(1, "posts")); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.Should().NotBeNull().And.BeEmpty(); - } - { - var res = await client.PostAsJsonAsync(generator(1, "posts"), - TimelineHelper.TextPostCreateRequest(null)); - res.Should().BeInvalidModel(); - } - const string mockContent = "aaa"; - TimelinePostInfo createRes; - { - var res = await client.PostAsJsonAsync(generator(1, "posts"), - TimelineHelper.TextPostCreateRequest(mockContent)); - var body = res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which; - body.Should().NotBeNull(); - body.Content.Should().BeEquivalentTo(TimelineHelper.TextPostContent(mockContent)); - body.Author.Should().BeEquivalentTo(UserInfos[1]); - body.Deleted.Should().BeFalse(); - createRes = body; - } - { - var res = await client.GetAsync(generator(1, "posts")); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.Should().NotBeNull().And.BeEquivalentTo(createRes); - } - const string mockContent2 = "bbb"; - var mockTime2 = DateTime.UtcNow.AddDays(-1); - TimelinePostInfo createRes2; - { - var res = await client.PostAsJsonAsync(generator(1, "posts"), - TimelineHelper.TextPostCreateRequest(mockContent2, mockTime2)); - var body = res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which; - body.Should().NotBeNull(); - 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; - } - { - var res = await client.GetAsync(generator(1, "posts")); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.Should().NotBeNull().And.BeEquivalentTo(createRes, createRes2); - } - { - var res = await client.DeleteAsync(generator(1, $"posts/{createRes.Id}")); - res.Should().BeDelete(true); - } - { - var res = await client.DeleteAsync(generator(1, $"posts/{createRes.Id}")); - res.Should().BeDelete(false); - } - { - var res = await client.DeleteAsync(generator(1, "posts/30000")); - res.Should().BeDelete(false); - } - { - var res = await client.GetAsync(generator(1, "posts")); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.Should().NotBeNull().And.BeEquivalentTo(createRes2); - } + var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts"); + body.Should().BeEmpty(); + } + + const string mockContent = "aaa"; + TimelinePostInfo createRes; + { + var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest(mockContent)); + body.Content.Should().BeEquivalentTo(TimelineHelper.TextPostContent(mockContent)); + body.Author.Should().BeEquivalentTo(await client.GetUserAsync("user1")); + body.Deleted.Should().BeFalse(); + createRes = body; + } + { + var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts"); + body.Should().BeEquivalentTo(createRes); + } + const string mockContent2 = "bbb"; + var mockTime2 = DateTime.UtcNow.AddDays(-1); + TimelinePostInfo createRes2; + { + var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest(mockContent2, mockTime2)); + body.Should().NotBeNull(); + body.Content.Should().BeEquivalentTo(TimelineHelper.TextPostContent(mockContent2)); + body.Author.Should().BeEquivalentTo(await client.GetUserAsync("user1")); + body.Time.Should().BeCloseTo(mockTime2, 1000); + body.Deleted.Should().BeFalse(); + createRes2 = body; + } + { + var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts"); + body.Should().BeEquivalentTo(createRes, createRes2); + } + { + await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{createRes.Id}", true); + await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{createRes.Id}", false); + await client.TestDeleteAsync($"timelines/{generator(1)}/posts/30000", false); + } + { + var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts"); + body.Should().BeEquivalentTo(createRes2); } } [Theory] - [MemberData(nameof(TimelineUrlGeneratorData))] - public async Task GetPost_Should_Ordered(TimelineUrlGenerator generator) + [MemberData(nameof(TimelineNameGeneratorTestData))] + public async Task GetPost_Should_Ordered(TimelineNameGenerator generator) { using var client = await CreateClientAsUser(); async Task CreatePost(DateTime time) { - var res = await client.PostAsJsonAsync(generator(1, "posts"), - TimelineHelper.TextPostCreateRequest("aaa", time)); - return res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.Id; + var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa", time)); + return body.Id; } var now = DateTime.UtcNow; @@ -610,60 +579,31 @@ namespace Timeline.Tests.IntegratedTests var id2 = await CreatePost(now); { - var res = await client.GetAsync(generator(1, "posts")); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.Select(p => p.Id).Should().Equal(id1, id2, id0); + var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts"); + body.Select(p => p.Id).Should().Equal(id1, id2, id0); } } [Theory] - [MemberData(nameof(TimelineUrlGeneratorData))] - public async Task CreatePost_InvalidModel(TimelineUrlGenerator generator) + [MemberData(nameof(TimelineNameGeneratorTestData))] + public async Task CreatePost_InvalidModel(TimelineNameGenerator generator) { using var client = await CreateClientAsUser(); - - { - var res = await client.PostAsJsonAsync(generator(1, "posts"), new TimelinePostCreateRequest { Content = null }); - res.Should().BeInvalidModel(); - } - - { - var res = await client.PostAsJsonAsync(generator(1, "posts"), new TimelinePostCreateRequest { Content = new TimelinePostCreateRequestContent { Type = null } }); - res.Should().BeInvalidModel(); - } - - { - var res = await client.PostAsJsonAsync(generator(1, "posts"), new TimelinePostCreateRequest { Content = new TimelinePostCreateRequestContent { Type = "hahaha" } }); - res.Should().BeInvalidModel(); - } - - { - var res = await client.PostAsJsonAsync(generator(1, "posts"), new TimelinePostCreateRequest { Content = new TimelinePostCreateRequestContent { Type = "text", Text = null } }); - res.Should().BeInvalidModel(); - } - - { - var res = await client.PostAsJsonAsync(generator(1, "posts"), new TimelinePostCreateRequest { Content = new TimelinePostCreateRequestContent { Type = "image", Data = null } }); - res.Should().BeInvalidModel(); - } - - { - // image not base64 - var res = await client.PostAsJsonAsync(generator(1, "posts"), new TimelinePostCreateRequest { Content = new TimelinePostCreateRequestContent { Type = "image", Data = "!!!" } }); - res.Should().BeInvalidModel(); - } - - { - // image base64 not image - var res = await client.PostAsJsonAsync(generator(1, "posts"), new TimelinePostCreateRequest { Content = new TimelinePostCreateRequestContent { Type = "image", Data = Convert.ToBase64String(new byte[] { 0x01, 0x02, 0x03 }) } }); - res.Should().BeInvalidModel(); - } + var postUrl = $"timelines/{generator(1)}/posts"; + await client.TestPostAssertInvalidModelAsync(postUrl, new TimelinePostCreateRequest { Content = null! }); + await client.TestPostAssertInvalidModelAsync(postUrl, new TimelinePostCreateRequest { Content = new TimelinePostCreateRequestContent { Type = null! } }); + await client.TestPostAssertInvalidModelAsync(postUrl, new TimelinePostCreateRequest { Content = new TimelinePostCreateRequestContent { Type = "hahaha" } }); + await client.TestPostAssertInvalidModelAsync(postUrl, new TimelinePostCreateRequest { Content = new TimelinePostCreateRequestContent { Type = "text", Text = null } }); + await client.TestPostAssertInvalidModelAsync(postUrl, new TimelinePostCreateRequest { Content = new TimelinePostCreateRequestContent { Type = "image", Data = null } }); + // image not base64 + await client.TestPostAssertInvalidModelAsync(postUrl, new TimelinePostCreateRequest { Content = new TimelinePostCreateRequestContent { Type = "image", Data = "!!!" } }); + // image base64 not image + await client.TestPostAssertInvalidModelAsync(postUrl, new TimelinePostCreateRequest { Content = new TimelinePostCreateRequestContent { Type = "image", Data = Convert.ToBase64String(new byte[] { 0x01, 0x02, 0x03 }) } }); } [Theory] - [MemberData(nameof(TimelineUrlGeneratorData))] - public async Task ImagePost_ShouldWork(TimelineUrlGenerator generator) + [MemberData(nameof(TimelineNameGeneratorTestData))] + public async Task ImagePost_ShouldWork(TimelineNameGenerator generator) { var imageData = ImageHelper.CreatePngWithSize(100, 200); @@ -673,14 +613,14 @@ namespace Timeline.Tests.IntegratedTests void AssertPostContent(TimelinePostContentInfo content) { content.Type.Should().Be(TimelinePostContentTypes.Image); - content.Url.Should().EndWith(generator(1, $"posts/{postId}/data")); + content.Url.Should().EndWith($"timelines/{generator(1)}/posts/{postId}/data"); content.Text.Should().Be(null); } using var client = await CreateClientAsUser(); { - var res = await client.PostAsJsonAsync(generator(1, "posts"), + var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", new TimelinePostCreateRequest { Content = new TimelinePostCreateRequestContent @@ -689,26 +629,22 @@ namespace Timeline.Tests.IntegratedTests Data = Convert.ToBase64String(imageData) } }); - var body = res.Should().HaveStatusCode(200) - .And.HaveJsonBody().Which; postId = body.Id; - postImageUrl = body.Content.Url; + postImageUrl = body.Content!.Url!; AssertPostContent(body.Content); } { - var res = await client.GetAsync(generator(1, "posts")); - var body = res.Should().HaveStatusCode(200) - .And.HaveJsonBody().Which; + var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts"); body.Should().HaveCount(1); var post = body[0]; post.Id.Should().Be(postId); - AssertPostContent(post.Content); + AssertPostContent(post.Content!); } { - var res = await client.GetAsync(generator(1, $"posts/{postId}/data")); - res.Content.Headers.ContentType.MediaType.Should().Be("image/png"); + var res = await client.GetAsync($"timelines/{generator(1)}/posts/{postId}/data"); + res.Content.Headers.ContentType!.MediaType.Should().Be("image/png"); var data = await res.Content.ReadAsByteArrayAsync(); var image = Image.Load(data, out var format); image.Width.Should().Be(100); @@ -716,25 +652,13 @@ namespace Timeline.Tests.IntegratedTests format.Name.Should().Be(PngFormat.Instance.Name); } - { - await CacheTestHelper.TestCache(client, generator(1, $"posts/{postId}/data")); - } + await CacheTestHelper.TestCache(client, $"timelines/{generator(1)}/posts/{postId}/data"); + await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{postId}/data", true); + await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{postId}/data", false); { - var res = await client.DeleteAsync(generator(1, $"posts/{postId}")); - res.Should().BeDelete(true); - } - - { - var res = await client.DeleteAsync(generator(1, $"posts/{postId}")); - res.Should().BeDelete(false); - } - - { - var res = await client.GetAsync(generator(1, "posts")); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.Should().BeEmpty(); + var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts/{postId}/data"); + body.Should().BeEmpty(); } { @@ -746,83 +670,60 @@ namespace Timeline.Tests.IntegratedTests } [Theory] - [MemberData(nameof(TimelineUrlGeneratorData))] - public async Task ImagePost_400(TimelineUrlGenerator generator) + [MemberData(nameof(TimelineNameGeneratorTestData))] + public async Task ImagePost_400(TimelineNameGenerator generator) { using var client = await CreateClientAsUser(); - { - var res = await client.GetAsync(generator(1, "posts/11234/data")); - res.Should().HaveStatusCode(404) - .And.HaveCommonBody(ErrorCodes.TimelineController.PostNotExist); - } + await client.TestGetAssertErrorAsync($"timelines/{generator(1)}/posts/11234/data", errorCode: ErrorCodes.TimelineController.PostNotExist); long postId; { - var res = await client.PostAsJsonAsync(generator(1, "posts"), - TimelineHelper.TextPostCreateRequest("aaa")); - var body = res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which; + var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelineHelper.TextPostCreateRequest("aaa")); postId = body.Id; } - { - var res = await client.GetAsync(generator(1, $"posts/{postId}/data")); - res.Should().HaveStatusCode(400) - .And.HaveCommonBody(ErrorCodes.TimelineController.PostNoData); - } + await client.TestGetAssertErrorAsync($"timelines/{generator(1)}/posts/{postId}/data", errorCode: ErrorCodes.TimelineController.PostNoData); } [Theory] - [MemberData(nameof(TimelineUrlGeneratorData))] - public async Task Timeline_LastModified(TimelineUrlGenerator generator) + [MemberData(nameof(TimelineNameGeneratorTestData))] + public async Task Timeline_LastModified(TimelineNameGenerator generator) { using var client = await CreateClientAsUser(); DateTime lastModified; { - var res = await client.GetAsync(generator(1)); - lastModified = res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.LastModified; + var body = await client.GetTimelineAsync(generator(1)); + lastModified = body.LastModified; } await Task.Delay(1000); { - var res = await client.PatchAsJsonAsync(generator(1), new TimelinePatchRequest { Description = "123" }); - lastModified = res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.LastModified.Should().BeAfter(lastModified).And.Subject.Value; + var body = await client.PatchTimelineAsync(generator(1), new() { Description = "123" }); + lastModified = body.LastModified.Should().BeAfter(lastModified).And.Subject!.Value; } { - var res = await client.GetAsync(generator(1)); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.LastModified.Should().Be(lastModified); + var body = await client.GetTimelineAsync(generator(1)); + body.LastModified.Should().Be(lastModified); } await Task.Delay(1000); - { - var res = await client.PutAsync(generator(1, "members/user2"), null); - res.Should().HaveStatusCode(200); - } + await client.PutTimelineMemberAsync(generator(1), "user2"); { - var res = await client.GetAsync(generator(1)); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.LastModified.Should().BeAfter(lastModified); + var body = await client.GetTimelineAsync(generator(1)); + body.LastModified.Should().BeAfter(lastModified); } } [Theory] - [MemberData(nameof(TimelineUrlGeneratorData))] - public async Task Post_ModifiedSince(TimelineUrlGenerator generator) + [MemberData(nameof(TimelineNameGeneratorTestData))] + public async Task Post_ModifiedSince(TimelineNameGenerator generator) { using var client = await CreateClientAsUser(); @@ -831,32 +732,24 @@ namespace Timeline.Tests.IntegratedTests foreach (var content in postContentList) { - var res = await client.PostAsJsonAsync(generator(1, "posts"), + var post = await client.TestPostAsync($"timelines/{generator(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(generator(1, $"posts/{posts[2].Id}")); - res.Should().BeDelete(true); - } + await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{posts[2].Id}", true); { - var res = await client.GetAsync(generator(1, "posts", - new Dictionary { { "modifiedSince", posts[1].LastUpdated.ToString("s", CultureInfo.InvariantCulture) } })); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody>() - .Which.Should().HaveCount(2) - .And.Subject.Select(p => p.Content.Text).Should().Equal("b", "d"); + var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts?modifiedSince={posts[1].LastUpdated.ToString("s", CultureInfo.InvariantCulture) }"); + body.Should().HaveCount(2) + .And.Subject.Select(p => p.Content!.Text).Should().Equal("b", "d"); } } [Theory] - [MemberData(nameof(TimelineUrlGeneratorData))] - public async Task PostList_IncludeDeleted(TimelineUrlGenerator urlGenerator) + [MemberData(nameof(TimelineNameGeneratorTestData))] + public async Task PostList_IncludeDeleted(TimelineNameGenerator generator) { using var client = await CreateClientAsUser(); @@ -865,23 +758,18 @@ namespace Timeline.Tests.IntegratedTests foreach (var content in postContentList) { - var res = await client.PostAsJsonAsync(urlGenerator(1, "posts"), + var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", new TimelinePostCreateRequest { Content = new TimelinePostCreateRequestContent { Text = content, Type = TimelinePostContentTypes.Text } }); - posts.Add(res.Should().HaveStatusCode(200) - .And.HaveJsonBody().Which); + posts.Add(body); } 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); + await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{id}", true); } { - var res = await client.GetAsync(urlGenerator(1, "posts", new Dictionary { ["includeDeleted"] = "true" })); - posts = res.Should().HaveStatusCode(200) - .And.HaveJsonBody>() - .Which; + posts = await client.TestGetAsync>($"timelines/{generator(1)}/posts?includeDeleted=true"); 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); @@ -889,8 +777,8 @@ namespace Timeline.Tests.IntegratedTests } [Theory] - [MemberData(nameof(TimelineUrlGeneratorData))] - public async Task Post_ModifiedSince_And_IncludeDeleted(TimelineUrlGenerator urlGenerator) + [MemberData(nameof(TimelineNameGeneratorTestData))] + public async Task Post_ModifiedSince_And_IncludeDeleted(TimelineNameGenerator generator) { using var client = await CreateClientAsUser(); @@ -899,29 +787,17 @@ namespace Timeline.Tests.IntegratedTests foreach (var (content, index) in postContentList.Select((v, i) => (v, i))) { - var res = await client.PostAsJsonAsync(urlGenerator(1, "posts"), + var post = await client.TestPostAsync($"timelines/{generator(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); - } + await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{posts[2].Id}", 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 = await client.TestGetAsync>($"timelines/{generator(1)}/posts?modifiedSince={posts[1].LastUpdated.ToString("s", CultureInfo.InvariantCulture)}&includeDeleted=true"); 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); @@ -929,8 +805,8 @@ namespace Timeline.Tests.IntegratedTests } [Theory] - [MemberData(nameof(TimelineUrlGeneratorData))] - public async Task Timeline_Get_IfModifiedSince_And_CheckUniqueId(TimelineUrlGenerator urlGenerator) + [MemberData(nameof(TimelineNameGeneratorTestData))] + public async Task Timeline_Get_IfModifiedSince_And_CheckUniqueId(TimelineNameGenerator urlGenerator) { using var client = await CreateClientAsUser(); @@ -939,96 +815,71 @@ namespace Timeline.Tests.IntegratedTests string uniqueId; { - var res = await client.GetAsync(urlGenerator(1)); - var body = res.Should().HaveStatusCode(200) - .And.HaveJsonBody().Which; + var body = await client.GetTimelineAsync(urlGenerator(1)); timeline = body; lastModifiedTime = body.LastModified; uniqueId = body.UniqueId; } { - using var req = new HttpRequestMessage - { - RequestUri = new Uri(client.BaseAddress, urlGenerator(1)), - Method = HttpMethod.Get, - }; - req.Headers.IfModifiedSince = lastModifiedTime.AddSeconds(1); - var res = await client.SendAsync(req); - res.Should().HaveStatusCode(304); + await client.TestGetAsync($"timelines/{urlGenerator(1)}", + expectedStatusCode: HttpStatusCode.NotModified, + headerSetup: (headers, _) => + { + headers.IfModifiedSince = lastModifiedTime.AddSeconds(1); + }); } { - using var req = new HttpRequestMessage - { - RequestUri = new Uri(client.BaseAddress, urlGenerator(1)), - Method = HttpMethod.Get, - }; - req.Headers.IfModifiedSince = lastModifiedTime.AddSeconds(-1); - var res = await client.SendAsync(req); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.Should().BeEquivalentTo(timeline); + + var body = await client.TestGetAsync($"timelines/{urlGenerator(1)}", + expectedStatusCode: HttpStatusCode.NotModified, + headerSetup: (headers, _) => + { + headers.IfModifiedSince = lastModifiedTime.AddSeconds(-1); + }); + body.Should().BeEquivalentTo(timeline); } { - var res = await client.GetAsync(urlGenerator(1, null, - new Dictionary { { "ifModifiedSince", lastModifiedTime.AddSeconds(1).ToString("s", CultureInfo.InvariantCulture) } })); - res.Should().HaveStatusCode(304); + await client.TestGetAsync($"timelines/{urlGenerator(1)}?ifModifiedSince={lastModifiedTime.AddSeconds(1).ToString("s", CultureInfo.InvariantCulture) }", expectedStatusCode: HttpStatusCode.NotModified); } { - var res = await client.GetAsync(urlGenerator(1, null, - new Dictionary { { "ifModifiedSince", lastModifiedTime.AddSeconds(-1).ToString("s", CultureInfo.InvariantCulture) } })); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.Should().BeEquivalentTo(timeline); + var body = await client.TestGetAsync($"timelines/{urlGenerator(1)}?ifModifiedSince={lastModifiedTime.AddSeconds(-1).ToString("s", CultureInfo.InvariantCulture) }"); + body.Should().BeEquivalentTo(timeline); } { - var res = await client.GetAsync(urlGenerator(1, null, - new Dictionary { { "ifModifiedSince", lastModifiedTime.AddSeconds(1).ToString("s", CultureInfo.InvariantCulture) }, - {"checkUniqueId", uniqueId } })); - res.Should().HaveStatusCode(304); + await client.TestGetAsync($"timelines/{urlGenerator(1)}?ifModifiedSince={lastModifiedTime.AddSeconds(1).ToString("s", CultureInfo.InvariantCulture) }&checkUniqueId={uniqueId}", expectedStatusCode: HttpStatusCode.NotModified); } { var testUniqueId = (uniqueId[0] == 'a' ? "b" : "a") + uniqueId[1..]; - var res = await client.GetAsync(urlGenerator(1, null, - new Dictionary { { "ifModifiedSince", lastModifiedTime.AddSeconds(1).ToString("s", CultureInfo.InvariantCulture) }, - {"checkUniqueId", testUniqueId } })); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.Should().BeEquivalentTo(timeline); + var body = await client.TestGetAsync($"timelines/{urlGenerator(1)}?ifModifiedSince={lastModifiedTime.AddSeconds(1).ToString("s", CultureInfo.InvariantCulture) }&checkUniqueId={testUniqueId}", expectedStatusCode: HttpStatusCode.NotModified); + body.Should().BeEquivalentTo(timeline); } } [Theory] - [MemberData(nameof(TimelineUrlGeneratorData))] - public async Task Title(TimelineUrlGenerator urlGenerator) + [MemberData(nameof(TimelineNameGeneratorTestData))] + public async Task Title(TimelineNameGenerator generator) { using var client = await CreateClientAsUser(); { - var res = await client.GetAsync(urlGenerator(1)); - var timeline = res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which; - timeline.Title.Should().Be(timeline.Name); + var body = await client.GetTimelineAsync(generator(1)); + body.Title.Should().Be(body.Name); } { - var res = await client.PatchAsJsonAsync(urlGenerator(1), new TimelinePatchRequest { Title = "atitle" }); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.Title.Should().Be("atitle"); + var body = await client.PatchTimelineAsync(generator(1), new TimelinePatchRequest { Title = "atitle" }); + body.Title.Should().Be("atitle"); } { - var res = await client.GetAsync(urlGenerator(1)); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.Title.Should().Be("atitle"); + var body = await client.GetTimelineAsync(generator(1)); + body.Title.Should().Be("atitle"); } } @@ -1037,53 +888,35 @@ namespace Timeline.Tests.IntegratedTests { { using var client = await CreateDefaultClient(); - var res = await client.PostAsJsonAsync("timelineop/changename", new TimelineChangeNameRequest { OldName = "t1", NewName = "tttttttt" }); - res.Should().HaveStatusCode(401); + await client.TestPostAssertUnauthorizedAsync("timelineop/changename", new TimelineChangeNameRequest { OldName = "t1", NewName = "tttttttt" }); } { using var client = await CreateClientAs(2); - var res = await client.PostAsJsonAsync("timelineop/changename", new TimelineChangeNameRequest { OldName = "t1", NewName = "tttttttt" }); - res.Should().HaveStatusCode(403); + await client.TestPostAssertForbiddenAsync("timelineop/changename", new TimelineChangeNameRequest { OldName = "t1", NewName = "tttttttt" }); } using (var client = await CreateClientAsUser()) { - { - var res = await client.PostAsJsonAsync("timelineop/changename", new TimelineChangeNameRequest { OldName = "!!!", NewName = "tttttttt" }); - res.Should().BeInvalidModel(); - } + await client.TestPostAssertInvalidModelAsync("timelineop/changename", new TimelineChangeNameRequest { OldName = "!!!", NewName = "tttttttt" }); - { - var res = await client.PostAsJsonAsync("timelineop/changename", new TimelineChangeNameRequest { OldName = "ttt", NewName = "!!!!" }); - res.Should().BeInvalidModel(); - } + await client.TestPostAssertInvalidModelAsync("timelineop/changename", new TimelineChangeNameRequest { OldName = "ttt", NewName = "!!!!" }); - { - var res = await client.PostAsJsonAsync("timelineop/changename", new TimelineChangeNameRequest { OldName = "ttttt", NewName = "tttttttt" }); - res.Should().HaveStatusCode(400).And.HaveCommonBody().Which.Code.Should().Be(ErrorCodes.TimelineController.NotExist); - } + await client.TestPostAssertInvalidModelAsync("timelineop/changename", new TimelineChangeNameRequest { OldName = "ttttt", NewName = "tttttttt" }); + await client.TestPostAssertInvalidModelAsync("timelineop/changename", new TimelineChangeNameRequest { OldName = "t1", NewName = "newt" }); - { - var res = await client.PostAsJsonAsync("timelineop/changename", new TimelineChangeNameRequest { OldName = "t1", NewName = "newt" }); - res.Should().HaveStatusCode(200).And.HaveJsonBody().Which.Name.Should().Be("newt"); - } - - { - var res = await client.GetAsync("timelines/t1"); - res.Should().HaveStatusCode(404); - } + await client.TestGetAsync("timelines/t1", expectedStatusCode: HttpStatusCode.NotFound); { - var res = await client.GetAsync("timelines/newt"); - res.Should().HaveStatusCode(200).And.HaveJsonBody().Which.Name.Should().Be("newt"); + var body = await client.TestGetAsync("timelines/newt"); + body.Name.Should().Be("newt"); } } } [Theory] - [MemberData(nameof(TimelineUrlGeneratorData))] - public async Task PostDataETag(TimelineUrlGenerator urlGenerator) + [MemberData(nameof(TimelineNameGeneratorTestData))] + public async Task PostDataETag(TimelineNameGenerator generator) { using var client = await CreateClientAsUser(); @@ -1091,7 +924,7 @@ namespace Timeline.Tests.IntegratedTests string etag; { - var res = await client.PostAsJsonAsync(urlGenerator(1, "posts"), new TimelinePostCreateRequest + var body = await client.TestPostAsync($"timelines/{generator(1)}/posts", new TimelinePostCreateRequest { Content = new TimelinePostCreateRequestContent { @@ -1099,19 +932,17 @@ namespace Timeline.Tests.IntegratedTests Data = Convert.ToBase64String(ImageHelper.CreatePngWithSize(100, 50)) } }); - res.Should().HaveStatusCode(200); - var body = await res.ReadBodyAsJsonAsync(); - body.Content.ETag.Should().NotBeNullOrEmpty(); + body.Content!.ETag.Should().NotBeNullOrEmpty(); id = body.Id; - etag = body.Content.ETag; + etag = body.Content.ETag!; } { - var res = await client.GetAsync(urlGenerator(1, $"posts/{id}/data")); - res.Should().HaveStatusCode(200); + var res = await client.GetAsync($"timelines/{generator(1)}/posts{id}/data"); + res.StatusCode.Should().Be(HttpStatusCode.OK); res.Headers.ETag.Should().NotBeNull(); - res.Headers.ETag.ToString().Should().Be(etag); + res.Headers.ETag!.ToString().Should().Be(etag); } } } -- cgit v1.2.3 From f9a2e795207287db997d0b00eef0893d99f388e0 Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 15 Nov 2020 16:39:09 +0800 Subject: ... --- .../IntegratedTests/HttpClientTestExtensions.cs | 32 ++++- .../IntegratedTests/UserAvatarTest.cs | 151 ++++++--------------- 2 files changed, 70 insertions(+), 113 deletions(-) (limited to 'BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs') diff --git a/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs b/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs index 5091a7f7..3d85c2ae 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs @@ -126,6 +126,11 @@ namespace Timeline.Tests.IntegratedTests await client.TestJsonSendAssertErrorAsync(HttpMethod.Delete, url, jsonBody, expectedStatusCode, errorCode, headerSetup); } + public static async Task TestSendAssertInvalidModelAsync(this HttpClient client, HttpMethod method, string url, HttpContent? body = null) + { + await client.TestSendAssertErrorAsync(method, url, body, expectedStatusCode: HttpStatusCode.BadRequest, errorCode: ErrorCodes.Common.InvalidModel); + } + public static async Task TestJsonSendAssertInvalidModelAsync(this HttpClient client, HttpMethod method, string url, object? jsonBody = null) { await client.TestJsonSendAssertErrorAsync(method, url, jsonBody, expectedStatusCode: HttpStatusCode.BadRequest, errorCode: ErrorCodes.Common.InvalidModel); @@ -186,12 +191,37 @@ namespace Timeline.Tests.IntegratedTests await client.TestJsonSendAssertForbiddenAsync(HttpMethod.Delete, url, jsonBody, errorCode, headerSetup); } + public static async Task TestJsonSendAssertNotFoundAsync(this HttpClient client, HttpMethod method, string url, object? jsonBody = null, int? errorCode = null, HeaderSetup? headerSetup = null) + { + await client.TestJsonSendAssertErrorAsync(method, url, jsonBody, HttpStatusCode.NotFound, errorCode, headerSetup); + } + + public static async Task TestGetAssertNotFoundAsync(this HttpClient client, string url, object? jsonBody = null, int? errorCode = null, HeaderSetup? headerSetup = null) + { + await client.TestJsonSendAssertNotFoundAsync(HttpMethod.Get, url, jsonBody, errorCode, headerSetup); + } + public static async Task TestPutByteArrayAsync(this HttpClient client, string url, byte[] body, string mimeType, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) { using var content = new ByteArrayContent(body); content.Headers.ContentLength = body.Length; content.Headers.ContentType = new MediaTypeHeaderValue(mimeType); - return await client.TestSendAsync(HttpMethod.Put, url, content); + return await client.TestSendAsync(HttpMethod.Put, url, content, expectedStatusCode); + } + + public static async Task TestPutByteArrayAssertErrorAsync(this HttpClient client, string url, byte[] body, string mimeType, HttpStatusCode expectedStatusCode = HttpStatusCode.BadRequest, int? errorCode = null) + { + var res = await client.TestPutByteArrayAsync(url, body, mimeType, expectedStatusCode); + if (errorCode.HasValue) + { + var resBody = await AssertJsonBodyAsync(res); + resBody.Code.Should().Be(errorCode.Value); + } + } + + public static async Task TestPutByteArrayAssertInvalidModelAsync(this HttpClient client, string url, byte[] body, string mimeType) + { + await client.TestPutByteArrayAssertErrorAsync(url, body, mimeType, errorCode: ErrorCodes.Common.InvalidModel); } } } diff --git a/BackEnd/Timeline.Tests/IntegratedTests/UserAvatarTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/UserAvatarTest.cs index 854a4ee6..79a3c2fa 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/UserAvatarTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/UserAvatarTest.cs @@ -31,117 +31,83 @@ namespace Timeline.Tests.IntegratedTests using (var client = await CreateClientAsUser()) { - { - var res = await client.GetAsync("users/usernotexist/avatar"); - res.Should().HaveStatusCode(404) - .And.HaveCommonBody() - .Which.Code.Should().Be(ErrorCodes.UserCommon.NotExist); - } + await client.TestGetAssertNotFoundAsync("users/usernotexist/avatar", errorCode: ErrorCodes.UserCommon.NotExist); var env = TestApp.Host.Services.GetRequiredService(); var defaultAvatarData = await File.ReadAllBytesAsync(Path.Combine(env.ContentRootPath, "default-avatar.png")); - async Task GetReturnDefault(string username = "user1") + async Task TestAvatar(string username, byte[] data) { var res = await client.GetAsync($"users/{username}/avatar"); - res.Should().HaveStatusCode(200); - res.Content.Headers.ContentType.MediaType.Should().Be("image/png"); + res.StatusCode.Should().Be(HttpStatusCode.OK); + var contentTypeHeader = res.Content.Headers.ContentType; + contentTypeHeader.Should().NotBeNull(); + contentTypeHeader!.MediaType.Should().Be("image/png"); var body = await res.Content.ReadAsByteArrayAsync(); - body.Should().Equal(defaultAvatarData); + body.Should().Equal(data); } - { - var res = await client.GetAsync("users/user1/avatar"); - res.Should().HaveStatusCode(200); - res.Content.Headers.ContentType.MediaType.Should().Be("image/png"); - var body = await res.Content.ReadAsByteArrayAsync(); - body.Should().Equal(defaultAvatarData); - } + await TestAvatar("user1", defaultAvatarData); await CacheTestHelper.TestCache(client, "users/user1/avatar"); - await GetReturnDefault("admin"); + await TestAvatar("admin", defaultAvatarData); { using var content = new ByteArrayContent(new[] { (byte)0x00 }); content.Headers.ContentLength = null; content.Headers.ContentType = new MediaTypeHeaderValue("image/png"); - var res = await client.PutAsync("users/user1/avatar", content); - res.Should().BeInvalidModel(); + await client.TestSendAssertInvalidModelAsync(HttpMethod.Put, "users/user1/avatar", content); } { using var content = new ByteArrayContent(new[] { (byte)0x00 }); content.Headers.ContentLength = 1; - var res = await client.PutAsync("users/user1/avatar", content); - res.Should().BeInvalidModel(); + await client.TestSendAssertInvalidModelAsync(HttpMethod.Put, "users/user1/avatar", content); } { using var content = new ByteArrayContent(new[] { (byte)0x00 }); content.Headers.ContentLength = 0; content.Headers.ContentType = new MediaTypeHeaderValue("image/png"); - var res = await client.PutAsync("users/user1/avatar", content); - res.Should().BeInvalidModel(); + await client.TestSendAssertInvalidModelAsync(HttpMethod.Put, "users/user1/avatar", content); } { - var res = await client.PutByteArrayAsync("users/user1/avatar", new[] { (byte)0x00 }, "image/notaccept"); - res.Should().HaveStatusCode(HttpStatusCode.UnsupportedMediaType); + await client.TestPutByteArrayAsync("users/user1/avatar", new[] { (byte)0x00 }, "image/notaccept", expectedStatusCode: HttpStatusCode.UnsupportedMediaType); } { using var content = new ByteArrayContent(new[] { (byte)0x00 }); content.Headers.ContentLength = 1000 * 1000 * 11; content.Headers.ContentType = new MediaTypeHeaderValue("image/png"); - var res = await client.PutAsync("users/user1/avatar", content); - res.Should().HaveStatusCode(HttpStatusCode.BadRequest) - .And.HaveCommonBody().Which.Code.Should().Be(ErrorCodes.Common.Content.TooBig); + await client.TestSendAssertErrorAsync(HttpMethod.Put, "users/user1/avatar", content, errorCode: ErrorCodes.Common.Content.TooBig); } { using var content = new ByteArrayContent(new[] { (byte)0x00 }); content.Headers.ContentLength = 2; content.Headers.ContentType = new MediaTypeHeaderValue("image/png"); - var res = await client.PutAsync("users/user1/avatar", content); - res.Should().BeInvalidModel(); + await client.TestSendAssertInvalidModelAsync(HttpMethod.Put, "users/user1/avatar", content); } { using var content = new ByteArrayContent(new[] { (byte)0x00, (byte)0x01 }); content.Headers.ContentLength = 1; content.Headers.ContentType = new MediaTypeHeaderValue("image/png"); - var res = await client.PutAsync("users/user1/avatar", content); - res.Should().BeInvalidModel(); - } + await client.TestSendAssertInvalidModelAsync(HttpMethod.Put, "users/user1/avatar", content); - { - var res = await client.PutByteArrayAsync("users/user1/avatar", new[] { (byte)0x00 }, "image/png"); - res.Should().HaveStatusCode(HttpStatusCode.BadRequest) - .And.HaveCommonBody().Which.Code.Should().Be(ErrorCodes.UserAvatar.BadFormat_CantDecode); } { - var res = await client.PutByteArrayAsync("users/user1/avatar", mockAvatar.Data, "image/jpeg"); - res.Should().HaveStatusCode(HttpStatusCode.BadRequest) - .And.HaveCommonBody().Which.Code.Should().Be(ErrorCodes.UserAvatar.BadFormat_UnmatchedFormat); + await client.TestPutByteArrayAssertErrorAsync("users/user1/avatar", new[] { (byte)0x00 }, "image/png", errorCode: ErrorCodes.UserAvatar.BadFormat_CantDecode); + await client.TestPutByteArrayAssertErrorAsync("users/user1/avatar", mockAvatar.Data, "image/png", errorCode: ErrorCodes.UserAvatar.BadFormat_UnmatchedFormat); + await client.TestPutByteArrayAssertErrorAsync("users/user1/avatar", ImageHelper.CreatePngWithSize(100, 200), "image/png", errorCode: ErrorCodes.UserAvatar.BadFormat_BadSize); } { - var res = await client.PutByteArrayAsync("users/user1/avatar", ImageHelper.CreatePngWithSize(100, 200), "image/png"); - res.Should().HaveStatusCode(HttpStatusCode.BadRequest) - .And.HaveCommonBody().Which.Code.Should().Be(ErrorCodes.UserAvatar.BadFormat_BadSize); - } - - { - var res = await client.PutByteArrayAsync("users/user1/avatar", mockAvatar.Data, mockAvatar.Type); - res.Should().HaveStatusCode(HttpStatusCode.OK); - - var res2 = await client.GetAsync("users/user1/avatar"); - res2.Should().HaveStatusCode(200); - res2.Content.Headers.ContentType.MediaType.Should().Be(mockAvatar.Type); - var body = await res2.Content.ReadAsByteArrayAsync(); - body.Should().Equal(mockAvatar.Data); + await client.TestPutByteArrayAsync("users/user1/avatar", mockAvatar.Data, mockAvatar.Type); + await TestAvatar("user1", mockAvatar.Data); } IEnumerable<(string, IImageFormat)> formats = new (string, IImageFormat)[] @@ -153,74 +119,36 @@ namespace Timeline.Tests.IntegratedTests foreach ((var mimeType, var format) in formats) { - var res = await client.PutByteArrayAsync("users/user1/avatar", ImageHelper.CreateImageWithSize(100, 100, format), mimeType); - res.Should().HaveStatusCode(HttpStatusCode.OK); + await client.TestPutByteArrayAsync("users/user1/avatar", ImageHelper.CreateImageWithSize(100, 100, format), mimeType); } - { - var res = await client.PutByteArrayAsync("users/admin/avatar", new[] { (byte)0x00 }, "image/png"); - res.Should().HaveStatusCode(HttpStatusCode.Forbidden) - .And.HaveCommonBody().Which.Code.Should().Be(ErrorCodes.Common.Forbid); - } + await client.TestPutByteArrayAssertErrorAsync("users/admin/avatar", new[] { (byte)0x00 }, "image/png", + expectedStatusCode: HttpStatusCode.Forbidden, errorCode: ErrorCodes.Common.Forbid); - { - var res = await client.DeleteAsync("users/admin/avatar"); - res.Should().HaveStatusCode(HttpStatusCode.Forbidden) - .And.HaveCommonBody().Which.Code.Should().Be(ErrorCodes.Common.Forbid); - } + await client.TestDeleteAssertForbiddenAsync("users/admin/avatar", errorCode: ErrorCodes.Common.Forbid); for (int i = 0; i < 2; i++) // double delete should work. { - var res = await client.DeleteAsync("users/user1/avatar"); - res.Should().HaveStatusCode(200); - await GetReturnDefault(); + await client.TestDeleteAsync("users/user1/avatar"); + await TestAvatar("user1", defaultAvatarData); } } // Authorization check. using (var client = await CreateClientAsAdministrator()) { - { - var res = await client.PutByteArrayAsync("users/user1/avatar", mockAvatar.Data, mockAvatar.Type); - res.Should().HaveStatusCode(HttpStatusCode.OK); - } - - { - var res = await client.DeleteAsync("users/user1/avatar"); - res.Should().HaveStatusCode(HttpStatusCode.OK); - } - - { - var res = await client.PutByteArrayAsync("users/usernotexist/avatar", new[] { (byte)0x00 }, "image/png"); - res.Should().HaveStatusCode(400) - .And.HaveCommonBody() - .Which.Code.Should().Be(ErrorCodes.UserCommon.NotExist); - } - - { - var res = await client.DeleteAsync("users/usernotexist/avatar"); - res.Should().HaveStatusCode(400) - .And.HaveCommonBody().Which.Code.Should().Be(ErrorCodes.UserCommon.NotExist); - } + await client.TestPutByteArrayAsync("users/user1/avatar", mockAvatar.Data, mockAvatar.Type); + await client.TestDeleteAsync("users/user1/avatar"); + await client.TestPutByteArrayAssertErrorAsync("users/usernotexist/avatar", new[] { (byte)0x00 }, "image/png", errorCode: ErrorCodes.UserCommon.NotExist); + await client.TestDeleteAssertErrorAsync("users/usernotexist/avatar", errorCode: ErrorCodes.UserCommon.NotExist); } // bad username check using (var client = await CreateClientAsAdministrator()) { - { - var res = await client.GetAsync("users/u!ser/avatar"); - res.Should().BeInvalidModel(); - } - - { - var res = await client.PutByteArrayAsync("users/u!ser/avatar", ImageHelper.CreatePngWithSize(100, 100), "image/png"); - res.Should().BeInvalidModel(); - } - - { - var res = await client.DeleteAsync("users/u!ser/avatar"); - res.Should().BeInvalidModel(); - } + await client.TestGetAssertInvalidModelAsync("users/u!ser/avatar"); + await client.TestPutByteArrayAssertInvalidModelAsync("users/u!ser/avatar", ImageHelper.CreatePngWithSize(100, 100), "image/png"); + await client.TestDeleteAssertInvalidModelAsync("users/u!ser/avatar"); } } @@ -229,22 +157,21 @@ namespace Timeline.Tests.IntegratedTests { using var client = await CreateClientAsUser(); - EntityTagHeaderValue etag; + EntityTagHeaderValue? etag; { var image = ImageHelper.CreatePngWithSize(100, 100); - var res = await client.PutByteArrayAsync("users/user1/avatar", image, PngFormat.Instance.DefaultMimeType); - res.Should().HaveStatusCode(200); + var res = await client.TestPutByteArrayAsync("users/user1/avatar", image, PngFormat.Instance.DefaultMimeType); etag = res.Headers.ETag; etag.Should().NotBeNull(); - etag.Tag.Should().NotBeNullOrEmpty(); + etag!.Tag.Should().NotBeNullOrEmpty(); } { var res = await client.GetAsync("users/user1/avatar"); - res.Should().HaveStatusCode(200); + res.StatusCode.Should().Be(HttpStatusCode.OK); res.Headers.ETag.Should().Be(etag); - res.Headers.ETag.Tag.Should().Be(etag.Tag); + res.Headers.ETag!.Tag.Should().Be(etag.Tag); } } } -- cgit v1.2.3 From 10d7b5aab88701bfe76c33c0573bec596cca297a Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 15 Nov 2020 16:50:01 +0800 Subject: ... --- .../IntegratedTests/HttpClientTestExtensions.cs | 15 ++ .../IntegratedTests/UserPermissionTest.cs | 175 +++++---------------- 2 files changed, 51 insertions(+), 139 deletions(-) (limited to 'BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs') diff --git a/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs b/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs index 3d85c2ae..cd884c5e 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs @@ -146,6 +146,11 @@ namespace Timeline.Tests.IntegratedTests await client.TestJsonSendAssertInvalidModelAsync(HttpMethod.Post, url, jsonBody); } + public static async Task TestPutAssertInvalidModelAsync(this HttpClient client, string url, object? jsonBody = null) + { + await client.TestJsonSendAssertInvalidModelAsync(HttpMethod.Put, url, jsonBody); + } + public static async Task TestDeleteAssertInvalidModelAsync(this HttpClient client, string url, object? jsonBody = null) { await client.TestJsonSendAssertInvalidModelAsync(HttpMethod.Delete, url, jsonBody); @@ -201,6 +206,16 @@ namespace Timeline.Tests.IntegratedTests await client.TestJsonSendAssertNotFoundAsync(HttpMethod.Get, url, jsonBody, errorCode, headerSetup); } + public static async Task TestPutAssertNotFoundAsync(this HttpClient client, string url, object? jsonBody = null, int? errorCode = null, HeaderSetup? headerSetup = null) + { + await client.TestJsonSendAssertNotFoundAsync(HttpMethod.Put, url, jsonBody, errorCode, headerSetup); + } + + public static async Task TestDeleteAssertNotFoundAsync(this HttpClient client, string url, object? jsonBody = null, int? errorCode = null, HeaderSetup? headerSetup = null) + { + await client.TestJsonSendAssertNotFoundAsync(HttpMethod.Delete, url, jsonBody, errorCode, headerSetup); + } + public static async Task TestPutByteArrayAsync(this HttpClient client, string url, byte[] body, string mimeType, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) { using var content = new ByteArrayContent(body); diff --git a/BackEnd/Timeline.Tests/IntegratedTests/UserPermissionTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/UserPermissionTest.cs index 80f31be9..418dd0f9 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/UserPermissionTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/UserPermissionTest.cs @@ -2,11 +2,9 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Net; using System.Threading.Tasks; using Timeline.Models.Http; using Timeline.Services; -using Timeline.Tests.Helpers; using Xunit; namespace Timeline.Tests.IntegratedTests @@ -19,9 +17,7 @@ namespace Timeline.Tests.IntegratedTests public async Task RootUserShouldReturnAllPermissions() { using var client = await CreateDefaultClient(); - var res = await client.GetAsync("users/admin"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - var body = await res.Should().HaveAndGetJsonBodyAsync(); + var body = await client.GetUserAsync("admin"); body.Permissions.Should().BeEquivalentTo(Enum.GetNames()); } @@ -29,9 +25,7 @@ namespace Timeline.Tests.IntegratedTests public async Task NonRootUserShouldReturnNonPermissions() { using var client = await CreateDefaultClient(); - var res = await client.GetAsync("users/user1"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - var body = await res.Should().HaveAndGetJsonBodyAsync(); + var body = await client.GetUserAsync("user1"); body.Permissions.Should().BeEmpty(); } @@ -46,27 +40,17 @@ namespace Timeline.Tests.IntegratedTests { using var client = await CreateClientAsAdministrator(); - { - var res = await client.DeleteAsync($"users/admin/permissions/{permission}"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - } + await client.TestDeleteAsync($"users/admin/permissions/{permission}"); { - var res = await client.GetAsync("users/admin"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - var body = await res.Should().HaveAndGetJsonBodyAsync(); + var body = await client.GetUserAsync("admin"); body.Permissions.Should().BeEquivalentTo(Enum.GetNames()); } - { - var res = await client.PutAsync($"users/admin/permissions/{permission}"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - } + await client.TestPutAsync($"users/admin/permissions/{permission}"); { - var res = await client.GetAsync("users/admin"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - var body = await res.Should().HaveAndGetJsonBodyAsync(); + var body = await client.GetUserAsync("users/admin"); body.Permissions.Should().BeEquivalentTo(Enum.GetNames()); } } @@ -77,27 +61,17 @@ namespace Timeline.Tests.IntegratedTests { using var client = await CreateClientAsAdministrator(); - { - var res = await client.PutAsync($"users/user1/permissions/{permission}"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - } + await client.TestPutAsync($"users/user1/permissions/{permission}"); { - var res = await client.GetAsync("users/user1"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - var body = await res.Should().HaveAndGetJsonBodyAsync(); + var body = await client.GetUserAsync("user1"); body.Permissions.Should().BeEquivalentTo(permission.ToString()); } - { - var res = await client.DeleteAsync($"users/user1/permissions/{permission}"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - } + await client.TestDeleteAsync($"users/user1/permissions/{permission}"); { - var res = await client.GetAsync("users/user1"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - var body = await res.Should().HaveAndGetJsonBodyAsync(); + var body = await client.GetUserAsync("user1"); body.Permissions.Should().BeEmpty(); } } @@ -108,27 +82,17 @@ namespace Timeline.Tests.IntegratedTests { using var client = await CreateClientAsAdministrator(); - { - var res = await client.PutAsync($"users/user1/permissions/{permission}"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - } + await client.TestPutAsync($"users/user1/permissions/{permission}"); { - var res = await client.GetAsync("users/user1"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - var body = await res.Should().HaveAndGetJsonBodyAsync(); + var body = await client.GetUserAsync("user1"); body.Permissions.Should().BeEquivalentTo(permission.ToString()); } - { - var res = await client.PutAsync($"users/user1/permissions/{permission}"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - } + await client.TestPutAsync($"users/user1/permissions/{permission}"); { - var res = await client.GetAsync("users/user1"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - var body = await res.Should().HaveAndGetJsonBodyAsync(); + var body = await client.GetUserAsync("user1"); body.Permissions.Should().BeEquivalentTo(permission.ToString()); } } @@ -139,15 +103,10 @@ namespace Timeline.Tests.IntegratedTests { using var client = await CreateClientAsAdministrator(); - { - var res = await client.DeleteAsync($"users/user1/permissions/{permission}"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - } + await client.TestDeleteAsync($"users/user1/permissions/{permission}"); { - var res = await client.GetAsync("users/user1"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - var body = await res.Should().HaveAndGetJsonBodyAsync(); + var body = await client.GetUserAsync("user1"); body.Permissions.Should().BeEmpty(); } } @@ -157,106 +116,66 @@ namespace Timeline.Tests.IntegratedTests { using var client = await CreateClientAsAdministrator(); - { - var res = await client.PutAsync($"users/user1/permissions/{UserPermission.AllTimelineManagement}"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - } + await client.TestPutAsync($"users/user1/permissions/{UserPermission.AllTimelineManagement}"); { - var res = await client.GetAsync("users/user1"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - var body = await res.Should().HaveAndGetJsonBodyAsync(); + var body = await client.GetUserAsync("user1"); body.Permissions.Should().BeEquivalentTo(UserPermission.AllTimelineManagement.ToString()); } - { - var res = await client.PutAsync($"users/user1/permissions/{UserPermission.HighlightTimelineManangement}"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - } + await client.TestPutAsync($"users/user1/permissions/{UserPermission.HighlightTimelineManangement}"); { - var res = await client.GetAsync("users/user1"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - var body = await res.Should().HaveAndGetJsonBodyAsync(); + var body = await client.GetUserAsync("user1"); body.Permissions.Should().BeEquivalentTo(UserPermission.AllTimelineManagement.ToString(), UserPermission.HighlightTimelineManangement.ToString()); } - { - var res = await client.PutAsync($"users/user1/permissions/{UserPermission.UserManagement}"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - } + await client.TestPutAsync($"users/user1/permissions/{UserPermission.UserManagement}"); { - var res = await client.GetAsync("users/user1"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - var body = await res.Should().HaveAndGetJsonBodyAsync(); + var body = await client.GetUserAsync("user1"); body.Permissions.Should().BeEquivalentTo( UserPermission.AllTimelineManagement.ToString(), UserPermission.HighlightTimelineManangement.ToString(), UserPermission.UserManagement.ToString()); } - { - var res = await client.DeleteAsync($"users/user1/permissions/{UserPermission.HighlightTimelineManangement}"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - } + await client.TestDeleteAsync($"users/user1/permissions/{UserPermission.HighlightTimelineManangement}"); { - var res = await client.GetAsync("users/user1"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - var body = await res.Should().HaveAndGetJsonBodyAsync(); + var body = await client.GetUserAsync("user1"); body.Permissions.Should().BeEquivalentTo( UserPermission.AllTimelineManagement.ToString(), UserPermission.UserManagement.ToString()); } - { - var res = await client.DeleteAsync($"users/user1/permissions/{UserPermission.AllTimelineManagement}"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - } + await client.TestDeleteAsync($"users/user1/permissions/{UserPermission.AllTimelineManagement}"); { - var res = await client.GetAsync("users/user1"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - var body = await res.Should().HaveAndGetJsonBodyAsync(); + var body = await client.GetUserAsync("user1"); body.Permissions.Should().BeEquivalentTo(UserPermission.UserManagement.ToString()); } - { - var res = await client.PutAsync($"users/user1/permissions/{UserPermission.HighlightTimelineManangement}"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - } + await client.TestPutAsync($"users/user1/permissions/{UserPermission.HighlightTimelineManangement}"); { - var res = await client.GetAsync("users/user1"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - var body = await res.Should().HaveAndGetJsonBodyAsync(); + var body = await client.GetUserAsync("user1"); body.Permissions.Should().BeEquivalentTo( UserPermission.HighlightTimelineManangement.ToString(), UserPermission.UserManagement.ToString()); } - { - var res = await client.DeleteAsync($"users/user1/permissions/{UserPermission.HighlightTimelineManangement}"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - } + await client.TestDeleteAsync($"users/user1/permissions/{UserPermission.HighlightTimelineManangement}"); { - var res = await client.GetAsync("users/user1"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - var body = await res.Should().HaveAndGetJsonBodyAsync(); + var body = await client.GetUserAsync("user1"); body.Permissions.Should().BeEquivalentTo(UserPermission.UserManagement.ToString()); } - { - var res = await client.DeleteAsync($"users/user1/permissions/{UserPermission.UserManagement}"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - } + await client.TestDeleteAsync($"users/user1/permissions/{UserPermission.UserManagement}"); { - var res = await client.GetAsync("users/user1"); - res.StatusCode.Should().Be(HttpStatusCode.OK); - var body = await res.Should().HaveAndGetJsonBodyAsync(); + var body = await client.GetUserAsync("user1"); body.Permissions.Should().BeEmpty(); } } @@ -268,19 +187,8 @@ namespace Timeline.Tests.IntegratedTests { using var client = await CreateClientAsAdministrator(); - { - var res = await client.PutAsync(url); - res.StatusCode.Should().Be(HttpStatusCode.BadRequest); - var body = await res.Should().HaveAndGetCommonBodyAsync(); - body.Code.Should().Be(ErrorCodes.Common.InvalidModel); - } - - { - var res = await client.DeleteAsync(url); - res.StatusCode.Should().Be(HttpStatusCode.BadRequest); - var body = await res.Should().HaveAndGetCommonBodyAsync(); - body.Code.Should().Be(ErrorCodes.Common.InvalidModel); - } + await client.TestPutAssertInvalidModelAsync(url); + await client.TestDeleteAssertInvalidModelAsync(url); } [Fact] @@ -290,19 +198,8 @@ namespace Timeline.Tests.IntegratedTests const string url = "users/user123/permissions/UserManagement"; - { - var res = await client.PutAsync(url); - res.StatusCode.Should().Be(HttpStatusCode.NotFound); - var body = await res.Should().HaveAndGetCommonBodyAsync(); - body.Code.Should().Be(ErrorCodes.UserCommon.NotExist); - } - - { - var res = await client.DeleteAsync(url); - res.StatusCode.Should().Be(HttpStatusCode.NotFound); - var body = await res.Should().HaveAndGetCommonBodyAsync(); - body.Code.Should().Be(ErrorCodes.UserCommon.NotExist); - } + await client.TestPutAssertNotFoundAsync(url, errorCode: ErrorCodes.UserCommon.NotExist); + await client.TestDeleteAssertNotFoundAsync(url, errorCode: ErrorCodes.UserCommon.NotExist); } } } -- cgit v1.2.3 From 03f1df804d74685ccb121fa93719aa24496b1361 Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 15 Nov 2020 17:07:34 +0800 Subject: ... --- .../IntegratedTests/HttpClientTestExtensions.cs | 25 +++ BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs | 221 +++++---------------- 2 files changed, 80 insertions(+), 166 deletions(-) (limited to 'BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs') diff --git a/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs b/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs index cd884c5e..9abe4b6b 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs @@ -121,6 +121,11 @@ namespace Timeline.Tests.IntegratedTests await client.TestJsonSendAssertErrorAsync(HttpMethod.Put, url, jsonBody, expectedStatusCode, errorCode, headerSetup); } + public static async Task TestPatchAssertErrorAsync(this HttpClient client, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.BadRequest, int? errorCode = null, HeaderSetup? headerSetup = null) + { + await client.TestJsonSendAssertErrorAsync(HttpMethod.Patch, url, jsonBody, expectedStatusCode, errorCode, headerSetup); + } + public static async Task TestDeleteAssertErrorAsync(this HttpClient client, string url, object? jsonBody = null, HttpStatusCode expectedStatusCode = HttpStatusCode.BadRequest, int? errorCode = null, HeaderSetup? headerSetup = null) { await client.TestJsonSendAssertErrorAsync(HttpMethod.Delete, url, jsonBody, expectedStatusCode, errorCode, headerSetup); @@ -151,6 +156,11 @@ namespace Timeline.Tests.IntegratedTests await client.TestJsonSendAssertInvalidModelAsync(HttpMethod.Put, url, jsonBody); } + public static async Task TestPatchAssertInvalidModelAsync(this HttpClient client, string url, object? jsonBody = null) + { + await client.TestJsonSendAssertInvalidModelAsync(HttpMethod.Patch, url, jsonBody); + } + public static async Task TestDeleteAssertInvalidModelAsync(this HttpClient client, string url, object? jsonBody = null) { await client.TestJsonSendAssertInvalidModelAsync(HttpMethod.Delete, url, jsonBody); @@ -171,6 +181,11 @@ namespace Timeline.Tests.IntegratedTests await client.TestJsonSendAssertUnauthorizedAsync(HttpMethod.Post, url, jsonBody, errorCode, headerSetup); } + public static async Task TestPatchAssertUnauthorizedAsync(this HttpClient client, string url, object? jsonBody = null, int? errorCode = null, HeaderSetup? headerSetup = null) + { + await client.TestJsonSendAssertUnauthorizedAsync(HttpMethod.Patch, url, jsonBody, errorCode, headerSetup); + } + public static async Task TestDeleteAssertUnauthorizedAsync(this HttpClient client, string url, object? jsonBody = null, int? errorCode = null, HeaderSetup? headerSetup = null) { await client.TestJsonSendAssertUnauthorizedAsync(HttpMethod.Delete, url, jsonBody, errorCode, headerSetup); @@ -191,6 +206,11 @@ namespace Timeline.Tests.IntegratedTests await client.TestJsonSendAssertForbiddenAsync(HttpMethod.Post, url, jsonBody, errorCode, headerSetup); } + public static async Task TestPatchAssertForbiddenAsync(this HttpClient client, string url, object? jsonBody = null, int? errorCode = null, HeaderSetup? headerSetup = null) + { + await client.TestJsonSendAssertForbiddenAsync(HttpMethod.Patch, url, jsonBody, errorCode, headerSetup); + } + public static async Task TestDeleteAssertForbiddenAsync(this HttpClient client, string url, object? jsonBody = null, int? errorCode = null, HeaderSetup? headerSetup = null) { await client.TestJsonSendAssertForbiddenAsync(HttpMethod.Delete, url, jsonBody, errorCode, headerSetup); @@ -211,6 +231,11 @@ namespace Timeline.Tests.IntegratedTests await client.TestJsonSendAssertNotFoundAsync(HttpMethod.Put, url, jsonBody, errorCode, headerSetup); } + public static async Task TestPatchAssertNotFoundAsync(this HttpClient client, string url, object? jsonBody = null, int? errorCode = null, HeaderSetup? headerSetup = null) + { + await client.TestJsonSendAssertNotFoundAsync(HttpMethod.Patch, url, jsonBody, errorCode, headerSetup); + } + public static async Task TestDeleteAssertNotFoundAsync(this HttpClient client, string url, object? jsonBody = null, int? errorCode = null, HeaderSetup? headerSetup = null) { await client.TestJsonSendAssertNotFoundAsync(HttpMethod.Delete, url, jsonBody, errorCode, headerSetup); diff --git a/BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs index 329e53f5..d13f6579 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs @@ -13,89 +13,41 @@ namespace Timeline.Tests.IntegratedTests public class UserTest : IntegratedTestBase { [Fact] - public void UserListShouldHaveUniqueId() + public async Task UserListShouldHaveUniqueId() { - foreach (var user in UserInfos) + using var client = await CreateDefaultClient(); + foreach (var user in await client.TestGetAsync>("users")) { user.UniqueId.Should().NotBeNullOrWhiteSpace(); } } [Fact] - public async Task GetList_NoAuth() + public async Task GetList() { using var client = await CreateDefaultClient(); - var res = await client.GetAsync("users"); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.Should().BeEquivalentTo(UserInfos); - } - - [Fact] - public async Task GetList_User() - { - using var client = await CreateClientAsUser(); - var res = await client.GetAsync("users"); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.Should().BeEquivalentTo(UserInfos); - } - - [Fact] - public async Task GetList_Admin() - { - using var client = await CreateClientAsAdministrator(); - var res = await client.GetAsync("users"); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.Should().BeEquivalentTo(UserInfos); + await client.TestGetAsync>("users"); } [Fact] - public async Task Get_NoAuth() + public async Task Get() { using var client = await CreateDefaultClient(); - var res = await client.GetAsync($"users/admin"); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.Should().BeEquivalentTo(UserInfos[0]); - } - - [Fact] - public async Task Get_User() - { - using var client = await CreateClientAsUser(); - var res = await client.GetAsync($"users/admin"); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.Should().BeEquivalentTo(UserInfos[0]); - } - - [Fact] - public async Task Get_Admin() - { - using var client = await CreateClientAsAdministrator(); - var res = await client.GetAsync($"users/user1"); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.Should().BeEquivalentTo(UserInfos[1]); + await client.TestGetAsync($"users/admin"); } [Fact] public async Task Get_InvalidModel() { - using var client = await CreateClientAsUser(); - var res = await client.GetAsync("users/aaa!a"); - res.Should().BeInvalidModel(); + using var client = await CreateDefaultClient(); + await client.TestGetAssertInvalidModelAsync("users/aaa!a"); } [Fact] public async Task Get_404() { - using var client = await CreateClientAsUser(); - var res = await client.GetAsync("users/usernotexist"); - res.Should().HaveStatusCode(404) - .And.HaveCommonBody(ErrorCodes.UserCommon.NotExist); + using var client = await CreateDefaultClient(); + await client.TestGetAssertNotFoundAsync("users/usernotexist", errorCode: ErrorCodes.UserCommon.NotExist); } [Fact] @@ -103,18 +55,14 @@ namespace Timeline.Tests.IntegratedTests { using var client = await CreateClientAsUser(); { - var res = await client.PatchAsJsonAsync("users/user1", + var body = await client.TestPatchAsync("users/user1", new UserPatchRequest { Nickname = "aaa" }); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.Nickname.Should().Be("aaa"); + body.Nickname.Should().Be("aaa"); } { - var res = await client.GetAsync("users/user1"); - res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which.Nickname.Should().Be("aaa"); + var body = await client.GetUserAsync("user1"); + body.Nickname.Should().Be("aaa"); } } @@ -125,32 +73,25 @@ namespace Timeline.Tests.IntegratedTests using var userClient = await CreateClientAsUser(); { - var res = await client.PatchAsJsonAsync("users/user1", + var body = await client.TestPatchAsync("users/user1", new UserPatchRequest { Username = "newuser", Password = "newpw", Nickname = "aaa" }); - var body = res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which; body.Nickname.Should().Be("aaa"); } { - var res = await client.GetAsync("users/newuser"); - var body = res.Should().HaveStatusCode(200) - .And.HaveJsonBody() - .Which; + var body = await client.GetUserAsync("users/newuser"); body.Nickname.Should().Be("aaa"); } { - var token = userClient.DefaultRequestHeaders.Authorization.Parameter; + var token = userClient.DefaultRequestHeaders.Authorization!.Parameter!; // Token should expire. - var res = await userClient.PostAsJsonAsync("token/verify", new() { Token = token }); - res.Should().HaveStatusCode(HttpStatusCode.BadRequest); + await userClient.TestPostAssertErrorAsync("token/verify", new VerifyTokenRequest() { Token = token }); } { @@ -163,18 +104,14 @@ namespace Timeline.Tests.IntegratedTests public async Task Patch_NotExist() { using var client = await CreateClientAsAdministrator(); - var res = await client.PatchAsJsonAsync("users/usernotexist", new UserPatchRequest { }); - res.Should().HaveStatusCode(404) - .And.HaveCommonBody() - .Which.Code.Should().Be(ErrorCodes.UserCommon.NotExist); + await client.TestPatchAssertNotFoundAsync("users/usernotexist", new UserPatchRequest { }, errorCode: ErrorCodes.UserCommon.NotExist); } [Fact] public async Task Patch_InvalidModel() { using var client = await CreateClientAsAdministrator(); - var res = await client.PatchAsJsonAsync("users/aaa!a", new UserPatchRequest { }); - res.Should().BeInvalidModel(); + await client.TestPatchAssertInvalidModelAsync("users/aaa!a", new UserPatchRequest { }); } public static IEnumerable Patch_InvalidModel_Body_Data() @@ -189,96 +126,78 @@ namespace Timeline.Tests.IntegratedTests public async Task Patch_InvalidModel_Body(UserPatchRequest body) { using var client = await CreateClientAsAdministrator(); - var res = await client.PatchAsJsonAsync("users/user1", body); - res.Should().BeInvalidModel(); + await client.TestPatchAssertInvalidModelAsync("users/user1", body); } [Fact] public async Task Patch_UsernameConflict() { using var client = await CreateClientAsAdministrator(); - var res = await client.PatchAsJsonAsync("users/user1", new UserPatchRequest { Username = "admin" }); - res.Should().HaveStatusCode(400) - .And.HaveCommonBody(ErrorCodes.UserController.UsernameConflict); + await client.TestPatchAssertErrorAsync("users/user1", new UserPatchRequest { Username = "admin" }, errorCode: ErrorCodes.UserController.UsernameConflict); } [Fact] public async Task Patch_NoAuth_Unauthorized() { using var client = await CreateDefaultClient(); - var res = await client.PatchAsJsonAsync("users/user1", new UserPatchRequest { Nickname = "aaa" }); - res.Should().HaveStatusCode(HttpStatusCode.Unauthorized); + await client.TestPatchAssertUnauthorizedAsync("users/user1", new UserPatchRequest { Nickname = "aaa" }); } [Fact] public async Task Patch_User_Forbid() { using var client = await CreateClientAsUser(); - var res = await client.PatchAsJsonAsync("users/admin", new UserPatchRequest { Nickname = "aaa" }); - res.Should().HaveStatusCode(HttpStatusCode.Forbidden); + await client.TestPatchAssertForbiddenAsync("users/admin", new UserPatchRequest { Nickname = "aaa" }); } [Fact] public async Task Patch_Username_Forbid() { using var client = await CreateClientAsUser(); - var res = await client.PatchAsJsonAsync("users/user1", new UserPatchRequest { Username = "aaa" }); - res.Should().HaveStatusCode(HttpStatusCode.Forbidden); + await client.TestPatchAssertForbiddenAsync("users/user1", new UserPatchRequest { Username = "aaa" }); } [Fact] public async Task Patch_Password_Forbid() { using var client = await CreateClientAsUser(); - var res = await client.PatchAsJsonAsync("users/user1", new UserPatchRequest { Password = "aaa" }); - res.Should().HaveStatusCode(HttpStatusCode.Forbidden); + await client.TestPatchAssertForbiddenAsync("users/user1", new UserPatchRequest { Password = "aaa" }); } [Fact] public async Task Delete_Deleted() { using var client = await CreateClientAsAdministrator(); - { - var res = await client.DeleteAsync("users/user1"); - res.Should().BeDelete(true); - } - - { - var res = await client.GetAsync("users/user1"); - res.Should().HaveStatusCode(404); - } + await client.TestDeleteAsync("users/user1", true); + await client.TestGetAssertNotFoundAsync("users/user1"); } [Fact] public async Task Delete_NotExist() { using var client = await CreateClientAsAdministrator(); - var res = await client.DeleteAsync("users/usernotexist"); - res.Should().BeDelete(false); + await client.TestDeleteAsync("users/usernotexist", false); } [Fact] public async Task Delete_InvalidModel() { using var client = await CreateClientAsAdministrator(); - var res = await client.DeleteAsync("users/aaa!a"); - res.Should().BeInvalidModel(); + await client.TestDeleteAssertInvalidModelAsync("users/aaa!a"); } [Fact] public async Task Delete_NoAuth_Unauthorized() { using var client = await CreateDefaultClient(); - var res = await client.DeleteAsync("users/aaa!a"); - res.Should().HaveStatusCode(HttpStatusCode.Unauthorized); + await client.TestDeleteAssertUnauthorizedAsync("users/aaa!a"); } [Fact] public async Task Delete_User_Forbid() { using var client = await CreateClientAsUser(); - var res = await client.DeleteAsync("users/aaa!a"); - res.Should().HaveStatusCode(HttpStatusCode.Forbidden); + await client.TestDeleteAssertForbiddenAsync("users/aaa!a"); } private const string createUserUrl = "userop/createuser"; @@ -288,19 +207,15 @@ namespace Timeline.Tests.IntegratedTests { using var client = await CreateClientAsAdministrator(); { - var res = await client.PostAsJsonAsync(createUserUrl, new CreateUserRequest + var body = await client.TestPostAsync(createUserUrl, new CreateUserRequest { Username = "aaa", Password = "bbb", }); - var body = res.Should().HaveStatusCode(200) - .And.HaveJsonBody().Which; body.Username.Should().Be("aaa"); } { - var res = await client.GetAsync("users/aaa"); - var body = res.Should().HaveStatusCode(200) - .And.HaveJsonBody().Which; + var body = await client.GetUserAsync("users/aaa"); body.Username.Should().Be("aaa"); } { @@ -322,53 +237,40 @@ namespace Timeline.Tests.IntegratedTests public async Task Op_CreateUser_InvalidModel(CreateUserRequest body) { using var client = await CreateClientAsAdministrator(); - { - var res = await client.PostAsJsonAsync(createUserUrl, body); - res.Should().BeInvalidModel(); - } + await client.TestPostAssertInvalidModelAsync(createUserUrl, body); } [Fact] public async Task Op_CreateUser_UsernameConflict() { using var client = await CreateClientAsAdministrator(); + await client.TestPostAssertErrorAsync(createUserUrl, new CreateUserRequest { - var res = await client.PostAsJsonAsync(createUserUrl, new CreateUserRequest - { - Username = "user1", - Password = "bbb", - }); - res.Should().HaveStatusCode(400) - .And.HaveCommonBody(ErrorCodes.UserController.UsernameConflict); - } + Username = "user1", + Password = "bbb", + }, errorCode: ErrorCodes.UserController.UsernameConflict); } [Fact] public async Task Op_CreateUser_NoAuth_Unauthorized() { using var client = await CreateDefaultClient(); + await client.TestPostAssertUnauthorizedAsync(createUserUrl, new CreateUserRequest { - var res = await client.PostAsJsonAsync(createUserUrl, new CreateUserRequest - { - Username = "aaa", - Password = "bbb", - }); - res.Should().HaveStatusCode(HttpStatusCode.Unauthorized); - } + Username = "aaa", + Password = "bbb", + }); } [Fact] public async Task Op_CreateUser_User_Forbid() { using var client = await CreateClientAsUser(); + await client.TestPostAssertForbiddenAsync(createUserUrl, new CreateUserRequest { - var res = await client.PostAsJsonAsync(createUserUrl, new CreateUserRequest - { - Username = "aaa", - Password = "bbb", - }); - res.Should().HaveStatusCode(HttpStatusCode.Forbidden); - } + Username = "aaa", + Password = "bbb", + }); } private const string changePasswordUrl = "userop/changepassword"; @@ -377,21 +279,12 @@ namespace Timeline.Tests.IntegratedTests public async Task Op_ChangePassword() { using var client = await CreateClientAsUser(); - { - var res = await client.PostAsJsonAsync(changePasswordUrl, - new ChangePasswordRequest { OldPassword = "user1pw", NewPassword = "newpw" }); - res.Should().HaveStatusCode(200); - } - { - var res = await client.PatchAsJsonAsync("users/user1", new UserPatchRequest { }); - res.Should().HaveStatusCode(HttpStatusCode.Unauthorized); - } - { - (await CreateClientWithCredential("user1", "newpw")).Dispose(); - } + await client.TestPostAsync(changePasswordUrl, new ChangePasswordRequest { OldPassword = "user1pw", NewPassword = "newpw" }); + await client.TestPatchAssertUnauthorizedAsync("users/user1", new UserPatchRequest { }); + (await CreateClientWithCredential("user1", "newpw")).Dispose(); } - public static IEnumerable Op_ChangePassword_InvalidModel_Data() + public static IEnumerable Op_ChangePassword_InvalidModel_Data() { yield return new[] { null, "ppp" }; yield return new[] { "ppp", null }; @@ -402,26 +295,22 @@ namespace Timeline.Tests.IntegratedTests public async Task Op_ChangePassword_InvalidModel(string oldPassword, string newPassword) { using var client = await CreateClientAsUser(); - var res = await client.PostAsJsonAsync(changePasswordUrl, + await client.TestPostAssertInvalidModelAsync(changePasswordUrl, new ChangePasswordRequest { OldPassword = oldPassword, NewPassword = newPassword }); - res.Should().BeInvalidModel(); } [Fact] public async Task Op_ChangePassword_BadOldPassword() { using var client = await CreateClientAsUser(); - var res = await client.PostAsJsonAsync(changePasswordUrl, new ChangePasswordRequest { OldPassword = "???", NewPassword = "???" }); - res.Should().HaveStatusCode(400) - .And.HaveCommonBody(ErrorCodes.UserController.ChangePassword_BadOldPassword); + await client.TestPostAssertErrorAsync(changePasswordUrl, new ChangePasswordRequest { OldPassword = "???", NewPassword = "???" }, errorCode: ErrorCodes.UserController.ChangePassword_BadOldPassword); } [Fact] public async Task Op_ChangePassword_NoAuth_Unauthorized() { using var client = await CreateDefaultClient(); - var res = await client.PostAsJsonAsync(changePasswordUrl, new ChangePasswordRequest { OldPassword = "???", NewPassword = "???" }); - res.Should().HaveStatusCode(HttpStatusCode.Unauthorized); + await client.TestPostAssertUnauthorizedAsync(changePasswordUrl, new ChangePasswordRequest { OldPassword = "???", NewPassword = "???" }); } } } -- cgit v1.2.3 From 8e9b32914a1bcfe3206c13816d89d4247483ab48 Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 15 Nov 2020 17:27:18 +0800 Subject: ... --- .../IntegratedTests/HttpClientTestExtensions.cs | 8 +++++- .../Timeline.Tests/IntegratedTests/TimelineTest.cs | 32 ++++++++++------------ .../Timeline.Tests/IntegratedTests/TokenTest.cs | 2 +- .../IntegratedTests/UserAvatarTest.cs | 2 +- .../IntegratedTests/UserPermissionTest.cs | 2 +- BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs | 4 +-- .../Timeline.Tests/Services/DatabaseBasedTest.cs | 2 +- .../Services/UserPermissionServiceTest.cs | 2 +- 8 files changed, 29 insertions(+), 25 deletions(-) (limited to 'BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs') diff --git a/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs b/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs index 9abe4b6b..7cff0c39 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/HttpClientTestExtensions.cs @@ -85,9 +85,15 @@ namespace Timeline.Tests.IntegratedTests public static async Task TestDeleteAsync(this HttpClient client, string url, bool? delete = null, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) { - var body = await client.TestJsonSendAsync(HttpMethod.Delete, url, expectedStatusCode: expectedStatusCode); if (delete.HasValue) + { + var body = await client.TestJsonSendAsync(HttpMethod.Delete, url, expectedStatusCode: expectedStatusCode); body.Data.Delete.Should().Be(delete.Value); + } + else + { + await client.TestJsonSendAsync(HttpMethod.Delete, url, expectedStatusCode: expectedStatusCode); + } } public static async Task TestSendAssertErrorAsync(this HttpClient client, HttpMethod method, string url, HttpContent? body = null, HttpStatusCode expectedStatusCode = HttpStatusCode.BadRequest, int? errorCode = null, HeaderSetup? headerSetup = null) diff --git a/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs index 1b7ed616..9845e1b1 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs @@ -651,11 +651,11 @@ namespace Timeline.Tests.IntegratedTests } await CacheTestHelper.TestCache(client, $"timelines/{generator(1)}/posts/{postId}/data"); - await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{postId}/data", true); - await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{postId}/data", false); + await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{postId}", true); + await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{postId}", false); { - var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts/{postId}/data"); + var body = await client.TestGetAsync>($"timelines/{generator(1)}/posts"); body.Should().BeEmpty(); } @@ -673,7 +673,7 @@ namespace Timeline.Tests.IntegratedTests { using var client = await CreateClientAsUser(); - await client.TestGetAssertErrorAsync($"timelines/{generator(1)}/posts/11234/data", errorCode: ErrorCodes.TimelineController.PostNotExist); + await client.TestGetAssertNotFoundAsync($"timelines/{generator(1)}/posts/11234/data", errorCode: ErrorCodes.TimelineController.PostNotExist); long postId; { @@ -804,7 +804,7 @@ namespace Timeline.Tests.IntegratedTests [Theory] [MemberData(nameof(TimelineNameGeneratorTestData))] - public async Task Timeline_Get_IfModifiedSince_And_CheckUniqueId(TimelineNameGenerator urlGenerator) + public async Task Timeline_Get_IfModifiedSince_And_CheckUniqueId(TimelineNameGenerator generator) { using var client = await CreateClientAsUser(); @@ -813,14 +813,14 @@ namespace Timeline.Tests.IntegratedTests string uniqueId; { - var body = await client.GetTimelineAsync(urlGenerator(1)); + var body = await client.GetTimelineAsync(generator(1)); timeline = body; lastModifiedTime = body.LastModified; uniqueId = body.UniqueId; } { - await client.TestGetAsync($"timelines/{urlGenerator(1)}", + await client.TestGetAsync($"timelines/{generator(1)}", expectedStatusCode: HttpStatusCode.NotModified, headerSetup: (headers, _) => { @@ -830,8 +830,7 @@ namespace Timeline.Tests.IntegratedTests { - var body = await client.TestGetAsync($"timelines/{urlGenerator(1)}", - expectedStatusCode: HttpStatusCode.NotModified, + var body = await client.TestGetAsync($"timelines/{generator(1)}", headerSetup: (headers, _) => { headers.IfModifiedSince = lastModifiedTime.AddSeconds(-1); @@ -840,21 +839,21 @@ namespace Timeline.Tests.IntegratedTests } { - await client.TestGetAsync($"timelines/{urlGenerator(1)}?ifModifiedSince={lastModifiedTime.AddSeconds(1).ToString("s", CultureInfo.InvariantCulture) }", expectedStatusCode: HttpStatusCode.NotModified); + await client.TestGetAsync($"timelines/{generator(1)}?ifModifiedSince={lastModifiedTime.AddSeconds(1).ToString("s", CultureInfo.InvariantCulture) }", expectedStatusCode: HttpStatusCode.NotModified); } { - var body = await client.TestGetAsync($"timelines/{urlGenerator(1)}?ifModifiedSince={lastModifiedTime.AddSeconds(-1).ToString("s", CultureInfo.InvariantCulture) }"); + var body = await client.TestGetAsync($"timelines/{generator(1)}?ifModifiedSince={lastModifiedTime.AddSeconds(-1).ToString("s", CultureInfo.InvariantCulture) }"); body.Should().BeEquivalentTo(timeline); } { - await client.TestGetAsync($"timelines/{urlGenerator(1)}?ifModifiedSince={lastModifiedTime.AddSeconds(1).ToString("s", CultureInfo.InvariantCulture) }&checkUniqueId={uniqueId}", expectedStatusCode: HttpStatusCode.NotModified); + await client.TestGetAsync($"timelines/{generator(1)}?ifModifiedSince={lastModifiedTime.AddSeconds(1).ToString("s", CultureInfo.InvariantCulture) }&checkUniqueId={uniqueId}", expectedStatusCode: HttpStatusCode.NotModified); } { var testUniqueId = (uniqueId[0] == 'a' ? "b" : "a") + uniqueId[1..]; - var body = await client.TestGetAsync($"timelines/{urlGenerator(1)}?ifModifiedSince={lastModifiedTime.AddSeconds(1).ToString("s", CultureInfo.InvariantCulture) }&checkUniqueId={testUniqueId}", expectedStatusCode: HttpStatusCode.NotModified); + var body = await client.TestGetAsync($"timelines/{generator(1)}?ifModifiedSince={lastModifiedTime.AddSeconds(1).ToString("s", CultureInfo.InvariantCulture) }&checkUniqueId={testUniqueId}"); body.Should().BeEquivalentTo(timeline); } } @@ -897,11 +896,10 @@ namespace Timeline.Tests.IntegratedTests using (var client = await CreateClientAsUser()) { await client.TestPostAssertInvalidModelAsync("timelineop/changename", new TimelineChangeNameRequest { OldName = "!!!", NewName = "tttttttt" }); - await client.TestPostAssertInvalidModelAsync("timelineop/changename", new TimelineChangeNameRequest { OldName = "ttt", NewName = "!!!!" }); + await client.TestPostAssertErrorAsync("timelineop/changename", new TimelineChangeNameRequest { OldName = "ttttt", NewName = "tttttttt" }, errorCode: ErrorCodes.TimelineController.NotExist); - await client.TestPostAssertInvalidModelAsync("timelineop/changename", new TimelineChangeNameRequest { OldName = "ttttt", NewName = "tttttttt" }); - await client.TestPostAssertInvalidModelAsync("timelineop/changename", new TimelineChangeNameRequest { OldName = "t1", NewName = "newt" }); + await client.TestPostAsync("timelineop/changename", new TimelineChangeNameRequest { OldName = "t1", NewName = "newt" }); await client.TestGetAsync("timelines/t1", expectedStatusCode: HttpStatusCode.NotFound); @@ -937,7 +935,7 @@ namespace Timeline.Tests.IntegratedTests } { - var res = await client.GetAsync($"timelines/{generator(1)}/posts{id}/data"); + var res = await client.GetAsync($"timelines/{generator(1)}/posts/{id}/data"); res.StatusCode.Should().Be(HttpStatusCode.OK); res.Headers.ETag.Should().NotBeNull(); res.Headers.ETag!.ToString().Should().Be(etag); diff --git a/BackEnd/Timeline.Tests/IntegratedTests/TokenTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/TokenTest.cs index 3c4a595d..c2f91eb1 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/TokenTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/TokenTest.cs @@ -144,7 +144,7 @@ namespace Timeline.Tests.IntegratedTests var createTokenResult = await CreateUserTokenAsync(client, "user1", "user1pw"); var body = await client.TestPostAsync(VerifyTokenUrl, new VerifyTokenRequest { Token = createTokenResult.Token }); - body.Should().BeEquivalentTo(await client.GetUserAsync("user1")); + body.User.Should().BeEquivalentTo(await client.GetUserAsync("user1")); } } } diff --git a/BackEnd/Timeline.Tests/IntegratedTests/UserAvatarTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/UserAvatarTest.cs index 79a3c2fa..893a5d28 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/UserAvatarTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/UserAvatarTest.cs @@ -101,7 +101,7 @@ namespace Timeline.Tests.IntegratedTests { await client.TestPutByteArrayAssertErrorAsync("users/user1/avatar", new[] { (byte)0x00 }, "image/png", errorCode: ErrorCodes.UserAvatar.BadFormat_CantDecode); - await client.TestPutByteArrayAssertErrorAsync("users/user1/avatar", mockAvatar.Data, "image/png", errorCode: ErrorCodes.UserAvatar.BadFormat_UnmatchedFormat); + await client.TestPutByteArrayAssertErrorAsync("users/user1/avatar", mockAvatar.Data, "image/jpeg", errorCode: ErrorCodes.UserAvatar.BadFormat_UnmatchedFormat); await client.TestPutByteArrayAssertErrorAsync("users/user1/avatar", ImageHelper.CreatePngWithSize(100, 200), "image/png", errorCode: ErrorCodes.UserAvatar.BadFormat_BadSize); } diff --git a/BackEnd/Timeline.Tests/IntegratedTests/UserPermissionTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/UserPermissionTest.cs index 418dd0f9..77cae590 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/UserPermissionTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/UserPermissionTest.cs @@ -50,7 +50,7 @@ namespace Timeline.Tests.IntegratedTests await client.TestPutAsync($"users/admin/permissions/{permission}"); { - var body = await client.GetUserAsync("users/admin"); + var body = await client.GetUserAsync("admin"); body.Permissions.Should().BeEquivalentTo(Enum.GetNames()); } } diff --git a/BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs index d13f6579..0d65478b 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs @@ -84,7 +84,7 @@ namespace Timeline.Tests.IntegratedTests } { - var body = await client.GetUserAsync("users/newuser"); + var body = await client.GetUserAsync("newuser"); body.Nickname.Should().Be("aaa"); } @@ -215,7 +215,7 @@ namespace Timeline.Tests.IntegratedTests body.Username.Should().Be("aaa"); } { - var body = await client.GetUserAsync("users/aaa"); + var body = await client.GetUserAsync("aaa"); body.Username.Should().Be("aaa"); } { diff --git a/BackEnd/Timeline.Tests/Services/DatabaseBasedTest.cs b/BackEnd/Timeline.Tests/Services/DatabaseBasedTest.cs index 7c97158c..3bb6ebb5 100644 --- a/BackEnd/Timeline.Tests/Services/DatabaseBasedTest.cs +++ b/BackEnd/Timeline.Tests/Services/DatabaseBasedTest.cs @@ -8,7 +8,7 @@ namespace Timeline.Tests.Services public abstract class DatabaseBasedTest : IAsyncLifetime { protected TestDatabase TestDatabase { get; } - protected DatabaseContext Database { get; private set; } + protected DatabaseContext Database { get; private set; } = default!; protected DatabaseBasedTest(bool databaseCreateUsers = true) { diff --git a/BackEnd/Timeline.Tests/Services/UserPermissionServiceTest.cs b/BackEnd/Timeline.Tests/Services/UserPermissionServiceTest.cs index cea11b34..5a4e4954 100644 --- a/BackEnd/Timeline.Tests/Services/UserPermissionServiceTest.cs +++ b/BackEnd/Timeline.Tests/Services/UserPermissionServiceTest.cs @@ -9,7 +9,7 @@ namespace Timeline.Tests.Services { public class UserPermissionServiceTest : DatabaseBasedTest { - private UserPermissionService _service; + private UserPermissionService _service = default!; public UserPermissionServiceTest() { -- cgit v1.2.3