コード例 #1
0
        public void RedactObject_InputPassedAsInObject_ReturnsRedactedMessage()
        {
            //Setup
            var usernameExpected = "username";
            var passwordExpected = RedactorService.REDACTED_REPLACEMENT_VALUE;

            var input = new TestObject
            {
                Username = usernameExpected,
                Password = "******"
            };

            var redactorServiceOptions = new RedactorServiceOptions
            {
                PropertiesToRedact  = new string[] { "Password" },
                RegexValuesToRedact = new string[] { "Bearer" },
            };

            var options = Options.Create <RedactorServiceOptions>(redactorServiceOptions);

            var uut = new RedactorService(options);

            //Act
            var observed = uut.Redact(input);

            //Assert
            var objObserved = System.Text.Json.JsonSerializer.Deserialize <TestObject>(observed);

            Assert.AreEqual(usernameExpected, objObserved.Username);
            Assert.AreEqual(passwordExpected, objObserved.Password);
        }
        public async Task Configure_Runs_DecryptCalled()
        {
            var accountAPITestsOptions = new RedactorServiceOptions
            {
                PropertiesToRedact  = new string[] { "SampleProp" },
                RegexValuesToRedact = new string[] { "SampleRegexValues" }
            };
            var configurationRoot = BuildConfigurationRoot(accountAPITestsOptions);

            await RunDependencyInjectedTestAsync
            (
                async (serviceProvider) =>
            {
                //Setup

                //Act
                var options = serviceProvider.GetRequiredService <IOptions <RedactorServiceOptions> >().Value;

                //Assert
                Assert.IsNotNull(options);

                Assert.AreEqual(accountAPITestsOptions.PropertiesToRedact.Count(), options.PropertiesToRedact.Count());
                Assert.AreEqual(accountAPITestsOptions.PropertiesToRedact.First(), options.PropertiesToRedact.First());

                Assert.AreEqual(accountAPITestsOptions.RegexValuesToRedact.Count(), options.RegexValuesToRedact.Count());
                Assert.AreEqual(accountAPITestsOptions.RegexValuesToRedact.First(), options.RegexValuesToRedact.First());

                await Task.CompletedTask.ConfigureAwait(false);
            },
                serviceCollection => ConfigureServices(serviceCollection, configurationRoot)
            );
        }
コード例 #3
0
        public void RedactString_InputIsJson_ReturnsRedactedMessage()
        {
            //Setup
            var usernameExpected = "username";

            var input = @"{
                          ""Username"": ""username""
                        }";

            var redactorServiceOptions = new RedactorServiceOptions
            {
                PropertiesToRedact  = new string[] { "Password" },
                RegexValuesToRedact = new string[] { "Bearer" },
            };

            var options = Options.Create <RedactorServiceOptions>(redactorServiceOptions);

            var uut = new RedactorService(options);

            //Act
            var observed = uut.Redact(input);

            //Assert
            var objObserved = System.Text.Json.JsonSerializer.Deserialize <TestObject>(observed);

            Assert.AreEqual(usernameExpected, objObserved.Username);
            Assert.IsNull(objObserved.Password);
        }
コード例 #4
0
        public void RedactJToken_JTokenMixedInput_ReturnsRedactedJson()
        {
            //Setup
            var Jsoninput =
                @"{
                      ""TestObject"": {
                                          ""Username"": ""username"",
                                          ""Password"": ""password""
                                      },
                        ""Json"": ""{}"",
                        ""ValueToRedact"": ""Bearer""
                  }";

            var input = JToken.Parse(Jsoninput);
            var redactorServiceOptions = new RedactorServiceOptions
            {
                PropertiesToRedact  = new string[] { "Password" },
                RegexValuesToRedact = new string[] { "Bearer" },
            };

            var options = Options.Create <RedactorServiceOptions>(redactorServiceOptions);

            var uut = new RedactorService(options);

            //Act
            var observed = uut.Redact(input);

            //Assert
            Assert.AreEqual(3, observed.Count());
            Assert.AreEqual("{}", observed["Json"].ToString());
            Assert.AreEqual(RedactorService.REDACTED_REPLACEMENT_VALUE, observed["ValueToRedact"].ToString());
            Assert.AreEqual(2, observed["TestObject"].Count());
            Assert.AreEqual("username", observed["TestObject"]["Username"].ToString());
            Assert.AreEqual(RedactorService.REDACTED_REPLACEMENT_VALUE, observed["TestObject"]["Password"].ToString());
        }
コード例 #5
0
        public void RedactJToken_JTokenNodeHasRedactableValue_ReturnsRedactedJson()
        {
            //Setup
            var Jsoninput =
                @"{
                      ""TestObject"": {
                                          ""Password"": ""password""
                                      },
                  }";

            var input = JToken.Parse(Jsoninput);
            var redactorServiceOptions = new RedactorServiceOptions
            {
                PropertiesToRedact  = new string[] { "Password" },
                RegexValuesToRedact = new string[] { "Bearer" },
            };

            var options = Options.Create <RedactorServiceOptions>(redactorServiceOptions);

            var uut = new RedactorService(options);

            //Act
            var observed = uut.Redact(input);

            //Assert
            Assert.AreEqual(1, observed.Count());
            Assert.AreEqual(RedactorService.REDACTED_REPLACEMENT_VALUE, observed["TestObject"]["Password"].ToString());
        }
コード例 #6
0
        public void RedactJToken_JTokenContainsEmbededJson_ReturnsRedactedJson()
        {
            //Setup
            var Jsoninput =
                @"{
                     ""Json"": ""{}""
                  }";

            var input = JToken.Parse(Jsoninput);
            var redactorServiceOptions = new RedactorServiceOptions
            {
                PropertiesToRedact  = new string[] { "Password" },
                RegexValuesToRedact = new string[] { "Bearer" },
            };

            var options = Options.Create <RedactorServiceOptions>(redactorServiceOptions);

            var uut = new RedactorService(options);

            //Act
            var observed = uut.Redact(input);

            //Assert
            Assert.AreEqual(1, observed.Count());
            Assert.AreEqual("{}", observed["Json"].ToString());
        }
コード例 #7
0
        public void Parse_FirstCharIsNotJson_ReturnsNull()
        {
            //Setup
            var input = "A";

            var redactorServiceOptions = new RedactorServiceOptions();
            var options = Options.Create(redactorServiceOptions);
            var uut     = new RedactorService(options);

            //Act
            var observed = uut.Parse(input);

            //Assert
            Assert.IsNull(observed);
        }
コード例 #8
0
        public void Parse_InputIsNull_ReturnsNull()
        {
            //Setup
            var input = (string)null;

            var redactorServiceOptions = new RedactorServiceOptions();
            var options = Options.Create(redactorServiceOptions);
            var uut     = new RedactorService(options);

            //Act
            var observed = uut.Parse(input);

            //Assert
            Assert.IsNull(observed);
        }
コード例 #9
0
        public void Parse_InputIsJsonArray_ReturnsNull()
        {
            //Setup
            var input = "[0,1]";

            var redactorServiceOptions = new RedactorServiceOptions();
            var options = Options.Create(redactorServiceOptions);
            var uut     = new RedactorService(options);

            //Act
            var observed = uut.Parse(input);

            //Assert
            Assert.AreEqual(2, observed.Count());
            Assert.AreEqual(0, observed.First());
            Assert.AreEqual(1, observed.Last());
        }
コード例 #10
0
        public void IsRedactedValueWithJProperty_NoValueToRedactFound_ReturnsFalse()
        {
            //Setup
            var input = new JProperty("Password", "");

            var redactorServiceOptions = new RedactorServiceOptions
            {
            };

            var options = Options.Create <RedactorServiceOptions>(redactorServiceOptions);
            var uut     = new RedactorService(options);

            //Act
            var observed = uut.IsRedactedValue(input);

            //Assert
            Assert.IsFalse(observed);
        }
コード例 #11
0
        public void IsRedactedValueWithString_NoValueToRedactFound_ReturnsFalse()
        {
            //Setup
            var input = @"Bearer 1000";

            var redactorServiceOptions = new RedactorServiceOptions
            {
            };

            var options = Options.Create <RedactorServiceOptions>(redactorServiceOptions);
            var uut     = new RedactorService(options);

            //Act
            var observed = uut.IsRedactedValue(input);

            //Assert
            Assert.IsFalse(observed);
        }
コード例 #12
0
        public void Constructor_NullRedactOptions_EmptyRedactOptionsSet()
        {
            //Setup
            var redactorServiceOptions = new RedactorServiceOptions
            {
                PropertiesToRedact  = null,
                RegexValuesToRedact = null,
            };

            var options = Options.Create <RedactorServiceOptions>(redactorServiceOptions);

            //Act
            var uut = new RedactorService(options);

            //Assert
            Assert.IsNotNull(uut._propertiesToRedact);
            Assert.IsNotNull(uut._propertiesToRedact);
        }
コード例 #13
0
        public void Parse_InvaildJson_ReturnsNull()
        {
            //Setup
            var input = "{ In%^(:'&vaild }";

            var redactorServiceOptions = new RedactorServiceOptions();
            var options = Options.Create(redactorServiceOptions);
            var uut     = new RedactorService(options);

            //Act
            try
            {
                var observed = uut.Parse(input);
                Assert.Fail("Exception Expected");
            }
            catch
            {
            }
        }
コード例 #14
0
        public void Parse_InputIsJsonObject_ReturnsNull()
        {
            //Setup
            var input = @"{
                          ""Username"": ""username"",
                          ""Password"": ""password""
                        }";

            var redactorServiceOptions = new RedactorServiceOptions();
            var options = Options.Create(redactorServiceOptions);
            var uut     = new RedactorService(options);

            //Act
            var observed = uut.Parse(input);

            //Assert
            Assert.AreEqual(2, observed.Count());
            Assert.AreEqual("username", observed["Username"].ToString());
            Assert.AreEqual("password", observed["Password"].ToString());
        }
コード例 #15
0
        public void IsRedactedValueWithJProperty_ValueToRedactFound_ReturnsTrue()
        {
            //Setup
            var input = new JProperty("Password", "");

            var redactorServiceOptions = new RedactorServiceOptions
            {
                PropertiesToRedact  = new string[] { "Password" },
                RegexValuesToRedact = new string[] { "Bearer" },
            };

            var options = Options.Create <RedactorServiceOptions>(redactorServiceOptions);

            var uut = new RedactorService(options);

            //Act
            var observed = uut.IsRedactedValue(input);

            //Assert
            Assert.IsTrue(observed);
        }
コード例 #16
0
        public void RedactJToken_JTokenIsNull_ReturnsNull()
        {
            //Setup
            var input = (JToken)null;

            var redactorServiceOptions = new RedactorServiceOptions
            {
                PropertiesToRedact  = new string[] { },
                RegexValuesToRedact = new string[] { },
            };

            var options = Options.Create <RedactorServiceOptions>(redactorServiceOptions);

            var uut = new RedactorService(options);

            //Act
            var observed = uut.Redact(input);

            //Assert
            Assert.IsNull(observed);
        }
コード例 #17
0
        public void RedactString_InputIsNotJsonAndDoesNotHaveARedactedValue_ReturnsRedactedMessage()
        {
            //Setup
            var inputExpected = @"DemoValue";

            var redactorServiceOptions = new RedactorServiceOptions
            {
                PropertiesToRedact  = new string[] {  },
                RegexValuesToRedact = new string[] { },
            };

            var options = Options.Create <RedactorServiceOptions>(redactorServiceOptions);

            var uut = new RedactorService(options);

            //Act
            var observed = uut.Redact(inputExpected);

            //Assert
            Assert.AreEqual(inputExpected, observed);
        }
コード例 #18
0
        public void RedactObject_InputPassedAsInObjectButIsAString_ReturnsRedactedMessage()
        {
            //Setup
            var input            = (object)@"Bearer 1000";
            var expectedRedacted = RedactorService.REDACTED_REPLACEMENT_VALUE;

            var redactorServiceOptions = new RedactorServiceOptions
            {
                PropertiesToRedact  = new string[] { "Password" },
                RegexValuesToRedact = new string[] { "Bearer" },
            };

            var options = Options.Create <RedactorServiceOptions>(redactorServiceOptions);

            var uut = new RedactorService(options);

            //Act
            var observed = uut.Redact(input);

            //Assert
            Assert.AreEqual(expectedRedacted, observed);
        }