@@ -2,10 +2,16 @@ jest.mock("@azure/arm-storage");
22jest . mock ( "azure-storage" ) ;
33jest . mock ( "../../config" ) ;
44
5+ import * as msRestNodeAuth from "@azure/ms-rest-nodeauth" ;
56import uuid from "uuid/v4" ;
67import { disableVerboseLogging , enableVerboseLogging } from "../../logger" ;
78import { 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" ;
915import * as storage from "./storage" ;
1016import * as azureStorage from "azure-storage" ;
1117import { getErrorMessage } from "../../lib/errorBuilder" ;
@@ -14,6 +20,17 @@ const resourceGroupName = uuid();
1420const storageAccountName = uuid ( ) ;
1521const location = uuid ( ) ;
1622
23+ jest . mock ( "@azure/arm-storage" , ( ) => {
24+ class MockClient {
25+ constructor ( ) {
26+ return { } ;
27+ }
28+ }
29+ return {
30+ StorageManagementClient : MockClient ,
31+ } ;
32+ } ) ;
33+
1734( Config as jest . Mock ) . mockReturnValue ( {
1835 introspection : {
1936 azure : {
@@ -373,3 +390,71 @@ describe("test validateStorageAccount function", () => {
373390 expect ( res ) . toBe ( true ) ;
374391 } ) ;
375392} ) ;
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