Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NullRedactor to the RedactorProvider during initialization #5424

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.Extensions.Compliance.Classification;
using Microsoft.Extensions.Options;
using Microsoft.Shared.Diagnostics;
Expand Down Expand Up @@ -37,6 +38,12 @@ public Redactor GetRedactor(DataClassificationSet classifications)

private static FrozenDictionary<DataClassificationSet, Redactor> GetClassRedactorMap(IEnumerable<Redactor> redactors, Dictionary<DataClassificationSet, Type> map)
{
if (!map.ContainsKey(DataClassification.None))
{
map.Add(DataClassification.None, typeof(NullRedactor));
redactors = redactors.Concat([NullRedactor.Instance]);
makazeu marked this conversation as resolved.
Show resolved Hide resolved
}

var dict = new Dictionary<DataClassificationSet, Redactor>(map.Count);
foreach (var m in map)
{
Expand All @@ -45,6 +52,7 @@ private static FrozenDictionary<DataClassificationSet, Redactor> GetClassRedacto
if (r.GetType() == m.Value)
{
dict[m.Key] = r;
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ namespace Microsoft.Extensions.Compliance.Redaction.Test;

public class RedactorProviderTests
{
[Fact]
public void RedactorProvider_Returns_NullRedactor_For_NoneDataClassification()
{
var redactorProvider = new RedactorProvider(
redactors: [ErasingRedactor.Instance],
options: Microsoft.Extensions.Options.Options.Create(new RedactorProviderOptions()));

Assert.IsType<NullRedactor>(redactorProvider.GetRedactor(DataClassification.None));
}

[Fact]
public void RedactorProvider_Returns_Redactor_For_Every_Data_Classification()
{
Expand Down Expand Up @@ -38,13 +48,15 @@ public void RedactorProvider_Returns_Redactor_For_Data_Classifications()
redactors: new Redactor[] { ErasingRedactor.Instance, NullRedactor.Instance },
options: Microsoft.Extensions.Options.Options.Create(opt));

var r1 = redactorProvider.GetRedactor(_dataClassification1);
var r2 = redactorProvider.GetRedactor(_dataClassification2);
var r3 = redactorProvider.GetRedactor(_dataClassification3);
Redactor r1 = redactorProvider.GetRedactor(_dataClassification1);
Redactor r2 = redactorProvider.GetRedactor(_dataClassification2);
Redactor r3 = redactorProvider.GetRedactor(_dataClassification3);
Redactor r4 = redactorProvider.GetRedactor(DataClassification.None);

Assert.Equal(typeof(ErasingRedactor), r1.GetType());
Assert.Equal(typeof(NullRedactor), r2.GetType());
Assert.Equal(typeof(ErasingRedactor), r3.GetType());
Assert.IsType<ErasingRedactor>(r1);
Assert.IsType<NullRedactor>(r2);
Assert.IsType<ErasingRedactor>(r3);
Assert.IsType<NullRedactor>(r4);
}

[Fact]
Expand Down