Skip to content

Commit 2244375

Browse files
committed
Added tests to bring code coverage to 100%. Added codecov badge.
1 parent 297f372 commit 2244375

File tree

8 files changed

+138
-240
lines changed

8 files changed

+138
-240
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# SecureStore.Contrib.Configuration
22

3-
A [SecureStore](https://github.com/neosmart/SecureStore) configuration provider to use with .NET Core's `Microsoft.Extensions.Configuration`.
4-
53
[![Nuget (with prereleases)](https://img.shields.io/nuget/vpre/SecureStore.Contrib.Configuration?color=blue)](https://www.nuget.org/packages/SecureStore.Contrib.Configuration)
64
![build](https://github.com/gowon/SecureStore.Contrib.Configuration/workflows/build/badge.svg)
5+
[![codecov](https://codecov.io/gh/gowon/SecureStore.Contrib.Configuration/branch/master/graph/badge.svg)](https://codecov.io/gh/gowon/SecureStore.Contrib.Configuration)
6+
7+
A [SecureStore](https://github.com/neosmart/SecureStore) configuration provider to use with .NET Core's `Microsoft.Extensions.Configuration`.
78

89
## Installing via NuGet
910

src/SecureStore.Contrib.Configuration/SecureStoreConfigurationExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ public static class SecureStoreConfigurationExtensions
1010
public static IConfigurationBuilder AddSecureStoreFile(this IConfigurationBuilder builder, string path,
1111
string key, KeyType keyType)
1212
{
13-
return AddSecureStoreFile(builder, null, path, key, keyType, false, false);
13+
return AddSecureStoreFile(builder, path, key, keyType, false);
1414
}
1515

1616
public static IConfigurationBuilder AddSecureStoreFile(this IConfigurationBuilder builder, string path,
1717
string key, KeyType keyType, bool optional)
1818
{
19-
return AddSecureStoreFile(builder, null, path, key, keyType, optional, false);
19+
return AddSecureStoreFile(builder, path, key, keyType, optional, false);
2020
}
2121

2222
public static IConfigurationBuilder AddSecureStoreFile(this IConfigurationBuilder builder, string path,
@@ -42,7 +42,7 @@ public static IConfigurationBuilder AddSecureStoreFile(this IConfigurationBuilde
4242

4343
if (string.IsNullOrEmpty(key))
4444
{
45-
throw new ArgumentException("File path must be a non-empty string.", nameof(key));
45+
throw new ArgumentException("File key/path must be a non-empty string.", nameof(key));
4646
}
4747

4848
if (provider == null && Path.IsPathRooted(path))

src/SecureStore.Contrib.Configuration/SecureStoreConfigurationProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override void Load(Stream stream)
2727
manager.LoadKeyFromPassword(source.Key);
2828
break;
2929
default:
30-
throw new ArgumentOutOfRangeException();
30+
throw new ArgumentOutOfRangeException(nameof(source.KeyType));
3131
}
3232

3333
foreach (var key in manager.Keys)

test/SecureStore.Contrib.Configuration.Tests/SecureStore.Contrib.Configuration.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.1" />
11-
<PackageReference Include="Microsoft.Extensions.FileProviders.Abstractions" Version="3.1.1" />
10+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.2" />
11+
<PackageReference Include="Microsoft.Extensions.FileProviders.Abstractions" Version="3.1.2" />
1212
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
1313
<PackageReference Include="SecureStore" Version="1.0.3" />
1414
<PackageReference Include="xunit" Version="2.4.1" />

test/SecureStore.Contrib.Configuration.Tests/SecureStoreConfigurationExtensionsTests.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ namespace SecureStore.Contrib.Configuration.Tests
77

88
public class SecureStoreConfigurationExtensionsTests
99
{
10+
[Fact]
11+
public void AddSecureStoreFile_ThrowsIfBuilderIsNull()
12+
{
13+
// Arrange
14+
var path = "does-not-exist.json";
15+
var key = "password";
16+
17+
// Act and Assert
18+
var ex = Assert.Throws<ArgumentNullException>(() =>
19+
SecureStoreConfigurationExtensions.AddSecureStoreFile(null, path, key, KeyType.Password).Build());
20+
Assert.StartsWith("builder", ex.ParamName);
21+
}
22+
1023
[Theory]
1124
[InlineData(null)]
1225
[InlineData("")]
@@ -36,7 +49,7 @@ public void AddSecureStoreFile_ThrowsIfKeyIsNullOrEmpty(string key)
3649
var ex = Assert.Throws<ArgumentException>(() =>
3750
configurationBuilder.AddSecureStoreFile(path, key, KeyType.Password));
3851
Assert.Equal("key", ex.ParamName);
39-
Assert.StartsWith("File path must be a non-empty string.", ex.Message);
52+
Assert.StartsWith("File key/path must be a non-empty string.", ex.Message);
4053
}
4154

4255
[Fact]
@@ -64,6 +77,9 @@ public void AddSecureStoreFile_ThrowsIfFileDoesNotExistAtKey()
6477
var ex = Assert.Throws<FileNotFoundException>(() =>
6578
new ConfigurationBuilder().AddSecureStoreFile(path, keyPath, KeyType.File).Build());
6679
Assert.StartsWith($"Could not find file ", ex.Message);
80+
81+
// Cleanup
82+
File.Delete(path);
6783
}
6884
}
6985
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
namespace SecureStore.Contrib.Configuration.Tests
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using NeoSmart.SecureStore;
7+
using Xunit;
8+
9+
public class SecureStoreConfigurationProviderTests
10+
{
11+
private static string Password => "P@$$w0rD!";
12+
13+
private static readonly Dictionary<string, string> SecureData = new Dictionary<string, string>
14+
{
15+
{"foo1", "bar1"},
16+
{"foo2", "bar2"},
17+
{"foo3", "bar3"}
18+
};
19+
20+
private void CreateTestStore(string storePath, string key, KeyType type)
21+
{
22+
using (var sman = SecretsManager.CreateStore())
23+
{
24+
if (type == KeyType.Password)
25+
{
26+
sman.LoadKeyFromPassword(key);
27+
}
28+
else
29+
{
30+
sman.GenerateKey();
31+
}
32+
33+
foreach (var secretKey in SecureData.Keys)
34+
{
35+
sman.Set(secretKey, SecureData[secretKey]);
36+
}
37+
38+
sman.SaveStore(storePath);
39+
sman.ExportKey(key);
40+
}
41+
}
42+
43+
[Fact]
44+
public void LoadStreamUsingKeyFile()
45+
{
46+
var storePath = Path.GetTempFileName();
47+
var keyPath = Path.GetTempFileName();
48+
49+
CreateTestStore(storePath, keyPath, KeyType.File);
50+
51+
var provider = new SecureStoreConfigurationProvider(new SecureStoreConfigurationSource
52+
{
53+
KeyType = KeyType.File,
54+
Key = keyPath,
55+
Optional = true
56+
});
57+
58+
using (var stream = new FileStream(storePath, FileMode.Open, FileAccess.Read))
59+
{
60+
provider.Load(stream);
61+
}
62+
63+
File.Delete(storePath);
64+
File.Delete(keyPath);
65+
}
66+
67+
[Fact]
68+
public void LoadStreamUsingPassword()
69+
{
70+
var storePath = Path.GetTempFileName();
71+
CreateTestStore(storePath, Password, KeyType.Password);
72+
73+
var provider = new SecureStoreConfigurationProvider(new SecureStoreConfigurationSource
74+
{
75+
KeyType = KeyType.Password,
76+
Key = Password,
77+
Optional = true
78+
});
79+
80+
using (var stream = new FileStream(storePath, FileMode.Open, FileAccess.Read))
81+
{
82+
provider.Load(stream);
83+
}
84+
85+
File.Delete(storePath);
86+
}
87+
88+
[Fact]
89+
public void LoadStreamUsingPassword_ThrowsIfKeyTypeNotInRange()
90+
{
91+
var storePath = Path.GetTempFileName();
92+
CreateTestStore(storePath, Password, KeyType.Password);
93+
94+
var source = new SecureStoreConfigurationSource
95+
{
96+
KeyType = (KeyType) 3,
97+
Key = Password,
98+
Optional = true
99+
};
100+
var provider = new SecureStoreConfigurationProvider(source);
101+
102+
using (var stream = new FileStream(storePath, FileMode.Open, FileAccess.Read))
103+
{
104+
var ex = Assert.Throws<ArgumentOutOfRangeException>(() =>
105+
provider.Load(stream));
106+
Assert.Equal(nameof(source.KeyType), ex.ParamName);
107+
}
108+
109+
File.Delete(storePath);
110+
}
111+
}
112+
}

test/SecureStore.Contrib.Configuration.Tests/SecureStoreConfigurationTests.cs

Lines changed: 0 additions & 104 deletions
This file was deleted.

0 commit comments

Comments
 (0)