From bd2462f2bc964839e9b64f167dfa47abb3f9afe6 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 13 Nov 2020 17:08:20 +0800 Subject: ... --- .../IntegratedTests/IntegratedTestBase.cs | 71 +++++++++------------- 1 file changed, 28 insertions(+), 43 deletions(-) (limited to 'BackEnd/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs') 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; } -- cgit v1.2.3