From ea8d82143c07afb38767d6afb4be0031452495fb Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Sat, 9 Nov 2019 23:44:20 +0800 Subject: Add PersonalTimelineController PostOperationCreate unit tests. --- .../Controllers/PersonalTimelineControllerTest.cs | 89 +++++++++++++++++++--- 1 file changed, 78 insertions(+), 11 deletions(-) (limited to 'Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs') 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() .And.BeDecoratedWith(); AssertUsernameParameter(m); } { - var m = type.GetMethod(nameof(PersonalTimelineController.TimelinePost)); + var m = type.GetMethod(nameof(PersonalTimelineController.PostOperationCreate)); m.Should().BeDecoratedWith() .And.BeDecoratedWith() .And.BeDecoratedWith(); @@ -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() .And.BeDecoratedWith() .And.BeDecoratedWith(); @@ -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() - .Which.Value.Should().BeAssignableTo() - .Which.Code.Should().Be(ErrorCodes.Http.Timeline.PostsGetForbid); + .Which; + result.StatusCode.Should().Be(StatusCodes.Status403Forbidden); + result.Value.Should().BeAssignableTo() + .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()); - (await _controller.PostsGet(username)).Value + (await _controller.PostListGet(username)).Value .Should().BeAssignableTo>() .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()); - (await _controller.PostsGet(username)).Value + (await _controller.PostListGet(username)).Value .Should().BeAssignableTo>() .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().Which; + result.StatusCode.Should().Be(StatusCodes.Status403Forbidden); + result.Value.Should().BeAssignableTo() + .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() + .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() + .And.BeEquivalentTo(response); + _service.VerifyAll(); + } + //TODO! Write all the other tests. } } -- cgit v1.2.3