12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
14
15
+ using System ;
16
+ using System . CodeDom ;
17
+ using System . Collections . Generic ;
18
+ using System . Linq ;
19
+ using System . Linq . Expressions ;
15
20
using Microsoft . Azure . Commands . Common . Authentication ;
16
21
using Microsoft . Azure . Commands . Common . Authentication . Models ;
17
22
using Microsoft . Azure . Commands . Profile ;
25
30
using System . Management . Automation ;
26
31
using Xunit ;
27
32
using Xunit . Abstractions ;
33
+ using Xunit . Sdk ;
28
34
29
35
namespace Microsoft . Azure . Commands . ResourceManager . Profile . Test
30
36
{
31
37
public class ContextCmdletTests : RMTestBase
32
38
{
33
39
private MemoryDataStore dataStore ;
34
40
private MockCommandRuntime commandRuntimeMock ;
35
-
41
+ const string guid1 = "a0cc8bd7-2c6a-47e9-a4c4-3f6ed136e240" ;
42
+ const string guid2 = "eab635c0-a35a-4f70-9e46-e5351c7b5c8b" ;
43
+ const string guid3 = "52f66548-2550-417b-941e-9d6e04f3ac8d" ;
44
+ const string guid4 = "40e67ee2-1a1a-4517-9253-ab6f93c5710f" ;
36
45
public ContextCmdletTests ( ITestOutputHelper output )
37
46
{
38
47
XunitTracingInterceptor . AddToContext ( new XunitTracingInterceptor ( output ) ) ;
@@ -78,8 +87,15 @@ public void SelectAzureContextWithNoSubscriptionAndTenant()
78
87
var existingTenants = account . GetProperty ( AzureAccount . Property . Tenants ) ;
79
88
var allowedTenants = existingTenants == null ? tenantToSet : existingTenants + "," + tenantToSet ;
80
89
account . SetProperty ( AzureAccount . Property . Tenants , allowedTenants ) ;
90
+ account . SetProperty ( AzureAccount . Property . Subscriptions , new string [ 0 ] ) ;
81
91
82
- ( ( RuntimeDefinedParameterDictionary ) cmdlt . GetDynamicParameters ( ) ) [ "TenantId" ] . Value = tenantToSet ;
92
+ var paramDictionary =
93
+ ( ( RuntimeDefinedParameterDictionary ) cmdlt . GetDynamicParameters ( ) ) ;
94
+ var tenantParam = paramDictionary [ "TenantId" ] ;
95
+ Assert . True ( tenantParam . Attributes . Any ( a => a is ValidateSetAttribute
96
+ && ( ( ValidateSetAttribute ) a ) . ValidValues . Any ( v => string . Equals ( v , tenantToSet , StringComparison . OrdinalIgnoreCase ) ) ) ) ;
97
+ Assert . False ( paramDictionary [ "SubscriptionId" ] . Attributes . Any ( a => a is ValidateSetAttribute ) ) ;
98
+ tenantParam . Value = tenantToSet ;
83
99
84
100
// Act
85
101
cmdlt . InvokeBeginProcessing ( ) ;
@@ -94,6 +110,50 @@ public void SelectAzureContextWithNoSubscriptionAndTenant()
94
110
Assert . NotEqual ( tenantToSet , context . Tenant . TenantId ) ;
95
111
}
96
112
113
+ [ Theory ]
114
+ [ InlineData ( null , null ) ]
115
+ [ InlineData ( new string [ 0 ] , new string [ 0 ] ) ]
116
+ [ InlineData ( new string [ ] { guid1 } , new string [ ] { guid2 } ) ]
117
+ [ InlineData ( new string [ ] { guid1 , guid2 } , new string [ ] { guid3 , guid4 } ) ]
118
+ [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
119
+ public void SetsDynamicParametersForContext ( string [ ] subscriptions , string [ ] tenants )
120
+ {
121
+ var cmdlt = new SetAzureRMContextCommand ( ) ;
122
+
123
+ // Setup
124
+ cmdlt . CommandRuntime = commandRuntimeMock ;
125
+
126
+ // Make sure that the tenant ID we are attempting to set is
127
+ // valid for the account
128
+ var account = AzureRmProfileProvider . Instance . Profile . Context . Account ;
129
+ account . SetProperty ( AzureAccount . Property . Tenants , tenants ) ;
130
+ account . SetProperty ( AzureAccount . Property . Subscriptions , subscriptions ) ;
131
+
132
+ var paramDictionary =
133
+ ( ( RuntimeDefinedParameterDictionary ) cmdlt . GetDynamicParameters ( ) ) ;
134
+ var subscriptionParams = paramDictionary [ "SubscriptionId" ] ;
135
+ VerifyValidationAttribute ( subscriptionParams , subscriptions ) ;
136
+ var tenantParams = paramDictionary [ "TenantId" ] ;
137
+ VerifyValidationAttribute ( tenantParams , tenants ) ;
138
+ }
139
+
140
+ private void VerifyValidationAttribute ( RuntimeDefinedParameter parameter , string [ ] expectedValues )
141
+ {
142
+ if ( expectedValues != null && expectedValues . Length > 0 )
143
+ {
144
+ var validateAttribute = parameter . Attributes . First ( a => a is ValidateSetAttribute ) as ValidateSetAttribute ;
145
+ Assert . NotNull ( validateAttribute ) ;
146
+ foreach ( var expectedValue in expectedValues )
147
+ {
148
+ Assert . Contains ( expectedValue , validateAttribute . ValidValues , StringComparer . OrdinalIgnoreCase ) ;
149
+ }
150
+ }
151
+ else
152
+ {
153
+ Assert . False ( parameter . Attributes . Any ( a => a is ValidateSetAttribute ) ) ;
154
+ }
155
+ }
156
+
97
157
[ Fact ]
98
158
[ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
99
159
public void SelectAzureContextWithNoSubscriptionAndNoTenant ( )
@@ -113,5 +173,7 @@ public void SelectAzureContextWithNoSubscriptionAndNoTenant()
113
173
var context = ( PSAzureContext ) commandRuntimeMock . OutputPipeline [ 0 ] ;
114
174
Assert . NotNull ( context ) ;
115
175
}
176
+
177
+
116
178
}
117
179
}
0 commit comments