Skip to content

Commit c896e1a

Browse files
add test
1 parent d8dee3b commit c896e1a

File tree

7 files changed

+143
-27
lines changed

7 files changed

+143
-27
lines changed

src/appConfigurationImpl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import {
4141
CLIENT_FILTERS_KEY_NAME
4242
} from "./featureManagement/constants.js";
4343
import { FM_PACKAGE_NAME, AI_MIME_PROFILE, AI_CHAT_COMPLETION_MIME_PROFILE } from "./requestTracing/constants.js";
44-
import { parseContentType, isJsonContentType, isFeatureFlagContentType, isSecretReferenceContentType, isSnapshotReferenceContentType } from "./common/contentType.js";
44+
import { parseContentType, isJsonContentType, isFeatureFlagContentType, isSecretReferenceContentType } from "./common/contentType.js";
4545
import { AzureKeyVaultKeyValueAdapter } from "./keyvault/keyVaultKeyValueAdapter.js";
4646
import { RefreshTimer } from "./refresh/refreshTimer.js";
4747
import {

src/common/contentType.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,3 @@ export function isSecretReferenceContentType(contentType: ContentType | undefine
6060
}
6161
return mediaType === secretReferenceContentType;
6262
}
63-
64-
export function isSnapshotReferenceContentType(contentType: ContentType | undefined): boolean {
65-
const mediaType = contentType?.mediaType;
66-
if (!mediaType) {
67-
return false;
68-
}
69-
// TODO: replace with constant when available in Azure SDK
70-
return mediaType === "application/json; profile=\"https://azconfig.io/mime-profiles/snapshot-ref\"; charset=utf-8";
71-
}

test/featureFlag.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,14 @@ describe("feature flags", function () {
415415

416416
it("should load feature flags from snapshot", async () => {
417417
const snapshotName = "Test";
418-
mockAppConfigurationClientGetSnapshot(snapshotName, {compositionType: "key"});
419-
mockAppConfigurationClientListConfigurationSettingsForSnapshot(snapshotName, [[createMockedFeatureFlag("TestFeature", { enabled: true })]]);
418+
const snapshotResponses = new Map([
419+
[snapshotName, { compositionType: "key" }]
420+
]);
421+
const snapshotKVs = new Map([
422+
[snapshotName, [[createMockedFeatureFlag("TestFeature", { enabled: true })]]]
423+
]);
424+
mockAppConfigurationClientGetSnapshot(snapshotResponses);
425+
mockAppConfigurationClientListConfigurationSettingsForSnapshot(snapshotKVs);
420426
const connectionString = createMockedConnectionString();
421427
const settings = await load(connectionString, {
422428
featureFlagOptions: {

test/load.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -581,18 +581,22 @@ describe("load", function () {
581581

582582
it("should load key values from snapshot", async () => {
583583
const snapshotName = "Test";
584-
mockAppConfigurationClientGetSnapshot(snapshotName, {compositionType: "key"});
585-
mockAppConfigurationClientListConfigurationSettingsForSnapshot(snapshotName, [[{key: "TestKey", value: "TestValue"}].map(createMockedKeyValue)]);
584+
const snapshotResponses = new Map([
585+
[snapshotName, { compositionType: "key" }]
586+
]);
587+
const snapshotKVs = new Map([
588+
[snapshotName, [[{key: "TestKey", value: "TestValue"}].map(createMockedKeyValue)]]]
589+
);
590+
mockAppConfigurationClientGetSnapshot(snapshotResponses);
591+
mockAppConfigurationClientListConfigurationSettingsForSnapshot(snapshotKVs);
586592
const connectionString = createMockedConnectionString();
587593
const settings = await load(connectionString, {
588594
selectors: [{
589595
snapshotName: snapshotName
590596
}]
591597
});
592598
expect(settings).not.undefined;
593-
expect(settings).not.undefined;
594599
expect(settings.get("TestKey")).eq("TestValue");
595-
restoreMocks();
596600
});
597601
});
598602
/* eslint-enable @typescript-eslint/no-unused-expressions */

test/snapshotReference.test.ts

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
/* eslint-disable @typescript-eslint/no-unused-expressions */
5+
import * as chai from "chai";
6+
import chaiAsPromised from "chai-as-promised";
7+
chai.use(chaiAsPromised);
8+
const expect = chai.expect;
9+
import { load } from "../src/index.js";
10+
import {
11+
mockAppConfigurationClientListConfigurationSettings,
12+
mockAppConfigurationClientGetSnapshot,
13+
mockAppConfigurationClientListConfigurationSettingsForSnapshot,
14+
restoreMocks,
15+
createMockedConnectionString,
16+
createMockedKeyValue,
17+
createMockedSnapshotReference,
18+
createMockedFeatureFlag,
19+
sleepInMs
20+
} from "./utils/testHelper.js";
21+
import * as uuid from "uuid";
22+
23+
const mockedKVs = [{
24+
key: "TestKey1",
25+
value: "Value1",
26+
}, {
27+
key: "TestKey2",
28+
value: "Value2",
29+
}
30+
].map(createMockedKeyValue);
31+
32+
mockedKVs.push(createMockedSnapshotReference("TestSnapshotRef", "TestSnapshot1"));
33+
34+
// TestSnapshot1
35+
const snapshot1 = [{
36+
key: "TestKey1",
37+
value: "Value1 in snapshot1",
38+
}
39+
].map(createMockedKeyValue);
40+
const testFeatureFlag = createMockedFeatureFlag("TestFeatureFlag");
41+
snapshot1.push(testFeatureFlag);
42+
43+
// TestSnapshot2
44+
const snapshot2 = [{
45+
key: "TestKey1",
46+
value: "Value1 in snapshot2",
47+
}
48+
].map(createMockedKeyValue);
49+
50+
describe("snapshot reference", function () {
51+
52+
beforeEach(() => {
53+
const snapshotResponses = new Map([
54+
["TestSnapshot1", { compositionType: "key" }],
55+
["TestSnapshot2", { compositionType: "key" }]]
56+
);
57+
const snapshotKVs = new Map([
58+
["TestSnapshot1", [snapshot1]],
59+
["TestSnapshot2", [snapshot2]]]
60+
);
61+
mockAppConfigurationClientGetSnapshot(snapshotResponses);
62+
mockAppConfigurationClientListConfigurationSettingsForSnapshot(snapshotKVs);
63+
mockAppConfigurationClientListConfigurationSettings([mockedKVs]);
64+
});
65+
66+
afterEach(() => {
67+
restoreMocks();
68+
});
69+
70+
it("should resolve snapshot reference", async () => {
71+
const connectionString = createMockedConnectionString();
72+
const settings = await load(connectionString);
73+
expect(settings.get("TestKey1")).eq("Value1 in snapshot1");
74+
75+
// it should ignore feature flags in snapshot
76+
expect(settings.get(testFeatureFlag.key)).to.be.undefined;
77+
expect(settings.get("feature_management")).to.be.undefined;
78+
79+
// it should not load the snapshot reference key
80+
expect(settings.get("TestSnapshotRef")).to.be.undefined;
81+
});
82+
83+
it("should refresh when snapshot reference changes", async () => {
84+
const connectionString = createMockedConnectionString();
85+
const settings = await load(connectionString, {
86+
refreshOptions: {
87+
enabled: true,
88+
refreshIntervalInMs: 2000
89+
}
90+
});
91+
expect(settings.get("TestKey1")).eq("Value1 in snapshot1");
92+
93+
const setting = mockedKVs.find(kv => kv.key === "TestSnapshotRef");
94+
setting!.value = "{\"snapshot_name\":\"TestSnapshot2\"}";
95+
setting!.etag = uuid.v4();
96+
97+
await sleepInMs(2 * 1000 + 1);
98+
99+
await settings.refresh();
100+
101+
expect(settings.get("TestKey1")).eq("Value1 in snapshot2");
102+
});
103+
104+
});

test/utils/testHelper.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT license.
33

44
import * as sinon from "sinon";
5-
import { AppConfigurationClient, ConfigurationSetting, featureFlagContentType } from "@azure/app-configuration";
5+
import { AppConfigurationClient, ConfigurationSetting, featureFlagContentType, secretReferenceContentType } from "@azure/app-configuration";
66
import { ClientSecretCredential } from "@azure/identity";
77
import { KeyVaultSecret, SecretClient } from "@azure/keyvault-secrets";
88
import * as uuid from "uuid";
@@ -182,29 +182,29 @@ function mockAppConfigurationClientGetConfigurationSetting(kvList: any[], custom
182182
});
183183
}
184184

185-
function mockAppConfigurationClientGetSnapshot(snapshotName: string, mockedResponse: any, customCallback?: (options) => any) {
185+
function mockAppConfigurationClientGetSnapshot(snapshotResponses: Map<string, any>, customCallback?: (options) => any) {
186186
sinon.stub(AppConfigurationClient.prototype, "getSnapshot").callsFake((name, options) => {
187187
if (customCallback) {
188188
customCallback(options);
189189
}
190190

191-
if (name === snapshotName) {
192-
return mockedResponse;
191+
if (snapshotResponses.has(name)) {
192+
return snapshotResponses.get(name);
193193
} else {
194194
throw new RestError("", { statusCode: 404 });
195195
}
196196
});
197197
}
198198

199-
function mockAppConfigurationClientListConfigurationSettingsForSnapshot(snapshotName: string, pages: ConfigurationSetting[][], customCallback?: (options) => any) {
199+
function mockAppConfigurationClientListConfigurationSettingsForSnapshot(snapshotResponses: Map<string, ConfigurationSetting[][]>, customCallback?: (options) => any) {
200200
sinon.stub(AppConfigurationClient.prototype, "listConfigurationSettingsForSnapshot").callsFake((name, listOptions) => {
201201
if (customCallback) {
202202
customCallback(listOptions);
203203
}
204204

205-
if (name === snapshotName) {
206-
const kvs = _filterKVs(pages.flat(), listOptions);
207-
return getMockedIterator(pages, kvs, listOptions);
205+
if (snapshotResponses.has(name)) {
206+
const kvs = _filterKVs(snapshotResponses.get(name)!.flat(), listOptions);
207+
return getMockedIterator(snapshotResponses.get(name)!, kvs, listOptions);
208208
} else {
209209
throw new RestError("", { statusCode: 404 });
210210
}
@@ -252,7 +252,7 @@ const createMockedKeyVaultReference = (key: string, vaultUri: string): Configura
252252
// https://${vaultName}.vault.azure.net/secrets/${secretName}
253253
value: `{"uri":"${vaultUri}"}`,
254254
key,
255-
contentType: "application/vnd.microsoft.appconfig.keyvaultref+json;charset=utf-8",
255+
contentType: secretReferenceContentType,
256256
lastModified: new Date(),
257257
tags: {},
258258
etag: uuid.v4(),
@@ -296,6 +296,16 @@ const createMockedFeatureFlag = (name: string, flagProps?: any, props?: any) =>
296296
isReadOnly: false
297297
}, props));
298298

299+
const createMockedSnapshotReference = (key: string, snapshotName: string): ConfigurationSetting => ({
300+
value: `{"snapshot_name":"${snapshotName}"}`,
301+
key,
302+
contentType: "application/json; profile=\"https://azconfig.io/mime-profiles/snapshot-ref\"; charset=utf-8",
303+
lastModified: new Date(),
304+
tags: {},
305+
etag: uuid.v4(),
306+
isReadOnly: false,
307+
});
308+
299309
class HttpRequestHeadersPolicy {
300310
headers: any;
301311
name: string;
@@ -328,6 +338,7 @@ export {
328338
createMockedJsonKeyValue,
329339
createMockedKeyValue,
330340
createMockedFeatureFlag,
341+
createMockedSnapshotReference,
331342

332343
sleepInMs,
333344
HttpRequestHeadersPolicy

vitest.browser.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ export default defineConfig({
1010
{ browser: "chromium" },
1111
],
1212
},
13-
include: ["out/esm/test/load.test.js", "out/esm/test/refresh.test.js", "out/esm/test/featureFlag.test.js", "out/esm/test/json.test.js", "out/esm/test/startup.test.js"],
13+
include: ["out/esm/test/load.test.js", "out/esm/test/refresh.test.js", "out/esm/test/featureFlag.test.js", "out/esm/test/json.test.js", "out/esm/test/startup.test.js", "out/esm/test/snapshotReference.test.js"],
1414
testTimeout: 200_000,
1515
hookTimeout: 200_000,
1616
reporters: "default",
1717
globals: true,
1818
// Provide Mocha-style hooks as globals
1919
setupFiles: ["./vitest.setup.mjs"],
2020
},
21-
});
21+
});

0 commit comments

Comments
 (0)