From d926789aa5ec788e938515b7b7676816d3aa794e Mon Sep 17 00:00:00 2001 From: Thomas Anderson Date: Tue, 3 Dec 2024 16:40:19 +0000 Subject: [PATCH] Added some tests against the import notification, and verified that the json is the same after the flatten, and unflattening --- Cdms.SensitiveData/SensitiveDataSerializer.cs | 12 +++++- .../SensitiveDataTests.cs | 43 +++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 CdmsBackend.IntegrationTests/SensitiveDataTests.cs diff --git a/Cdms.SensitiveData/SensitiveDataSerializer.cs b/Cdms.SensitiveData/SensitiveDataSerializer.cs index 1a2a8d2d..8c222478 100644 --- a/Cdms.SensitiveData/SensitiveDataSerializer.cs +++ b/Cdms.SensitiveData/SensitiveDataSerializer.cs @@ -48,7 +48,12 @@ public T Deserialize(string json, Action optionsOverri public string RedactRawJson(string json, Type type) { + if (options.Value.Include) + { + return json; + } var sensitiveFields = SensitiveFieldsProvider.Get(type); + var jObject = JObject.Parse(json); var fields = jObject.Flatten(); @@ -57,7 +62,10 @@ public string RedactRawJson(string json, Type type) { if (fields.TryGetValue(field, out var value)) { - fields[field] = options.Value.Getter(value.ToString()!); + if (!options.Value.Include) + { + fields[field] = options.Value.Getter(value.ToString()!); + } } else { @@ -65,7 +73,7 @@ public string RedactRawJson(string json, Type type) { var key = fields.Keys.ElementAt(i); var replaced = Regex.Replace(key, "\\[.*?\\]", "", RegexOptions.NonBacktracking); - if (replaced == field && fields.TryGetValue(key, out var v)) + if (replaced == field && fields.TryGetValue(key, out var v) && !options.Value.Include) { fields[key] = options.Value.Getter(v.ToString()!); } diff --git a/CdmsBackend.IntegrationTests/SensitiveDataTests.cs b/CdmsBackend.IntegrationTests/SensitiveDataTests.cs new file mode 100644 index 00000000..d30e94bf --- /dev/null +++ b/CdmsBackend.IntegrationTests/SensitiveDataTests.cs @@ -0,0 +1,43 @@ +using System.Text.Json.Nodes; +using Cdms.SensitiveData; +using Cdms.Types.Ipaffs; +using FluentAssertions; +using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; +using Xunit; + +namespace CdmsBackend.IntegrationTests; + +public class SensitiveDataTests +{ + [Fact] + public void WhenIncludeSensitiveData_RedactedShouldBeSameAsJson() + { + string json = + File.ReadAllText(Path.GetFullPath("..\\..\\..\\Fixtures\\SmokeTest\\IPAFFS\\CHEDA\\CHEDA_GB_2024_1041389-ee0e6fcf-52a4-45ea-8830-d4553ee70361.json")); + + SensitiveDataOptions options = new SensitiveDataOptions { Getter = s => "TestRedacted", Include = true }; + var serializer = new SensitiveDataSerializer(Options.Create(options), NullLogger.Instance); + + var result = serializer.RedactRawJson(json, typeof(ImportNotification)); + + JsonNode.DeepEquals(JsonNode.Parse(json), JsonNode.Parse(result)).Should().BeTrue(); + + } + + [Fact] + public void WhenIncludeSensitiveData_RedactedShouldBeDifferentJson() + { + string json = + File.ReadAllText(Path.GetFullPath("..\\..\\..\\Fixtures\\SmokeTest\\IPAFFS\\CHEDA\\CHEDA_GB_2024_1041389-ee0e6fcf-52a4-45ea-8830-d4553ee70361.json")); + + SensitiveDataOptions options = new SensitiveDataOptions { Getter = s => "TestRedacted", Include = false }; + var serializer = new SensitiveDataSerializer(Options.Create(options), NullLogger.Instance); + + var result = serializer.RedactRawJson(json, typeof(ImportNotification)); + + JsonNode.DeepEquals(JsonNode.Parse(json), JsonNode.Parse(result)).Should().BeFalse(); + result.Should().Contain("TestRedacted"); + + } +} \ No newline at end of file