aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-11-12 20:09:41 +0800
committer杨宇千 <crupest@outlook.com>2019-11-12 20:09:41 +0800
commit299481eecc8c1b7bc40770d58c85ff1fddeddb96 (patch)
tree9ed402bdfdf633a5d49116e7ce03abb36454856a
parentcc59e67f948d206a8bc466ed116d1bb870d3fb7b (diff)
downloadtimeline-299481eecc8c1b7bc40770d58c85ff1fddeddb96.tar.gz
timeline-299481eecc8c1b7bc40770d58c85ff1fddeddb96.tar.bz2
timeline-299481eecc8c1b7bc40770d58c85ff1fddeddb96.zip
Complete personal timeline controller unit tests.
-rw-r--r--Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs153
-rw-r--r--Timeline/Controllers/PersonalTimelineController.cs25
-rw-r--r--Timeline/Resources/Controllers/TimelineController.Designer.cs28
-rw-r--r--Timeline/Resources/Controllers/TimelineController.resx12
-rw-r--r--Timeline/Resources/Controllers/TimelineController.zh.resx12
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<CommonResponse>()
- .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<ObjectResult>().Which;
result.StatusCode.Should().Be(StatusCodes.Status403Forbidden);
result.Value.Should().BeAssignableTo<CommonResponse>()
- .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<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 = 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<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.MemberAddNotExist);
+ _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/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
@@ -97,20 +97,38 @@ namespace Timeline.Resources.Controllers {
}
/// <summary>
+ /// Looks up a localized string similar to You have no permission to read posts of the timeline..
+ /// </summary>
+ internal static string MessagePostListGetForbid {
+ get {
+ return ResourceManager.GetString("MessagePostListGetForbid", resourceCulture);
+ }
+ }
+
+ /// <summary>
/// Looks up a localized string similar to You have no permission to create posts in the timeline..
/// </summary>
- internal static string MessagePostsCreateForbid {
+ internal static string MessagePostOperationCreateForbid {
get {
- return ResourceManager.GetString("MessagePostsCreateForbid", resourceCulture);
+ return ResourceManager.GetString("MessagePostOperationCreateForbid", resourceCulture);
}
}
/// <summary>
- /// 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..
+ /// </summary>
+ internal static string MessagePostOperationDeleteForbid {
+ get {
+ return ResourceManager.GetString("MessagePostOperationDeleteForbid", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to The post to delete does not exist..
/// </summary>
- 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 @@
<data name="MessageMemberUserNotExist" xml:space="preserve">
<value>The {0}-st user to do operation {1} on does not exist.</value>
</data>
- <data name="MessagePostsCreateForbid" xml:space="preserve">
+ <data name="MessagePostListGetForbid" xml:space="preserve">
+ <value>You have no permission to read posts of the timeline.</value>
+ </data>
+ <data name="MessagePostOperationCreateForbid" xml:space="preserve">
<value>You have no permission to create posts in the timeline.</value>
</data>
- <data name="MessagePostsGetForbid" xml:space="preserve">
- <value>You have no permission to read posts of the timeline.</value>
+ <data name="MessagePostOperationDeleteForbid" xml:space="preserve">
+ <value>You have no permission to delete posts in the timeline.</value>
+ </data>
+ <data name="MessagePostOperationDeleteNotExist" xml:space="preserve">
+ <value>The post to delete does not exist.</value>
</data>
</root> \ 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 @@
<data name="MessageMemberUserNotExist" xml:space="preserve">
<value>第{0}个做{1}操作的用户不存在。</value>
</data>
- <data name="MessagePostsCreateForbid" xml:space="preserve">
+ <data name="MessagePostListGetForbid" xml:space="preserve">
+ <value>你没有权限读取这个时间线消息。</value>
+ </data>
+ <data name="MessagePostOperationCreateForbid" xml:space="preserve">
<value>你没有权限在这个时间线中创建消息。</value>
</data>
- <data name="MessagePostsGetForbid" xml:space="preserve">
- <value>你没有权限读取这个时间线消息。</value>
+ <data name="MessagePostOperationDeleteForbid" xml:space="preserve">
+ <value>你没有权限在这个时间线中删除消息。</value>
+ </data>
+ <data name="MessagePostOperationDeleteNotExist" xml:space="preserve">
+ <value>要删除的消息不存在。</value>
</data>
</root> \ No newline at end of file