From 58f67e95e12130af897f64a9e3ce509d25eef310 Mon Sep 17 00:00:00 2001 From: Jonathan Idland Olsnes <73334350+Jonathanio123@users.noreply.github.com> Date: Tue, 29 Oct 2024 09:29:58 +0100 Subject: [PATCH] Added tests --- .../Helpers/ProjectHelpers.cs | 37 +++++++++++ .../Helpers/WeeklyTaskOwnerReportHelpers.cs | 65 +++++++++++++++++++ .../IntegrationTests/ProjectTests.cs | 42 ++++++++++++ .../WeeklyTaskOwnerReportTests.cs | 62 ++++++++++++++++++ 4 files changed, 206 insertions(+) create mode 100644 src/tests/Fusion.Summary.Api.Tests/Helpers/ProjectHelpers.cs create mode 100644 src/tests/Fusion.Summary.Api.Tests/Helpers/WeeklyTaskOwnerReportHelpers.cs create mode 100644 src/tests/Fusion.Summary.Api.Tests/IntegrationTests/ProjectTests.cs create mode 100644 src/tests/Fusion.Summary.Api.Tests/IntegrationTests/WeeklyTaskOwnerReportTests.cs diff --git a/src/tests/Fusion.Summary.Api.Tests/Helpers/ProjectHelpers.cs b/src/tests/Fusion.Summary.Api.Tests/Helpers/ProjectHelpers.cs new file mode 100644 index 000000000..a59b9b6b9 --- /dev/null +++ b/src/tests/Fusion.Summary.Api.Tests/Helpers/ProjectHelpers.cs @@ -0,0 +1,37 @@ +using FluentAssertions; +using Fusion.Summary.Api.Controllers.ApiModels; +using Fusion.Testing; + +namespace Fusion.Summary.Api.Tests.Helpers; + +public static class ProjectHelpers +{ + public static ApiProject GenerateProject(Guid? directorAzureUniqueId = null, Guid[]? additionalAdmins = null) + { + var id = Guid.NewGuid(); + return new ApiProject + { + Id = id, + Name = "Test Project - " + id, + OrgProjectExternalId = Guid.NewGuid(), + DirectorAzureUniqueId = directorAzureUniqueId, + AssignedAdminsAzureUniqueId = additionalAdmins ?? [] + }; + } + + public static async Task> PutProjectAsync(this HttpClient client, Action? setup = null) + { + var project = GenerateProject(); + setup?.Invoke(project); + + var response = await client.TestClientPutAsync($"projects/{project.OrgProjectExternalId}", project); + return response; + } + + public static async Task> GetProjectAsync(this HttpClient client, + string projectId) + { + var response = await client.TestClientGetAsync($"projects/{projectId}"); + return response; + } +} \ No newline at end of file diff --git a/src/tests/Fusion.Summary.Api.Tests/Helpers/WeeklyTaskOwnerReportHelpers.cs b/src/tests/Fusion.Summary.Api.Tests/Helpers/WeeklyTaskOwnerReportHelpers.cs new file mode 100644 index 000000000..f84f8d4c9 --- /dev/null +++ b/src/tests/Fusion.Summary.Api.Tests/Helpers/WeeklyTaskOwnerReportHelpers.cs @@ -0,0 +1,65 @@ +using Fusion.Summary.Api.Controllers.ApiModels; +using Fusion.Summary.Api.Controllers.Requests; +using Fusion.Testing; + +namespace Fusion.Summary.Api.Tests.Helpers; + +public static class WeeklyTaskOwnerReportHelpers +{ + public static async Task>> + GetWeeklyTaskOwnerReportsAsync( + this HttpClient client, string projectId) + { + var response = + await client.TestClientGetAsync>( + $"projects/{projectId}/task-owners-summary-reports/weekly"); + + return response; + } + + public static async Task> PutWeeklyTaskOwnerReportAsync(this HttpClient client, + string projectId, Action? setup = null) + { + var now = CreateDayOfWeek(DayOfWeek.Monday); + var request = new PutWeeklyTaskOwnerReportRequest() + { + PeriodStart = now, + PeriodEnd = now.AddDays(7), + ActionsAwaitingTaskOwnerAction = 1, + PositionAllocationsEndingInNextThreeMonths = Enumerable.Range(1, 5).Select(i => new ApiPositionAllocationEnding + { + PositionName = i.ToString(), + PositionNameDetailed = i.ToString(), + PositionAppliesTo = now.AddDays(20), + PositionExternalId = i.ToString() + }).ToArray(), + AdminAccessExpiringInLessThanThreeMonths = Enumerable.Range(1, 5).Select(i => new ApiAdminAccessExpiring + { + AzureUniqueId = Guid.NewGuid(), + FullName = i.ToString(), + Expires = now.AddDays(20) + }).ToArray(), + TBNPositionsStartingInLessThanThreeMonths = Enumerable.Range(6, 5).Select(i => new ApiTBNPositionStartingSoon + { + PositionName = i.ToString(), + PositionNameDetailed = i.ToString(), + PositionAppliesFrom = now.AddDays(20), + PositionExternalId = i.ToString() + }).ToArray() + }; + + setup?.Invoke(request); + + return await client.TestClientPutAsync($"projects/{projectId}/task-owners-summary-reports/weekly", + request); + } + + private static DateTime CreateDayOfWeek(DayOfWeek dayOfWeek) + { + var newDate = DateTime.UtcNow; + + var daysUntil = ((int)dayOfWeek - (int)newDate.DayOfWeek + 7) % 7; + + return newDate.AddDays(daysUntil); + } +} \ No newline at end of file diff --git a/src/tests/Fusion.Summary.Api.Tests/IntegrationTests/ProjectTests.cs b/src/tests/Fusion.Summary.Api.Tests/IntegrationTests/ProjectTests.cs new file mode 100644 index 000000000..9df231b4a --- /dev/null +++ b/src/tests/Fusion.Summary.Api.Tests/IntegrationTests/ProjectTests.cs @@ -0,0 +1,42 @@ +using FluentAssertions; +using Fusion.Summary.Api.Tests.Fixture; +using Fusion.Summary.Api.Tests.Helpers; +using Fusion.Summary.Api.Tests.IntegrationTests.Base; +using Fusion.Testing; +using Xunit.Abstractions; + +namespace Fusion.Summary.Api.Tests.IntegrationTests; + +[Collection(TestCollections.SUMMARY)] +public class ProjectTests : TestBase +{ + private readonly SummaryApiFixture _fixture; + private HttpClient _client; + + public ProjectTests(SummaryApiFixture fixture, ITestOutputHelper output) + { + _fixture = fixture; + _client = fixture.GetClient(); + SetOutput(output); + } + + [Fact] + public async Task PutProject_Then_GetProject_ShouldBeSuccess() + { + using var adminScope = _fixture.AdminScope(); + var testUser = _fixture.Fusion.CreateUser().AsEmployee().AzureUniqueId!.Value; + + var externalId = Guid.NewGuid(); + + var response = await _client.PutProjectAsync(s => + { + s.OrgProjectExternalId = externalId; + s.DirectorAzureUniqueId = testUser; + }); + response.Should().BeSuccessfull(); + + var getResponse = await _client.GetProjectAsync(externalId.ToString()); + getResponse.Should().BeSuccessfull(); + getResponse.Value!.OrgProjectExternalId.Should().Be(externalId); + } +} \ No newline at end of file diff --git a/src/tests/Fusion.Summary.Api.Tests/IntegrationTests/WeeklyTaskOwnerReportTests.cs b/src/tests/Fusion.Summary.Api.Tests/IntegrationTests/WeeklyTaskOwnerReportTests.cs new file mode 100644 index 000000000..3da45e9ff --- /dev/null +++ b/src/tests/Fusion.Summary.Api.Tests/IntegrationTests/WeeklyTaskOwnerReportTests.cs @@ -0,0 +1,62 @@ +using FluentAssertions; +using Fusion.Summary.Api.Tests.Fixture; +using Fusion.Summary.Api.Tests.Helpers; +using Fusion.Summary.Api.Tests.IntegrationTests.Base; +using Fusion.Testing; +using Xunit.Abstractions; + +namespace Fusion.Summary.Api.Tests.IntegrationTests; + +[Collection(TestCollections.SUMMARY)] +public class WeeklyTaskOwnerReportTests : TestBase +{ + private readonly SummaryApiFixture _fixture; + private HttpClient _client; + + public WeeklyTaskOwnerReportTests(SummaryApiFixture fixture, ITestOutputHelper output) + { + _fixture = fixture; + _client = _fixture.GetClient(); + SetOutput(output); + } + + + [Fact] + public async Task PutAndGetWeeklyTaskOwnerReport_ShouldReturnReport() + { + using var adminScope = _fixture.AdminScope(); + var testUser = _fixture.Fusion.CreateUser().AsEmployee().AzureUniqueId!.Value; + + var projectResponse = await _client.PutProjectAsync(s => { s.DirectorAzureUniqueId = testUser; }); + projectResponse.Should().BeSuccessfull(); + var projectExternalId = projectResponse.Value!.OrgProjectExternalId.ToString(); + + var response = await _client.PutWeeklyTaskOwnerReportAsync(projectExternalId); + response.Should().BeSuccessfull(); + + var getResponse = await _client.GetWeeklyTaskOwnerReportsAsync(projectExternalId); + getResponse.Should().BeSuccessfull(); + getResponse.Value!.Items.Should().HaveCount(1); + } + + [Fact] + public async Task PutWeeklyTaskOwnerReport_WithInvalidPeriodDate_ShouldReturnBadRequest() + { + using var adminScope = _fixture.AdminScope(); + var testUser = _fixture.Fusion.CreateUser().AsEmployee().AzureUniqueId!.Value; + + var projectResponse = await _client.PutProjectAsync(s => { s.DirectorAzureUniqueId = testUser; }); + projectResponse.Should().BeSuccessfull(); + var project = projectResponse.Value!; + + var reportResponse = await _client.PutWeeklyTaskOwnerReportAsync(project.OrgProjectExternalId.ToString(), (report) => + { + var nowDate = DateTime.UtcNow; + if (nowDate.DayOfWeek == DayOfWeek.Monday) + nowDate = nowDate.AddDays(1); + report.PeriodStart = nowDate; + }); + + reportResponse.Should().BeBadRequest(); + } +} \ No newline at end of file