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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
using FluentAssertions;
using Microsoft.AspNetCore.Mvc.Testing;
using System.Net.Http;
using System.Threading.Tasks;
using Timeline.Controllers;
using Timeline.Models;
using Timeline.Models.Http;
using Timeline.Tests.Helpers;
using Timeline.Tests.Helpers.Authentication;
using Timeline.Tests.Mock.Data;
using Xunit;
using Xunit.Abstractions;
namespace Timeline.Tests
{
public class UserUnitTest : IClassFixture<MyWebApplicationFactory<Startup>>
{
private readonly WebApplicationFactory<Startup> _factory;
public UserUnitTest(MyWebApplicationFactory<Startup> factory, ITestOutputHelper outputHelper)
{
_factory = factory.WithTestLogging(outputHelper);
}
[Fact]
public async Task Get_Users_List()
{
using (var client = await _factory.CreateClientAsAdmin())
{
var res = await client.GetAsync("users");
// Because tests are running asyncronized. So database may be modified and
// we can't check the exact user lists at this point. So only check the format.
res.Should().HaveStatusCodeOk().And.Should().HaveBodyAsJson<UserInfo[]>();
}
}
[Fact]
public async Task Get_Users_User()
{
using (var client = await _factory.CreateClientAsAdmin())
{
var res = await client.GetAsync("users/" + MockUsers.UserUsername);
res.Should().HaveStatusCodeOk()
.And.Should().HaveBodyAsJson<UserInfo>()
.Which.Should().BeEquivalentTo(MockUsers.UserUserInfo);
}
}
[Fact]
public async Task Get_Users_404()
{
using (var client = await _factory.CreateClientAsAdmin())
{
var res = await client.GetAsync("users/usernotexist");
res.Should().HaveStatusCodeNotFound()
.And.Should().HaveBodyAsCommonResponseWithCode(UserController.ErrorCodes.Get_NotExist);
}
}
[Fact]
public async Task Put_Patch_Delete_User()
{
using (var client = await _factory.CreateClientAsAdmin())
{
const string username = "putpatchdeleteuser";
const string password = "password";
const string url = "users/" + username;
// Put Invalid Model
await InvalidModelTestHelpers.TestPutInvalidModel(client, url, new UserPutRequest { Password = null, Administrator = false });
await InvalidModelTestHelpers.TestPutInvalidModel(client, url, new UserPutRequest { Password = password, Administrator = null });
async Task CheckAdministrator(bool administrator)
{
var res = await client.GetAsync(url);
res.Should().HaveStatusCodeOk()
.And.Should().HaveBodyAsJson<UserInfo>()
.Which.Administrator.Should().Be(administrator);
}
{
// Put Bad Username.
var res = await client.PutAsJsonAsync("users/dsf fddf", new UserPutRequest
{
Password = password,
Administrator = false
});
res.Should().HaveStatusCodeBadRequest()
.And.Should().HaveBodyAsCommonResponseWithCode(UserController.ErrorCodes.Put_BadUsername);
}
{
// Put Created.
var res = await client.PutAsJsonAsync(url, new UserPutRequest
{
Password = password,
Administrator = false
});
res.Should().BePutCreated();
await CheckAdministrator(false);
}
{
// Put Modified.
var res = await client.PutAsJsonAsync(url, new UserPutRequest
{
Password = password,
Administrator = true
});
res.Should().BePutModified();
await CheckAdministrator(true);
}
// Patch Not Exist
{
var res = await client.PatchAsJsonAsync("users/usernotexist", new UserPatchRequest { });
res.Should().HaveStatusCodeNotFound()
.And.Should().HaveBodyAsCommonResponseWithCode(UserController.ErrorCodes.Patch_NotExist);
}
// Patch Success
{
var res = await client.PatchAsJsonAsync(url, new UserPatchRequest { Administrator = false });
res.Should().HaveStatusCodeOk();
await CheckAdministrator(false);
}
// Delete Deleted
{
var res = await client.DeleteAsync(url);
res.Should().BeDeleteDeleted();
var res2 = await client.GetAsync(url);
res2.Should().HaveStatusCodeNotFound();
}
// Delete Not Exist
{
var res = await client.DeleteAsync(url);
res.Should().BeDeleteNotExist();
}
}
}
public class ChangePasswordUnitTest : IClassFixture<MyWebApplicationFactory<Startup>>
{
private const string url = "userop/changepassword";
private readonly WebApplicationFactory<Startup> _factory;
public ChangePasswordUnitTest(MyWebApplicationFactory<Startup> factory, ITestOutputHelper outputHelper)
{
_factory = factory.WithTestLogging(outputHelper);
}
[Fact]
public async Task InvalidModel_OldPassword()
{
using (var client = await _factory.CreateClientAsUser())
{
await InvalidModelTestHelpers.TestPostInvalidModel(client, url, new ChangePasswordRequest { OldPassword = null, NewPassword = "???" });
}
}
[Fact]
public async Task InvalidModel_NewPassword()
{
using (var client = await _factory.CreateClientAsUser())
{
await InvalidModelTestHelpers.TestPostInvalidModel(client, url, new ChangePasswordRequest { OldPassword = "???", NewPassword = null });
}
}
[Fact]
public async Task BadOldPassword()
{
using (var client = await _factory.CreateClientAsUser())
{
var res = await client.PostAsJsonAsync(url, new ChangePasswordRequest { OldPassword = "???", NewPassword = "???" });
res.Should().HaveStatusCodeBadRequest()
.And.Should().HaveBodyAsCommonResponseWithCode(UserController.ErrorCodes.ChangePassword_BadOldPassword);
}
}
[Fact]
public async Task Success()
{
const string username = "changepasswordtest";
const string password = "password";
// create a new user to avoid interference
using (var client = await _factory.CreateClientAsAdmin())
{
var res = await client.PutAsJsonAsync("users/" + username, new UserPutRequest { Password = password, Administrator = false });
res.Should().BePutCreated();
}
using (var client = await _factory.CreateClientWithCredential(username, password))
{
const string newPassword = "new";
var res = await client.PostAsJsonAsync(url, new ChangePasswordRequest { OldPassword = password, NewPassword = newPassword });
res.Should().HaveStatusCodeOk();
await client.CreateUserTokenAsync(username, newPassword);
}
}
}
}
}
|