generated from DFE-Digital/govuk-dotnet-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #286 from DFE-Digital/feat/resources
- Loading branch information
Showing
57 changed files
with
1,331 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 184 additions & 0 deletions
184
Childrens-Social-Care-CPD-Tests/Controllers/ResourcesControllerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
using Castle.Core.Logging; | ||
using Childrens_Social_Care_CPD.Contentful; | ||
using Childrens_Social_Care_CPD.Contentful.Models; | ||
using Childrens_Social_Care_CPD.Controllers; | ||
using Childrens_Social_Care_CPD.Models; | ||
using Contentful.Core.Models; | ||
using Contentful.Core.Search; | ||
using FluentAssertions; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.ViewFeatures; | ||
using Microsoft.Extensions.Logging; | ||
using NSubstitute; | ||
using NSubstitute.ExceptionExtensions; | ||
using NUnit.Framework; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading.Tasks; | ||
|
||
namespace Childrens_Social_Care_CPD_Tests.Controllers; | ||
|
||
public class ResourcesControllerTests | ||
{ | ||
private ResourcesController _resourcesController; | ||
private IRequestCookieCollection _cookies; | ||
private HttpContext _httpContext; | ||
private HttpRequest _httpRequest; | ||
private ICpdContentfulClient _contentfulClient; | ||
private ILogger<ResourcesController> _logger; | ||
|
||
private void SetContent(Content content, ContentfulCollection<Resource> resourceCollection) | ||
{ | ||
resourceCollection ??= new (); | ||
|
||
_contentfulClient | ||
.GetEntries(Arg.Any<QueryBuilder<Resource>>(), default) | ||
.Returns(resourceCollection); | ||
|
||
var contentCollection = new ContentfulCollection<Content>(); | ||
|
||
contentCollection.Items = content == null | ||
? new List<Content>() | ||
: contentCollection.Items = new List<Content> { content }; | ||
|
||
_contentfulClient | ||
.GetEntries(Arg.Any<QueryBuilder<Content>>(), default) | ||
.Returns(contentCollection); | ||
} | ||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
_logger = Substitute.For<ILogger<ResourcesController>>(); | ||
_cookies = Substitute.For<IRequestCookieCollection>(); | ||
_httpContext = Substitute.For<HttpContext>(); | ||
_httpRequest = Substitute.For<HttpRequest>(); | ||
var controllerContext = Substitute.For<ControllerContext>(); | ||
|
||
_httpRequest.Cookies.Returns(_cookies); | ||
_httpContext.Request.Returns(_httpRequest); | ||
controllerContext.HttpContext = _httpContext; | ||
|
||
_contentfulClient = Substitute.For<ICpdContentfulClient>(); | ||
|
||
_resourcesController = new ResourcesController(_logger, _contentfulClient) | ||
{ | ||
ControllerContext = controllerContext, | ||
TempData = Substitute.For<ITempDataDictionary>() | ||
}; | ||
} | ||
|
||
[Test] | ||
public async Task Search_With_Empty_Query_Returns_View() | ||
{ | ||
// arrange | ||
SetContent(null, null); | ||
|
||
// act | ||
var actual = await _resourcesController.Search(query: null) as ViewResult; | ||
|
||
// assert | ||
actual.Should().BeOfType<ViewResult>(); | ||
actual.Model.Should().BeOfType<ResourcesListViewModel>(); | ||
} | ||
|
||
[Test] | ||
public async Task Search_Page_Resource_Is_Passed_To_View() | ||
{ | ||
// arrange | ||
var content = new Content(); | ||
SetContent(content, null); | ||
var query = new ResourcesQuery | ||
{ | ||
Page = 1, | ||
Tags = Array.Empty<int>() | ||
}; | ||
|
||
// act | ||
var actual = (await _resourcesController.Search(query: null) as ViewResult)?.Model as ResourcesListViewModel; | ||
|
||
// assert | ||
actual.Content.Should().Be(content); | ||
} | ||
|
||
[Test] | ||
public async Task Search_Sets_The_ViewState_ContextModel() | ||
{ | ||
// arrange | ||
SetContent(null, null); | ||
|
||
// act | ||
await _resourcesController.Search(null); | ||
var actual = _resourcesController.ViewData["ContextModel"] as ContextModel; | ||
|
||
// assert | ||
actual.Should().NotBeNull(); | ||
actual.Id.Should().Be(string.Empty); | ||
actual.Title.Should().Be("Resources"); | ||
actual.Category.Should().Be("Resources"); | ||
} | ||
|
||
[Test] | ||
public async Task Search_Selected_Tags_Are_Passed_Into_View() | ||
{ | ||
// arrange | ||
SetContent(null, null); | ||
var query = new ResourcesQuery | ||
{ | ||
Page = 1, | ||
Tags = new int[] { 1, 2 } | ||
}; | ||
|
||
// act | ||
var actual = (await _resourcesController.Search(query) as ViewResult)?.Model as ResourcesListViewModel; | ||
|
||
// assert | ||
actual.SelectedTags.Should().Equal(query.Tags); | ||
} | ||
|
||
[Test] | ||
public async Task Search_Page_Set_To_Be_In_Bounds() | ||
{ | ||
// arrange | ||
SetContent(null, new ContentfulCollection<Resource> { | ||
Items = new List<Resource> { | ||
new Resource(), | ||
new Resource(), | ||
new Resource(), | ||
}, | ||
Total = 3 | ||
}); | ||
var query = new ResourcesQuery | ||
{ | ||
Page = 2, | ||
Tags = new int[] { 1, 2 } | ||
}; | ||
|
||
// act | ||
var actual = (await _resourcesController.Search(query) as ViewResult)?.Model as ResourcesListViewModel; | ||
|
||
// assert | ||
actual.CurrentPage.Should().Be(1); | ||
} | ||
|
||
[Test] | ||
public async Task Search_Invalid_Tags_Logs_Warning() | ||
{ | ||
// arrange | ||
SetContent(null, null); | ||
var tags = new int[] { -1 }; | ||
var query = new ResourcesQuery | ||
{ | ||
Page = 2, | ||
Tags = tags | ||
}; | ||
|
||
// act | ||
await _resourcesController.Search(query); | ||
|
||
//assert | ||
_logger.ReceivedWithAnyArgs(1).LogWarning(default, args: default); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.