@@ -4,9 +4,10 @@ import os from "os";
44import path from "path" ;
55import uuid from "uuid/v4" ;
66import { 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" ;
99import * as servicePrincipalService from "./servicePrincipalService" ;
10+ import * as subscriptionService from "./subscriptionService" ;
1011
1112describe ( "test prompt function" , ( ) => {
1213 it ( "positive test: No App Creation" , async ( ) => {
@@ -40,11 +41,19 @@ describe("test prompt function", () => {
4041 jest
4142 . spyOn ( servicePrincipalService , "createWithAzCLI" )
4243 . mockReturnValueOnce ( Promise . resolve ( ) ) ;
44+ jest . spyOn ( subscriptionService , "getSubscriptions" ) . mockResolvedValueOnce ( [
45+ {
46+ id : "72f988bf-86f1-41af-91ab-2d7cd011db48" ,
47+ name : "test"
48+ }
49+ ] ) ;
50+
4351 const ans = await prompt ( ) ;
4452 expect ( ans ) . toStrictEqual ( {
4553 accessToken : "pat" ,
4654 orgName : "org" ,
4755 projectName : "project" ,
56+ subscriptionId : "72f988bf-86f1-41af-91ab-2d7cd011db48" ,
4857 toCreateAppRepo : true ,
4958 toCreateSP : true ,
5059 workspace : WORKSPACE
@@ -66,6 +75,12 @@ describe("test prompt function", () => {
6675 az_sp_password : "a510c1ff-358c-4ed4-96c8-eb23f42bbc5b" ,
6776 az_sp_tenant : "72f988bf-86f1-41af-91ab-2d7cd011db47"
6877 } ) ;
78+ jest . spyOn ( subscriptionService , "getSubscriptions" ) . mockResolvedValueOnce ( [
79+ {
80+ id : "72f988bf-86f1-41af-91ab-2d7cd011db48" ,
81+ name : "test"
82+ }
83+ ] ) ;
6984 const ans = await prompt ( ) ;
7085 expect ( ans ) . toStrictEqual ( {
7186 accessToken : "pat" ,
@@ -74,6 +89,7 @@ describe("test prompt function", () => {
7489 servicePrincipalId : "b510c1ff-358c-4ed4-96c8-eb23f42bb65b" ,
7590 servicePrincipalPassword : "a510c1ff-358c-4ed4-96c8-eb23f42bbc5b" ,
7691 servicePrincipalTenantId : "72f988bf-86f1-41af-91ab-2d7cd011db47" ,
92+ subscriptionId : "72f988bf-86f1-41af-91ab-2d7cd011db48" ,
7793 toCreateAppRepo : true ,
7894 toCreateSP : false ,
7995 workspace : WORKSPACE
@@ -153,7 +169,8 @@ describe("test getAnswerFromFile function", () => {
153169 "az_create_app=true" ,
154170 "az_sp_id=b510c1ff-358c-4ed4-96c8-eb23f42bb65b" ,
155171 "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"
157174 ] ;
158175 fs . writeFileSync ( file , data . join ( "\n" ) ) ;
159176 const requestContext = getAnswerFromFile ( file ) ;
@@ -171,6 +188,9 @@ describe("test getAnswerFromFile function", () => {
171188 expect ( requestContext . servicePrincipalTenantId ) . toBe (
172189 "72f988bf-86f1-41af-91ab-2d7cd011db47"
173190 ) ;
191+ expect ( requestContext . subscriptionId ) . toBe (
192+ "72f988bf-86f1-41af-91ab-2d7cd011db48"
193+ ) ;
174194 } ) ;
175195 it ( "negative test: with app creation, incorrect SP values" , ( ) => {
176196 const dir = createTempDir ( ) ;
@@ -179,7 +199,8 @@ describe("test getAnswerFromFile function", () => {
179199 "azdo_org_name=orgname" ,
180200 "azdo_pat=pat" ,
181201 "azdo_project_name=project" ,
182- "az_create_app=true"
202+ "az_create_app=true" ,
203+ "az_subscription_id=72f988bf-86f1-41af-91ab-2d7cd011db48"
183204 ] ;
184205 [ "." , ".##" , ".abc" ] . forEach ( ( v , i ) => {
185206 if ( i === 0 ) {
@@ -199,4 +220,60 @@ describe("test getAnswerFromFile function", () => {
199220 } ) . toThrow ( ) ;
200221 } ) ;
201222 } ) ;
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+ } ) ;
202279} ) ;
0 commit comments