aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/Controllers/UserDetailControllerTest.cs
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-10-31 15:02:03 +0800
committerGitHub <noreply@github.com>2019-10-31 15:02:03 +0800
commit37a2e6340ab20de1f9e847d795c0cbec9846de97 (patch)
treeba42530cf4f13621a7a3a7ff661e383117119883 /Timeline.Tests/Controllers/UserDetailControllerTest.cs
parenta175f8328d7a6c36464676d54fc50d03e64be0af (diff)
parent2c3744ab5db476b64a32c19b50153e3e6166b0e6 (diff)
downloadtimeline-37a2e6340ab20de1f9e847d795c0cbec9846de97.tar.gz
timeline-37a2e6340ab20de1f9e847d795c0cbec9846de97.tar.bz2
timeline-37a2e6340ab20de1f9e847d795c0cbec9846de97.zip
Merge pull request #53 from crupest/nickname
Add nickname support.
Diffstat (limited to 'Timeline.Tests/Controllers/UserDetailControllerTest.cs')
-rw-r--r--Timeline.Tests/Controllers/UserDetailControllerTest.cs98
1 files changed, 98 insertions, 0 deletions
diff --git a/Timeline.Tests/Controllers/UserDetailControllerTest.cs b/Timeline.Tests/Controllers/UserDetailControllerTest.cs
new file mode 100644
index 00000000..ffd88790
--- /dev/null
+++ b/Timeline.Tests/Controllers/UserDetailControllerTest.cs
@@ -0,0 +1,98 @@
+using FluentAssertions;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Mvc;
+using Moq;
+using System;
+using System.ComponentModel.DataAnnotations;
+using System.Threading.Tasks;
+using Timeline.Controllers;
+using Timeline.Filters;
+using Timeline.Models.Validation;
+using Timeline.Services;
+using Timeline.Tests.Helpers;
+using Xunit;
+
+namespace Timeline.Tests.Controllers
+{
+ public class UserDetailControllerTest : IDisposable
+ {
+ private readonly Mock<IUserDetailService> _mockUserDetailService;
+ private readonly UserDetailController _controller;
+
+ public UserDetailControllerTest()
+ {
+ _mockUserDetailService = new Mock<IUserDetailService>();
+ _controller = new UserDetailController(_mockUserDetailService.Object);
+ }
+
+ public void Dispose()
+ {
+ _controller.Dispose();
+ }
+
+ [Fact]
+ public void AttributeTest()
+ {
+ typeof(UserDetailController).Should().BeDecoratedWith<ApiControllerAttribute>();
+
+ var getNickname = typeof(UserDetailController).GetMethod(nameof(UserDetailController.GetNickname));
+ getNickname.Should().BeDecoratedWith<HttpGetAttribute>()
+ .And.BeDecoratedWith<CatchUserNotExistExceptionAttribute>();
+ getNickname.GetParameter("username").Should().BeDecoratedWith<UsernameAttribute>()
+ .And.BeDecoratedWith<FromRouteAttribute>();
+
+ var putNickname = typeof(UserDetailController).GetMethod(nameof(UserDetailController.PutNickname));
+ putNickname.Should().BeDecoratedWith<HttpPutAttribute>()
+ .And.BeDecoratedWith<AuthorizeAttribute>()
+ .And.BeDecoratedWith<SelfOrAdminAttribute>()
+ .And.BeDecoratedWith<CatchUserNotExistExceptionAttribute>();
+ putNickname.GetParameter("username").Should().BeDecoratedWith<UsernameAttribute>()
+ .And.BeDecoratedWith<FromRouteAttribute>();
+ var stringLengthAttributeOnPutBody = putNickname.GetParameter("body").Should().BeDecoratedWith<FromBodyAttribute>()
+ .And.BeDecoratedWith<StringLengthAttribute>()
+ .Which;
+ stringLengthAttributeOnPutBody.MinimumLength.Should().Be(1);
+ stringLengthAttributeOnPutBody.MaximumLength.Should().Be(10);
+
+ var deleteNickname = typeof(UserDetailController).GetMethod(nameof(UserDetailController.DeleteNickname));
+ deleteNickname.Should().BeDecoratedWith<HttpDeleteAttribute>()
+ .And.BeDecoratedWith<AuthorizeAttribute>()
+ .And.BeDecoratedWith<SelfOrAdminAttribute>()
+ .And.BeDecoratedWith<CatchUserNotExistExceptionAttribute>();
+ deleteNickname.GetParameter("username").Should().BeDecoratedWith<UsernameAttribute>()
+ .And.BeDecoratedWith<FromRouteAttribute>();
+ }
+
+ [Fact]
+ public async Task GetNickname_ShouldWork()
+ {
+ const string username = "uuu";
+ const string nickname = "nnn";
+ _mockUserDetailService.Setup(s => s.GetNickname(username)).ReturnsAsync(nickname);
+ var actionResult = await _controller.GetNickname(username);
+ actionResult.Result.Should().BeAssignableTo<OkObjectResult>(nickname);
+ _mockUserDetailService.VerifyAll();
+ }
+
+ [Fact]
+ public async Task PutNickname_ShouldWork()
+ {
+ const string username = "uuu";
+ const string nickname = "nnn";
+ _mockUserDetailService.Setup(s => s.SetNickname(username, nickname)).Returns(Task.CompletedTask);
+ var actionResult = await _controller.PutNickname(username, nickname);
+ actionResult.Should().BeAssignableTo<OkResult>();
+ _mockUserDetailService.VerifyAll();
+ }
+
+ [Fact]
+ public async Task DeleteNickname_ShouldWork()
+ {
+ const string username = "uuu";
+ _mockUserDetailService.Setup(s => s.SetNickname(username, null)).Returns(Task.CompletedTask);
+ var actionResult = await _controller.DeleteNickname(username);
+ actionResult.Should().BeAssignableTo<OkResult>();
+ _mockUserDetailService.VerifyAll();
+ }
+ }
+}