aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs88
-rw-r--r--Timeline.Tests/Helpers/Authentication/PrincipalHelper.cs23
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;
+ }
+ }
+}