@@ -2,10 +2,16 @@ jest.mock("@azure/arm-storage");
2
2
jest . mock ( "azure-storage" ) ;
3
3
jest . mock ( "../../config" ) ;
4
4
5
+ import * as msRestNodeAuth from "@azure/ms-rest-nodeauth" ;
5
6
import uuid from "uuid/v4" ;
6
7
import { disableVerboseLogging , enableVerboseLogging } from "../../logger" ;
7
8
import { Config } from "../../config" ;
8
- import { getStorageAccount , validateStorageAccount } from "./storage" ;
9
+ import * as config from "../../config" ;
10
+ import {
11
+ getStorageAccount ,
12
+ getStorageManagementClient ,
13
+ validateStorageAccount ,
14
+ } from "./storage" ;
9
15
import * as storage from "./storage" ;
10
16
import * as azureStorage from "azure-storage" ;
11
17
import { getErrorMessage } from "../../lib/errorBuilder" ;
@@ -14,6 +20,17 @@ const resourceGroupName = uuid();
14
20
const storageAccountName = uuid ( ) ;
15
21
const location = uuid ( ) ;
16
22
23
+ jest . mock ( "@azure/arm-storage" , ( ) => {
24
+ class MockClient {
25
+ constructor ( ) {
26
+ return { } ;
27
+ }
28
+ }
29
+ return {
30
+ StorageManagementClient : MockClient ,
31
+ } ;
32
+ } ) ;
33
+
17
34
( Config as jest . Mock ) . mockReturnValue ( {
18
35
introspection : {
19
36
azure : {
@@ -373,3 +390,71 @@ describe("test validateStorageAccount function", () => {
373
390
expect ( res ) . toBe ( true ) ;
374
391
} ) ;
375
392
} ) ;
393
+
394
+ describe ( "test getStorageManagementClient function" , ( ) => {
395
+ it ( "negative test: missing credential" , async ( ) => {
396
+ jest . spyOn ( config , "Config" ) . mockReturnValueOnce ( { } ) ;
397
+ await expect ( getStorageManagementClient ( { } ) ) . rejects . toThrow (
398
+ getErrorMessage ( "storage-client-err-missing-creds" )
399
+ ) ;
400
+ } ) ;
401
+ it ( "negative test: incorrect credential" , async ( ) => {
402
+ jest . spyOn ( config , "Config" ) . mockReturnValueOnce ( { } ) ;
403
+ await expect (
404
+ getStorageManagementClient ( {
405
+ servicePrincipalId : "servicePrincipalId" ,
406
+ servicePrincipalPassword : "servicePrincipalPassword" ,
407
+ tenantId : "tenantId" ,
408
+ } )
409
+ ) . rejects . toThrow ( getErrorMessage ( "azure-client-auth-sp-err" ) ) ;
410
+ } ) ;
411
+ it ( "negative test: authentication to management client failed" , async ( ) => {
412
+ jest . spyOn ( config , "Config" ) . mockReturnValueOnce ( { } ) ;
413
+ jest
414
+ . spyOn ( msRestNodeAuth , "loginWithServicePrincipalSecret" )
415
+ . mockResolvedValueOnce ( null as never ) ;
416
+ await expect (
417
+ getStorageManagementClient ( {
418
+ servicePrincipalId : "servicePrincipalId" ,
419
+ servicePrincipalPassword : "servicePrincipalPassword" ,
420
+ tenantId : "tenantId" ,
421
+ } )
422
+ ) . rejects . toThrow ( getErrorMessage ( "storage-client-err-missing-creds" ) ) ;
423
+ } ) ;
424
+ it ( "negative test: missing storage cred." , async ( ) => {
425
+ jest . spyOn ( config , "Config" ) . mockReturnValueOnce ( { } ) ;
426
+ jest . spyOn ( config , "Config" ) . mockReturnValueOnce ( { } ) ;
427
+ jest
428
+ . spyOn ( msRestNodeAuth , "loginWithServicePrincipalSecret" )
429
+ . mockResolvedValueOnce ( { } as never ) ;
430
+ await expect (
431
+ getStorageManagementClient ( {
432
+ servicePrincipalId : "servicePrincipalId" ,
433
+ servicePrincipalPassword : "servicePrincipalPassword" ,
434
+ tenantId : "tenantId" ,
435
+ } )
436
+ ) . rejects . toThrow ( getErrorMessage ( "storage-client-err-missing-sub-id" ) ) ;
437
+ } ) ;
438
+ it ( "positive test: missing storage cred." , async ( ) => {
439
+ jest . spyOn ( config , "Config" ) . mockReturnValueOnce ( { } ) ;
440
+ jest . spyOn ( config , "Config" ) . mockReturnValueOnce ( {
441
+ introspection : {
442
+ azure : {
443
+ subscription_id : "something" ,
444
+ } ,
445
+ } ,
446
+ } ) ;
447
+ jest
448
+ . spyOn ( msRestNodeAuth , "loginWithServicePrincipalSecret" )
449
+ . mockResolvedValueOnce ( { } as never ) ;
450
+ await getStorageManagementClient ( {
451
+ servicePrincipalId : "servicePrincipalId" ,
452
+ servicePrincipalPassword : "servicePrincipalPassword" ,
453
+ tenantId : "tenantId" ,
454
+ } ) ;
455
+ } ) ;
456
+ it ( "positive test: client should be cached." , async ( ) => {
457
+ const client = await getStorageManagementClient ( ) ; // cached copy will be returned
458
+ expect ( client ) . toBeDefined ( ) ;
459
+ } ) ;
460
+ } ) ;
0 commit comments