aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-11-13 17:08:20 +0800
committercrupest <crupest@outlook.com>2020-11-13 17:08:20 +0800
commitbd2462f2bc964839e9b64f167dfa47abb3f9afe6 (patch)
tree9aefc6cb0f10ac9e8e90d66d115849469813a08f /BackEnd/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
parent379b4bafe982a8a8cd3158f0a9fa38a563dbdb57 (diff)
downloadtimeline-bd2462f2bc964839e9b64f167dfa47abb3f9afe6.tar.gz
timeline-bd2462f2bc964839e9b64f167dfa47abb3f9afe6.tar.bz2
timeline-bd2462f2bc964839e9b64f167dfa47abb3f9afe6.zip
...
Diffstat (limited to 'BackEnd/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs')
-rw-r--r--BackEnd/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs71
1 files changed, 28 insertions, 43 deletions
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<UserInfo> 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<UserInfo>();
-
- var userService = scope.ServiceProvider.GetRequiredService<IUserService>();
- 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<UserInfo>(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<IUserService>();
+ 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<HttpClient> 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<CreateTokenResponse>().Which.Token;
+ var token = (await response.Should().HaveStatusCode(200)
+ .And.HaveAndGetJsonBodyAsync<CreateTokenResponse>()).Token;
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
return client;
}