From 482ba6da08b781a312a3cf4a5930ea4bb7dc1ba5 Mon Sep 17 00:00:00 2001 From: Kjetil Haugland <801342+kjetilhau@users.noreply.github.com> Date: Wed, 4 Sep 2024 11:27:32 +0200 Subject: [PATCH] chore: implemented portal configuration integration tests (#736) Co-authored-by: kjetilhau --- .changeset/pr-736-2050972397.md | 3 + .../IntegrationTests/AssertHelpers.cs | 13 +- .../IntegrationTests/PortalControllerTests.cs | 290 +++++++++++++++++- 3 files changed, 294 insertions(+), 12 deletions(-) create mode 100644 .changeset/pr-736-2050972397.md diff --git a/.changeset/pr-736-2050972397.md b/.changeset/pr-736-2050972397.md new file mode 100644 index 000000000..017b123fe --- /dev/null +++ b/.changeset/pr-736-2050972397.md @@ -0,0 +1,3 @@ + +--- +--- diff --git a/backend/src/tests/Equinor.ProjectExecutionPortal.Tests.WebApi/IntegrationTests/AssertHelpers.cs b/backend/src/tests/Equinor.ProjectExecutionPortal.Tests.WebApi/IntegrationTests/AssertHelpers.cs index c0541ae4f..994627173 100644 --- a/backend/src/tests/Equinor.ProjectExecutionPortal.Tests.WebApi/IntegrationTests/AssertHelpers.cs +++ b/backend/src/tests/Equinor.ProjectExecutionPortal.Tests.WebApi/IntegrationTests/AssertHelpers.cs @@ -24,7 +24,18 @@ public static void AssertPortalValues(ApiPortal? portal) Assert.IsNotNull(portal); } - + public static void AssertPortalConfigurationValues(ApiPortalConfiguration? portalConfiguration, bool acceptNullValues) + { + if (portalConfiguration == null) + { + Assert.Fail(); + } + + if (!acceptNullValues) + { + Assert.IsNotNull(portalConfiguration.Router); + } + } public static void AssertPortalAppValues(ApiPortalApp? app) { diff --git a/backend/src/tests/Equinor.ProjectExecutionPortal.Tests.WebApi/IntegrationTests/PortalControllerTests.cs b/backend/src/tests/Equinor.ProjectExecutionPortal.Tests.WebApi/IntegrationTests/PortalControllerTests.cs index a966a012b..ba39ab0a3 100644 --- a/backend/src/tests/Equinor.ProjectExecutionPortal.Tests.WebApi/IntegrationTests/PortalControllerTests.cs +++ b/backend/src/tests/Equinor.ProjectExecutionPortal.Tests.WebApi/IntegrationTests/PortalControllerTests.cs @@ -1,10 +1,12 @@ using System.Net; +using System.Text; using Equinor.ProjectExecutionPortal.Tests.WebApi.Data; using Equinor.ProjectExecutionPortal.Tests.WebApi.Setup; using Equinor.ProjectExecutionPortal.WebApi.ViewModels.Portal; using Equinor.ProjectExecutionPortal.WebApi.ViewModels.PortalApp; using Microsoft.VisualStudio.TestTools.UnitTesting; using Newtonsoft.Json; +using JsonSerializer = System.Text.Json.JsonSerializer; namespace Equinor.ProjectExecutionPortal.Tests.WebApi.IntegrationTests { @@ -49,9 +51,7 @@ public async Task Get_Portal_AsAuthenticatedUser_ShouldReturnOk() public async Task Get_NonExistentPortal_AsAuthenticatedUser_ShouldReturnNotFound() { // Act & Assert - var response = await GetAppsForPortal(Guid.NewGuid(), null, null, UserType.Authenticated); - - Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode); + await AssertGetPortal(Guid.NewGuid(), UserType.Authenticated, HttpStatusCode.NotFound); } [TestMethod] @@ -64,6 +64,212 @@ public async Task Get_Portal_AsAnonymous_ShouldReturnUnauthorized() Assert.IsNull(portal); } + [TestMethod] + public async Task Create_Portal_AsAdministratorUser_ShouldReturnOk() + { + // Arrange + var getAllBeforeCreation = await AssertGetAllPortals(UserType.Administrator, HttpStatusCode.OK); + + var payload = new ApiCreatePortalRequest + { + Name = "Created portal name", + Description = "Created description", + ShortName = "Created short name", + Subtext = "Created subtext", + Icon = "Created icon", + ContextTypes = new List { "ProjectMaster" } + }; + + // Act + var response = await CreatePortal(UserType.Administrator, payload); + var getAllAfterCreation = await AssertGetAllPortals(UserType.Administrator, HttpStatusCode.OK); + var theOneCreated = getAllAfterCreation!.Last(); + + // Assert + var createdPortal = await AssertGetPortal(theOneCreated.Id, UserType.Authenticated, HttpStatusCode.OK); + + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); + Assert.AreEqual(getAllBeforeCreation!.Count + 1, getAllAfterCreation!.Count); + Assert.AreEqual(theOneCreated.Id, createdPortal!.Id); + Assert.AreEqual(payload.Name, createdPortal.Name); + Assert.AreEqual(payload.Description, createdPortal.Description); + Assert.AreEqual(payload.ShortName, createdPortal.ShortName); + Assert.AreEqual(payload.Subtext, createdPortal.Subtext); + Assert.AreEqual(payload.Icon, createdPortal.Icon); + Assert.AreEqual(payload.ContextTypes.Count, createdPortal.ContextTypes.Count); + } + + [TestMethod] + public async Task Create_Portal_AsAuthenticatedUser_ShouldReturnForbidden() + { + // Arrange + var payload = new ApiCreatePortalRequest(); + + // Act + var response = await CreatePortal(UserType.Authenticated, payload); + + // Assert + Assert.AreEqual(HttpStatusCode.Forbidden, response.StatusCode); + } + + [TestMethod] + public async Task Create_Portal_AsAnonymousUser_ShouldReturnUnauthorized() + { + // Arrange + var payload = new ApiCreatePortalRequest(); + + // Act + var response = await CreatePortal(UserType.Anonymous, payload); + + // Assert + Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode); + } + + [TestMethod] + public async Task Update_Portal_AsAdministratorUser_ShouldReturnOk() + { + // Arrange + var getAllBeforeUpdated = await AssertGetAllPortals(UserType.Administrator, HttpStatusCode.OK); + var theOneToUpdate = getAllBeforeUpdated!.First(); + + var payload = new ApiUpdatePortalRequest + { + Name = "Updated portal name", + Description = "Updated description", + ShortName = "Updated short name", + Subtext = "Updated subtext", + Icon = "Updated icon", + ContextTypes = new List { "ProjectMaster", "Facility" } + }; + + // Act + var response = await UpdatePortal(UserType.Administrator, payload, theOneToUpdate.Id); + + // Assert + var theOneAfterUpdate = await AssertGetPortal(theOneToUpdate.Id, UserType.Authenticated, HttpStatusCode.OK); + + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); + Assert.AreEqual(theOneToUpdate.Id, theOneAfterUpdate!.Id); + Assert.AreEqual(payload.Name, theOneAfterUpdate.Name); + Assert.AreEqual(payload.Description, theOneAfterUpdate.Description); + Assert.AreEqual(payload.ShortName, theOneAfterUpdate.ShortName); + Assert.AreEqual(payload.Subtext, theOneAfterUpdate.Subtext); + Assert.AreEqual(payload.Icon, theOneAfterUpdate.Icon); + Assert.AreEqual(payload.ContextTypes.Count, theOneAfterUpdate.ContextTypes.Count); + } + + [TestMethod] + public async Task Update_Portal_AsAuthenticatedUser_ShouldReturnForbidden() + { + // Arrange + var payload = new ApiUpdatePortalRequest(); + + // Act + var response = await UpdatePortal(UserType.Authenticated, payload, Guid.NewGuid()); + + // Assert + Assert.AreEqual(HttpStatusCode.Forbidden, response.StatusCode); + } + + [TestMethod] + public async Task Update_Portal_AsAnonymousUser_ShouldReturnUnauthorized() + { + // Arrange + var payload = new ApiUpdatePortalRequest(); + + // Act + var response = await UpdatePortal(UserType.Anonymous, payload, Guid.NewGuid()); + + // Assert + Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode); + } + + [TestMethod] + public async Task Get_PortalConfiguration_AsAuthenticatedUser_ShouldReturnOk() + { + // Arrange + var portals = await AssertGetAllPortals(UserType.Authenticated, HttpStatusCode.OK); + var portalToTest = portals!.First(); + + // Act & Assert + await AssertGetPortalConfiguration(portalToTest.Id, UserType.Authenticated, HttpStatusCode.OK); + } + + [TestMethod] + public async Task Get_NonExistentPortalConfiguration_AsAuthenticatedUser_ShouldReturnNotFound() + { + // Act & Assert + await AssertGetPortalConfiguration(Guid.NewGuid(), UserType.Authenticated, HttpStatusCode.NotFound); + } + + [TestMethod] + public async Task Get_PortalConfiguration_AsAnonymous_ShouldReturnUnauthorized() + { + // Act + var portal = await AssertGetPortalConfiguration(Guid.NewGuid(), UserType.Anonymous, HttpStatusCode.Unauthorized); + + // Assert + Assert.IsNull(portal); + } + + [TestMethod] + public async Task Update_PortalConfiguration_AsAdministratorUser_ShouldReturnOk() + { + // Arrange + var getAllBeforeUpdated = await AssertGetAllPortals(UserType.Administrator, HttpStatusCode.OK); + var portalToTest = getAllBeforeUpdated!.First(); + var theOneToUpdate = await AssertGetPortalConfiguration(portalToTest.Id, UserType.Authenticated, HttpStatusCode.OK); + + var routerData = JsonSerializer.Serialize( + new + { + Router = new { Root = "" }, + Routes = (List)[new { Id = 1 }, new { Id = 2 },] + } + ); + + var payload = new ApiUpdatePortalConfigurationRequest + { + Router = routerData + }; + + // Act + var response = await UpdatePortalConfiguration(UserType.Administrator, payload, portalToTest.Id); + + // Assert + var theOneAfterUpdate = await AssertGetPortalConfiguration(portalToTest.Id, UserType.Authenticated, HttpStatusCode.OK); + + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); + Assert.AreNotEqual(payload.Router, theOneToUpdate!.Router); + Assert.AreEqual(payload.Router, theOneAfterUpdate!.Router); + } + + [TestMethod] + public async Task Update_PortalConfiguration_AsAuthenticatedUser_ShouldReturnForbidden() + { + // Arrange + var payload = new ApiUpdatePortalConfigurationRequest(); + + // Act + var response = await UpdatePortalConfiguration(UserType.Authenticated, payload, Guid.NewGuid()); + + // Assert + Assert.AreEqual(HttpStatusCode.Forbidden, response.StatusCode); + } + + [TestMethod] + public async Task Update_PortalConfiguration_AsAnonymousUser_ShouldReturnUnauthorized() + { + // Arrange + var payload = new ApiUpdatePortalConfigurationRequest(); + + // Act + var response = await UpdatePortalConfiguration(UserType.Anonymous, payload, Guid.NewGuid()); + + // Assert + Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode); + } + [Ignore] // TODO: Need to perform clean up after each test [TestMethod] public async Task Get_OnlyGlobalAppsForPortal_WithoutContext_AsAuthenticatedUser_ShouldReturnOk() @@ -78,7 +284,6 @@ public async Task Get_OnlyGlobalAppsForPortal_WithoutContext_AsAuthenticatedUser // Assert Assert.IsNotNull(apps); Assert.AreEqual(apps.Count, 2); - } [Ignore] // TODO: Need to perform clean up after each test @@ -90,12 +295,11 @@ public async Task Get_BothGlobalAndContextAppsForPortal_WithValidContext_AsAuthe var portalToTest = portals?.FirstOrDefault(); // Act - var apps = await AssertGetAppsForPortal(portalToTest!.Id, FusionContextData.InitialSeedData.JcaContextExternalId, FusionContextData.InitialSeedData.ContextType,UserType.Authenticated, HttpStatusCode.OK); + var apps = await AssertGetAppsForPortal(portalToTest!.Id, FusionContextData.InitialSeedData.JcaContextExternalId, FusionContextData.InitialSeedData.ContextType, UserType.Authenticated, HttpStatusCode.OK); // Assert Assert.IsNotNull(apps); Assert.AreEqual(apps.Count, 3); - } [Ignore] @@ -107,7 +311,7 @@ public async Task Get_BothGlobalAndContextAppsForPortal_WithInvalidContext_AsAut var portalToTest = portals?.FirstOrDefault(); // Act - var apps = await AssertGetAppsForPortal(portalToTest!.Id, FusionContextData.InitialSeedData.InvalidContextExternalId, FusionContextData.InitialSeedData.ContextType,UserType.Authenticated, HttpStatusCode.OK); + await AssertGetAppsForPortal(portalToTest!.Id, FusionContextData.InitialSeedData.InvalidContextExternalId, FusionContextData.InitialSeedData.ContextType, UserType.Authenticated, HttpStatusCode.OK); // Assert // TODO Fusion 404 returned @@ -117,7 +321,7 @@ public async Task Get_BothGlobalAndContextAppsForPortal_WithInvalidContext_AsAut public async Task Get_AppsForNonExistentPortal_AsAuthenticatedUser_ShouldReturnNotFound() { // Act & Assert - var response = await GetAppsForPortal(Guid.NewGuid(), null,null, UserType.Authenticated); + var response = await GetAppsForPortal(Guid.NewGuid(), null, null, UserType.Authenticated); Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode); } @@ -126,7 +330,7 @@ public async Task Get_AppsForNonExistentPortal_AsAuthenticatedUser_ShouldReturnN public async Task Get_AppsForPortal_WithoutContext_AsAnonymousUser_ShouldReturnUnauthorized() { // Act - var apps = await AssertGetAppsForPortal(new Guid(), null, null, UserType.Anonymous, HttpStatusCode.Unauthorized); + var apps = await AssertGetAppsForPortal(Guid.NewGuid(), null, null, UserType.Anonymous, HttpStatusCode.Unauthorized); // Assert Assert.IsNull(apps); @@ -136,7 +340,7 @@ public async Task Get_AppsForPortal_WithoutContext_AsAnonymousUser_ShouldReturnU public async Task Get_AppsForPortal_WithValidContext_AsAnonymousUser_ShouldReturnUnauthorized() { // Act - var apps = await AssertGetAppsForPortal(new Guid(), FusionContextData.InitialSeedData.JcaContextExternalId, FusionContextData.InitialSeedData.ContextType, UserType.Anonymous, HttpStatusCode.Unauthorized); + var apps = await AssertGetAppsForPortal(Guid.NewGuid(), FusionContextData.InitialSeedData.JcaContextExternalId, FusionContextData.InitialSeedData.ContextType, UserType.Anonymous, HttpStatusCode.Unauthorized); // Assert Assert.IsNull(apps); @@ -144,7 +348,7 @@ public async Task Get_AppsForPortal_WithValidContext_AsAnonymousUser_ShouldRetur #region Helpers - public static async Task?> AssertGetAllPortals(UserType userType, HttpStatusCode expectedStatusCode) + private static async Task?> AssertGetAllPortals(UserType userType, HttpStatusCode expectedStatusCode) { // Act var response = await GetAllPortals(userType); @@ -189,11 +393,67 @@ public async Task Get_AppsForPortal_WithValidContext_AsAnonymousUser_ShouldRetur Assert.IsNotNull(content); Assert.IsNotNull(portal); AssertHelpers.AssertPortalValues(portal); + AssertHelpers.AssertPortalConfigurationValues(portal.Configuration, acceptNullValues: true); Assert.AreEqual(portal.Apps.Count, 0); // No relational data should be included in this request return portal; } + private static async Task AssertGetPortalConfiguration(Guid portalId, UserType userType, HttpStatusCode expectedStatusCode) + { + // Act + var response = await GetPortalConfiguration(portalId, userType); + var content = await response.Content.ReadAsStringAsync(); + var portalConfiguration = JsonConvert.DeserializeObject(content); + + // Assert + Assert.AreEqual(expectedStatusCode, response.StatusCode); + + if (response.StatusCode != HttpStatusCode.OK) + { + return portalConfiguration; + } + + Assert.IsNotNull(content); + Assert.IsNotNull(portalConfiguration); + AssertHelpers.AssertPortalConfigurationValues(portalConfiguration, acceptNullValues: true); + + return portalConfiguration; + } + + private static async Task CreatePortal(UserType userType, ApiCreatePortalRequest createdPortal) + { + var serializePayload = JsonConvert.SerializeObject(createdPortal); + var content = new StringContent(serializePayload, Encoding.UTF8, "application/json"); + + var client = TestFactory.Instance.GetHttpClient(userType); + var response = await client.PostAsync($"{string.Format(Route)}", content); + + return response; + } + + private static async Task UpdatePortal(UserType userType, ApiUpdatePortalRequest updatedPortal, Guid portalId) + { + var serializePayload = JsonConvert.SerializeObject(updatedPortal); + var content = new StringContent(serializePayload, Encoding.UTF8, "application/json"); + + var client = TestFactory.Instance.GetHttpClient(userType); + var response = await client.PutAsync($"{string.Format(Route)}/{portalId}", content); + + return response; + } + + private static async Task UpdatePortalConfiguration(UserType userType, ApiUpdatePortalConfigurationRequest updatedPortalConfiguration, Guid portalId) + { + var serializePayload = JsonConvert.SerializeObject(updatedPortalConfiguration); + var content = new StringContent(serializePayload, Encoding.UTF8, "application/json"); + + var client = TestFactory.Instance.GetHttpClient(userType); + var response = await client.PutAsync($"{string.Format(Route)}/{portalId}/configuration", content); + + return response; + } + private static async Task?> AssertGetAppsForPortal(Guid portalId, string? contextExternalId, string? contextType, UserType userType, HttpStatusCode expectedStatusCode) { // Act @@ -236,6 +496,14 @@ private static async Task GetPortal(Guid portalId, UserType return response; } + private static async Task GetPortalConfiguration(Guid portalId, UserType userType) + { + var client = TestFactory.Instance.GetHttpClient(userType); + var response = await client.GetAsync($"{Route}/{portalId}/configuration"); + + return response; + } + private static async Task GetAppsForPortal(Guid portalId, string? contextExternalId, string? contextType, UserType userType) { var route = contextExternalId != null ? $"{Route}/{portalId}/contexts/{contextExternalId}/apps" : $"{Route}/{portalId}/apps";