From 4018487acf3bfec4177be5e1073cfab78dc9eab9 Mon Sep 17 00:00:00 2001 From: Brant DeBow Date: Mon, 31 Mar 2025 08:17:48 -0400 Subject: [PATCH] [PM-17562] Add Dapper Repositories For Ogranization Integrations and Configurations --- ...nizationIntegrationConfigurationDetails.cs | 39 ++++++++- ...ionIntegrationConfigurationDetailsTests.cs | 82 +++++++++++++++++++ 2 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 test/Core.Test/AdminConsole/Models/Data/Organizations/OrganizationIntegrationConfigurationDetailsTests.cs diff --git a/src/Core/AdminConsole/Models/Data/Organizations/OrganizationIntegrationConfigurationDetails.cs b/src/Core/AdminConsole/Models/Data/Organizations/OrganizationIntegrationConfigurationDetails.cs index e00387fcf7..3829ef5749 100644 --- a/src/Core/AdminConsole/Models/Data/Organizations/OrganizationIntegrationConfigurationDetails.cs +++ b/src/Core/AdminConsole/Models/Data/Organizations/OrganizationIntegrationConfigurationDetails.cs @@ -19,15 +19,46 @@ public class OrganizationIntegrationConfigurationDetails { get { - var configJson = JsonNode.Parse(Configuration ?? string.Empty) as JsonObject ?? new JsonObject(); - var integrationJson = JsonNode.Parse(IntegrationConfiguration ?? string.Empty) as JsonObject ?? new JsonObject(); + var integrationJson = IntegrationConfigurationJson; - foreach (var kvp in configJson) + foreach (var kvp in ConfigurationJson) { - integrationJson[kvp.Key] = kvp.Value; + integrationJson[kvp.Key] = kvp.Value?.DeepClone(); } return integrationJson; } } + + private JsonObject ConfigurationJson + { + get + { + try + { + var configuration = Configuration ?? "{}"; + return JsonNode.Parse(configuration) as JsonObject ?? new JsonObject(); + } + catch + { + return new JsonObject(); + } + } + } + + private JsonObject IntegrationConfigurationJson + { + get + { + try + { + var integration = IntegrationConfiguration ?? "{}"; + return JsonNode.Parse(integration) as JsonObject ?? new JsonObject(); + } + catch + { + return new JsonObject(); + } + } + } } diff --git a/test/Core.Test/AdminConsole/Models/Data/Organizations/OrganizationIntegrationConfigurationDetailsTests.cs b/test/Core.Test/AdminConsole/Models/Data/Organizations/OrganizationIntegrationConfigurationDetailsTests.cs new file mode 100644 index 0000000000..3c5eec751f --- /dev/null +++ b/test/Core.Test/AdminConsole/Models/Data/Organizations/OrganizationIntegrationConfigurationDetailsTests.cs @@ -0,0 +1,82 @@ +using System.Text.Json; +using Bit.Core.Models.Data.Organizations; +using Xunit; + +namespace Bit.Core.Test.Models.Data.Organizations; + +public class OrganizationIntegrationConfigurationDetailsTests +{ + [Fact] + public void MergedConfiguration_BothHaveValues() + { + var config = new { config = "A new config value" }; + var integration = new { integration = "An integration value" }; + var expectedObj = new { integration = "An integration value", config = "A new config value" }; + var expected = JsonSerializer.Serialize(expectedObj); + + var sut = new OrganizationIntegrationConfigurationDetails(); + sut.Configuration = JsonSerializer.Serialize(config); + sut.IntegrationConfiguration = JsonSerializer.Serialize(integration); + + var result = sut.MergedConfiguration; + Assert.Equal(expected, result.ToJsonString()); + } + + [Fact] + public void MergedConfiguration_BothNotJson() + { + var expectedObj = new { }; + var expected = JsonSerializer.Serialize(expectedObj); + + var sut = new OrganizationIntegrationConfigurationDetails(); + sut.Configuration = "Not JSON"; + sut.IntegrationConfiguration = "Not JSON"; + + var result = sut.MergedConfiguration; + Assert.Equal(expected, result.ToJsonString()); + } + + [Fact] + public void MergedConfiguration_BothNull() + { + var expectedObj = new { }; + var expected = JsonSerializer.Serialize(expectedObj); + + var sut = new OrganizationIntegrationConfigurationDetails(); + sut.Configuration = null; + sut.IntegrationConfiguration = null; + + var result = sut.MergedConfiguration; + Assert.Equal(expected, result.ToJsonString()); + } + + [Fact] + public void MergedConfiguration_ConfigNull() + { + var integration = new { integration = "An integration value" }; + var expectedObj = new { integration = "An integration value" }; + var expected = JsonSerializer.Serialize(expectedObj); + + var sut = new OrganizationIntegrationConfigurationDetails(); + sut.Configuration = null; + sut.IntegrationConfiguration = JsonSerializer.Serialize(integration); + + var result = sut.MergedConfiguration; + Assert.Equal(expected, result.ToJsonString()); + } + + [Fact] + public void MergedConfiguration_IntegrationNull() + { + var config = new { config = "A new config value" }; + var expectedObj = new { config = "A new config value" }; + var expected = JsonSerializer.Serialize(expectedObj); + + var sut = new OrganizationIntegrationConfigurationDetails(); + sut.Configuration = JsonSerializer.Serialize(config); + sut.IntegrationConfiguration = null; + + var result = sut.MergedConfiguration; + Assert.Equal(expected, result.ToJsonString()); + } +}