diff options
-rw-r--r-- | Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs | 89 | ||||
-rw-r--r-- | Timeline/Controllers/PersonalTimelineController.cs | 6 | ||||
-rw-r--r-- | Timeline/Models/Http/Timeline.cs | 2 |
3 files changed, 82 insertions, 15 deletions
diff --git a/Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs b/Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs index 6857a27f..27b37f94 100644 --- a/Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs +++ b/Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs @@ -65,14 +65,14 @@ namespace Timeline.Tests.Controllers }
{
- var m = type.GetMethod(nameof(PersonalTimelineController.PostsGet));
+ var m = type.GetMethod(nameof(PersonalTimelineController.PostListGet));
m.Should().BeDecoratedWith<CatchTimelineNotExistExceptionAttribute>()
.And.BeDecoratedWith<HttpGetAttribute>();
AssertUsernameParameter(m);
}
{
- var m = type.GetMethod(nameof(PersonalTimelineController.TimelinePost));
+ var m = type.GetMethod(nameof(PersonalTimelineController.PostOperationCreate));
m.Should().BeDecoratedWith<CatchTimelineNotExistExceptionAttribute>()
.And.BeDecoratedWith<AuthorizeAttribute>()
.And.BeDecoratedWith<HttpPostAttribute>();
@@ -81,7 +81,7 @@ namespace Timeline.Tests.Controllers }
{
- var m = type.GetMethod(nameof(PersonalTimelineController.TimelinePostDelete));
+ var m = type.GetMethod(nameof(PersonalTimelineController.PostOperationDelete));
m.Should().BeDecoratedWith<CatchTimelineNotExistExceptionAttribute>()
.And.BeDecoratedWith<AuthorizeAttribute>()
.And.BeDecoratedWith<HttpPostAttribute>();
@@ -133,43 +133,110 @@ namespace Timeline.Tests.Controllers }
[Fact]
- public async Task PostsGet_Forbid()
+ public async Task PostListGet_Forbid()
{
const string username = "username";
SetUser(false);
_service.Setup(s => s.HasReadPermission(username, authUsername)).ReturnsAsync(false);
- (await _controller.PostsGet(username)).Result
+ var result = (await _controller.PostListGet(username)).Result
.Should().BeAssignableTo<ObjectResult>()
- .Which.Value.Should().BeAssignableTo<CommonResponse>()
- .Which.Code.Should().Be(ErrorCodes.Http.Timeline.PostsGetForbid);
+ .Which;
+ result.StatusCode.Should().Be(StatusCodes.Status403Forbidden);
+ result.Value.Should().BeAssignableTo<CommonResponse>()
+ .Which.Code.Should().Be(ErrorCodes.Http.Timeline.PostsGetForbid);
_service.VerifyAll();
}
[Fact]
- public async Task PostsGet_Admin_Success()
+ public async Task PostListGet_Admin_Success()
{
const string username = "username";
SetUser(true);
_service.Setup(s => s.GetPosts(username)).ReturnsAsync(new List<TimelinePostInfo>());
- (await _controller.PostsGet(username)).Value
+ (await _controller.PostListGet(username)).Value
.Should().BeAssignableTo<IList<TimelinePostInfo>>()
.Which.Should().NotBeNull().And.BeEmpty();
_service.VerifyAll();
}
[Fact]
- public async Task PostsGet_User_Success()
+ 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.PostsGet(username)).Value
+ (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.PostsCreateForbid);
+ _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();
+ }
+
//TODO! Write all the other tests.
}
}
diff --git a/Timeline/Controllers/PersonalTimelineController.cs b/Timeline/Controllers/PersonalTimelineController.cs index f006ad47..f41e354b 100644 --- a/Timeline/Controllers/PersonalTimelineController.cs +++ b/Timeline/Controllers/PersonalTimelineController.cs @@ -74,7 +74,7 @@ namespace Timeline.Controllers [HttpGet("users/{username}/timeline/posts")]
[CatchTimelineNotExistException]
- public async Task<ActionResult<IList<TimelinePostInfo>>> PostsGet([FromRoute][Username] string username)
+ public async Task<ActionResult<IList<TimelinePostInfo>>> PostListGet([FromRoute][Username] string username)
{
if (!IsAdmin() && !await _service.HasReadPermission(username, GetAuthUsername()))
{
@@ -88,7 +88,7 @@ namespace Timeline.Controllers [HttpPost("user/{username}/timeline/postop/create")]
[Authorize]
[CatchTimelineNotExistException]
- public async Task<ActionResult<TimelinePostCreateResponse>> TimelinePost([FromRoute][Username] string username, [FromBody] TimelinePostCreateRequest body)
+ public async Task<ActionResult<TimelinePostCreateResponse>> PostOperationCreate([FromRoute][Username] string username, [FromBody] TimelinePostCreateRequest body)
{
if (!IsAdmin() && !await _service.IsMemberOf(username, GetAuthUsername()!))
{
@@ -103,7 +103,7 @@ namespace Timeline.Controllers [HttpPost("user/{username}/timeline/postop/delete")]
[Authorize]
[CatchTimelineNotExistException]
- public async Task<ActionResult> TimelinePostDelete([FromRoute][Username] string username, [FromBody] TimelinePostDeleteRequest body)
+ public async Task<ActionResult> PostOperationDelete([FromRoute][Username] string username, [FromBody] TimelinePostDeleteRequest body)
{
var postId = body.Id!.Value;
if (!IsAdmin() && !await _service.HasPostModifyPermission(username, postId, GetAuthUsername()!))
diff --git a/Timeline/Models/Http/Timeline.cs b/Timeline/Models/Http/Timeline.cs index f676afa0..06b88ad1 100644 --- a/Timeline/Models/Http/Timeline.cs +++ b/Timeline/Models/Http/Timeline.cs @@ -9,7 +9,7 @@ namespace Timeline.Models.Http {
public class TimelinePostCreateRequest
{
- [Required(AllowEmptyStrings = false)]
+ [Required(AllowEmptyStrings = true)]
public string Content { get; set; } = default!;
public DateTime? Time { get; set; }
|