From aef725716532c61945dbab885665ab2baec0f097 Mon Sep 17 00:00:00 2001 From: ThomasWhittington <46750921+ThomasWhittington@users.noreply.github.com> Date: Fri, 16 Aug 2024 12:54:32 +0100 Subject: [PATCH] Create code-pr-check.yml (#134) * Create code-pr-check.yml * Removed home controller tests * Fixed broken test --------- Co-authored-by: Tom Whittington --- .github/workflows/code-pr-check.yml | 45 ++++++++++ .../Controllers/HomeControllerTests.cs | 87 ------------------- .../Mapped/Custom/CustomAttachmentTests.cs | 4 +- 3 files changed, 48 insertions(+), 88 deletions(-) create mode 100644 .github/workflows/code-pr-check.yml delete mode 100644 tests/Dfe.ContentSupport.Web.Tests/Controllers/HomeControllerTests.cs diff --git a/.github/workflows/code-pr-check.yml b/.github/workflows/code-pr-check.yml new file mode 100644 index 0000000..655db94 --- /dev/null +++ b/.github/workflows/code-pr-check.yml @@ -0,0 +1,45 @@ +name: Code PR Check + +on: + push: + branches: ["main", "Development"] + paths: + - "src/**" + - "tests/**" + pull_request: + branches: ["main", "Development"] + paths: + - "src/**" + - "tests/**" + - ".github/workflows/code-pr-check.yml" + +concurrency: + group: "${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}" + cancel-in-progress: true + +env: + DOTNET_VERSION: '8.0.x' + +jobs: + build-test-web-app: + name: Build and run unit tests + runs-on: ubuntu-22.04 + + steps: + - uses: actions/checkout@v4 + - name: Setup .NET + uses: actions/setup-dotnet@v4 + with: + dotnet-version: 8.0.x + + - name: Restore dependencies + run: dotnet restore + + - name: Build + run: dotnet build --no-restore + + - name: Test + run: dotnet test --no-build --verbosity normal + + - name: Install dotnet coverage + run: dotnet tool install --global dotnet-coverage --version 17.9.3 diff --git a/tests/Dfe.ContentSupport.Web.Tests/Controllers/HomeControllerTests.cs b/tests/Dfe.ContentSupport.Web.Tests/Controllers/HomeControllerTests.cs deleted file mode 100644 index 2b4ae39..0000000 --- a/tests/Dfe.ContentSupport.Web.Tests/Controllers/HomeControllerTests.cs +++ /dev/null @@ -1,87 +0,0 @@ -using Dfe.ContentSupport.Web.Controllers; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; - -namespace Dfe.ContentSupport.Web.Tests.Controllers; - -public class HomeControllerTests -{ - private readonly Mock _contentServiceMock = new(); - - private HomeController GetController() - { - return new HomeController(_contentServiceMock.Object); - } - - - [Fact] - public async void Index_NoSlug_Returns_ErrorAction() - { - var sut = GetController(); - - var result = await sut.Index(string.Empty); - - result.Should().BeOfType(); - (result as RedirectToActionResult)!.ActionName.Should().BeEquivalentTo("error"); - } - - [Fact] - public async void Index_Calls_Service_GetContent() - { - const string dummySlug = "dummySlug"; - const bool isPreview = true; - var sut = GetController(); - - await sut.Index(dummySlug, isPreview); - - _contentServiceMock.Verify(o => o.GetContent(dummySlug, isPreview), Times.Once); - } - - [Fact] - public async void Index_NullResponse_ReturnsErrorAction() - { - _contentServiceMock.Setup(o => o.GetContent(It.IsAny(), It.IsAny())) - .ReturnsAsync((ContentSupportPage?)null); - - var sut = GetController(); - - var result = await sut.Index("slug"); - - result.Should().BeOfType(); - (result as RedirectToActionResult)!.ActionName.Should().BeEquivalentTo("error"); - } - - [Fact] - public async void Index_WithSlug_Returns_View() - { - _contentServiceMock.Setup(o => o.GetContent(It.IsAny(), It.IsAny())) - .ReturnsAsync(new ContentSupportPage()); - - var sut = GetController(); - var result = await sut.Index("slug1"); - - result.Should().BeOfType(); - (result as ViewResult)!.Model.Should().BeOfType(); - } - - [Fact] - public void Privacy_Returns_EmptyView() - { - var sut = GetController(); - - var result = sut.Privacy(); - - result.Should().BeOfType(); - } - - [Fact] - public void Error_Returns_ErrorView() - { - var sut = GetController(); - sut.ControllerContext.HttpContext = new DefaultHttpContext(); - var result = sut.Error(); - - result.Should().BeOfType(); - (result as ViewResult)!.Model.Should().BeOfType(); - } -} \ No newline at end of file diff --git a/tests/Dfe.ContentSupport.Web.Tests/Models/Mapped/Custom/CustomAttachmentTests.cs b/tests/Dfe.ContentSupport.Web.Tests/Models/Mapped/Custom/CustomAttachmentTests.cs index 241aabd..62831d2 100644 --- a/tests/Dfe.ContentSupport.Web.Tests/Models/Mapped/Custom/CustomAttachmentTests.cs +++ b/tests/Dfe.ContentSupport.Web.Tests/Models/Mapped/Custom/CustomAttachmentTests.cs @@ -51,7 +51,9 @@ public class CustomAttachmentTests { Size = Size } - } + }, + SystemProperties =new SystemProperties() + } } }