Skip to content

Commit

Permalink
feat: Add CancellationToken into all Controller methods calling Conte…
Browse files Browse the repository at this point in the history
…ntful

Merge pull request #291 from DFE-Digital/feat/add-cancellation-tokens
  • Loading branch information
killij authored Oct 3, 2023
2 parents 7b5e56c + 8fd23e4 commit b1f2b70
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Childrens_Social_Care_CPD_Tests.Controllers;
Expand All @@ -17,7 +18,7 @@ public class ContentControllerServerTests
{
private CpdTestServerApplication _application;
private HttpClient _httpClient;
private static string ContentUrl = "/";
private static readonly string _contentUrl = "/";

[SetUp]
public void SetUp()
Expand All @@ -36,10 +37,10 @@ public async Task Content_Will_Contain_Warning_If_Data_Is_Self_Referential()
var content = new Content();
content.Items = new List<IContent> { content };
var contentCollection = new ContentfulCollection<Content>() { Items = new List<Content>() { content } };
_application.CpdContentfulClient.GetEntries(Arg.Any<QueryBuilder<Content>>(), default).Returns(contentCollection);
_application.CpdContentfulClient.GetEntries(Arg.Any<QueryBuilder<Content>>(), Arg.Any<CancellationToken>()).Returns(contentCollection);

// act
var response = await _httpClient.GetAsync(ContentUrl);
var response = await _httpClient.GetAsync(_contentUrl);
var responseContent = await response.Content.ReadAsStringAsync();

// assert
Expand All @@ -51,13 +52,15 @@ public async Task Content_Will_Contain_Warning_If_Data_Is_Self_Referential()
public async Task Content_Will_Contain_Warning_If_Data_Has_An_Unknown_Content_Type()
{
// arrange
var content = new Content();
content.Items = new List<IContent> { new TestingContentItem() };
var content = new Content
{
Items = new List<IContent> { new TestingContentItem() }
};
var contentCollection = new ContentfulCollection<Content>() { Items = new List<Content>() { content } };
_application.CpdContentfulClient.GetEntries(Arg.Any<QueryBuilder<Content>>(), default).Returns(contentCollection);
_application.CpdContentfulClient.GetEntries(Arg.Any<QueryBuilder<Content>>(), Arg.Any<CancellationToken>()).Returns(contentCollection);

// act
var response = await _httpClient.GetAsync(ContentUrl);
var response = await _httpClient.GetAsync(_contentUrl);
var responseContent = await response.Content.ReadAsStringAsync();

// assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using NSubstitute;
using NUnit.Framework;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Childrens_Social_Care_CPD_Tests.Controllers;
Expand All @@ -22,6 +23,7 @@ public class ContentControllerTests
private HttpContext _httpContext;
private HttpRequest _httpRequest;
private ICpdContentfulClient _contentfulClient;
private CancellationTokenSource _cancellationTokenSource;

private void SetContent(Content content)
{
Expand All @@ -32,8 +34,10 @@ private void SetContent(Content content)
: contentCollection.Items = new List<Content> { content };

_contentfulClient
.GetEntries(Arg.Any<QueryBuilder<Content>>(), default)
.GetEntries(Arg.Any<QueryBuilder<Content>>(), Arg.Any<CancellationToken>())
.Returns(contentCollection);

_cancellationTokenSource = new CancellationTokenSource();
}

[SetUp]
Expand All @@ -50,9 +54,11 @@ public void SetUp()

_contentfulClient = Substitute.For<ICpdContentfulClient>();

_contentController = new ContentController(_contentfulClient);
_contentController.ControllerContext = controllerContext;
_contentController.TempData = Substitute.For<ITempDataDictionary>();
_contentController = new ContentController(_contentfulClient)
{
ControllerContext = controllerContext,
TempData = Substitute.For<ITempDataDictionary>()
};
}

[Test]
Expand All @@ -62,7 +68,7 @@ public async Task Index_Returns_404_When_No_Content_Found()
SetContent(null);

// act
var actual = await _contentController.Index("home");
var actual = await _contentController.Index(_cancellationTokenSource.Token, "home");

// assert
actual.Should().BeOfType<NotFoundResult>();
Expand All @@ -75,7 +81,7 @@ public async Task Index_Returns_View()
SetContent(new Content());

// act
var actual = await _contentController.Index("home");
var actual = await _contentController.Index(_cancellationTokenSource.Token, "home");

// assert
actual.Should().BeOfType<ViewResult>();
Expand All @@ -94,7 +100,7 @@ public async Task Index_Sets_The_ViewState_ContextModel()
SetContent(rootContent);

// act
await _contentController.Index("home");
await _contentController.Index(_cancellationTokenSource.Token, "home");
var actual = _contentController.ViewData["ContextModel"] as ContextModel;

// assert
Expand All @@ -112,21 +118,21 @@ public async Task Index_Sets_The_ContextModel_Preferences_Set_Value_Correctly(bo
SetContent(new Content());

// act
await _contentController.Index("home", preferenceSet);
await _contentController.Index(_cancellationTokenSource.Token, "home", preferenceSet);
var actual = _contentController.ViewData["ContextModel"] as ContextModel;

// assert
actual.Should().NotBeNull();
actual.PreferenceSet.Should().Be(preferenceSet);
}

public static object[] SideMenuContent =
private static readonly object[] _sideMenuContent =
{
new object[] { new SideMenu() },
new object[] { null },
};

[TestCaseSource(nameof(SideMenuContent))]
[TestCaseSource(nameof(_sideMenuContent))]
public async Task Index_Sets_The_ContextModel_UseContainers_From_SideMenu_Value_Correctly(SideMenu sideMenu)
{
// arrange
Expand All @@ -138,7 +144,7 @@ public async Task Index_Sets_The_ContextModel_UseContainers_From_SideMenu_Value_
SetContent(rootContent);

// act
await _contentController.Index("home");
await _contentController.Index(_cancellationTokenSource.Token, "home");
var actual = _contentController.ViewData["ContextModel"] as ContextModel;

// assert
Expand All @@ -152,10 +158,10 @@ public async Task Index_Trims_Trailing_Slashes()
// arrange
SetContent(new Content());
var query = "";
await _contentfulClient.GetEntries(Arg.Do<QueryBuilder<Content>>(value => query = value.Build()));
await _contentfulClient.GetEntries(Arg.Do<QueryBuilder<Content>>(value => query = value.Build()), Arg.Any<CancellationToken>());

// act
var actual = await _contentController.Index("home/");
var actual = await _contentController.Index(_cancellationTokenSource.Token, "home/");

// assert
query.Should().Contain("fields.id=home&");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System;
using Childrens_Social_Care_CPD;
using Childrens_Social_Care_CPD.Configuration;
using System.Threading;

namespace Childrens_Social_Care_CPD_Tests.Controllers;

Expand All @@ -26,6 +27,7 @@ public partial class CookieControllerTests
private HttpContext _httpContext;
private HttpRequest _httpRequest;
private ICpdContentfulClient _contentfulClient;
private CancellationTokenSource _cancellationTokenSource;

private void SetContent(Content content)
{
Expand All @@ -36,8 +38,10 @@ private void SetContent(Content content)
: contentCollection.Items = new List<Content> { content };

_contentfulClient
.GetEntries(Arg.Any<QueryBuilder<Content>>(), default)
.GetEntries(Arg.Any<QueryBuilder<Content>>(), Arg.Any<CancellationToken>())
.Returns(contentCollection);

_cancellationTokenSource = new CancellationTokenSource();
}

[SetUp]
Expand All @@ -59,9 +63,11 @@ public void SetUp()

_contentfulClient = Substitute.For<ICpdContentfulClient>();

_cookieController = new CookieController(_contentfulClient, new CookieHelper(new ApplicationConfiguration()));
_cookieController.ControllerContext = controllerContext;
_cookieController.TempData = Substitute.For<ITempDataDictionary>();
_cookieController = new CookieController(_contentfulClient, new CookieHelper(new ApplicationConfiguration()))
{
ControllerContext = controllerContext,
TempData = Substitute.For<ITempDataDictionary>()
};
}

[Test]
Expand All @@ -71,7 +77,7 @@ public async Task Cookies_Returns_404_When_No_Content_Found()
SetContent(null);

// act
var actual = await _cookieController.Cookies();
var actual = await _cookieController.Cookies(_cancellationTokenSource.Token);

// assert
actual.Should().BeOfType<NotFoundResult>();
Expand All @@ -90,7 +96,7 @@ public async Task Cookies_Sets_The_ViewState_ContextModel()
SetContent(rootContent);

// act
await _cookieController.Cookies();
await _cookieController.Cookies(_cancellationTokenSource.Token);
var actual = _cookieController.ViewData["ContextModel"] as ContextModel;

// assert
Expand All @@ -108,21 +114,21 @@ public async Task Cookies_Sets_The_ContextModel_Preferences_Set_Value_Correctly(
SetContent(new Content());

// act
await _cookieController.Cookies(preferenceSet: preferenceSet);
await _cookieController.Cookies(_cancellationTokenSource.Token, preferenceSet: preferenceSet);
var actual = _cookieController.ViewData["ContextModel"] as ContextModel;

// assert
actual.Should().NotBeNull();
actual.PreferenceSet.Should().Be(preferenceSet);
}

public static object[] SideMenuContent =
private static readonly object[] _sideMenuContent =
{
new object[] { new SideMenu() },
new object[] { null },
};

[TestCaseSource(nameof(SideMenuContent))]
[TestCaseSource(nameof(_sideMenuContent))]
public async Task Cookies_Sets_The_ContextModel_UseContainers_Ignoring_The_SideMenu_Value(SideMenu sideMenu)
{
// arrange
Expand All @@ -133,7 +139,7 @@ public async Task Cookies_Sets_The_ContextModel_UseContainers_Ignoring_The_SideM
SetContent(rootContent);

// act
await _cookieController.Cookies();
await _cookieController.Cookies(_cancellationTokenSource.Token);
var actual = _cookieController.ViewData["ContextModel"] as ContextModel;

// assert
Expand All @@ -148,7 +154,7 @@ public async Task Cookies_Action_Should_Not_Show_Consent_Panel()
SetContent(new Content());

// act
await _cookieController.Cookies();
await _cookieController.Cookies(_cancellationTokenSource.Token);
var actual = _cookieController.ViewData["ContextModel"] as ContextModel;

// assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Childrens_Social_Care_CPD_Tests.Controllers;
Expand All @@ -37,7 +38,7 @@ public async Task Non_Existant_Page_Will_Return_404()
{
// arrange
var contentCollection = new ContentfulCollection<Content>() { Items = new List<Content>() };
_application.CpdContentfulClient.GetEntries(Arg.Any<QueryBuilder<Content>>(), default).Returns(contentCollection);
_application.CpdContentfulClient.GetEntries(Arg.Any<QueryBuilder<Content>>(), Arg.Any<CancellationToken>()).Returns(contentCollection);
var url = "/does_not_exist";

// act
Expand All @@ -52,7 +53,7 @@ public async Task Non_Existant_Page_Will_Return_404()
public async Task Exception_Will_Return_500()
{
// arrange
_application.CpdContentfulClient.GetEntries(Arg.Any<QueryBuilder<Content>>(), default).Throws(new Exception("Test exception"));
_application.CpdContentfulClient.GetEntries(Arg.Any<QueryBuilder<Content>>(), Arg.Any<CancellationToken>()).Throws(new Exception("Test exception"));
var url = "/something";

// act
Expand All @@ -72,7 +73,7 @@ public async Task Exception_Will_Be_Logged()
var exception = new TestException();
var logger = Substitute.For<ILogger<ErrorController>>();
_application.LoggerFactory.CreateLogger<ErrorController>().Returns(logger);
_application.CpdContentfulClient.GetEntries(Arg.Any<QueryBuilder<Content>>(), default).Throws(exception);
_application.CpdContentfulClient.GetEntries(Arg.Any<QueryBuilder<Content>>(), Arg.Any<CancellationToken>()).Throws(exception);
var url = "/something";

// act
Expand Down
Loading

0 comments on commit b1f2b70

Please sign in to comment.