From 2bb83325aeef0fe632390e11830c88c4b55ab293 Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Mon, 21 Oct 2019 17:38:35 +0800 Subject: ... --- Timeline.Tests/Controllers/UserControllerTest.cs | 132 ++++++++++++++++++++++- 1 file changed, 131 insertions(+), 1 deletion(-) (limited to 'Timeline.Tests/Controllers/UserControllerTest.cs') diff --git a/Timeline.Tests/Controllers/UserControllerTest.cs b/Timeline.Tests/Controllers/UserControllerTest.cs index 9fec477f..ddbc3fbc 100644 --- a/Timeline.Tests/Controllers/UserControllerTest.cs +++ b/Timeline.Tests/Controllers/UserControllerTest.cs @@ -1,9 +1,11 @@ using FluentAssertions; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging.Abstractions; using Moq; using System; using System.Linq; +using System.Security.Claims; using System.Threading.Tasks; using Timeline.Controllers; using Timeline.Models; @@ -104,6 +106,134 @@ namespace Timeline.Tests.Controllers .Which.Code.Should().Be(Put.BadUsername); } - //TODO! Complete this. + [Fact] + public async Task Patch_Success() + { + const string username = "aaa"; + const string password = "ppp"; + const bool administrator = true; + _mockUserService.Setup(s => s.PatchUser(username, password, administrator)).Returns(Task.CompletedTask); + var action = await _controller.Patch(new UserPatchRequest + { + Password = password, + Administrator = administrator + }, username); + action.Should().BeAssignableTo(); + } + + [Fact] + public async Task Patch_NotExist() + { + const string username = "aaa"; + const string password = "ppp"; + const bool administrator = true; + _mockUserService.Setup(s => s.PatchUser(username, password, administrator)).ThrowsAsync(new UserNotExistException()); + var action = await _controller.Patch(new UserPatchRequest + { + Password = password, + Administrator = administrator + }, username); + action.Should().BeAssignableTo() + .Which.Value.Should().BeAssignableTo() + .Which.Code.Should().Be(Patch.NotExist); + } + + [Fact] + public async Task Delete_Delete() + { + const string username = "aaa"; + _mockUserService.Setup(s => s.DeleteUser(username)).Returns(Task.CompletedTask); + var action = await _controller.Delete(username); + var body = action.Result.Should().BeAssignableTo() + .Which.Value.Should().BeAssignableTo() + .Which; + body.Code.Should().Be(0); + body.Data.Delete.Should().BeTrue(); + } + + [Fact] + public async Task Delete_NotExist() + { + const string username = "aaa"; + _mockUserService.Setup(s => s.DeleteUser(username)).ThrowsAsync(new UserNotExistException()); + var action = await _controller.Delete(username); + var body = action.Result.Should().BeAssignableTo() + .Which.Value.Should().BeAssignableTo() + .Which; + body.Code.Should().Be(0); + body.Data.Delete.Should().BeFalse(); + } + + [Fact] + public async Task Op_ChangeUsername_Success() + { + const string oldUsername = "aaa"; + const string newUsername = "bbb"; + _mockUserService.Setup(s => s.ChangeUsername(oldUsername, newUsername)).Returns(Task.CompletedTask); + var action = await _controller.ChangeUsername(new ChangeUsernameRequest { OldUsername = oldUsername, NewUsername = newUsername }); + action.Should().BeAssignableTo(); + } + + [Theory] + [InlineData(typeof(UserNotExistException), Op.ChangeUsername.NotExist)] + [InlineData(typeof(UserAlreadyExistException), Op.ChangeUsername.AlreadyExist)] + public async Task Op_ChangeUsername_Failure(Type exceptionType, int code) + { + const string oldUsername = "aaa"; + const string newUsername = "bbb"; + _mockUserService.Setup(s => s.ChangeUsername(oldUsername, newUsername)).ThrowsAsync(Activator.CreateInstance(exceptionType) as Exception); + var action = await _controller.ChangeUsername(new ChangeUsernameRequest { OldUsername = oldUsername, NewUsername = newUsername }); + action.Should().BeAssignableTo() + .Which.Value.Should().BeAssignableTo() + .Which.Code.Should().Be(code); + } + + [Fact] + public async Task Op_ChangePassword_Success() + { + const string username = "aaa"; + const string oldPassword = "aaa"; + const string newPassword = "bbb"; + _mockUserService.Setup(s => s.ChangePassword(username, oldPassword, newPassword)).Returns(Task.CompletedTask); + + _controller.ControllerContext = new ControllerContext() + { + HttpContext = new DefaultHttpContext() + { + User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] + { + new Claim(ClaimTypes.Name, username) + }, "TestAuthType")) + } + }; + + var action = await _controller.ChangePassword(new ChangePasswordRequest { OldPassword = oldPassword, NewPassword = newPassword }); + action.Should().BeAssignableTo(); + } + + [Fact] + public async Task Op_ChangePassword_BadPassword() + { + const string username = "aaa"; + const string oldPassword = "aaa"; + const string newPassword = "bbb"; + _mockUserService.Setup(s => s.ChangePassword(username, oldPassword, newPassword)).ThrowsAsync(new BadPasswordException()); + + _controller.ControllerContext = new ControllerContext() + { + HttpContext = new DefaultHttpContext() + { + User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] + { + new Claim(ClaimTypes.Name, username) + }, "TestAuthType")) + } + }; + + var action = await _controller.ChangePassword(new ChangePasswordRequest { OldPassword = oldPassword, NewPassword = newPassword }); + action.Should().BeAssignableTo() + .Which.Value.Should().BeAssignableTo() + .Which.Code.Should().Be(Op.ChangePassword.BadOldPassword); + } } } -- cgit v1.2.3