27
27
using Microsoft . Azure . Commands . Common . Authentication . Models ;
28
28
using Microsoft . Azure . Commands . Profile ;
29
29
using Microsoft . Azure . Commands . Profile . Models ;
30
+ using Microsoft . Azure . Subscriptions . Models ;
31
+ using Hyak . Common ;
32
+ using System . Management . Automation ;
30
33
31
34
namespace Microsoft . Azure . Commands . ResourceManager . Common . Test
32
35
{
@@ -67,6 +70,180 @@ private static RMProfileClient SetupTestEnvironment(List<string> tenants, params
67
70
return new RMProfileClient ( profile ) ;
68
71
}
69
72
73
+ [ Fact ]
74
+ [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
75
+ public void SpecifyTenantAndSubscriptionIdSucceed ( )
76
+ {
77
+ var tenants = new List < string > { DefaultTenant . ToString ( ) } ;
78
+ var firstList = new List < string > { DefaultSubscription . ToString ( ) , Guid . NewGuid ( ) . ToString ( ) } ;
79
+ var secondList = new List < string > { Guid . NewGuid ( ) . ToString ( ) } ;
80
+ var client = SetupTestEnvironment ( tenants , firstList , secondList ) ;
81
+
82
+ ( ( MockTokenAuthenticationFactory ) AzureSession . AuthenticationFactory ) . TokenProvider = ( account , environment , tenant ) =>
83
+ new MockAccessToken
84
+ {
85
+
86
+ LoginType = LoginType . OrgId ,
87
+ AccessToken = "bbb" ,
88
+ TenantId = DefaultTenant . ToString ( )
89
+ } ;
90
+
91
+ var azureRmProfile = client . Login (
92
+ Context . Account ,
93
+ Context . Environment ,
94
+ DefaultTenant . ToString ( ) ,
95
+ DefaultSubscription . ToString ( ) ,
96
+ null ,
97
+ null ) ;
98
+ }
99
+
100
+ [ Fact ]
101
+ [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
102
+ public void SubscriptionIdNotExist ( )
103
+ {
104
+ var tenants = new List < string > { DefaultTenant . ToString ( ) } ;
105
+ var firstList = new List < string > { Guid . NewGuid ( ) . ToString ( ) } ;
106
+ var client = SetupTestEnvironment ( tenants , firstList ) ;
107
+
108
+ ( ( MockTokenAuthenticationFactory ) AzureSession . AuthenticationFactory ) . TokenProvider = ( account , environment , tenant ) =>
109
+ new MockAccessToken
110
+ {
111
+
112
+ LoginType = LoginType . OrgId ,
113
+ AccessToken = "bbb" ,
114
+ TenantId = DefaultTenant . ToString ( )
115
+ } ;
116
+
117
+ var getAsyncResponses = new Queue < Func < GetSubscriptionResult > > ( ) ;
118
+ getAsyncResponses . Enqueue ( ( ) =>
119
+ {
120
+ throw new CloudException ( "InvalidAuthenticationTokenTenant: The access token is from the wrong issuer" ) ;
121
+ } ) ;
122
+ MockSubscriptionClientFactory . SetGetAsyncResponses ( getAsyncResponses ) ;
123
+
124
+ Assert . Throws < PSInvalidOperationException > ( ( ) => client . Login (
125
+ Context . Account ,
126
+ Context . Environment ,
127
+ null ,
128
+ DefaultSubscription . ToString ( ) ,
129
+ null ,
130
+ null ) ) ;
131
+ }
132
+
133
+ [ Fact ]
134
+ [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
135
+ public void SpecifyTenantAndNotExistingSubscriptionId ( )
136
+ {
137
+ var tenants = new List < string > { DefaultTenant . ToString ( ) } ;
138
+ var firstList = new List < string > { Guid . NewGuid ( ) . ToString ( ) , Guid . NewGuid ( ) . ToString ( ) } ;
139
+ var secondList = new List < string > { Guid . NewGuid ( ) . ToString ( ) } ;
140
+ var client = SetupTestEnvironment ( tenants , firstList , secondList ) ;
141
+
142
+ ( ( MockTokenAuthenticationFactory ) AzureSession . AuthenticationFactory ) . TokenProvider = ( account , environment , tenant ) =>
143
+ new MockAccessToken
144
+ {
145
+
146
+ LoginType = LoginType . OrgId ,
147
+ AccessToken = "bbb" ,
148
+ TenantId = DefaultTenant . ToString ( )
149
+ } ;
150
+
151
+ Assert . Throws < PSInvalidOperationException > ( ( ) => client . Login (
152
+ Context . Account ,
153
+ Context . Environment ,
154
+ DefaultTenant . ToString ( ) ,
155
+ DefaultSubscription . ToString ( ) ,
156
+ null ,
157
+ null ) ) ;
158
+ }
159
+
160
+ [ Fact ]
161
+ [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
162
+ public void SubscriptionIdNotInFirstTenant ( )
163
+ {
164
+ var tenants = new List < string > { DefaultTenant . ToString ( ) , Guid . NewGuid ( ) . ToString ( ) } ;
165
+ var subscriptionInSecondTenant = Guid . NewGuid ( ) . ToString ( ) ;
166
+ var firstList = new List < string > { DefaultSubscription . ToString ( ) } ;
167
+ var secondList = new List < string > { Guid . NewGuid ( ) . ToString ( ) , subscriptionInSecondTenant } ;
168
+ var client = SetupTestEnvironment ( tenants , firstList , secondList ) ;
169
+
170
+ ( ( MockTokenAuthenticationFactory ) AzureSession . AuthenticationFactory ) . TokenProvider = ( account , environment , tenant ) =>
171
+ new MockAccessToken
172
+ {
173
+
174
+ LoginType = LoginType . OrgId ,
175
+ AccessToken = "bbb" ,
176
+ TenantId = DefaultTenant . ToString ( )
177
+ } ;
178
+
179
+ var getAsyncResponses = new Queue < Func < GetSubscriptionResult > > ( ) ;
180
+ getAsyncResponses . Enqueue ( ( ) =>
181
+ {
182
+ throw new CloudException ( "InvalidAuthenticationTokenTenant: The access token is from the wrong issuer" ) ;
183
+ } ) ;
184
+ MockSubscriptionClientFactory . SetGetAsyncResponses ( getAsyncResponses ) ;
185
+
186
+ var azureRmProfile = client . Login (
187
+ Context . Account ,
188
+ Context . Environment ,
189
+ null ,
190
+ subscriptionInSecondTenant ,
191
+ null ,
192
+ null ) ;
193
+ }
194
+
195
+ [ Fact ]
196
+ [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
197
+ public void SubscriptionNameNotInFirstTenant ( )
198
+ {
199
+ var tenants = new List < string > { DefaultTenant . ToString ( ) , Guid . NewGuid ( ) . ToString ( ) } ;
200
+ var subscriptionInSecondTenant = Guid . NewGuid ( ) . ToString ( ) ;
201
+ var firstList = new List < string > { DefaultSubscription . ToString ( ) } ;
202
+ var secondList = new List < string > { Guid . NewGuid ( ) . ToString ( ) , subscriptionInSecondTenant } ;
203
+ var client = SetupTestEnvironment ( tenants , firstList , secondList ) ;
204
+
205
+ ( ( MockTokenAuthenticationFactory ) AzureSession . AuthenticationFactory ) . TokenProvider = ( account , environment , tenant ) =>
206
+ new MockAccessToken
207
+ {
208
+
209
+ LoginType = LoginType . OrgId ,
210
+ AccessToken = "bbb" ,
211
+ TenantId = DefaultTenant . ToString ( )
212
+ } ;
213
+
214
+ var listAsyncResponses = new Queue < Func < SubscriptionListResult > > ( ) ;
215
+ listAsyncResponses . Enqueue ( ( ) =>
216
+ {
217
+ var sub1 = new Subscription
218
+ {
219
+ Id = DefaultSubscription . ToString ( ) ,
220
+ SubscriptionId = DefaultSubscription . ToString ( ) ,
221
+ DisplayName = DefaultSubscriptionName ,
222
+ State = "enabled"
223
+ } ;
224
+ var sub2 = new Subscription
225
+ {
226
+ Id = subscriptionInSecondTenant ,
227
+ SubscriptionId = subscriptionInSecondTenant ,
228
+ DisplayName = MockSubscriptionClientFactory . GetSubscriptionNameFromId ( subscriptionInSecondTenant ) ,
229
+ State = "enabled"
230
+ } ;
231
+ return new SubscriptionListResult
232
+ {
233
+ Subscriptions = new List < Subscription > { sub1 , sub2 }
234
+ } ;
235
+ } ) ;
236
+ MockSubscriptionClientFactory . SetListAsyncResponses ( listAsyncResponses ) ;
237
+
238
+ var azureRmProfile = client . Login (
239
+ Context . Account ,
240
+ Context . Environment ,
241
+ null ,
242
+ null ,
243
+ MockSubscriptionClientFactory . GetSubscriptionNameFromId ( subscriptionInSecondTenant ) ,
244
+ null ) ;
245
+ }
246
+
70
247
[ Fact ]
71
248
[ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
72
249
public void TokenIdAndAccountIdMismatch ( )
@@ -78,7 +255,6 @@ public void TokenIdAndAccountIdMismatch()
78
255
var thirdList = new List < string > { DefaultSubscription . ToString ( ) , secondsubscriptionInTheFirstTenant } ;
79
256
var fourthList = new List < string > { DefaultSubscription . ToString ( ) , secondsubscriptionInTheFirstTenant } ;
80
257
var client = SetupTestEnvironment ( tenants , firstList , secondList , thirdList , fourthList ) ;
81
-
82
258
var tokens = new Queue < MockAccessToken > ( ) ;
83
259
tokens . Enqueue ( new MockAccessToken
84
260
{
0 commit comments