oav/test/utilsTests.ts

110 строки
4.4 KiB
TypeScript
Исходник Обычный вид История

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
2020-11-24 06:07:36 +03:00
import assert from "assert";
import * as utils from "../lib/util/utils";
2018-06-04 23:06:00 +03:00
describe("Utility functions", () => {
describe("Get Provider", () => {
it("should throw on empty", () => {
assert.throws(() => {
2020-11-24 06:07:36 +03:00
utils.getProvider("");
});
});
2018-06-04 23:06:00 +03:00
it("should throw null", () => {
assert.throws(() => {
2020-11-24 06:07:36 +03:00
utils.getProvider(null);
});
});
2018-06-04 23:06:00 +03:00
it("should throw undefined", () => {
assert.throws(() => {
2020-11-24 06:07:36 +03:00
utils.getProvider();
});
});
2018-06-04 23:06:00 +03:00
it("should return Microsoft.Resources", () => {
const path =
"/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/" +
2020-11-24 06:07:36 +03:00
"Microsoft.Resources/{parentResourcePath}/{resourceType}/{resourceName}";
const provider = utils.getProvider(path);
assert.strictEqual(provider, "Microsoft.Resources");
});
2018-06-04 23:06:00 +03:00
it("should return undefined", () => {
2020-11-24 06:07:36 +03:00
const path = "/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/";
const provider = utils.getProvider(path);
assert.strictEqual(provider, undefined);
});
2018-06-04 23:06:00 +03:00
it("should return Microsoft.Authorization", () => {
const path =
"/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/" +
"Microsoft.Resources/{parentResourcePath}/{resourceType}/{resourceName}/providers/" +
2020-11-24 06:07:36 +03:00
"Microsoft.Authorization/roleAssignments";
const provider = utils.getProvider(path);
assert.strictEqual(provider, "Microsoft.Authorization");
});
});
describe("Get value by json pointer", () => {
const resp = {
body: {
sku: {
sku: "standard",
},
properties: {
value: "kkk",
arrayValue: ["abc", "def"],
},
},
};
it("should return expected value", () => {
const jsonPointer = "/body/properties/value";
const res = utils.getValueByJsonPointer(resp, jsonPointer);
expect(res).toBe("kkk");
const secondArrayItemJsonPointer = "/body/properties/arrayValue/1";
const arrayItem = utils.getValueByJsonPointer(resp, secondArrayItemJsonPointer);
expect(arrayItem).toBe("def");
});
it("should throw error when the jsonPointer is invalid", () => {
const t = () => {
const jsonPointer = "/body/unknown";
utils.getValueByJsonPointer(resp, jsonPointer);
};
expect(t).toThrow("Invalid reference token: unknown");
});
});
describe("Get swagger path by object key&value", () => {
const key = "operationId";
const value = "ConfigurationStores_List";
const spec = {
"swagger": "2.0",
"paths": {
"/subscriptions/{subscriptionId}/providers/Microsoft.AppConfiguration/configurationStores": {
"get": {
"description": "Lists the configuration stores for a given resource group.",
"operationId": "ConfigurationStores_List"
}
},
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AppConfiguration/configurationStores": {
"get": {
"description": "Lists the configuration stores for a given resource group.",
"operationId": "ConfigurationStores_ListByResourceGroup"
}
},
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AppConfiguration/configurationStores/{configStoreName}": {
"put": {
"description": "Creates a configuration store with the specified parameters.",
"operationId": "ConfigurationStores_Create"
}
}
}
}
const paths = utils.findPathsToKey({key, obj: spec})
expect(paths).toEqual([".paths['/subscriptions/{subscriptionId}/providers/Microsoft.AppConfiguration/configurationStores'].get.operationId", ".paths['/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AppConfiguration/configurationStores'].get.operationId", ".paths['/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AppConfiguration/configurationStores/{configStoreName}'].put.operationId"]);
const path = utils.findPathToValue(paths, spec, value)
expect(path).toEqual([".paths['/subscriptions/{subscriptionId}/providers/Microsoft.AppConfiguration/configurationStores'].get.operationId"]);
})
2020-11-24 06:07:36 +03:00
});