Skip to content

Commit

Permalink
Update ResourceController tests
Browse files Browse the repository at this point in the history
  • Loading branch information
killij committed Nov 9, 2023
1 parent 6785a4b commit baf8447
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
using Childrens_Social_Care_CPD.Configuration;
using Childrens_Social_Care_CPD.Contentful.Models;
using Childrens_Social_Care_CPD.Controllers;
using Childrens_Social_Care_CPD.Core.Resources;
using Childrens_Social_Care_CPD.DataAccess;
using Childrens_Social_Care_CPD.GraphQL.Queries;
using Childrens_Social_Care_CPD.Models;
using FluentAssertions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using NSubstitute;
using NSubstitute.Core;
using NUnit.Framework;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -18,6 +23,7 @@ public class ResourcesControllerTests
{
private IFeaturesConfig _featuresConfig;
private IResourcesSearchStrategy _searchStrategy;
private IResourcesRepository _resourcesRepository;
private ResourcesController _resourcesController;
private IRequestCookieCollection _cookies;
private HttpContext _httpContext;
Expand All @@ -26,6 +32,7 @@ public class ResourcesControllerTests
[SetUp]
public void SetUp()
{
_resourcesRepository = Substitute.For<IResourcesRepository>();
_cookies = Substitute.For<IRequestCookieCollection>();
_httpContext = Substitute.For<HttpContext>();
_httpRequest = Substitute.For<HttpRequest>();
Expand All @@ -38,7 +45,7 @@ public void SetUp()
_featuresConfig = Substitute.For<IFeaturesConfig>();
_featuresConfig.IsEnabled(Features.ResourcesAndLearning).Returns(true);
_searchStrategy = Substitute.For<IResourcesSearchStrategy>();
_resourcesController = new ResourcesController(_featuresConfig, _searchStrategy, null)
_resourcesController = new ResourcesController(_featuresConfig, _searchStrategy, _resourcesRepository)
{
ControllerContext = controllerContext,
TempData = Substitute.For<ITempDataDictionary>()
Expand Down Expand Up @@ -88,4 +95,151 @@ public async Task Disabling_Resources_Feature_Returns_NotFoundResult()
// assert
actual.Should().BeOfType<NotFoundResult>();
}

[Test]
public async Task Index_Returns_404_When_Resource_Feature_Disabled()
{
// arrange
_featuresConfig.IsEnabled(Features.ResourcesAndLearning).Returns(false);

// act
var actual = await _resourcesController.Index();

// assert
actual.Should().BeOfType<NotFoundResult>();
}

[Test]
public async Task Index_Searches_For_Page_Under_Resources_Area()
{
// arrange
var actual = string.Empty;
_resourcesRepository.GetByIdAsync(Arg.Do<string>(x => actual = x), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Task.FromResult(Tuple.Create((Content)null, (GetContentTags.ResponseType)null)));

// act
await _resourcesController.Index("foo");

// assert
actual.Should().Be("resources-learning/foo");
}

[Test]
public async Task Index_Returns_Not_Found_When_Content_Does_Not_Exist()
{
// arrange
_resourcesRepository.GetByIdAsync(Arg.Any<string>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Task.FromResult(Tuple.Create((Content)null, (GetContentTags.ResponseType)null)));

// act
var actual = await _resourcesController.Index("foo");

// assert
actual.Should().BeOfType<NotFoundResult>();
}

private static GetContentTags.ResponseType CreateTagsResponse(List<GetContentTags.Tag> tags)
{
return new GetContentTags.ResponseType
{
ContentCollection = new()
{
Items = new[]
{
new GetContentTags.ContentItem
{
ContentfulMetaData = new()
{
Tags = tags
}
}
}
}
};
}

[TestCase("Published")]
[TestCase("Last updated")]
public async Task Index_Passes_Default_Properties(string propertyName)
{
// arrange
var createdAt = DateTime.UtcNow.AddMinutes(-10);
var updatedAt = DateTime.UtcNow;
var content = new Content
{
Sys = new()
{
CreatedAt = createdAt,
UpdatedAt = updatedAt,
}
};
var tags = CreateTagsResponse(new());

_resourcesRepository.GetByIdAsync(Arg.Any<string>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Task.FromResult(Tuple.Create(content, tags)));

// act
var result = await _resourcesController.Index("foo") as ViewResult;
var properties = result.ViewData["Properties"] as IDictionary<string, string>;

// assert
properties.Should().NotBeNull();
properties.ContainsKey(propertyName).Should().BeTrue();
}

[Test]
public async Task Index_Passes_Correctly_Tagged_Properties()
{
// arrange
var createdAt = DateTime.UtcNow.AddMinutes(-10);
var updatedAt = DateTime.UtcNow;
var content = new Content
{
Sys = new ()
{
CreatedAt = createdAt,
UpdatedAt = updatedAt,
}
};
var tags = CreateTagsResponse(new() { new () { Id = "foo", Name = "Resource:Foo=foo" }, });

_resourcesRepository.GetByIdAsync(Arg.Any<string>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Task.FromResult(Tuple.Create(content, tags)));

// act
var result = await _resourcesController.Index("foo") as ViewResult;
var properties = result.ViewData["Properties"] as IDictionary<string, string>;

// assert
properties.Should().NotBeNull();
properties["Foo"].Should().Be("foo");
}

[Test]
public async Task Index_Ignores_Unrelated_Tags_For_Properties()
{
// arrange
var createdAt = DateTime.UtcNow.AddMinutes(-10);
var updatedAt = DateTime.UtcNow;
var content = new Content
{
Sys = new()
{
CreatedAt = createdAt,
UpdatedAt = updatedAt,
}
};
var tags = CreateTagsResponse(new() { new() { Id = "foo", Name = "Topic:Foo=foo" }, });

_resourcesRepository.GetByIdAsync(Arg.Any<string>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Task.FromResult(Tuple.Create(content, tags)));

// act
var result = await _resourcesController.Index("foo") as ViewResult;
var properties = result.ViewData["Properties"] as IDictionary<string, string>;

// assert
properties.Should().NotBeNull();
properties.ContainsKey("Foo").Should().BeFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public async Task Output_Is_A_Nav_Element()
}

[TestCase("role", "navigation")]
[TestCase("aria-label", "Resource pages")]
public async Task Nav_Has_Attributes(string name, string value)
{
// arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Task<TagHelperContent> func(bool result, HtmlEncoder encoder)

// act
await sut.ProcessAsync(_tagHelperContext, tagHelperOutput);
var actual = _tagHelperOutput.AsString();
var actual = tagHelperOutput.AsString();

// assert
actual.Should().Contain("<child content>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Task<TagHelperContent> func(bool result, HtmlEncoder encoder)

// act
await sut.ProcessAsync(_tagHelperContext, tagHelperOutput);
var actual = _tagHelperOutput.AsString();
var actual = tagHelperOutput.AsString();

// assert
actual.Should().Contain("<child content>");
Expand Down
4 changes: 1 addition & 3 deletions Childrens-Social-Care-CPD/Controllers/ResourcesController.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using Childrens_Social_Care_CPD.Configuration;
using Childrens_Social_Care_CPD.Contentful.Models;
using Childrens_Social_Care_CPD.Core.Resources;
using Childrens_Social_Care_CPD.DataAccess;
using Childrens_Social_Care_CPD.Models;
using Microsoft.AspNetCore.Mvc;
using System.Reflection;

namespace Childrens_Social_Care_CPD.Controllers;

Expand Down Expand Up @@ -81,7 +79,7 @@ public async Task<IActionResult> Index(string pageName = "home", bool preference
}))
{
{ "Published", content.Sys.CreatedAt?.ToString("dd MMMM yyyy") },
{ "Last updated", content.Sys.CreatedAt?.ToString("dd MMMM yyyy") }
{ "Last updated", content.Sys.UpdatedAt?.ToString("dd MMMM yyyy") }
};

var contextModel = new ContextModel(
Expand Down
9 changes: 1 addition & 8 deletions Childrens-Social-Care-CPD/TagHelpers/CpdResourceNav.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ public class CpdResourceNav : TagHelper
{
internal const string TagName = "cpd-resource-nav";

[HtmlAttributeName("label")]
public string Label { get; set; }

[HtmlAttributeName("navigation")]
public IList<ContentLink> Navigation { get; set; }

Expand All @@ -34,11 +31,7 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
output.TagMode = TagMode.StartTagAndEndTag;
output.AddClass("gem-c-contents-list", HtmlEncoder.Default);
output.Attributes.Add("role", "navigation");

if (Label != null)
{
output.Attributes.Add("aria-label", Label);
}
output.Attributes.Add("aria-label", "Resource pages");

var h2 = new TagBuilder("h2");
h2.AddCssClass("gem-c-contents-list__title");
Expand Down

0 comments on commit baf8447

Please sign in to comment.