77using System . Linq ;
88using System . Security . Cryptography ;
99using System . Threading . Tasks ;
10+ using Microsoft . Azure . WebJobs . Extensions . Http ;
1011using Microsoft . Azure . WebJobs . Logging ;
1112using Microsoft . Azure . WebJobs . Script . Diagnostics ;
1213using Microsoft . Azure . WebJobs . Script . WebHost ;
@@ -63,8 +64,8 @@ public async Task CachedSecrets_UsedWhenPresent()
6364 var functionSecrets = await secretManager . GetFunctionSecretsAsync ( "function1" , true ) ;
6465
6566 Assert . Equal ( 4 , functionSecrets . Count ) ;
66- Assert . Equal ( "function1value " , functionSecrets [ "test-function -1" ] ) ;
67- Assert . Equal ( "function2value " , functionSecrets [ "test-function -2" ] ) ;
67+ Assert . Equal ( "function1value1 " , functionSecrets [ "test-function1 -1" ] ) ;
68+ Assert . Equal ( "function1value2 " , functionSecrets [ "test-function1 -2" ] ) ;
6869 Assert . Equal ( "hostfunction1value" , functionSecrets [ "test-host-function-1" ] ) ;
6970 Assert . Equal ( "hostfunction2value" , functionSecrets [ "test-host-function-2" ] ) ;
7071
@@ -87,6 +88,46 @@ public async Task CachedSecrets_UsedWhenPresent()
8788 }
8889 }
8990
91+ [ Theory ]
92+ [ InlineData ( "function1value1" , "test-function1-1" , "function1" , AuthorizationLevel . Function ) ]
93+ [ InlineData ( "function1value2" , "test-function1-2" , "function1" , AuthorizationLevel . Function ) ]
94+ [ InlineData ( "function2value1" , "test-function2-1" , "function2" , AuthorizationLevel . Function ) ]
95+ [ InlineData ( "function2value2" , "test-function2-2" , "function2" , AuthorizationLevel . Function ) ]
96+ [ InlineData ( "function2value1" , null , "function1" , AuthorizationLevel . Anonymous ) ]
97+ [ InlineData ( "function1value1" , null , "function2" , AuthorizationLevel . Anonymous ) ]
98+ [ InlineData ( "invalid" , null , "function1" , AuthorizationLevel . Anonymous ) ]
99+ [ InlineData ( "invalid" , null , "function2" , AuthorizationLevel . Anonymous ) ]
100+ [ InlineData ( "hostfunction1value" , "test-host-function-1" , "function1" , AuthorizationLevel . Function ) ]
101+ [ InlineData ( "hostfunction2value" , "test-host-function-2" , "function1" , AuthorizationLevel . Function ) ]
102+ [ InlineData ( "hostfunction1value" , "test-host-function-1" , "function2" , AuthorizationLevel . Function ) ]
103+ [ InlineData ( "hostfunction2value" , "test-host-function-2" , "function2" , AuthorizationLevel . Function ) ]
104+ [ InlineData ( "test-master-key" , "master" , "function1" , AuthorizationLevel . Admin ) ]
105+ [ InlineData ( "test-master-key" , "master" , "function2" , AuthorizationLevel . Admin ) ]
106+ [ InlineData ( "test-master-key" , "master" , null , AuthorizationLevel . Admin ) ]
107+ [ InlineData ( "system1value" , "test-system-1" , null , AuthorizationLevel . System ) ]
108+ [ InlineData ( "system2value" , "test-system-2" , null , AuthorizationLevel . System ) ]
109+ public async Task GetAuthorizationLevelOrNullAsync_ReturnsExpectedResult ( string keyValue , string expectedKeyName , string functionName , AuthorizationLevel expectedLevel )
110+ {
111+ using ( var directory = new TempDirectory ( ) )
112+ {
113+ string startupContextPath = Path . Combine ( directory . Path , Guid . NewGuid ( ) . ToString ( ) ) ;
114+ _testEnvironment . SetEnvironmentVariable ( EnvironmentSettingNames . AzureWebsiteStartupContextCache , startupContextPath ) ;
115+ _testEnvironment . SetEnvironmentVariable ( EnvironmentSettingNames . WebSiteAuthEncryptionKey , TestEncryptionKey ) ;
116+
117+ WriteStartContextCache ( startupContextPath ) ;
118+
119+ using ( var secretManager = CreateSecretManager ( directory . Path ) )
120+ {
121+ for ( int i = 0 ; i < 3 ; i ++ )
122+ {
123+ ( string , AuthorizationLevel ) result = await secretManager . GetAuthorizationLevelOrNullAsync ( keyValue , functionName ) ;
124+ Assert . Equal ( result . Item2 , expectedLevel ) ;
125+ Assert . Equal ( result . Item1 , expectedKeyName ) ;
126+ }
127+ }
128+ }
129+ }
130+
90131 private FunctionAppSecrets WriteStartContextCache ( string path )
91132 {
92133 var secrets = new FunctionAppSecrets ( ) ;
@@ -111,17 +152,17 @@ private FunctionAppSecrets WriteStartContextCache(string path)
111152 Name = "function1" ,
112153 Secrets = new Dictionary < string , string >
113154 {
114- { "test-function -1" , "function1value " } ,
115- { "test-function -2" , "function2value " }
155+ { "test-function1 -1" , "function1value1 " } ,
156+ { "test-function1 -2" , "function1value2 " }
116157 }
117158 } ,
118159 new FunctionAppSecrets . FunctionSecrets
119160 {
120161 Name = "function2" ,
121162 Secrets = new Dictionary < string , string >
122163 {
123- { "test-function -1" , "function1value " } ,
124- { "test-function -2" , "function2value " }
164+ { "test-function2 -1" , "function2value1 " } ,
165+ { "test-function2 -2" , "function2value2 " }
125166 }
126167 }
127168 } ;
0 commit comments