Skip to content

Update mocked functions for app-configuration v1.5.0 #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions test/utils/testHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.

import * as sinon from "sinon";
import { AppConfigurationClient } from "@azure/app-configuration";
import { AppConfigurationClient, ConfigurationSetting } from "@azure/app-configuration";
import { ClientSecretCredential } from "@azure/identity";
import { KeyVaultSecret, SecretClient } from "@azure/keyvault-secrets";
import * as uuid from "uuid";
Expand All @@ -11,17 +11,26 @@ const TEST_CLIENT_ID = "00000000-0000-0000-0000-000000000000";
const TEST_TENANT_ID = "00000000-0000-0000-0000-000000000000";
const TEST_CLIENT_SECRET = "0000000000000000000000000000000000000000";

function mockAppConfigurationClientListConfigurationSettings(kvList: any[]) {
function mockAppConfigurationClientListConfigurationSettings(kvList: ConfigurationSetting[]) {
function* testKvSetGnerator(kvs: any[]) {
yield* kvs;
}
sinon.stub(AppConfigurationClient.prototype, "listConfigurationSettings").callsFake((listOptions) => {
const keyFilter = listOptions?.keyFilter ?? "*";
const labelFilter = listOptions?.labelFilter ?? "*";
const kvs = kvList.filter(kv => {
const keyMatched = keyFilter.endsWith("*") ? kv.key.startsWith(keyFilter.slice(0, keyFilter.length - 1)) : kv.key === keyFilter;
const labelMatched = labelFilter.endsWith("*") ? kv.label.startsWith(labelFilter.slice(0, labelFilter.length - 1))
: (labelFilter === "\0" ? kv.label === null : kv.label === labelFilter); // '\0' in labelFilter, null in config setting.
const keyMatched = keyFilter.endsWith("*") ? kv.key.startsWith(keyFilter.slice(0, -1)) : kv.key === keyFilter;

let labelMatched = false;
if (labelFilter === "*") {
labelMatched = true;
} else if (labelFilter === "\0") {
labelMatched = kv.label === undefined;
} else if (labelFilter.endsWith("*")) {
labelMatched = kv.label !== undefined && kv.label.startsWith(labelFilter.slice(0, -1));
} else {
labelMatched = kv.label === labelFilter;
}
return keyMatched && labelMatched;
})
return testKvSetGnerator(kvs) as any;
Expand Down Expand Up @@ -64,36 +73,33 @@ const createMockedTokenCredential = (tenantId = TEST_TENANT_ID, clientId = TEST_
return new ClientSecretCredential(tenantId, clientId, clientSecret);
}

const createMockedKeyVaultReference = (key: string, vaultUri: string) => ({
const createMockedKeyVaultReference = (key: string, vaultUri: string): ConfigurationSetting => ({
// https://${vaultName}.vault.azure.net/secrets/${secretName}
value: `{"uri":"${vaultUri}"}`,
key,
label: null,
contentType: "application/vnd.microsoft.appconfig.keyvaultref+json;charset=utf-8",
lastModified: "2023-05-09T08:51:11.000Z",
lastModified: new Date(),
tags: {
},
etag: "SPJSMnJ2ph4BAjftWfdIctV2VIyQxtcIzRbh1oxTBkM",
isReadOnly: false,
});

const createMockedJsonKeyValue = (key: string, value: any) => ({
const createMockedJsonKeyValue = (key: string, value: any): ConfigurationSetting => ({
value: value,
key: key,
label: null,
contentType: "application/json",
lastModified: "2023-05-04T04:32:56.000Z",
lastModified: new Date(),
tags: {},
etag: "GdmsLWq3mFjFodVEXUYRmvFr3l_qRiKAW_KdpFbxZKk",
isReadOnly: false
});

const createMockedKeyValue = (props: {[key: string]: any}) => (Object.assign({
const createMockedKeyValue = (props: {[key: string]: any}): ConfigurationSetting => (Object.assign({
value: "TestValue",
key: "TestKey",
label: null,
contentType: "",
lastModified: new Date().toISOString(),
lastModified: new Date(),
tags: {},
etag: uuid.v4(),
isReadOnly: false
Expand Down