diff options
author | 杨宇千 <crupest@outlook.com> | 2019-11-07 22:57:04 +0800 |
---|---|---|
committer | 杨宇千 <crupest@outlook.com> | 2019-11-07 22:57:04 +0800 |
commit | e1757f98c0f5a337f1b5c44ef1638210a59f2811 (patch) | |
tree | ef1873d7e6b86d9cbeded9a5c84e63e3cd6b3baa /Timeline.Tests/Controllers | |
parent | 2f36e9a1c8d6db2a808f874134c9cb7d57c3ef16 (diff) | |
download | timeline-e1757f98c0f5a337f1b5c44ef1638210a59f2811.tar.gz timeline-e1757f98c0f5a337f1b5c44ef1638210a59f2811.tar.bz2 timeline-e1757f98c0f5a337f1b5c44ef1638210a59f2811.zip |
Add Get method tests for PersonalTimelineController.
Diffstat (limited to 'Timeline.Tests/Controllers')
-rw-r--r-- | Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs | 88 |
1 files changed, 76 insertions, 12 deletions
diff --git a/Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs b/Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs index d5c470ee..6857a27f 100644 --- a/Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs +++ b/Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs @@ -1,20 +1,22 @@ -using System;
+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.Linq;
+using System.Reflection;
using System.Threading.Tasks;
using Timeline.Controllers;
-using Timeline.Services;
-using Moq;
-using Microsoft.Extensions.Logging.Abstractions;
-using Xunit;
-using FluentAssertions;
-using Microsoft.AspNetCore.Mvc;
using Timeline.Filters;
-using Timeline.Tests.Helpers;
-using Timeline.Models.Validation;
-using System.Reflection;
-using Microsoft.AspNetCore.Authorization;
+using Timeline.Models;
using Timeline.Models.Http;
+using Timeline.Models.Validation;
+using Timeline.Services;
+using Timeline.Tests.Helpers;
+using Timeline.Tests.Helpers.Authentication;
+using Xunit;
namespace Timeline.Tests.Controllers
{
@@ -107,5 +109,67 @@ namespace Timeline.Tests.Controllers 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 PostsGet_Forbid()
+ {
+ const string username = "username";
+ SetUser(false);
+ _service.Setup(s => s.HasReadPermission(username, authUsername)).ReturnsAsync(false);
+ (await _controller.PostsGet(username)).Result
+ .Should().BeAssignableTo<ObjectResult>()
+ .Which.Value.Should().BeAssignableTo<CommonResponse>()
+ .Which.Code.Should().Be(ErrorCodes.Http.Timeline.PostsGetForbid);
+ _service.VerifyAll();
+ }
+
+ [Fact]
+ public async Task PostsGet_Admin_Success()
+ {
+ const string username = "username";
+ SetUser(true);
+ _service.Setup(s => s.GetPosts(username)).ReturnsAsync(new List<TimelinePostInfo>());
+ (await _controller.PostsGet(username)).Value
+ .Should().BeAssignableTo<IList<TimelinePostInfo>>()
+ .Which.Should().NotBeNull().And.BeEmpty();
+ _service.VerifyAll();
+ }
+
+ [Fact]
+ public async Task PostsGet_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
+ .Should().BeAssignableTo<IList<TimelinePostInfo>>()
+ .Which.Should().NotBeNull().And.BeEmpty();
+ _service.VerifyAll();
+ }
+
+ //TODO! Write all the other tests.
}
}
|