From e1757f98c0f5a337f1b5c44ef1638210a59f2811 Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Thu, 7 Nov 2019 22:57:04 +0800 Subject: Add Get method tests for PersonalTimelineController. --- .../Controllers/PersonalTimelineControllerTest.cs | 88 +++++++++++++++++++--- 1 file changed, 76 insertions(+), 12 deletions(-) (limited to 'Timeline.Tests/Controllers/PersonalTimelineControllerTest.cs') 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(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() + .Which.Value.Should().BeAssignableTo() + .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()); + (await _controller.PostsGet(username)).Value + .Should().BeAssignableTo>() + .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()); + (await _controller.PostsGet(username)).Value + .Should().BeAssignableTo>() + .Which.Should().NotBeNull().And.BeEmpty(); + _service.VerifyAll(); + } + + //TODO! Write all the other tests. } } -- cgit v1.2.3