1- // ------------------------------------------------------------------------
1+ // ------------------------------------------------------------------------
22// Copyright 2021 The Dapr Authors
33// Licensed under the Apache License, Version 2.0 (the "License");
44// you may not use this file except in compliance with the License.
@@ -146,15 +146,15 @@ public void LoadSecrets_FromSecretStoreThatReturnsOneValue()
146146 var storeName = "store" ;
147147 var secretKey = "secretName" ;
148148 var secretValue = "secret" ;
149-
149+
150150 var secretDescriptors = new [ ]
151151 {
152152 new DaprSecretDescriptor ( secretKey ) ,
153153 } ;
154-
154+
155155 var daprClient = new Mock < DaprClient > ( ) ;
156-
157- daprClient . Setup ( c => c . GetSecretAsync ( storeName , secretKey ,
156+
157+ daprClient . Setup ( c => c . GetSecretAsync ( storeName , secretKey ,
158158 It . IsAny < Dictionary < string , string > > ( ) , default ) )
159159 . ReturnsAsync ( new Dictionary < string , string > { { secretKey , secretValue } } ) ;
160160
@@ -173,20 +173,20 @@ public void LoadSecrets_FromSecretStoreThatCanReturnsMultipleValues()
173173 var secondSecretKey = "second_secret" ;
174174 var firstSecretValue = "secret1" ;
175175 var secondSecretValue = "secret2" ;
176-
176+
177177 var secretDescriptors = new [ ]
178178 {
179179 new DaprSecretDescriptor ( firstSecretKey ) ,
180180 new DaprSecretDescriptor ( secondSecretKey ) ,
181181 } ;
182-
182+
183183 var daprClient = new Mock < DaprClient > ( ) ;
184-
185- daprClient . Setup ( c => c . GetSecretAsync ( storeName , firstSecretKey ,
184+
185+ daprClient . Setup ( c => c . GetSecretAsync ( storeName , firstSecretKey ,
186186 It . IsAny < Dictionary < string , string > > ( ) , default ) )
187187 . ReturnsAsync ( new Dictionary < string , string > { { firstSecretKey , firstSecretValue } } ) ;
188188
189- daprClient . Setup ( c => c . GetSecretAsync ( storeName , secondSecretKey ,
189+ daprClient . Setup ( c => c . GetSecretAsync ( storeName , secondSecretKey ,
190190 It . IsAny < Dictionary < string , string > > ( ) , default ) )
191191 . ReturnsAsync ( new Dictionary < string , string > { { secondSecretKey , secondSecretValue } } ) ;
192192
@@ -197,7 +197,7 @@ public void LoadSecrets_FromSecretStoreThatCanReturnsMultipleValues()
197197 config [ firstSecretKey ] . Should ( ) . Be ( firstSecretValue ) ;
198198 config [ secondSecretKey ] . Should ( ) . Be ( secondSecretValue ) ;
199199 }
200-
200+
201201 [ Fact ]
202202 public void LoadSecrets_FromSecretStoreWithADifferentSecretKeyAndName ( )
203203 {
@@ -208,13 +208,13 @@ public void LoadSecrets_FromSecretStoreWithADifferentSecretKeyAndName()
208208
209209 var secretDescriptors = new [ ]
210210 {
211- new DaprSecretDescriptor ( secretName , new Dictionary < string , string > ( ) , true ,
211+ new DaprSecretDescriptor ( secretName , new Dictionary < string , string > ( ) , true ,
212212 secretKey )
213213 } ;
214-
214+
215215 var daprClient = new Mock < DaprClient > ( ) ;
216-
217- daprClient . Setup ( c => c . GetSecretAsync ( storeName , secretKey ,
216+
217+ daprClient . Setup ( c => c . GetSecretAsync ( storeName , secretKey ,
218218 It . IsAny < Dictionary < string , string > > ( ) , default ) )
219219 . ReturnsAsync ( new Dictionary < string , string > { { secretKey , secretValue } } ) ;
220220
@@ -225,7 +225,70 @@ public void LoadSecrets_FromSecretStoreWithADifferentSecretKeyAndName()
225225 config [ secretName ] . Should ( ) . Be ( secretValue ) ;
226226 }
227227
228- //Here
228+ [ Fact ]
229+ public void LoadSecrets_FromSecretStoreNotRequiredAndDoesNotExist_ShouldNotThrowException ( )
230+ {
231+ var storeName = "store" ;
232+ var secretName = "ConnectionStrings:DatabaseConnStr" ;
233+
234+ var secretDescriptors = new [ ]
235+ {
236+ new DaprSecretDescriptor ( secretName , new Dictionary < string , string > ( ) , false )
237+ } ;
238+
239+ var httpClient = new TestHttpClient
240+ {
241+ Handler = async entry =>
242+ {
243+ await SendEmptyResponse ( entry ) ;
244+ }
245+ } ;
246+
247+ var daprClient = new DaprClientBuilder ( )
248+ . UseHttpClientFactory ( ( ) => httpClient )
249+ . Build ( ) ;
250+
251+ var config = CreateBuilder ( )
252+ . AddDaprSecretStore ( storeName , secretDescriptors , daprClient )
253+ . Build ( ) ;
254+
255+ config [ secretName ] . Should ( ) . BeNull ( ) ;
256+ }
257+
258+ [ Fact ]
259+ public void LoadSecrets_FromSecretStoreRequiredAndDoesNotExist_ShouldThrowException ( )
260+ {
261+ var storeName = "store" ;
262+ var secretName = "ConnectionStrings:DatabaseConnStr" ;
263+
264+ var secretDescriptors = new [ ]
265+ {
266+ new DaprSecretDescriptor ( secretName , new Dictionary < string , string > ( ) , true )
267+ } ;
268+
269+ var httpClient = new TestHttpClient
270+ {
271+ Handler = async entry =>
272+ {
273+ await SendEmptyResponse ( entry ) ;
274+ }
275+ } ;
276+
277+ var daprClient = new DaprClientBuilder ( )
278+ . UseHttpClientFactory ( ( ) => httpClient )
279+ . Build ( ) ;
280+
281+ var ex = Assert . Throws < DaprException > ( ( ) =>
282+ {
283+ var config = CreateBuilder ( )
284+ . AddDaprSecretStore ( storeName , secretDescriptors , daprClient )
285+ . Build ( ) ;
286+ } ) ;
287+
288+ Assert . Contains ( "Secret" , ex . Message ) ;
289+ }
290+
291+
229292 [ Fact ]
230293 public void AddDaprSecretStore_WithoutStore_ReportsError ( )
231294 {
@@ -598,7 +661,7 @@ await SendResponseWithSecrets(new Dictionary<string, string>()
598661 [ "otherSecretName≡value" ] = "secret" ,
599662 } , entry ) ;
600663 }
601- }
664+ }
602665 }
603666 } ;
604667
0 commit comments