aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/UserUnitTest.cs
blob: 77ec37eee2872ffe22bde0a19e48a4f9693e587c (plain)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using FluentAssertions;
using Microsoft.AspNetCore.Mvc.Testing;
using System;
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>>, IDisposable
    {
        private readonly WebApplicationFactory<Startup> _factory;
        private readonly Action _disposeAction;

        public UserUnitTest(MyWebApplicationFactory<Startup> factory, ITestOutputHelper outputHelper)
        {
            _factory = factory.WithTestConfig(outputHelper, out _disposeAction);
        }

        public void Dispose()
        {
            _disposeAction();
        }

        [Fact]
        public async Task Get_Users_List()
        {
            using (var client = await _factory.CreateClientAsAdmin())
            {
                var res = await client.GetAsync("users");
                res.Should().HaveStatusCodeOk().And.Should().HaveBodyAsJson<UserInfo[]>()
                    .Which.Should().BeEquivalentTo(MockUsers.UserInfos);
            }
        }

        [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_InvalidModel()
        {
            using (var client = await _factory.CreateClientAsAdmin())
            {
                const string url = "users/aaaaaaaa";
                // missing password
                await InvalidModelTestHelpers.TestPutInvalidModel(client, url, new UserPutRequest { Password = null, Administrator = false });
                // missing administrator
                await InvalidModelTestHelpers.TestPutInvalidModel(client, url, new UserPutRequest { Password = "???", Administrator = null });
            }
        }

        [Fact]
        public async Task Put_BadUsername()
        {
            using (var client = await _factory.CreateClientAsAdmin())
            {
                var res = await client.PutAsJsonAsync("users/dsf fddf", new UserPutRequest
                {
                    Password = "???",
                    Administrator = false
                });
                res.Should().HaveStatusCodeBadRequest()
                    .And.Should().HaveBodyAsCommonResponseWithCode(UserController.ErrorCodes.Put_BadUsername);
            }
        }

        private async Task CheckAdministrator(HttpClient client, string username, bool administrator)
        {
            var res = await client.GetAsync("users/" + username);
            res.Should().HaveStatusCodeOk()
                .And.Should().HaveBodyAsJson<UserInfo>()
                .Which.Administrator.Should().Be(administrator);
        }

        [Fact]
        public async Task Put_Modiefied()
        {
            using (var client = await _factory.CreateClientAsAdmin())
            {
                var res = await client.PutAsJsonAsync("users/" + MockUsers.UserUsername, new UserPutRequest
                {
                    Password = "password",
                    Administrator = false
                });
                res.Should().BePutModified();
                await CheckAdministrator(client, MockUsers.UserUsername, false);
            }
        }

        [Fact]
        public async Task Put_Created()
        {
            using (var client = await _factory.CreateClientAsAdmin())
            {
                const string username = "puttest";
                const string url = "users/" + username;

                var res = await client.PutAsJsonAsync(url, new UserPutRequest
                {
                    Password = "password",
                    Administrator = false
                });
                res.Should().BePutCreated();
                await CheckAdministrator(client, username, false);
            }
        }

        [Fact]
        public async Task Patch_NotExist()
        {
            using (var client = await _factory.CreateClientAsAdmin())
            {
                var res = await client.PatchAsJsonAsync("users/usernotexist", new UserPatchRequest { });
                res.Should().HaveStatusCodeNotFound()
                    .And.Should().HaveBodyAsCommonResponseWithCode(UserController.ErrorCodes.Patch_NotExist);
            }
        }

        [Fact]
        public async Task Patch_Success()
        {
            using (var client = await _factory.CreateClientAsAdmin())
            {
                {
                    var res = await client.PatchAsJsonAsync("users/" + MockUsers.UserUsername,
                        new UserPatchRequest { Administrator = false });
                    res.Should().HaveStatusCodeOk();
                    await CheckAdministrator(client, MockUsers.UserUsername, false);
                }
            }
        }

        [Fact]
        public async Task Delete_Deleted()
        {
            using (var client = await _factory.CreateClientAsAdmin())
            {
                {
                    var url = "users/" + MockUsers.UserUsername;
                    var res = await client.DeleteAsync(url);
                    res.Should().BeDeleteDeleted();

                    var res2 = await client.GetAsync(url);
                    res2.Should().HaveStatusCodeNotFound();
                }
            }
        }

        [Fact]
        public async Task Delete_NotExist()
        {
            using (var client = await _factory.CreateClientAsAdmin())
            {
                {
                    var res = await client.DeleteAsync("users/usernotexist");
                    res.Should().BeDeleteNotExist();
                }
            }
        }


        public class ChangeUsernameUnitTest : IClassFixture<MyWebApplicationFactory<Startup>>, IDisposable
        {
            private const string url = "userop/changeusername";

            private readonly WebApplicationFactory<Startup> _factory;
            private readonly Action _disposeAction;

            public ChangeUsernameUnitTest(MyWebApplicationFactory<Startup> factory, ITestOutputHelper outputHelper)
            {
                _factory = factory.WithTestConfig(outputHelper, out _disposeAction);
            }

            public void Dispose()
            {
                _disposeAction();
            }

            [Fact]
            public async Task InvalidModel()
            {
                using (var client = await _factory.CreateClientAsAdmin())
                {
                    // missing old username
                    await InvalidModelTestHelpers.TestPostInvalidModel(client, url,
                        new ChangeUsernameRequest { OldUsername= null, NewUsername= "hhh" });
                    // missing new username
                    await InvalidModelTestHelpers.TestPostInvalidModel(client, url,
                        new ChangeUsernameRequest { OldUsername= "hhh", NewUsername= null });
                    // bad username
                    await InvalidModelTestHelpers.TestPostInvalidModel(client, url,
                        new ChangeUsernameRequest { OldUsername = "hhh", NewUsername = "???" });
                }
            }

            [Fact]
            public async Task UserNotExist()
            {
                using (var client = await _factory.CreateClientAsAdmin())
                {
                    var res = await client.PostAsJsonAsync(url,
                        new ChangeUsernameRequest{ OldUsername= "usernotexist", NewUsername= "newUsername" });
                    res.Should().HaveStatusCodeBadRequest()
                        .And.Should().HaveBodyAsCommonResponseWithCode(UserController.ErrorCodes.ChangeUsername_NotExist);
                }
            }

            [Fact]
            public async Task UserAlreadyExist()
            {
                using (var client = await _factory.CreateClientAsAdmin())
                {
                    var res = await client.PostAsJsonAsync(url,
                        new ChangeUsernameRequest { OldUsername = MockUsers.UserUsername, NewUsername = MockUsers.AdminUsername });
                    res.Should().HaveStatusCodeBadRequest()
                        .And.Should().HaveBodyAsCommonResponseWithCode(UserController.ErrorCodes.ChangeUsername_AlreadyExist);
                }
            }

            [Fact]
            public async Task Success()
            {
                using (var client = await _factory.CreateClientAsAdmin())
                {
                    const string newUsername = "hahaha";
                    var res = await client.PostAsJsonAsync(url,
                        new ChangeUsernameRequest { OldUsername = MockUsers.UserUsername, NewUsername = newUsername });
                    res.Should().HaveStatusCodeOk();
                    await client.CreateUserTokenAsync(newUsername, MockUsers.UserPassword);
                }
            }
        }


        public class ChangePasswordUnitTest : IClassFixture<MyWebApplicationFactory<Startup>>, IDisposable
        {
            private const string url = "userop/changepassword";

            private readonly WebApplicationFactory<Startup> _factory;
            private readonly Action _disposeAction;

            public ChangePasswordUnitTest(MyWebApplicationFactory<Startup> factory, ITestOutputHelper outputHelper)
            {
                _factory = factory.WithTestConfig(outputHelper, out _disposeAction);
            }

            public void Dispose()
            {
                _disposeAction();
            }

            [Fact]
            public async Task InvalidModel()
            {
                using (var client = await _factory.CreateClientAsUser())
                {
                    // missing old password
                    await InvalidModelTestHelpers.TestPostInvalidModel(client, url,
                        new ChangePasswordRequest { OldPassword = null, NewPassword = "???" });
                    // missing new password
                    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()
            {
                using (var client = await _factory.CreateClientAsUser())
                {
                    const string newPassword = "new";
                    var res = await client.PostAsJsonAsync(url,
                        new ChangePasswordRequest { OldPassword = MockUsers.UserPassword, NewPassword = newPassword });
                    res.Should().HaveStatusCodeOk();
                    await client.CreateUserTokenAsync(MockUsers.UserUsername, newPassword);
                }
            }
        }
    }
}