@@ -4,9 +4,10 @@ import os from "os";
4
4
import path from "path" ;
5
5
import uuid from "uuid/v4" ;
6
6
import { createTempDir } from "../../lib/ioUtil" ;
7
- import { DEFAULT_PROJECT_NAME , WORKSPACE } from "./constants" ;
8
- import { getAnswerFromFile , prompt } from "./prompt" ;
7
+ import { DEFAULT_PROJECT_NAME , IRequestContext , WORKSPACE } from "./constants" ;
8
+ import { getAnswerFromFile , prompt , promptForSubscriptionId } from "./prompt" ;
9
9
import * as servicePrincipalService from "./servicePrincipalService" ;
10
+ import * as subscriptionService from "./subscriptionService" ;
10
11
11
12
describe ( "test prompt function" , ( ) => {
12
13
it ( "positive test: No App Creation" , async ( ) => {
@@ -40,11 +41,19 @@ describe("test prompt function", () => {
40
41
jest
41
42
. spyOn ( servicePrincipalService , "createWithAzCLI" )
42
43
. mockReturnValueOnce ( Promise . resolve ( ) ) ;
44
+ jest . spyOn ( subscriptionService , "getSubscriptions" ) . mockResolvedValueOnce ( [
45
+ {
46
+ id : "72f988bf-86f1-41af-91ab-2d7cd011db48" ,
47
+ name : "test"
48
+ }
49
+ ] ) ;
50
+
43
51
const ans = await prompt ( ) ;
44
52
expect ( ans ) . toStrictEqual ( {
45
53
accessToken : "pat" ,
46
54
orgName : "org" ,
47
55
projectName : "project" ,
56
+ subscriptionId : "72f988bf-86f1-41af-91ab-2d7cd011db48" ,
48
57
toCreateAppRepo : true ,
49
58
toCreateSP : true ,
50
59
workspace : WORKSPACE
@@ -66,6 +75,12 @@ describe("test prompt function", () => {
66
75
az_sp_password : "a510c1ff-358c-4ed4-96c8-eb23f42bbc5b" ,
67
76
az_sp_tenant : "72f988bf-86f1-41af-91ab-2d7cd011db47"
68
77
} ) ;
78
+ jest . spyOn ( subscriptionService , "getSubscriptions" ) . mockResolvedValueOnce ( [
79
+ {
80
+ id : "72f988bf-86f1-41af-91ab-2d7cd011db48" ,
81
+ name : "test"
82
+ }
83
+ ] ) ;
69
84
const ans = await prompt ( ) ;
70
85
expect ( ans ) . toStrictEqual ( {
71
86
accessToken : "pat" ,
@@ -74,6 +89,7 @@ describe("test prompt function", () => {
74
89
servicePrincipalId : "b510c1ff-358c-4ed4-96c8-eb23f42bb65b" ,
75
90
servicePrincipalPassword : "a510c1ff-358c-4ed4-96c8-eb23f42bbc5b" ,
76
91
servicePrincipalTenantId : "72f988bf-86f1-41af-91ab-2d7cd011db47" ,
92
+ subscriptionId : "72f988bf-86f1-41af-91ab-2d7cd011db48" ,
77
93
toCreateAppRepo : true ,
78
94
toCreateSP : false ,
79
95
workspace : WORKSPACE
@@ -153,7 +169,8 @@ describe("test getAnswerFromFile function", () => {
153
169
"az_create_app=true" ,
154
170
"az_sp_id=b510c1ff-358c-4ed4-96c8-eb23f42bb65b" ,
155
171
"az_sp_password=a510c1ff-358c-4ed4-96c8-eb23f42bbc5b" ,
156
- "az_sp_tenant=72f988bf-86f1-41af-91ab-2d7cd011db47"
172
+ "az_sp_tenant=72f988bf-86f1-41af-91ab-2d7cd011db47" ,
173
+ "az_subscription_id=72f988bf-86f1-41af-91ab-2d7cd011db48"
157
174
] ;
158
175
fs . writeFileSync ( file , data . join ( "\n" ) ) ;
159
176
const requestContext = getAnswerFromFile ( file ) ;
@@ -171,6 +188,9 @@ describe("test getAnswerFromFile function", () => {
171
188
expect ( requestContext . servicePrincipalTenantId ) . toBe (
172
189
"72f988bf-86f1-41af-91ab-2d7cd011db47"
173
190
) ;
191
+ expect ( requestContext . subscriptionId ) . toBe (
192
+ "72f988bf-86f1-41af-91ab-2d7cd011db48"
193
+ ) ;
174
194
} ) ;
175
195
it ( "negative test: with app creation, incorrect SP values" , ( ) => {
176
196
const dir = createTempDir ( ) ;
@@ -179,7 +199,8 @@ describe("test getAnswerFromFile function", () => {
179
199
"azdo_org_name=orgname" ,
180
200
"azdo_pat=pat" ,
181
201
"azdo_project_name=project" ,
182
- "az_create_app=true"
202
+ "az_create_app=true" ,
203
+ "az_subscription_id=72f988bf-86f1-41af-91ab-2d7cd011db48"
183
204
] ;
184
205
[ "." , ".##" , ".abc" ] . forEach ( ( v , i ) => {
185
206
if ( i === 0 ) {
@@ -199,4 +220,60 @@ describe("test getAnswerFromFile function", () => {
199
220
} ) . toThrow ( ) ;
200
221
} ) ;
201
222
} ) ;
223
+ it ( "negative test: with app creation, incorrect subscription id value" , ( ) => {
224
+ const dir = createTempDir ( ) ;
225
+ const file = path . join ( dir , "testfile" ) ;
226
+ const data = [
227
+ "azdo_org_name=orgname" ,
228
+ "azdo_pat=pat" ,
229
+ "azdo_project_name=project" ,
230
+ "az_create_app=true" ,
231
+ "az_sp_id=b510c1ff-358c-4ed4-96c8-eb23f42bb65b" ,
232
+ "az_sp_password=a510c1ff-358c-4ed4-96c8-eb23f42bbc5b" ,
233
+ "az_sp_tenant=72f988bf-86f1-41af-91ab-2d7cd011db47" ,
234
+ "az_subscription_id=xyz"
235
+ ] ;
236
+ fs . writeFileSync ( file , data . join ( "\n" ) ) ;
237
+ expect ( ( ) => {
238
+ getAnswerFromFile ( file ) ;
239
+ } ) . toThrow ( ) ;
240
+ } ) ;
241
+ } ) ;
242
+
243
+ describe ( "test promptForSubscriptionId function" , ( ) => {
244
+ it ( "no subscriptions" , async ( ) => {
245
+ jest
246
+ . spyOn ( subscriptionService , "getSubscriptions" )
247
+ . mockResolvedValueOnce ( [ ] ) ;
248
+ const mockRc : IRequestContext = {
249
+ accessToken : "pat" ,
250
+ orgName : "org" ,
251
+ projectName : "project" ,
252
+ workspace : WORKSPACE
253
+ } ;
254
+ await expect ( promptForSubscriptionId ( mockRc ) ) . rejects . toThrow ( ) ;
255
+ } ) ;
256
+ it ( "2 subscriptions" , async ( ) => {
257
+ jest . spyOn ( subscriptionService , "getSubscriptions" ) . mockResolvedValueOnce ( [
258
+ {
259
+ id : "123345" ,
260
+ name : "subscription1"
261
+ } ,
262
+ {
263
+ id : "12334567890" ,
264
+ name : "subscription2"
265
+ }
266
+ ] ) ;
267
+ jest . spyOn ( inquirer , "prompt" ) . mockResolvedValueOnce ( {
268
+ az_subscription : "subscription2"
269
+ } ) ;
270
+ const mockRc : IRequestContext = {
271
+ accessToken : "pat" ,
272
+ orgName : "org" ,
273
+ projectName : "project" ,
274
+ workspace : WORKSPACE
275
+ } ;
276
+ await promptForSubscriptionId ( mockRc ) ;
277
+ expect ( mockRc . subscriptionId ) . toBe ( "12334567890" ) ;
278
+ } ) ;
202
279
} ) ;
0 commit comments