aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/Controllers/UserControllerTest.cs
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-10-21 17:38:35 +0800
committer杨宇千 <crupest@outlook.com>2019-10-21 17:38:35 +0800
commit160f933a38a1c4b82df2038ab2446acde873ff8f (patch)
tree1d0e5eed0691f2a6babc2cdda2350ea16ba86420 /Timeline.Tests/Controllers/UserControllerTest.cs
parentea8dd31e88aaf13af1f51e764623d6a7c73fb429 (diff)
downloadtimeline-160f933a38a1c4b82df2038ab2446acde873ff8f.tar.gz
timeline-160f933a38a1c4b82df2038ab2446acde873ff8f.tar.bz2
timeline-160f933a38a1c4b82df2038ab2446acde873ff8f.zip
...
Diffstat (limited to 'Timeline.Tests/Controllers/UserControllerTest.cs')
-rw-r--r--Timeline.Tests/Controllers/UserControllerTest.cs132
1 files changed, 131 insertions, 1 deletions
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<OkResult>();
+ }
+
+ [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<NotFoundObjectResult>()
+ .Which.Value.Should().BeAssignableTo<CommonResponse>()
+ .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<OkObjectResult>()
+ .Which.Value.Should().BeAssignableTo<CommonDeleteResponse>()
+ .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<OkObjectResult>()
+ .Which.Value.Should().BeAssignableTo<CommonDeleteResponse>()
+ .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<OkResult>();
+ }
+
+ [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<BadRequestObjectResult>()
+ .Which.Value.Should().BeAssignableTo<CommonResponse>()
+ .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<OkResult>();
+ }
+
+ [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<BadRequestObjectResult>()
+ .Which.Value.Should().BeAssignableTo<CommonResponse>()
+ .Which.Code.Should().Be(Op.ChangePassword.BadOldPassword);
+ }
}
}