|
| 1 | +import Serverless from "serverless"; |
| 2 | +import { MockFactory } from "../test/mockFactory"; |
| 3 | +import { |
| 4 | + AzureKeyVaultService, |
| 5 | + AzureKeyVaultConfig |
| 6 | +} from "./azureKeyVaultService"; |
| 7 | + |
| 8 | +import { Vaults } from "@azure/arm-keyvault"; |
| 9 | +import { FunctionAppService } from "./functionAppService"; |
| 10 | + |
| 11 | +describe("Azure Key Vault Service", () => { |
| 12 | + const options: Serverless.Options = MockFactory.createTestServerlessOptions(); |
| 13 | + const knownVaults = { |
| 14 | + testVault: { |
| 15 | + location: "WestUS", |
| 16 | + properties: { |
| 17 | + accessPolicies: [] |
| 18 | + } |
| 19 | + } |
| 20 | + }; |
| 21 | + |
| 22 | + let serverless: Serverless = MockFactory.createTestServerless(); |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + Vaults.prototype.createOrUpdate = jest.fn(); |
| 26 | + Vaults.prototype.get = jest.fn(async (resourceGroup, vaultName) => { |
| 27 | + if (!knownVaults.hasOwnProperty(vaultName)) { |
| 28 | + throw new Error("No matching vaults"); |
| 29 | + } |
| 30 | + return knownVaults["testVault"]; |
| 31 | + }) as any; |
| 32 | + |
| 33 | + FunctionAppService.prototype.get = jest.fn(() => { |
| 34 | + console.log("testing"); |
| 35 | + return { |
| 36 | + identity: { |
| 37 | + tenantId: "tid", |
| 38 | + principalId: "oid" |
| 39 | + } |
| 40 | + } as any; |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it("is defined", () => { |
| 45 | + expect(AzureKeyVaultService).toBeDefined(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("can be instantiated", () => { |
| 49 | + const service = new AzureKeyVaultService(serverless, options); |
| 50 | + expect(service).not.toBeNull(); |
| 51 | + }); |
| 52 | + |
| 53 | + it("Throws an error if keyvault doesn't exist", async () => { |
| 54 | + const service = new AzureKeyVaultService(serverless, options); |
| 55 | + const keyVault: AzureKeyVaultConfig = MockFactory.createTestKeyVaultConfig( |
| 56 | + "fake-vault" |
| 57 | + ); |
| 58 | + |
| 59 | + await expect(service.setPolicy(keyVault)).rejects.toThrowError( |
| 60 | + "Error: Specified vault not found" |
| 61 | + ); |
| 62 | + await expect(Vaults.prototype.createOrUpdate).not.toBeCalled(); |
| 63 | + }); |
| 64 | + |
| 65 | + it("Sets correct policy if correct keyvault name is specified", async () => { |
| 66 | + const keyVault: AzureKeyVaultConfig = MockFactory.createTestKeyVaultConfig( |
| 67 | + "testVault" |
| 68 | + ); |
| 69 | + const slsConfig: any = { |
| 70 | + ...MockFactory.createTestService( |
| 71 | + MockFactory.createTestSlsFunctionConfig() |
| 72 | + ), |
| 73 | + service: "test-sls", |
| 74 | + provider: { |
| 75 | + name: "azure", |
| 76 | + resourceGroup: "test-sls-rg", |
| 77 | + region: "West US", |
| 78 | + keyVault |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + serverless = MockFactory.createTestServerless({ |
| 83 | + service: slsConfig |
| 84 | + }); |
| 85 | + |
| 86 | + const service = new AzureKeyVaultService(serverless, options); |
| 87 | + await service.setPolicy(keyVault); |
| 88 | + |
| 89 | + await expect(Vaults.prototype.createOrUpdate).toBeCalledWith( |
| 90 | + keyVault.resourceGroup, |
| 91 | + keyVault.name, |
| 92 | + { |
| 93 | + location: knownVaults["testVault"].location, |
| 94 | + properties: knownVaults["testVault"].properties |
| 95 | + } |
| 96 | + ); |
| 97 | + }); |
| 98 | +}); |
0 commit comments