From 48d53341db4953b3d583dd825b48d854c0a166e9 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 13 Nov 2020 17:08:20 +0800 Subject: ... --- .../Timeline.Tests/IntegratedTests/FrontEndTest.cs | 8 ++- .../IntegratedTests/IntegratedTestBase.cs | 71 +++++++++------------- .../Timeline.Tests/IntegratedTests/TimelineTest.cs | 23 +++---- 3 files changed, 43 insertions(+), 59 deletions(-) (limited to 'BackEnd/Timeline.Tests/IntegratedTests') diff --git a/BackEnd/Timeline.Tests/IntegratedTests/FrontEndTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/FrontEndTest.cs index 39a6e545..86cde11f 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/FrontEndTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/FrontEndTest.cs @@ -14,7 +14,9 @@ namespace Timeline.Tests.IntegratedTests using var client = await CreateDefaultClient(false); var res = await client.GetAsync("index.html"); res.Should().HaveStatusCode(200); - res.Content.Headers.ContentType.MediaType.Should().Be(MediaTypeNames.Text.Html); + var contentTypeHeader = res.Content.Headers.ContentType; + contentTypeHeader.Should().NotBeNull(); + contentTypeHeader!.MediaType.Should().Be(MediaTypeNames.Text.Html); } [Fact] @@ -23,7 +25,9 @@ namespace Timeline.Tests.IntegratedTests using var client = await CreateDefaultClient(false); var res = await client.GetAsync("aaaaaaaaaaaaaaa"); res.Should().HaveStatusCode(200); - res.Content.Headers.ContentType.MediaType.Should().Be(MediaTypeNames.Text.Html); + var contentTypeHeader = res.Content.Headers.ContentType; + contentTypeHeader.Should().NotBeNull(); + contentTypeHeader!.MediaType.Should().Be(MediaTypeNames.Text.Html); } } } diff --git a/BackEnd/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs b/BackEnd/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs index f75ce69c..79d2d5bf 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs @@ -19,8 +19,6 @@ namespace Timeline.Tests.IntegratedTests { protected TestApplication TestApp { get; } - public IReadOnlyList UserInfos { get; private set; } - private readonly int _userCount; public IntegratedTestBase() : this(1) @@ -48,54 +46,45 @@ namespace Timeline.Tests.IntegratedTests return Task.CompletedTask; } + protected virtual void OnInitialize() + { + + } + protected virtual void OnDispose() { } - public async Task InitializeAsync() + private async Task CreateUsers() { - await TestApp.InitializeAsync(); + using var scope = TestApp.Host.Services.CreateScope(); - using (var scope = TestApp.Host.Services.CreateScope()) - { - var users = new List<(string username, string password, string nickname)>() + var users = new List<(string username, string password, string nickname)>() { ("admin", "adminpw", "administrator") }; - for (int i = 1; i <= _userCount; i++) - { - users.Add(($"user{i}", $"user{i}pw", $"imuser{i}")); - } - - var userInfoList = new List(); - - var userService = scope.ServiceProvider.GetRequiredService(); - foreach (var user in users) - { - var (username, password, nickname) = user; - var u = await userService.CreateUser(username, password); - await userService.ModifyUser(u.Id, new ModifyUserParams() { Nickname = nickname }); - } - - using var client = await CreateDefaultClient(); - var options = new JsonSerializerOptions - { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase - }; - options.Converters.Add(new JsonStringEnumConverter()); - options.Converters.Add(new JsonDateTimeConverter()); - foreach (var user in users) - { - var s = await client.GetStringAsync($"users/{user.username}"); - userInfoList.Add(JsonSerializer.Deserialize(s, options)); - } + for (int i = 1; i <= _userCount; i++) + { + users.Add(($"user{i}", $"user{i}pw", $"imuser{i}")); + } - UserInfos = userInfoList; + var userService = scope.ServiceProvider.GetRequiredService(); + foreach (var user in users) + { + var (username, password, nickname) = user; + var u = await userService.CreateUser(username, password); + await userService.ModifyUser(u.Id, new ModifyUserParams() { Nickname = nickname }); } + } + public async Task InitializeAsync() + { + await TestApp.InitializeAsync(); + await CreateUsers(); await OnInitializeAsync(); + OnInitialize(); } public async Task DisposeAsync() @@ -110,22 +99,18 @@ namespace Timeline.Tests.IntegratedTests var client = TestApp.Host.GetTestServer().CreateClient(); if (setApiBase) { - client.BaseAddress = new Uri(client.BaseAddress, "api/"); + client.BaseAddress = new Uri(client.BaseAddress!, "api/"); } return Task.FromResult(client); } public async Task CreateClientWithCredential(string username, string password, bool setApiBase = true) { - var client = TestApp.Host.GetTestServer().CreateClient(); - if (setApiBase) - { - client.BaseAddress = new Uri(client.BaseAddress, "api/"); - } + var client = await CreateDefaultClient(setApiBase); var response = await client.PostAsJsonAsync("token/create", new CreateTokenRequest { Username = username, Password = password }); - var token = response.Should().HaveStatusCode(200) - .And.HaveJsonBody().Which.Token; + var token = (await response.Should().HaveStatusCode(200) + .And.HaveAndGetJsonBodyAsync()).Token; client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token); return client; } diff --git a/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs index ec46b96a..fd574a82 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs @@ -55,22 +55,17 @@ namespace Timeline.Tests.IntegratedTests await CreateTestTimelines(); } - private List _testTimelines; - private async Task CreateTestTimelines() { - _testTimelines = new List(); for (int i = 0; i <= 3; i++) { var client = await CreateClientAs(i); var res = await client.PostAsJsonAsync("timelines", new TimelineCreateRequest { Name = $"t{i}" }); - var timelineInfo = res.Should().HaveStatusCode(200) - .And.HaveJsonBody().Which; - _testTimelines.Add(timelineInfo); + res.Should().HaveStatusCode(200); } } - private static string CalculateUrlTail(string subpath, ICollection> query) + private static string CalculateUrlTail(string? subpath, ICollection>? query) { StringBuilder result = new StringBuilder(); if (subpath != null) @@ -96,12 +91,12 @@ namespace Timeline.Tests.IntegratedTests return result.ToString(); } - private static string GeneratePersonalTimelineUrl(int id, string subpath = null, ICollection> query = null) + private static string GeneratePersonalTimelineUrl(int id, string? subpath = null, ICollection>? query = null) { return $"timelines/@{(id == 0 ? "admin" : ("user" + id))}{CalculateUrlTail(subpath, query)}"; } - private static string GenerateOrdinaryTimelineUrl(int id, string subpath = null, ICollection> query = null) + private static string GenerateOrdinaryTimelineUrl(int id, string? subpath = null, ICollection>? query = null) { return $"timelines/t{id}{CalculateUrlTail(subpath, query)}"; } @@ -114,12 +109,12 @@ namespace Timeline.Tests.IntegratedTests yield return new[] { new TimelineUrlGenerator(GenerateOrdinaryTimelineUrl) }; } - private static string GeneratePersonalTimelineUrlByName(string name, string subpath = null) + private static string GeneratePersonalTimelineUrlByName(string name, string? subpath = null) { return $"timelines/@{name}{(subpath == null ? "" : "/" + subpath)}"; } - private static string GenerateOrdinaryTimelineUrlByName(string name, string subpath = null) + private static string GenerateOrdinaryTimelineUrlByName(string name, string? subpath = null) { return $"timelines/{name}{(subpath == null ? "" : "/" + subpath)}"; } @@ -136,9 +131,9 @@ namespace Timeline.Tests.IntegratedTests using var client = await CreateDefaultClient(); { var res = await client.GetAsync("timelines/@user1"); - var body = res.Should().HaveStatusCode(200) - .And.HaveJsonBody().Which; - body.Owner.Should().BeEquivalentTo(UserInfos[1]); + var body = await res.Should().HaveStatusCode(200) + .And.HaveAndGetJsonBodyAsync(); + body.Owner.Should().; body.Visibility.Should().Be(TimelineVisibility.Register); body.Description.Should().Be(""); body.Members.Should().NotBeNull().And.BeEmpty(); -- cgit v1.2.3