diff options
Diffstat (limited to 'Timeline.Tests/Controllers')
-rw-r--r-- | Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs | 388 | ||||
-rw-r--r-- | Timeline.Tests/Controllers/TokenControllerTest.cs | 119 | ||||
-rw-r--r-- | Timeline.Tests/Controllers/UserControllerTest.cs | 218 | ||||
-rw-r--r-- | Timeline.Tests/Controllers/UserDetailControllerTest.cs | 98 |
4 files changed, 0 insertions, 823 deletions
diff --git a/Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs b/Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs deleted file mode 100644 index 372ba8a7..00000000 --- a/Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs +++ /dev/null @@ -1,388 +0,0 @@ -using FluentAssertions;
-using Microsoft.AspNetCore.Authorization;
-using Microsoft.AspNetCore.Http;
-using Microsoft.AspNetCore.Mvc;
-using Microsoft.Extensions.Logging.Abstractions;
-using Moq;
-using System;
-using System.Collections.Generic;
-using System.Reflection;
-using System.Threading.Tasks;
-using Timeline.Controllers;
-using Timeline.Filters;
-using Timeline.Models;
-using Timeline.Models.Http;
-using Timeline.Models.Validation;
-using Timeline.Services;
-using Timeline.Tests.Helpers;
-using Xunit;
-
-namespace Timeline.Tests.Controllers
-{
- public class PersonalTimelineControllerTest : IDisposable
- {
- private readonly Mock<IPersonalTimelineService> _service;
-
- private readonly PersonalTimelineController _controller;
-
- public PersonalTimelineControllerTest()
- {
- _service = new Mock<IPersonalTimelineService>();
- _controller = new PersonalTimelineController(NullLogger<PersonalTimelineController>.Instance, _service.Object);
- }
-
- public void Dispose()
- {
- _controller.Dispose();
- }
-
- [Fact]
- public void AttributeTest()
- {
- static void AssertUsernameParameter(MethodInfo m)
- {
- m.GetParameter("username")
- .Should().BeDecoratedWith<FromRouteAttribute>()
- .And.BeDecoratedWith<UsernameAttribute>();
- }
-
- static void AssertBodyParamter<TBody>(MethodInfo m)
- {
- var p = m.GetParameter("body");
- p.Should().BeDecoratedWith<FromBodyAttribute>();
- p.ParameterType.Should().Be(typeof(TBody));
- }
-
- var type = typeof(PersonalTimelineController);
- type.Should().BeDecoratedWith<ApiControllerAttribute>();
-
- {
- var m = type.GetMethod(nameof(PersonalTimelineController.TimelineGet));
- m.Should().BeDecoratedWith<CatchTimelineNotExistExceptionAttribute>()
- .And.BeDecoratedWith<HttpGetAttribute>();
- AssertUsernameParameter(m);
- }
-
- {
- var m = type.GetMethod(nameof(PersonalTimelineController.PostListGet));
- m.Should().BeDecoratedWith<CatchTimelineNotExistExceptionAttribute>()
- .And.BeDecoratedWith<HttpGetAttribute>();
- AssertUsernameParameter(m);
- }
-
- {
- var m = type.GetMethod(nameof(PersonalTimelineController.PostOperationCreate));
- m.Should().BeDecoratedWith<CatchTimelineNotExistExceptionAttribute>()
- .And.BeDecoratedWith<AuthorizeAttribute>()
- .And.BeDecoratedWith<HttpPostAttribute>();
- AssertUsernameParameter(m);
- AssertBodyParamter<TimelinePostCreateRequest>(m);
- }
-
- {
- var m = type.GetMethod(nameof(PersonalTimelineController.PostOperationDelete));
- m.Should().BeDecoratedWith<CatchTimelineNotExistExceptionAttribute>()
- .And.BeDecoratedWith<AuthorizeAttribute>()
- .And.BeDecoratedWith<HttpPostAttribute>();
- AssertUsernameParameter(m);
- AssertBodyParamter<TimelinePostDeleteRequest>(m);
- }
-
- {
- var m = type.GetMethod(nameof(PersonalTimelineController.TimelineChangeProperty));
- m.Should().BeDecoratedWith<CatchTimelineNotExistExceptionAttribute>()
- .And.BeDecoratedWith<AuthorizeAttribute>()
- .And.BeDecoratedWith<SelfOrAdminAttribute>()
- .And.BeDecoratedWith<HttpPostAttribute>();
- AssertUsernameParameter(m);
- AssertBodyParamter<TimelinePropertyChangeRequest>(m);
- }
-
- {
- var m = type.GetMethod(nameof(PersonalTimelineController.TimelineChangeMember));
- m.Should().BeDecoratedWith<CatchTimelineNotExistExceptionAttribute>()
- .And.BeDecoratedWith<AuthorizeAttribute>()
- .And.BeDecoratedWith<SelfOrAdminAttribute>()
- .And.BeDecoratedWith<HttpPostAttribute>();
- AssertUsernameParameter(m);
- AssertBodyParamter<TimelineMemberChangeRequest>(m);
- }
- }
-
- const string authUsername = "authuser";
- private void SetUser(bool administrator)
- {
- _controller.ControllerContext = new ControllerContext
- {
- HttpContext = new DefaultHttpContext
- {
- User = PrincipalHelper.Create(authUsername, administrator)
- }
- };
- }
-
- [Fact]
- public async Task TimelineGet()
- {
- const string username = "username";
- var timelineInfo = new BaseTimelineInfo();
- _service.Setup(s => s.GetTimeline(username)).ReturnsAsync(timelineInfo);
- (await _controller.TimelineGet(username)).Value.Should().Be(timelineInfo);
- _service.VerifyAll();
- }
-
- [Fact]
- public async Task PostListGet_Forbid()
- {
- const string username = "username";
- SetUser(false);
- _service.Setup(s => s.HasReadPermission(username, authUsername)).ReturnsAsync(false);
- var result = (await _controller.PostListGet(username)).Result
- .Should().BeAssignableTo<ObjectResult>()
- .Which;
- result.StatusCode.Should().Be(StatusCodes.Status403Forbidden);
- result.Value.Should().BeAssignableTo<CommonResponse>()
- .Which.Code.Should().Be(ErrorCodes.Http.Timeline.PostListGetForbid);
- _service.VerifyAll();
- }
-
- [Fact]
- public async Task PostListGet_Admin_Success()
- {
- const string username = "username";
- SetUser(true);
- _service.Setup(s => s.GetPosts(username)).ReturnsAsync(new List<TimelinePostInfo>());
- (await _controller.PostListGet(username)).Value
- .Should().BeAssignableTo<IList<TimelinePostInfo>>()
- .Which.Should().NotBeNull().And.BeEmpty();
- _service.VerifyAll();
- }
-
- [Fact]
- public async Task PostListGet_User_Success()
- {
- const string username = "username";
- SetUser(false);
- _service.Setup(s => s.HasReadPermission(username, authUsername)).ReturnsAsync(true);
- _service.Setup(s => s.GetPosts(username)).ReturnsAsync(new List<TimelinePostInfo>());
- (await _controller.PostListGet(username)).Value
- .Should().BeAssignableTo<IList<TimelinePostInfo>>()
- .Which.Should().NotBeNull().And.BeEmpty();
- _service.VerifyAll();
- }
-
- [Fact]
- public async Task PostOperationCreate_Forbid()
- {
- const string username = "username";
- const string content = "cccc";
- SetUser(false);
- _service.Setup(s => s.IsMemberOf(username, authUsername)).ReturnsAsync(false);
- var result = (await _controller.PostOperationCreate(username, new TimelinePostCreateRequest
- {
- Content = content,
- Time = null
- })).Result.Should().NotBeNull().And.BeAssignableTo<ObjectResult>().Which;
- result.StatusCode.Should().Be(StatusCodes.Status403Forbidden);
- result.Value.Should().BeAssignableTo<CommonResponse>()
- .Which.Code.Should().Be(ErrorCodes.Http.Timeline.PostOperationCreateForbid);
- _service.VerifyAll();
- }
-
- [Fact]
- public async Task PostOperationCreate_Admin_Success()
- {
- const string username = "username";
- const string content = "cccc";
- var response = new TimelinePostCreateResponse
- {
- Id = 3,
- Time = DateTime.Now
- };
- SetUser(true);
- _service.Setup(s => s.CreatePost(username, authUsername, content, null)).ReturnsAsync(response);
- var resultValue = (await _controller.PostOperationCreate(username, new TimelinePostCreateRequest
- {
- Content = content,
- Time = null
- })).Value;
- resultValue.Should().NotBeNull()
- .And.BeAssignableTo<TimelinePostCreateResponse>()
- .And.BeEquivalentTo(response);
- _service.VerifyAll();
- }
-
- [Fact]
- public async Task PostOperationCreate_User_Success()
- {
- const string username = "username";
- const string content = "cccc";
- var response = new TimelinePostCreateResponse
- {
- Id = 3,
- Time = DateTime.Now
- };
- SetUser(false);
- _service.Setup(s => s.IsMemberOf(username, authUsername)).ReturnsAsync(true);
- _service.Setup(s => s.CreatePost(username, authUsername, content, null)).ReturnsAsync(response);
- var resultValue = (await _controller.PostOperationCreate(username, new TimelinePostCreateRequest
- {
- Content = content,
- Time = null
- })).Value;
- resultValue.Should().NotBeNull()
- .And.BeAssignableTo<TimelinePostCreateResponse>()
- .And.BeEquivalentTo(response);
- _service.VerifyAll();
- }
-
- [Fact]
- public async Task PostOperationDelete_Forbid()
- {
- const string username = "username";
- const long postId = 2;
- SetUser(false);
- _service.Setup(s => s.HasPostModifyPermission(username, postId, authUsername)).ReturnsAsync(false);
- var result = (await _controller.PostOperationDelete(username, new TimelinePostDeleteRequest
- {
- Id = postId
- })).Should().NotBeNull().And.BeAssignableTo<ObjectResult>().Which;
- result.StatusCode.Should().Be(StatusCodes.Status403Forbidden);
- result.Value.Should().BeAssignableTo<CommonResponse>()
- .Which.Code.Should().Be(ErrorCodes.Http.Timeline.PostOperationDeleteForbid);
- _service.VerifyAll();
- }
-
- [Fact]
- public async Task PostOperationDelete_NotExist()
- {
- const string username = "username";
- const long postId = 2;
- SetUser(true);
- _service.Setup(s => s.DeletePost(username, postId)).ThrowsAsync(new TimelinePostNotExistException());
- var result = (await _controller.PostOperationDelete(username, new TimelinePostDeleteRequest
- {
- Id = postId
- })).Should().NotBeNull().And.BeAssignableTo<ObjectResult>().Which;
- result.StatusCode.Should().Be(StatusCodes.Status400BadRequest);
- result.Value.Should().BeAssignableTo<CommonResponse>()
- .Which.Code.Should().Be(ErrorCodes.Http.Timeline.PostOperationDeleteNotExist);
- _service.VerifyAll();
- }
-
- [Fact]
- public async Task PostOperationDelete_Admin_Success()
- {
- const string username = "username";
- const long postId = 2;
- SetUser(true);
- _service.Setup(s => s.DeletePost(username, postId)).Returns(Task.CompletedTask);
- var result = await _controller.PostOperationDelete(username, new TimelinePostDeleteRequest
- {
- Id = postId
- });
- result.Should().NotBeNull().And.BeAssignableTo<OkResult>();
- _service.VerifyAll();
- }
-
- [Fact]
- public async Task PostOperationDelete_User_Success()
- {
- const string username = "username";
- const long postId = 2;
- SetUser(false);
- _service.Setup(s => s.DeletePost(username, postId)).Returns(Task.CompletedTask);
- _service.Setup(s => s.HasPostModifyPermission(username, postId, authUsername)).ReturnsAsync(true);
- var result = await _controller.PostOperationDelete(username, new TimelinePostDeleteRequest
- {
- Id = postId
- });
- result.Should().NotBeNull().And.BeAssignableTo<OkResult>();
- _service.VerifyAll();
- }
-
- [Fact]
- public async Task TimelineChangeProperty_Success()
- {
- const string username = "username";
- var req = new TimelinePropertyChangeRequest
- {
- Description = "",
- Visibility = TimelineVisibility.Private
- };
- _service.Setup(s => s.ChangeProperty(username, req)).Returns(Task.CompletedTask);
- var result = await _controller.TimelineChangeProperty(username, req);
- result.Should().NotBeNull().And.BeAssignableTo<OkResult>();
- _service.VerifyAll();
- }
-
- [Fact]
- public async Task TimelineChangeMember_Success()
- {
- const string username = "username";
- var add = new List<string> { "aaa" };
- var remove = new List<string> { "rrr" };
- _service.Setup(s => s.ChangeMember(username, add, remove)).Returns(Task.CompletedTask);
- var result = await _controller.TimelineChangeMember(username, new TimelineMemberChangeRequest
- {
- Add = add,
- Remove = remove
- });
- result.Should().NotBeNull().And.BeAssignableTo<OkResult>();
- _service.VerifyAll();
- }
-
- [Fact]
- public async Task TimelineChangeMember_UsernameBadFormat()
- {
- const string username = "username";
- var add = new List<string> { "aaa" };
- var remove = new List<string> { "rrr" };
- _service.Setup(s => s.ChangeMember(username, add, remove)).ThrowsAsync(
- new TimelineMemberOperationUserException("test", new UsernameBadFormatException()));
- var result = await _controller.TimelineChangeMember(username, new TimelineMemberChangeRequest
- {
- Add = add,
- Remove = remove
- });
- result.Should().NotBeNull().And.BeAssignableTo<BadRequestObjectResult>()
- .Which.Value.Should().BeAssignableTo<CommonResponse>()
- .Which.Code.Should().Be(ErrorCodes.Http.Common.InvalidModel);
- _service.VerifyAll();
- }
-
- [Fact]
- public async Task TimelineChangeMember_AddNotExist()
- {
- const string username = "username";
- var add = new List<string> { "aaa" };
- var remove = new List<string> { "rrr" };
- _service.Setup(s => s.ChangeMember(username, add, remove)).ThrowsAsync(
- new TimelineMemberOperationUserException("test", new UserNotExistException()));
- var result = await _controller.TimelineChangeMember(username, new TimelineMemberChangeRequest
- {
- Add = add,
- Remove = remove
- });
- result.Should().NotBeNull().And.BeAssignableTo<BadRequestObjectResult>()
- .Which.Value.Should().BeAssignableTo<CommonResponse>()
- .Which.Code.Should().Be(ErrorCodes.Http.Timeline.ChangeMemberUserNotExist);
- _service.VerifyAll();
- }
-
- [Fact]
- public async Task TimelineChangeMember_UnknownTimelineMemberOperationUserException()
- {
- const string username = "username";
- var add = new List<string> { "aaa" };
- var remove = new List<string> { "rrr" };
- _service.Setup(s => s.ChangeMember(username, add, remove)).ThrowsAsync(
- new TimelineMemberOperationUserException("test", null));
- await _controller.Awaiting(c => c.TimelineChangeMember(username, new TimelineMemberChangeRequest
- {
- Add = add,
- Remove = remove
- })).Should().ThrowAsync<TimelineMemberOperationUserException>(); // Should rethrow.
- }
- }
-}
diff --git a/Timeline.Tests/Controllers/TokenControllerTest.cs b/Timeline.Tests/Controllers/TokenControllerTest.cs deleted file mode 100644 index 238fc237..00000000 --- a/Timeline.Tests/Controllers/TokenControllerTest.cs +++ /dev/null @@ -1,119 +0,0 @@ -using FluentAssertions;
-using Microsoft.AspNetCore.Mvc;
-using Microsoft.Extensions.Logging.Abstractions;
-using Moq;
-using System;
-using System.Collections.Generic;
-using System.Threading.Tasks;
-using Timeline.Controllers;
-using Timeline.Models.Http;
-using Timeline.Services;
-using Timeline.Tests.Helpers;
-using Xunit;
-using static Timeline.ErrorCodes.Http.Token;
-
-namespace Timeline.Tests.Controllers
-{
- public class TokenControllerTest : IDisposable
- {
- private readonly Mock<IUserService> _mockUserService = new Mock<IUserService>();
- private readonly TestClock _mockClock = new TestClock();
-
-
- private readonly TokenController _controller;
-
- public TokenControllerTest()
- {
- _controller = new TokenController(_mockUserService.Object, NullLogger<TokenController>.Instance, _mockClock);
- }
-
- public void Dispose()
- {
- _controller.Dispose();
- }
-
- [Theory]
- [InlineData(null)]
- [InlineData(100)]
- public async Task Create_Ok(int? expire)
- {
- var mockCurrentTime = DateTime.Now;
- _mockClock.MockCurrentTime = mockCurrentTime;
- var createResult = new CreateTokenResult
- {
- Token = "mocktokenaaaaa",
- User = MockUser.User.Info
- };
- _mockUserService.Setup(s => s.CreateToken("u", "p", expire == null ? null : (DateTime?)mockCurrentTime.AddDays(expire.Value))).ReturnsAsync(createResult);
- var action = await _controller.Create(new CreateTokenRequest
- {
- Username = "u",
- Password = "p",
- Expire = expire
- });
- action.Result.Should().BeAssignableTo<OkObjectResult>()
- .Which.Value.Should().BeEquivalentTo(createResult);
- }
-
- [Fact]
- public async Task Create_UserNotExist()
- {
- _mockUserService.Setup(s => s.CreateToken("u", "p", null)).ThrowsAsync(new UserNotExistException("u"));
- var action = await _controller.Create(new CreateTokenRequest
- {
- Username = "u",
- Password = "p",
- Expire = null
- });
- action.Result.Should().BeAssignableTo<BadRequestObjectResult>()
- .Which.Value.Should().BeAssignableTo<CommonResponse>()
- .Which.Code.Should().Be(Create.BadCredential);
- }
-
- [Fact]
- public async Task Create_BadPassword()
- {
- _mockUserService.Setup(s => s.CreateToken("u", "p", null)).ThrowsAsync(new BadPasswordException("u"));
- var action = await _controller.Create(new CreateTokenRequest
- {
- Username = "u",
- Password = "p",
- Expire = null
- });
- action.Result.Should().BeAssignableTo<BadRequestObjectResult>()
- .Which.Value.Should().BeAssignableTo<CommonResponse>()
- .Which.Code.Should().Be(Create.BadCredential);
- }
-
- [Fact]
- public async Task Verify_Ok()
- {
- const string token = "aaaaaaaaaaaaaa";
- _mockUserService.Setup(s => s.VerifyToken(token)).ReturnsAsync(MockUser.User.Info);
- var action = await _controller.Verify(new VerifyTokenRequest { Token = token });
- action.Result.Should().BeAssignableTo<OkObjectResult>()
- .Which.Value.Should().BeAssignableTo<VerifyTokenResponse>()
- .Which.User.Should().BeEquivalentTo(MockUser.User.Info);
- }
-
- public static IEnumerable<object[]> Verify_BadRequest_Data()
- {
- yield return new object[] { new JwtVerifyException(JwtVerifyException.ErrorCodes.Expired), Verify.Expired };
- yield return new object[] { new JwtVerifyException(JwtVerifyException.ErrorCodes.IdClaimBadFormat), Verify.BadFormat };
- yield return new object[] { new JwtVerifyException(JwtVerifyException.ErrorCodes.OldVersion), Verify.OldVersion };
- yield return new object[] { new UserNotExistException(), Verify.UserNotExist };
- }
-
- [Theory]
- [MemberData(nameof(Verify_BadRequest_Data))]
- public async Task Verify_BadRequest(Exception e, int code)
- {
- const string token = "aaaaaaaaaaaaaa";
- _mockUserService.Setup(s => s.VerifyToken(token)).ThrowsAsync(e);
- var action = await _controller.Verify(new VerifyTokenRequest { Token = token });
- action.Result.Should().BeAssignableTo<BadRequestObjectResult>()
- .Which.Value.Should().BeAssignableTo<CommonResponse>()
- .Which.Code.Should().Be(code);
- }
- }
-}
diff --git a/Timeline.Tests/Controllers/UserControllerTest.cs b/Timeline.Tests/Controllers/UserControllerTest.cs deleted file mode 100644 index a5ca7a2b..00000000 --- a/Timeline.Tests/Controllers/UserControllerTest.cs +++ /dev/null @@ -1,218 +0,0 @@ -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;
-using Timeline.Models.Http;
-using Timeline.Services;
-using Timeline.Tests.Helpers;
-using Xunit;
-using static Timeline.ErrorCodes.Http.User;
-
-namespace Timeline.Tests.Controllers
-{
- public class UserControllerTest : IDisposable
- {
- private readonly Mock<IUserService> _mockUserService = new Mock<IUserService>();
-
- private readonly UserController _controller;
-
- public UserControllerTest()
- {
- _controller = new UserController(NullLogger<UserController>.Instance, _mockUserService.Object);
- }
-
- public void Dispose()
- {
- _controller.Dispose();
- }
-
- [Fact]
- public async Task GetList_Success()
- {
- var array = MockUser.UserInfoList.ToArray();
- _mockUserService.Setup(s => s.ListUsers()).ReturnsAsync(array);
- var action = await _controller.List();
- action.Result.Should().BeAssignableTo<OkObjectResult>()
- .Which.Value.Should().BeEquivalentTo(array);
- }
-
- [Fact]
- public async Task Get_Success()
- {
- const string username = "aaa";
- _mockUserService.Setup(s => s.GetUser(username)).ReturnsAsync(MockUser.User.Info);
- var action = await _controller.Get(username);
- action.Result.Should().BeAssignableTo<OkObjectResult>()
- .Which.Value.Should().BeEquivalentTo(MockUser.User.Info);
- }
-
- [Fact]
- public async Task Get_NotFound()
- {
- const string username = "aaa";
- _mockUserService.Setup(s => s.GetUser(username)).Returns(Task.FromResult<UserInfo>(null));
- var action = await _controller.Get(username);
- action.Result.Should().BeAssignableTo<NotFoundObjectResult>()
- .Which.Value.Should().BeAssignableTo<CommonResponse>()
- .Which.Code.Should().Be(Get.NotExist);
- }
-
- [Theory]
- [InlineData(PutResult.Create, true)]
- [InlineData(PutResult.Modify, false)]
- public async Task Put_Success(PutResult result, bool create)
- {
- const string username = "aaa";
- const string password = "ppp";
- const bool administrator = true;
- _mockUserService.Setup(s => s.PutUser(username, password, administrator)).ReturnsAsync(result);
- var action = await _controller.Put(new UserPutRequest
- {
- Password = password,
- Administrator = administrator
- }, username);
- var response = action.Result.Should().BeAssignableTo<ObjectResult>()
- .Which.Value.Should().BeAssignableTo<CommonPutResponse>()
- .Which;
- response.Code.Should().Be(0);
- response.Data.Create.Should().Be(create);
- }
-
- [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(UsernameConfictException), 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);
- }
- }
-}
diff --git a/Timeline.Tests/Controllers/UserDetailControllerTest.cs b/Timeline.Tests/Controllers/UserDetailControllerTest.cs deleted file mode 100644 index ffd88790..00000000 --- a/Timeline.Tests/Controllers/UserDetailControllerTest.cs +++ /dev/null @@ -1,98 +0,0 @@ -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();
- }
- }
-}
|