Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit 7882da8

Browse files
authored
[TEST] Added test for azure storage lib (#534)
1 parent de4e936 commit 7882da8

File tree

3 files changed

+106
-10
lines changed

3 files changed

+106
-10
lines changed

src/lib/azure/azurecredentials.ts

+19-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";
33
import { Config } from "../../config";
44
import { logger } from "../../logger";
55
import { AzureAccessOpts } from "../../types";
6+
import { build as buildError } from "../../lib/errorBuilder";
7+
import { errorStatusCode } from "../errorStatusCode";
68

79
const verifyConfigDefined = (
810
servicePrincipalId?: string,
@@ -85,13 +87,21 @@ export const getManagementCredentials = async (
8587
return undefined;
8688
}
8789

88-
// verifyConfigDefined has confirmed that these values are defined.
89-
return msRestNodeAuth.loginWithServicePrincipalSecret(
90-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
91-
servicePrincipalId!,
92-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
93-
servicePrincipalPassword!,
94-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
95-
tenantId!
96-
);
90+
try {
91+
// verifyConfigDefined has confirmed that these values are defined.
92+
return await msRestNodeAuth.loginWithServicePrincipalSecret(
93+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
94+
servicePrincipalId!,
95+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
96+
servicePrincipalPassword!,
97+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
98+
tenantId!
99+
);
100+
} catch (err) {
101+
throw buildError(
102+
errorStatusCode.AZURE_CLIENT,
103+
"azure-client-auth-sp-err",
104+
err
105+
);
106+
}
97107
};

src/lib/azure/storage.test.ts

+86-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ jest.mock("@azure/arm-storage");
22
jest.mock("azure-storage");
33
jest.mock("../../config");
44

5+
import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";
56
import uuid from "uuid/v4";
67
import { disableVerboseLogging, enableVerboseLogging } from "../../logger";
78
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";
915
import * as storage from "./storage";
1016
import * as azureStorage from "azure-storage";
1117
import { getErrorMessage } from "../../lib/errorBuilder";
@@ -14,6 +20,17 @@ const resourceGroupName = uuid();
1420
const storageAccountName = uuid();
1521
const 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+
});

src/lib/i18n.json

+1
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@
310310
"service-endpoint-err-create-missing-name": "Could not create service endpoint because name was missing.",
311311
"service-endpoint-err-create": "Could not create service endpoint.",
312312

313+
"azure-client-auth-sp-err": "Could not authenticate with service principal credential.",
313314
"azure-client-get-web-api-err-missing-access-token": "Could not get azure web API because personal access token was missing. Provide it.",
314315
"azure-client-get-web-api-err-missing-org": "Could not get azure web API because organization name was missing. Provide it.",
315316
"azure-client-get-rest-client-err": "Could not get REST client. Check the Azure credential",

0 commit comments

Comments
 (0)