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 | 976d6158e67e79e9d6d7e140f5749aa6630d0fb6 (patch) | |
tree | ef1873d7e6b86d9cbeded9a5c84e63e3cd6b3baa | |
parent | 5f9f9a9e40306f83bf360c3d27e4e33e78565fce (diff) | |
download | timeline-976d6158e67e79e9d6d7e140f5749aa6630d0fb6.tar.gz timeline-976d6158e67e79e9d6d7e140f5749aa6630d0fb6.tar.bz2 timeline-976d6158e67e79e9d6d7e140f5749aa6630d0fb6.zip |
Add Get method tests for PersonalTimelineController.
-rw-r--r-- | Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs | 88 | ||||
-rw-r--r-- | Timeline.Tests/Helpers/Authentication/PrincipalHelper.cs | 23 |
2 files changed, 99 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.
}
}
diff --git a/Timeline.Tests/Helpers/Authentication/PrincipalHelper.cs b/Timeline.Tests/Helpers/Authentication/PrincipalHelper.cs new file mode 100644 index 00000000..214472a2 --- /dev/null +++ b/Timeline.Tests/Helpers/Authentication/PrincipalHelper.cs @@ -0,0 +1,23 @@ +using System.Linq;
+using System.Security.Claims;
+using Timeline.Models;
+
+namespace Timeline.Tests.Helpers.Authentication
+{
+ public static class PrincipalHelper
+ {
+ internal const string AuthScheme = "TESTAUTH";
+
+ internal static ClaimsPrincipal Create(string username, bool administrator)
+ {
+ var identity = new ClaimsIdentity(AuthScheme);
+ identity.AddClaim(new Claim(identity.NameClaimType, username, ClaimValueTypes.String));
+ identity.AddClaims(UserRoleConvert.ToArray(administrator).Select(role => new Claim(identity.RoleClaimType, role, ClaimValueTypes.String)));
+
+ var principal = new ClaimsPrincipal();
+ principal.AddIdentity(identity);
+
+ return principal;
+ }
+ }
+}
|