Skip to content
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

[App Config] Fix App Config Tests #31079

Merged
merged 12 commits into from
Sep 17, 2024
1 change: 0 additions & 1 deletion sdk/appconfiguration/app-configuration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@
"karma-mocha-reporter": "^2.2.5",
"karma-sourcemap-loader": "^0.3.8",
"mocha": "^10.0.0",
"nock": "^13.5.4",
"rimraf": "^5.0.5",
"sinon": "^17.0.0",
"tsx": "^4.7.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ export async function main() {

// let's create the setting
await client.addConfigurationSetting({
key: "sample key",
key: "listLabelsSample",
value: "sample value",
label: "production",
});

await client.addConfigurationSetting({
key: "sample key",
key: "listLabelsSample",
value: "sample value",
label: "developmentA",
tags: {
Expand All @@ -38,7 +38,7 @@ export async function main() {
});

await client.addConfigurationSetting({
key: "sample key",
key: "listLabelsSample",
value: "value",
label: "developmentB",
tags: {
Expand Down Expand Up @@ -92,7 +92,7 @@ export async function main() {
}
}

cleanupSampleValues(["sample key"], client);
cleanupSampleValues(["listLabelsSample"], client);
}

async function cleanupSampleValues(keys: string[], client: AppConfigurationClient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ async function main() {

// let's create the setting
await client.addConfigurationSetting({
key: "sample key",
key: "listLabelsSample",
value: "sample value",
label: "production",
});

await client.addConfigurationSetting({
key: "sample key",
key: "listLabelsSample",
value: "sample value",
label: "developmentA",
tags: {
Expand All @@ -36,7 +36,7 @@ async function main() {
});

await client.addConfigurationSetting({
key: "sample key",
key: "listLabelsSample",
value: "value",
label: "developmentB",
tags: {
Expand Down Expand Up @@ -90,7 +90,7 @@ async function main() {
}
}

cleanupSampleValues(["sample key"], client);
cleanupSampleValues(["listLabelsSample"], client);
}

async function cleanupSampleValues(keys, client) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ export async function main() {

// let's create the setting
await client.addConfigurationSetting({
key: "sample key",
key: "listLabelsSample",
value: "sample value",
label: "production",
});

await client.addConfigurationSetting({
key: "sample key",
key: "listLabelsSample",
value: "sample value",
label: "developmentA",
tags: {
Expand All @@ -37,7 +37,7 @@ export async function main() {
});

await client.addConfigurationSetting({
key: "sample key",
key: "listLabelsSample",
value: "value",
label: "developmentB",
tags: {
Expand Down Expand Up @@ -91,7 +91,7 @@ export async function main() {
}
}

cleanupSampleValues(["sample key"], client);
cleanupSampleValues(["listLabelsSample"], client);
}

async function cleanupSampleValues(keys: string[], client: AppConfigurationClient) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { SyncTokens, parseSyncToken } from "../../../src/internal/synctokenpolicy";
import {
assertThrowsRestError,
Expand All @@ -15,8 +12,13 @@ import { Context } from "mocha";
import { InternalAppConfigurationClientOptions } from "../../../src/appConfigurationClient";
import { Recorder } from "@azure-tools/test-recorder";
import { assert } from "chai";
import nock from "nock";
import { NoOpCredential } from "@azure-tools/test-credential";
import {
createHttpHeaders,
HttpClient,
PipelineRequest,
SendRequest,
} from "@azure/core-rest-pipeline";

describe("http request related tests", function () {
describe("unit tests", () => {
Expand Down Expand Up @@ -116,61 +118,17 @@ describe("http request related tests", function () {
describe("request/reply tests for sync token headers", () => {
let client: AppConfigurationClient;
let syncTokens: SyncTokens;
let scope: nock.Scope;

beforeEach(function (this: Context) {
if (nock == null || nock.recorder == null) {
this.skip();
return;
}

beforeEach(async function (this: Context) {
syncTokens = new SyncTokens();

// Use NoOpCredential for nock tests to avoid interception for credential request
client =
createAppConfigurationClientForTests({
syncTokens: syncTokens,
testCredential: new NoOpCredential(),
} as InternalAppConfigurationClientOptions) || this.skip();

nock.recorder.clear();
nock.restore();
nock.cleanAll();
if (!nock.isActive()) {
nock.activate();
}

// Add authorization request headers to match GET requests with NoOpCredential
scope = nock(/.*/, {
reqheaders: {
authorization: /.*/,
},
});
});

afterEach(function (this: Context) {
if (nock == null || nock.recorder == null) {
return;
}

if (!this.currentTest?.isPending()) {
assert.ok(scope.isDone());
}
nock.recorder.clear();
nock.restore();
nock.cleanAll();
});

it("policy is setup properly to send sync tokens", async function () {
syncTokens.addSyncTokenFromHeaderValue(`hello=world;sn=1`);
client = createMockSyncTokenClient(syncTokens, async (request: PipelineRequest) => {
return { headers: createHttpHeaders(), status: 418, request };
});

const policyScope = nock(/.*/, {
reqheaders: {
"sync-token": "hello=world",
},
})
.get(/.*/)
.reply(418);
syncTokens.addSyncTokenFromHeaderValue(`hello=world;sn=1`);

await assertThrowsRestError(
async () =>
Expand All @@ -179,12 +137,16 @@ describe("http request related tests", function () {
}),
418,
);

assert.ok(policyScope.isDone());
});

it("addConfigurationSetting", async () => {
scope.put(/.*/).reply(200, "", { "sync-token": "addConfigurationSetting=value;sn=1" });
client = createMockSyncTokenClient(syncTokens, async (request: PipelineRequest) => {
return {
headers: createHttpHeaders({ "sync-token": "addConfigurationSetting=value;sn=1" }),
status: 200,
request,
};
});

await client.addConfigurationSetting({
key: "doesntmatter",
Expand All @@ -194,7 +156,13 @@ describe("http request related tests", function () {
});

it("getConfigurationSetting", async () => {
scope.get(/.*/).reply(200, "", { "sync-token": "getConfigurationSetting=value;sn=1" });
client = createMockSyncTokenClient(syncTokens, async (request: PipelineRequest) => {
return {
headers: createHttpHeaders({ "sync-token": "getConfigurationSetting=value;sn=1" }),
status: 200,
request,
};
});

await client.getConfigurationSetting({
key: "doesntmatter",
Expand All @@ -204,7 +172,13 @@ describe("http request related tests", function () {
});

it("setConfigurationSetting", async () => {
scope.put(/.*/).reply(200, "", { "sync-token": "setConfigurationSetting=value;sn=1" });
client = createMockSyncTokenClient(syncTokens, async (request: PipelineRequest) => {
return {
headers: createHttpHeaders({ "sync-token": "setConfigurationSetting=value;sn=1" }),
status: 200,
request,
};
});

await client.setConfigurationSetting({
key: "doesntmatter",
Expand All @@ -214,7 +188,13 @@ describe("http request related tests", function () {
});

it("deleteConfigurationSetting", async () => {
scope.delete(/.*/).reply(200, "", { "sync-token": "deleteConfigurationSetting=value;sn=1" });
client = createMockSyncTokenClient(syncTokens, async (request: PipelineRequest) => {
return {
headers: createHttpHeaders({ "sync-token": "deleteConfigurationSetting=value;sn=1" }),
status: 200,
request,
};
});

await client.deleteConfigurationSetting({
key: "doesntmatter",
Expand All @@ -224,7 +204,13 @@ describe("http request related tests", function () {
});

it("listConfigurationSetting", async () => {
scope.get(/.*/).reply(200, "", { "sync-token": "listConfigurationSetting=value;sn=1" });
client = createMockSyncTokenClient(syncTokens, async (request: PipelineRequest) => {
return {
headers: createHttpHeaders({ "sync-token": "listConfigurationSetting=value;sn=1" }),
status: 200,
request,
};
});

const iterator = client.listConfigurationSettings({
keyFilter: "doesntmatter",
Expand All @@ -235,7 +221,13 @@ describe("http request related tests", function () {
});

it("listRevisions", async () => {
scope.get(/.*/).reply(200, "", { "sync-token": "listRevisions=value;sn=1" });
client = createMockSyncTokenClient(syncTokens, async (request: PipelineRequest) => {
return {
headers: createHttpHeaders({ "sync-token": "listRevisions=value;sn=1" }),
status: 200,
request,
};
});

const iterator = client.listRevisions({
keyFilter: "doesntmatter",
Expand All @@ -246,9 +238,13 @@ describe("http request related tests", function () {
});

it("setReadOnly (clear and set)", async () => {
scope.put(/.*/).reply(200, "", { "sync-token": "setReadOnly=value;sn=1" });

scope.delete(/.*/).reply(200, "", { "sync-token": "clearReadOnly=value;sn=1" });
client = createMockSyncTokenClient(syncTokens, async (request: PipelineRequest) => {
return {
headers: createHttpHeaders({ "sync-token": "setReadOnly=value;sn=1" }),
status: 200,
request,
};
});

await client.setReadOnly(
{
Expand All @@ -260,6 +256,13 @@ describe("http request related tests", function () {
assert.equal(syncTokens.getSyncTokenHeaderValue(), "setReadOnly=value");

syncTokens.addSyncTokenFromHeaderValue(undefined); // clear out any previous sync tokens
client = createMockSyncTokenClient(syncTokens, async (request: PipelineRequest) => {
return {
headers: createHttpHeaders({ "sync-token": "clearReadOnly=value;sn=1" }),
status: 200,
request,
};
});

await client.setReadOnly(
{
Expand Down Expand Up @@ -312,3 +315,18 @@ function splitAndSort(syncTokens: string | undefined): string {

return syncTokens.split(",").sort().join(",");
}

function createMockSyncTokenClient(
syncTokens: SyncTokens,
sendRequest: SendRequest,
): AppConfigurationClient {
const fakeHttpClient: HttpClient = {
sendRequest,
};

// Use NoOpCredential to avoid interception for credential request
return new AppConfigurationClient("https://example.com", new NoOpCredential(), {
syncTokens: syncTokens,
httpClient: fakeHttpClient,
} as InternalAppConfigurationClientOptions);
}
Loading