|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +const chai = require("chai"); |
| 5 | +const chaiAsPromised = require("chai-as-promised"); |
| 6 | +chai.use(chaiAsPromised); |
| 7 | +const expect = chai.expect; |
| 8 | +const { load } = require("../dist/index"); |
| 9 | +const { |
| 10 | + mockAppConfigurationClientListConfigurationSettings, |
| 11 | + restoreMocks, |
| 12 | + createMockedConnectionString, |
| 13 | + createMockedKeyVaultReference |
| 14 | +} = require("./utils/testHelper"); |
| 15 | + |
| 16 | +const jsonKeyValue = { |
| 17 | + value: '{"Test":{"Level":"Debug"},"Prod":{"Level":"Warning"}}', |
| 18 | + key: 'json.settings.logging', |
| 19 | + label: null, |
| 20 | + contentType: 'application/json', |
| 21 | + lastModified: '2023-05-04T04:32:56.000Z', |
| 22 | + tags: {}, |
| 23 | + etag: 'GdmsLWq3mFjFodVEXUYRmvFr3l_qRiKAW_KdpFbxZKk', |
| 24 | + isReadOnly: false |
| 25 | +}; |
| 26 | +const keyVaultKeyValue = createMockedKeyVaultReference("TestKey", "https://fake-vault-name.vault.azure.net/secrets/fakeSecretName"); |
| 27 | + |
| 28 | +describe("json", function () { |
| 29 | + beforeEach(() => { |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + restoreMocks(); |
| 34 | + }) |
| 35 | + |
| 36 | + it("should load and parse if content type is application/json", async () => { |
| 37 | + mockAppConfigurationClientListConfigurationSettings([jsonKeyValue]); |
| 38 | + |
| 39 | + const connectionString = createMockedConnectionString(); |
| 40 | + const settings = await load(connectionString); |
| 41 | + expect(settings).not.undefined; |
| 42 | + const logging = settings.get("json.settings.logging"); |
| 43 | + expect(logging).not.undefined; |
| 44 | + expect(logging.Test).not.undefined; |
| 45 | + expect(logging.Test.Level).eq("Debug"); |
| 46 | + expect(logging.Prod).not.undefined; |
| 47 | + expect(logging.Prod.Level).eq("Warning"); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should not parse key-vault reference", async () => { |
| 51 | + mockAppConfigurationClientListConfigurationSettings([jsonKeyValue, keyVaultKeyValue]); |
| 52 | + |
| 53 | + const connectionString = createMockedConnectionString(); |
| 54 | + const settings = await load(connectionString, { |
| 55 | + keyVaultOptions: { |
| 56 | + secretResolver: (url) => `Resolved: ${url.toString()}` |
| 57 | + } |
| 58 | + }); |
| 59 | + expect(settings).not.undefined; |
| 60 | + const resolvedSecret = settings.get("TestKey"); |
| 61 | + expect(resolvedSecret).not.undefined; |
| 62 | + expect(resolvedSecret.uri).undefined; |
| 63 | + expect(typeof resolvedSecret).eq("string"); |
| 64 | + }); |
| 65 | +}) |
0 commit comments