-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubstitutingConfigurationFactoryTest.cs
More file actions
63 lines (57 loc) · 2.6 KB
/
Copy pathSubstitutingConfigurationFactoryTest.cs
File metadata and controls
63 lines (57 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using EncryptedConfigValue.AspNetCore.Test.Util;
using EncryptedConfigValue.Crypto;
using EncryptedConfigValue.Crypto.Util;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xunit;
using Shouldly;
using System.Text.RegularExpressions;
namespace EncryptedConfigValue.AspNetCore.Test
{
public class SubstitutingConfigurationFactoryTest
{
static SubstitutingConfigurationFactoryTest()
{
System.Environment.SetEnvironmentVariable(KeyFileUtils.KeyPathProperty, Path.Combine("Resources", "test.key"));
}
[Fact]
public void TestDecryptionSucceeds()
{
var configuration = new ConfigurationBuilder()
.AddJsonFile(Path.Combine("Resources", "testConfig.json"), optional: false, reloadOnChange: false)
.AddEncryptedConfigValueProvider()
.Build();
configuration["Unencrypted"].ShouldBe("value");
configuration["Encrypted"].ShouldBe("value");
configuration["EncryptedWithSingleQuote"].ShouldBe("don't use quotes");
configuration["EncryptedWithDoubleQuote"].ShouldBe("double quote is \"");
configuration["EncryptedMalformedYaml"].ShouldBe("[oh dear");
configuration
.GetSection("ArrayWithSomeEncryptedValues")
.GetChildren()
.Select(x => x.Value)
.ToList()
.ShouldBeEquivalentTo(new List<string> { "value", "value", "other value", "[oh dear" });
var person = new Person();
configuration
.GetSection("PocoWithEncryptedValues")
.Bind(person);
person.Username.ShouldBeEquivalentTo("some-user");
person.Password.ShouldBeEquivalentTo("value");
}
[Fact]
public void TestDecryptionFailsWithNiceMessage()
{
var act = () => new ConfigurationBuilder()
.AddJsonFile(Path.Combine("Resources", "testConfigWithError.json"), optional: false, reloadOnChange: false)
.AddEncryptedConfigValueProvider()
.Build();
var ex = Should.Throw<ConfigurationDecryptionException>(act);
ex.Message.ShouldMatch($@"Configuration decryption error at {Regex.Escape(Path.Combine("Resources", "testConfigWithError.json"))} \(.*\)");
ex.InnerException.ShouldBeOfType<StringSubstitutionException>();
ex.InnerException.Message.ShouldBe("The value 'enc:ERROR' for field 'ArrayWithSomeEncryptedValues:3' could not be replaced");
}
}
}