Skip to content

Commit

Permalink
Add tests for required/not required exception handling
Browse files Browse the repository at this point in the history
Signed-off-by: James Croft <jamz_c@hotmail.co.uk>
  • Loading branch information
jamesmcroft committed Feb 9, 2024
1 parent 397572e commit bed18d5
Showing 1 changed file with 80 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// Copyright 2021 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -146,15 +146,15 @@ public void LoadSecrets_FromSecretStoreThatReturnsOneValue()
var storeName = "store";
var secretKey = "secretName";
var secretValue = "secret";

var secretDescriptors = new[]
{
new DaprSecretDescriptor(secretKey),
};

var daprClient = new Mock<DaprClient>();
daprClient.Setup(c => c.GetSecretAsync(storeName, secretKey,

daprClient.Setup(c => c.GetSecretAsync(storeName, secretKey,
It.IsAny<Dictionary<string, string>>(), default))
.ReturnsAsync(new Dictionary<string, string> { { secretKey, secretValue } });

Expand All @@ -173,20 +173,20 @@ public void LoadSecrets_FromSecretStoreThatCanReturnsMultipleValues()
var secondSecretKey = "second_secret";
var firstSecretValue = "secret1";
var secondSecretValue = "secret2";

var secretDescriptors = new[]
{
new DaprSecretDescriptor(firstSecretKey),
new DaprSecretDescriptor(secondSecretKey),
};

var daprClient = new Mock<DaprClient>();
daprClient.Setup(c => c.GetSecretAsync(storeName, firstSecretKey,

daprClient.Setup(c => c.GetSecretAsync(storeName, firstSecretKey,
It.IsAny<Dictionary<string, string>>(), default))
.ReturnsAsync(new Dictionary<string, string> { { firstSecretKey, firstSecretValue } });

daprClient.Setup(c => c.GetSecretAsync(storeName, secondSecretKey,
daprClient.Setup(c => c.GetSecretAsync(storeName, secondSecretKey,
It.IsAny<Dictionary<string, string>>(), default))
.ReturnsAsync(new Dictionary<string, string> { { secondSecretKey, secondSecretValue } });

Expand All @@ -197,7 +197,7 @@ public void LoadSecrets_FromSecretStoreThatCanReturnsMultipleValues()
config[firstSecretKey].Should().Be(firstSecretValue);
config[secondSecretKey].Should().Be(secondSecretValue);
}

[Fact]
public void LoadSecrets_FromSecretStoreWithADifferentSecretKeyAndName()
{
Expand All @@ -208,13 +208,13 @@ public void LoadSecrets_FromSecretStoreWithADifferentSecretKeyAndName()

var secretDescriptors = new[]
{
new DaprSecretDescriptor(secretName, new Dictionary<string, string>(), true,
new DaprSecretDescriptor(secretName, new Dictionary<string, string>(), true,
secretKey)
};

var daprClient = new Mock<DaprClient>();
daprClient.Setup(c => c.GetSecretAsync(storeName, secretKey,

daprClient.Setup(c => c.GetSecretAsync(storeName, secretKey,
It.IsAny<Dictionary<string, string>>(), default))
.ReturnsAsync(new Dictionary<string, string> { { secretKey, secretValue } });

Expand All @@ -225,7 +225,70 @@ public void LoadSecrets_FromSecretStoreWithADifferentSecretKeyAndName()
config[secretName].Should().Be(secretValue);
}

//Here
[Fact]
public void LoadSecrets_FromSecretStoreNotRequiredAndDoesNotExist_ShouldNotThrowException()
{
var storeName = "store";
var secretName = "ConnectionStrings:DatabaseConnStr";

var secretDescriptors = new[]
{
new DaprSecretDescriptor(secretName, new Dictionary<string, string>(), false)
};

var httpClient = new TestHttpClient
{
Handler = async entry =>
{
await SendEmptyResponse(entry);
}
};

var daprClient = new DaprClientBuilder()
.UseHttpClientFactory(() => httpClient)
.Build();

var config = CreateBuilder()
.AddDaprSecretStore(storeName, secretDescriptors, daprClient)
.Build();

config[secretName].Should().BeNull();
}

[Fact]
public void LoadSecrets_FromSecretStoreRequiredAndDoesNotExist_ShouldThrowException()
{
var storeName = "store";
var secretName = "ConnectionStrings:DatabaseConnStr";

var secretDescriptors = new[]
{
new DaprSecretDescriptor(secretName, new Dictionary<string, string>(), true)
};

var httpClient = new TestHttpClient
{
Handler = async entry =>
{
await SendEmptyResponse(entry);
}
};

var daprClient = new DaprClientBuilder()
.UseHttpClientFactory(() => httpClient)
.Build();

var ex = Assert.Throws<DaprException>(() =>
{
var config = CreateBuilder()
.AddDaprSecretStore(storeName, secretDescriptors, daprClient)
.Build();
});

Assert.Contains("Secret", ex.Message);
}


[Fact]
public void AddDaprSecretStore_WithoutStore_ReportsError()
{
Expand Down Expand Up @@ -598,7 +661,7 @@ await SendResponseWithSecrets(new Dictionary<string, string>()
["otherSecretName≡value"] = "secret",
}, entry);
}
}
}
}
};

Expand Down

0 comments on commit bed18d5

Please sign in to comment.