From fa2a3282c51d831b25f374803301e75eac15d11c Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Thu, 17 Oct 2019 20:46:57 +0800 Subject: ... --- Timeline.Tests/IntegratedTests/UserUnitTest.cs | 320 +++++++++++++++++++++++++ 1 file changed, 320 insertions(+) create mode 100644 Timeline.Tests/IntegratedTests/UserUnitTest.cs (limited to 'Timeline.Tests/IntegratedTests/UserUnitTest.cs') diff --git a/Timeline.Tests/IntegratedTests/UserUnitTest.cs b/Timeline.Tests/IntegratedTests/UserUnitTest.cs new file mode 100644 index 00000000..d228c563 --- /dev/null +++ b/Timeline.Tests/IntegratedTests/UserUnitTest.cs @@ -0,0 +1,320 @@ +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; + +namespace Timeline.Tests +{ + public class UserUnitTest : IClassFixture>, IDisposable + { + private readonly TestApplication _testApp; + private readonly WebApplicationFactory _factory; + + public UserUnitTest(WebApplicationFactory factory) + { + _testApp = new TestApplication(factory); + _factory = _testApp.Factory; + } + + public void Dispose() + { + _testApp.Dispose(); + } + + [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() + .Which.Should().BeEquivalentTo(MockUser.UserInfoList); + } + } + + [Fact] + public async Task Get_Users_User() + { + using (var client = await _factory.CreateClientAsAdmin()) + { + var res = await client.GetAsync("users/" + MockUser.User.Username); + res.Should().HaveStatusCodeOk() + .And.Should().HaveBodyAsJson() + .Which.Should().BeEquivalentTo(MockUser.User.Info); + } + } + + [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() + .Which.Administrator.Should().Be(administrator); + } + + [Fact] + public async Task Put_Modiefied() + { + using (var client = await _factory.CreateClientAsAdmin()) + { + var res = await client.PutAsJsonAsync("users/" + MockUser.User.Username, new UserPutRequest + { + Password = "password", + Administrator = false + }); + res.Should().BePutModify(); + await CheckAdministrator(client, MockUser.User.Username, 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().BePutCreate(); + 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/" + MockUser.User.Username, + new UserPatchRequest { Administrator = false }); + res.Should().HaveStatusCodeOk(); + await CheckAdministrator(client, MockUser.User.Username, false); + } + } + } + + [Fact] + public async Task Delete_Deleted() + { + using (var client = await _factory.CreateClientAsAdmin()) + { + { + var url = "users/" + MockUser.User.Username; + var res = await client.DeleteAsync(url); + res.Should().BeDeleteDelete(); + + 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>, IDisposable + { + private const string url = "userop/changeusername"; + + private readonly TestApplication _testApp; + private readonly WebApplicationFactory _factory; + + public ChangeUsernameUnitTest(WebApplicationFactory factory) + { + _testApp = new TestApplication(factory); + _factory = _testApp.Factory; + } + + public void Dispose() + { + _testApp.Dispose(); + } + + [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 = MockUser.User.Username, NewUsername = MockUser.Admin.Username }); + 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 = MockUser.User.Username, NewUsername = newUsername }); + res.Should().HaveStatusCodeOk(); + await client.CreateUserTokenAsync(newUsername, MockUser.User.Password); + } + } + } + + + public class ChangePasswordUnitTest : IClassFixture>, IDisposable + { + private const string url = "userop/changepassword"; + + private readonly TestApplication _testApp; + private readonly WebApplicationFactory _factory; + + public ChangePasswordUnitTest(WebApplicationFactory factory) + { + _testApp = new TestApplication(factory); + _factory = _testApp.Factory; + } + + public void Dispose() + { + _testApp.Dispose(); + } + + [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 = MockUser.User.Password, NewPassword = newPassword }); + res.Should().HaveStatusCodeOk(); + await client.CreateUserTokenAsync(MockUser.User.Username, newPassword); + } + } + } + } +} -- cgit v1.2.3