using Strata.ApiCommunication.Http; using Strata.ContinuousImprovement.Api.Test.Integration.Info; using System.Net.Http.Json; using System.Text.RegularExpressions; namespace Strata.ContinuousImprovement.Api.Test.Integration.Tests { [TestFixture(), Category("Integration"), Category("API")] public class TestInitiativesController_Export : ApiTestBase { protected const string InitiativesEndpoint = "api/v1/initiatives"; private Guid ConfigurationGuid { get; set; } = Guid.Empty; private string Workstream { get; set; } = string.Empty; private object FilterSettings { get; set; } [Test, Order(1)] public async Task TestInitiativesExport_Step1() { // arrange // act var configurations = await GetConfigurations(); var configuration = configurations.OrderBy(c => c.DisplayOrder).First(); var configurationGuid = configuration.ConfigurationGuid; ConfigurationGuid = configurationGuid; // assert Assert.That(ConfigurationGuid, Is.Not.EqualTo(Guid.Empty)); } [Test, Order(2)] public async Task TestInitiativesExport_Step2() { // arrange var request = new HttpRequestMessage(HttpMethod.Get, InitiativesEndpoint + $"/{ConfigurationGuid}/filter-options"); var response = await HttpClient.SendAsync(request); response.EnsureSuccessStatusCode(); var filterOptions = await response.Content.ReadFromJsonAsync(); // assert Assert.Multiple(() => { Assert.That(filterOptions, Is.Not.Null); Assert.That(filterOptions.Workstreams, Is.Not.Null.And.Not.Empty); }); var workstream = filterOptions.Workstreams.First().Name; Workstream = workstream; } [Test, Order(3)] public async Task TestInitiativesExport_Step3() { // arrange FilterSettings = new { ConfigurationGuid, name = "", status = 0, costDrivers = Array.Empty(), costLeaders = Array.Empty(), owners = Array.Empty(), executiveSponsors = Array.Empty(), workstreams = new string[] { Workstream }, rollupColumn1Values = Array.Empty(), rollupColumn2Values = Array.Empty(), rollupColumn3Values = Array.Empty(), groupByCategory = "CostDriver" }; // act var request = new HttpRequestMessage(HttpMethod.Post, InitiativesEndpoint + "/set-filter-settings"); await request.SetContentAsJsonAsync(FilterSettings); var response = await HttpClient.SendAsync(request); // assert response.EnsureSuccessStatusCode(); } [Test, Order(4)] public async Task TestInitiativesExport_Step4() { // arrange var request = new HttpRequestMessage(HttpMethod.Post, InitiativesEndpoint); await request.SetContentAsJsonAsync(FilterSettings); var response = await HttpClient.SendAsync(request); // act response.EnsureSuccessStatusCode(); } [Test, Order(5)] public async Task TestInitiativesExport_Step5() { // arrange var data = new { sortOptions = new { sortField = "name", sortOrder = "Asc" }, filters = new { ConfigurationGuid, name = "", status = 0, costDrivers = Array.Empty(), costLeaders = Array.Empty(), owners = Array.Empty(), executiveSponsors = Array.Empty(), workstreams = new string[] { Workstream }, rollupColumn1Values = Array.Empty(), rollupColumn2Values = Array.Empty(), rollupColumn3Values = Array.Empty(), groupByCategory = "CostDriver" } }; var request = new HttpRequestMessage(HttpMethod.Post, InitiativesEndpoint + "/export"); await request.SetContentAsJsonAsync(data); // act var response = await HttpClient.SendAsync(request); response.EnsureSuccessStatusCode(); // assert var headers = response.Content.Headers.Where(x => x.Value != null) .ToDictionary(k => k.Key, v => v.Value); var disposition = headers["Content-Disposition"].First(); var regex = new Regex("attachment; filename=Initiatives_[\\d]{2}-[\\d]{2}-[\\d]{4}\\.xlsx; filename\\*=UTF-8''Initiatives_[\\d]{2}-[\\d]{2}-[\\d]{4}\\.xlsx", RegexOptions.IgnoreCase); Assert.That(regex.IsMatch(disposition)); } } }