From 299481eecc8c1b7bc40770d58c85ff1fddeddb96 Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Tue, 12 Nov 2019 20:09:41 +0800 Subject: Complete personal timeline controller unit tests. --- .../Controllers/PersonalTimelineControllerTest.cs | 153 ++++++++++++++++++++- Timeline/Controllers/PersonalTimelineController.cs | 25 +++- .../Controllers/TimelineController.Designer.cs | 28 +++- .../Resources/Controllers/TimelineController.resx | 12 +- .../Controllers/TimelineController.zh.resx | 12 +- 5 files changed, 209 insertions(+), 21 deletions(-) diff --git a/Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs b/Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs index 27b37f94..aecd10af 100644 --- a/Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs +++ b/Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs @@ -143,7 +143,7 @@ namespace Timeline.Tests.Controllers .Which; result.StatusCode.Should().Be(StatusCodes.Status403Forbidden); result.Value.Should().BeAssignableTo() - .Which.Code.Should().Be(ErrorCodes.Http.Timeline.PostsGetForbid); + .Which.Code.Should().Be(ErrorCodes.Http.Timeline.PostListGetForbid); _service.VerifyAll(); } @@ -186,7 +186,7 @@ namespace Timeline.Tests.Controllers })).Result.Should().NotBeNull().And.BeAssignableTo().Which; result.StatusCode.Should().Be(StatusCodes.Status403Forbidden); result.Value.Should().BeAssignableTo() - .Which.Code.Should().Be(ErrorCodes.Http.Timeline.PostsCreateForbid); + .Which.Code.Should().Be(ErrorCodes.Http.Timeline.PostOperationCreateForbid); _service.VerifyAll(); } @@ -237,6 +237,153 @@ namespace Timeline.Tests.Controllers _service.VerifyAll(); } - //TODO! Write all the other tests. + [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().Which; + result.StatusCode.Should().Be(StatusCodes.Status403Forbidden); + result.Value.Should().BeAssignableTo() + .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().Which; + result.StatusCode.Should().Be(StatusCodes.Status400BadRequest); + result.Value.Should().BeAssignableTo() + .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(); + _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(); + _service.VerifyAll(); + } + + [Fact] + public async Task TimelineChangeProperty_Success() + { + const string username = "username"; + var req = new TimelinePropertyChangeRequest + { + Description = "", + Visibility = Entities.TimelineVisibility.Private + }; + _service.Setup(s => s.ChangeProperty(username, req)).Returns(Task.CompletedTask); + var result = await _controller.TimelineChangeProperty(username, req); + result.Should().NotBeNull().And.BeAssignableTo(); + _service.VerifyAll(); + } + + [Fact] + public async Task TimelineChangeMember_Success() + { + const string username = "username"; + var add = new List { "aaa" }; + var remove = new List { "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(); + _service.VerifyAll(); + } + + [Fact] + public async Task TimelineChangeMember_UsernameBadFormat() + { + const string username = "username"; + var add = new List { "aaa" }; + var remove = new List { "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() + .Which.Value.Should().BeAssignableTo() + .Which.Code.Should().Be(ErrorCodes.Http.Common.InvalidModel); + _service.VerifyAll(); + } + + [Fact] + public async Task TimelineChangeMember_AddNotExist() + { + const string username = "username"; + var add = new List { "aaa" }; + var remove = new List { "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() + .Which.Value.Should().BeAssignableTo() + .Which.Code.Should().Be(ErrorCodes.Http.Timeline.MemberAddNotExist); + _service.VerifyAll(); + } + + [Fact] + public async Task TimelineChangeMember_UnknownTimelineMemberOperationUserException() + { + const string username = "username"; + var add = new List { "aaa" }; + var remove = new List { "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(); // Should rethrow. + } } } diff --git a/Timeline/Controllers/PersonalTimelineController.cs b/Timeline/Controllers/PersonalTimelineController.cs index f41e354b..f0f4e4c2 100644 --- a/Timeline/Controllers/PersonalTimelineController.cs +++ b/Timeline/Controllers/PersonalTimelineController.cs @@ -21,9 +21,11 @@ namespace Timeline { public static class Timeline // ccc = 004 { - public const int PostsGetForbid = 10040101; - public const int PostsCreateForbid = 10040102; - public const int MemberAddNotExist = 10040201; + public const int PostListGetForbid = 10040101; + public const int PostOperationCreateForbid = 10040102; + public const int PostOperationDeleteForbid = 10040103; + public const int PostOperationDeleteNotExist = 10040201; + public const int MemberAddNotExist = 10040301; } } } @@ -79,7 +81,7 @@ namespace Timeline.Controllers if (!IsAdmin() && !await _service.HasReadPermission(username, GetAuthUsername())) { return StatusCode(StatusCodes.Status403Forbidden, - new CommonResponse(ErrorCodes.Http.Timeline.PostsGetForbid, MessagePostsGetForbid)); + new CommonResponse(ErrorCodes.Http.Timeline.PostListGetForbid, MessagePostListGetForbid)); } return await _service.GetPosts(username); @@ -93,7 +95,7 @@ namespace Timeline.Controllers if (!IsAdmin() && !await _service.IsMemberOf(username, GetAuthUsername()!)) { return StatusCode(StatusCodes.Status403Forbidden, - new CommonResponse(ErrorCodes.Http.Timeline.PostsCreateForbid, MessagePostsCreateForbid)); + new CommonResponse(ErrorCodes.Http.Timeline.PostOperationCreateForbid, MessagePostOperationCreateForbid)); } var res = await _service.CreatePost(username, User.Identity.Name!, body.Content, body.Time); @@ -109,9 +111,18 @@ namespace Timeline.Controllers if (!IsAdmin() && !await _service.HasPostModifyPermission(username, postId, GetAuthUsername()!)) { return StatusCode(StatusCodes.Status403Forbidden, - new CommonResponse(ErrorCodes.Http.Timeline.PostsCreateForbid, MessagePostsCreateForbid)); + new CommonResponse(ErrorCodes.Http.Timeline.PostOperationDeleteForbid, MessagePostOperationCreateForbid)); + } + try + { + await _service.DeletePost(username, postId); + } + catch (TimelinePostNotExistException) + { + return BadRequest(new CommonResponse( + ErrorCodes.Http.Timeline.PostOperationDeleteNotExist, + MessagePostOperationDeleteNotExist)); } - await _service.DeletePost(username, postId); return Ok(); } diff --git a/Timeline/Resources/Controllers/TimelineController.Designer.cs b/Timeline/Resources/Controllers/TimelineController.Designer.cs index 5a4209c3..47c43fa2 100644 --- a/Timeline/Resources/Controllers/TimelineController.Designer.cs +++ b/Timeline/Resources/Controllers/TimelineController.Designer.cs @@ -96,21 +96,39 @@ namespace Timeline.Resources.Controllers { } } + /// + /// Looks up a localized string similar to You have no permission to read posts of the timeline.. + /// + internal static string MessagePostListGetForbid { + get { + return ResourceManager.GetString("MessagePostListGetForbid", resourceCulture); + } + } + /// /// Looks up a localized string similar to You have no permission to create posts in the timeline.. /// - internal static string MessagePostsCreateForbid { + internal static string MessagePostOperationCreateForbid { get { - return ResourceManager.GetString("MessagePostsCreateForbid", resourceCulture); + return ResourceManager.GetString("MessagePostOperationCreateForbid", resourceCulture); } } /// - /// Looks up a localized string similar to You have no permission to read posts of the timeline.. + /// Looks up a localized string similar to You have no permission to delete posts in the timeline.. + /// + internal static string MessagePostOperationDeleteForbid { + get { + return ResourceManager.GetString("MessagePostOperationDeleteForbid", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The post to delete does not exist.. /// - internal static string MessagePostsGetForbid { + internal static string MessagePostOperationDeleteNotExist { get { - return ResourceManager.GetString("MessagePostsGetForbid", resourceCulture); + return ResourceManager.GetString("MessagePostOperationDeleteNotExist", resourceCulture); } } } diff --git a/Timeline/Resources/Controllers/TimelineController.resx b/Timeline/Resources/Controllers/TimelineController.resx index 7e323164..0cf7e881 100644 --- a/Timeline/Resources/Controllers/TimelineController.resx +++ b/Timeline/Resources/Controllers/TimelineController.resx @@ -129,10 +129,16 @@ The {0}-st user to do operation {1} on does not exist. - + + You have no permission to read posts of the timeline. + + You have no permission to create posts in the timeline. - - You have no permission to read posts of the timeline. + + You have no permission to delete posts in the timeline. + + + The post to delete does not exist. \ No newline at end of file diff --git a/Timeline/Resources/Controllers/TimelineController.zh.resx b/Timeline/Resources/Controllers/TimelineController.zh.resx index cacce5fa..170ab4cd 100644 --- a/Timeline/Resources/Controllers/TimelineController.zh.resx +++ b/Timeline/Resources/Controllers/TimelineController.zh.resx @@ -123,10 +123,16 @@ 第{0}个做{1}操作的用户不存在。 - + + 你没有权限读取这个时间线消息。 + + 你没有权限在这个时间线中创建消息。 - - 你没有权限读取这个时间线消息。 + + 你没有权限在这个时间线中删除消息。 + + + 要删除的消息不存在。 \ No newline at end of file -- cgit v1.2.3