1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Timeline.Models.Http;
using Timeline.Services.User;
using Timeline.Tests.Helpers;
using Xunit;
namespace Timeline.Tests.IntegratedTests
{
public abstract class IntegratedTestBase : IAsyncLifetime
{
protected TestApplication TestApp { get; }
protected int TestUserCount { get; }
public IntegratedTestBase() : this(1)
{
}
public IntegratedTestBase(int userCount)
{
if (userCount < 0)
throw new ArgumentOutOfRangeException(nameof(userCount), userCount, "User count can't be negative.");
TestUserCount = userCount;
TestApp = new TestApplication();
}
protected virtual Task OnInitializeAsync()
{
return Task.CompletedTask;
}
protected virtual Task OnDisposeAsync()
{
return Task.CompletedTask;
}
protected virtual void OnInitialize()
{
}
protected virtual void OnDispose()
{
}
private async Task CreateUsers()
{
using var scope = TestApp.Host.Services.CreateScope();
var users = new List<(string username, string password, string nickname)>();
for (int i = 1; i <= TestUserCount; i++)
{
users.Add(($"user{i}", $"user{i}pw", $"imuser{i}"));
}
var userService = scope.ServiceProvider.GetRequiredService<IUserService>();
await userService.ModifyUserAsync(await userService.GetUserIdByUsernameAsync("administrator"), new ModifyUserParams
{
Username = "admin",
Password = "adminpw",
Nickname = "administrator"
});
foreach (var user in users)
{
var (username, password, nickname) = user;
var u = await userService.CreateUserAsync(new CreateUserParams(username, password));
await userService.ModifyUserAsync(u.Id, new ModifyUserParams() { Nickname = nickname });
}
}
public async Task InitializeAsync()
{
await TestApp.InitializeAsync();
await CreateUsers();
await OnInitializeAsync();
OnInitialize();
}
public async Task DisposeAsync()
{
await OnDisposeAsync();
OnDispose();
await TestApp.DisposeAsync();
}
public Task<HttpClient> CreateDefaultClient(bool setApiBase = true)
{
var client = TestApp.Host.GetTestServer().CreateClient();
if (setApiBase)
{
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 = await CreateDefaultClient(setApiBase);
var res = await client.TestPostAsync<HttpCreateTokenResponse>("token/create",
new HttpCreateTokenRequest { Username = username, Password = password });
var token = res.Token;
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
return client;
}
public Task<HttpClient> CreateClientAs(int userNumber, bool setApiBase = true)
{
if (userNumber < 0)
return CreateDefaultClient(setApiBase);
if (userNumber == 0)
return CreateClientWithCredential("admin", "adminpw", setApiBase);
else
return CreateClientWithCredential($"user{userNumber}", $"user{userNumber}pw", setApiBase);
}
public Task<HttpClient> CreateClientAsAdministrator(bool setApiBase = true)
{
return CreateClientAs(0, setApiBase);
}
public Task<HttpClient> CreateClientAsUser(bool setApiBase = true)
{
return CreateClientAs(1, setApiBase);
}
}
}
|