Skip to content

Commit

Permalink
Added some tests against the import notification, and verified that t…
Browse files Browse the repository at this point in the history
…he json is the same after the flatten, and unflattening
  • Loading branch information
Thomas Anderson committed Dec 3, 2024
1 parent 4ce11e7 commit d926789
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Cdms.SensitiveData/SensitiveDataSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ public T Deserialize<T>(string json, Action<JsonSerializerOptions> 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();
Expand All @@ -57,15 +62,18 @@ 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
{
for (int i = 0; i < fields.Keys.Count; i++)
{
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()!);
}
Expand Down
43 changes: 43 additions & 0 deletions CdmsBackend.IntegrationTests/SensitiveDataTests.cs
Original file line number Diff line number Diff line change
@@ -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<SensitiveDataSerializer>.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<SensitiveDataSerializer>.Instance);

var result = serializer.RedactRawJson(json, typeof(ImportNotification));

JsonNode.DeepEquals(JsonNode.Parse(json), JsonNode.Parse(result)).Should().BeFalse();
result.Should().Contain("TestRedacted");

}
}

0 comments on commit d926789

Please sign in to comment.