oav/test/liveValidatorTests.ts

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

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
2018-06-04 16:16:47 +03:00
import assert from "assert"
import * as path from "path"
import * as os from "os"
import * as glob from "glob"
import { LiveValidator } from "../lib/validators/liveValidator"
import { Constants } from "../lib/util/constants"
import * as utils from "../lib/util/utils"
2018-06-04 16:16:47 +03:00
const livePaths = glob.sync(path.join(__dirname, "liveValidation/swaggers/**/live/*.json"))
describe("Live Validator", function () {
describe("Initialization", function () {
it("should initialize with defaults", function () {
let options = {
"swaggerPaths": [],
"git": {
"url": "https://github.com/Azure/azure-rest-api-specs.git",
"shouldClone": false
},
2018-06-04 16:16:47 +03:00
"directory": path.resolve(os.homedir(), "repo")
};
2018-05-30 01:38:27 +03:00
let validator = new LiveValidator()
assert.deepEqual(validator.cache, {})
assert.deepEqual(validator.options, options)
})
2018-06-04 16:16:47 +03:00
it("should initialize with user provided swaggerPaths", function () {
2018-05-30 01:38:27 +03:00
let swaggerPaths = ["swaggerPath1", "swaggerPath2"]
let options = {
"swaggerPaths": swaggerPaths,
"git": {
"url": "https://github.com/Azure/azure-rest-api-specs.git",
"shouldClone": false
},
2018-06-04 16:16:47 +03:00
"directory": path.resolve(os.homedir(), "repo")
2018-05-30 01:38:27 +03:00
}
let validator = new LiveValidator({ "swaggerPaths": swaggerPaths })
assert.deepEqual(validator.cache, {})
assert.deepEqual(validator.options, options)
})
2018-06-04 16:16:47 +03:00
it("should initialize with user provided swaggerPaths & directory", function () {
2018-05-30 01:38:27 +03:00
let swaggerPaths = ["swaggerPath1", "swaggerPath2"]
let directory = "/Users/username/repos/"
let options = {
"swaggerPaths": swaggerPaths,
"git": {
"url": "https://github.com/Azure/azure-rest-api-specs.git",
"shouldClone": false
},
"directory": directory
2018-05-30 01:38:27 +03:00
}
let validator = new LiveValidator({ "swaggerPaths": swaggerPaths, "directory": directory })
assert.deepEqual(validator.cache, {})
assert.deepEqual(validator.options, options)
})
2018-06-04 16:16:47 +03:00
it("should initialize with user provided partial git configuration", function () {
2018-05-30 01:38:27 +03:00
let swaggerPaths = ["swaggerPath1", "swaggerPath2"]
let directory = "/Users/username/repos/"
let git = {
2017-11-07 04:45:42 +03:00
"url": "https://github.com/Azure/azure-rest-api-specs.git"
}
let options = {
"swaggerPaths": swaggerPaths,
"git": {
"url": git.url,
"shouldClone": false
},
"directory": directory
2018-05-30 01:38:27 +03:00
}
let validator = new LiveValidator({
"swaggerPaths": swaggerPaths, "directory": directory, "git": git })
assert.deepEqual(validator.cache, {})
assert.deepEqual(validator.options, options)
})
2018-06-04 16:16:47 +03:00
it("should initialize with user provided full git configuration", function () {
2018-05-30 01:38:27 +03:00
let swaggerPaths = ["swaggerPath1", "swaggerPath2"]
let directory = "/Users/username/repos/"
let git = {
2017-11-07 04:45:42 +03:00
"url": "https://github.com/vladbarosan/azure-rest-api-specs.git",
"shouldClone": true,
"branch": "oav-test-branch"
}
let options = {
"swaggerPaths": swaggerPaths,
"git": git,
"directory": directory
2018-05-30 01:38:27 +03:00
}
let validator = new LiveValidator({
"swaggerPaths": swaggerPaths, "directory": directory, "git": git })
assert.deepEqual(validator.cache, {})
assert.deepEqual(validator.options, options)
})
2018-06-04 16:16:47 +03:00
it("should throw on invalid options types", function () {
assert.throws(() => {
2018-06-04 16:16:47 +03:00
new LiveValidator("string")
2018-05-30 01:38:27 +03:00
}, /must be of type "object"/)
assert.throws(() => {
2018-05-30 01:38:27 +03:00
new LiveValidator({ "swaggerPaths": "should be array" })
}, /must be of type "array"/)
assert.throws(() => {
2018-05-30 01:38:27 +03:00
new LiveValidator({ "git": 1 })
}, /must be of type "object"/)
assert.throws(() => {
2018-05-30 01:38:27 +03:00
new LiveValidator({ "git": { "url": [] } })
}, /must be of type "string"/)
assert.throws(() => {
2018-05-30 01:38:27 +03:00
new LiveValidator({ "git": { "url": "url", "shouldClone": "no" } })
}, /must be of type "boolean"/)
})
})
2018-06-04 16:16:47 +03:00
describe("Initialize cache", function () {
it("should initialize for arm-mediaservices", function (done) {
let expectedProvider = "microsoft.media"
let expectedApiVersion = "2015-10-01"
let options = {
"directory": "./test/liveValidation/swaggers/"
2018-05-30 01:38:27 +03:00
}
let validator = new LiveValidator(options)
validator.initialize().then(function () {
2018-05-30 01:38:27 +03:00
assert.equal(true, expectedProvider in validator.cache)
assert.equal(6, Object.keys(validator.cache).length)
assert.equal(true, expectedApiVersion in (validator.cache[expectedProvider]))
assert.equal(1, Object.keys(validator.cache[expectedProvider]).length)
2018-06-04 16:16:47 +03:00
assert.equal(2, validator.cache[expectedProvider][expectedApiVersion]["get"].length)
assert.equal(1, validator.cache[expectedProvider][expectedApiVersion]["put"].length)
assert.equal(1, validator.cache[expectedProvider][expectedApiVersion]["patch"].length)
assert.equal(1, validator.cache[expectedProvider][expectedApiVersion]["delete"].length)
assert.equal(4, validator.cache[expectedProvider][expectedApiVersion]["post"].length)
2018-05-30 01:38:27 +03:00
done()
2018-05-26 03:18:04 +03:00
}).catch((err: any) => {
2018-05-30 01:38:27 +03:00
assert.ifError(err)
done()
}).catch(done)
})
2018-06-04 16:16:47 +03:00
it("should initialize for arm-resources", function (done) {
let expectedProvider = "microsoft.resources"
let expectedApiVersion = "2016-09-01"
let options = {
"directory": "./test/liveValidation/swaggers/"
2018-05-30 01:38:27 +03:00
}
let validator = new LiveValidator(options)
validator.initialize().then(function () {
2018-05-30 01:38:27 +03:00
assert.equal(true, expectedProvider in validator.cache)
assert.equal(6, Object.keys(validator.cache).length)
assert.equal(true, expectedApiVersion in (validator.cache[expectedProvider]))
assert.equal(1, Object.keys(validator.cache[expectedProvider]).length)
// 'microsoft.resources' -> '2016-09-01'
2018-06-04 16:16:47 +03:00
assert.equal(2, validator.cache[expectedProvider][expectedApiVersion]["get"].length)
assert.equal(1, validator.cache[expectedProvider][expectedApiVersion]["delete"].length)
assert.equal(3, validator.cache[expectedProvider][expectedApiVersion]["post"].length)
assert.equal(1, validator.cache[expectedProvider][expectedApiVersion]["head"].length)
assert.equal(1, validator.cache[expectedProvider][expectedApiVersion]["put"].length)
// 'microsoft.unknown' -> 'unknown-api-version'
2018-05-30 01:38:27 +03:00
assert.equal(
2018-06-04 16:16:47 +03:00
4, validator.cache[Constants.unknownResourceProvider][Constants.unknownApiVersion]["post"].length)
2018-05-30 01:38:27 +03:00
assert.equal(
2018-06-04 16:16:47 +03:00
11, validator.cache[Constants.unknownResourceProvider][Constants.unknownApiVersion]["get"].length)
2018-05-30 01:38:27 +03:00
assert.equal(
2018-06-04 16:16:47 +03:00
3, validator.cache[Constants.unknownResourceProvider][Constants.unknownApiVersion]["head"].length)
2018-05-30 01:38:27 +03:00
assert.equal(
2018-06-04 16:16:47 +03:00
5, validator.cache[Constants.unknownResourceProvider][Constants.unknownApiVersion]["put"].length)
2018-05-30 01:38:27 +03:00
assert.equal(
2018-06-04 16:16:47 +03:00
5, validator.cache[Constants.unknownResourceProvider][Constants.unknownApiVersion]["delete"].length)
2018-05-30 01:38:27 +03:00
assert.equal(
2018-06-04 16:16:47 +03:00
1, validator.cache[Constants.unknownResourceProvider][Constants.unknownApiVersion]["patch"].length)
2018-05-30 01:38:27 +03:00
done()
2018-05-26 03:18:04 +03:00
}).catch((err: any) => {
2018-05-30 01:38:27 +03:00
assert.ifError(err)
done()
}).catch(done)
})
2018-06-04 16:16:47 +03:00
it("should initialize for all swaggers", function (done) {
let options = {
"directory": "./test/liveValidation/swaggers/"
2018-05-30 01:38:27 +03:00
}
let validator = new LiveValidator(options)
validator.initialize().then(function () {
2018-05-30 01:38:27 +03:00
assert.equal(6, Object.keys(validator.cache).length)
2018-06-04 16:16:47 +03:00
assert.equal(2, validator.cache["microsoft.resources"]["2016-09-01"]["get"].length)
assert.equal(1, validator.cache["microsoft.resources"]["2016-09-01"]["head"].length)
assert.equal(1, validator.cache["microsoft.media"]["2015-10-01"]["patch"].length)
assert.equal(4, validator.cache["microsoft.media"]["2015-10-01"]["post"].length)
assert.equal(2, validator.cache["microsoft.search"]["2015-02-28"]["get"].length)
assert.equal(3, validator.cache["microsoft.search"]["2015-08-19"]["get"].length)
assert.equal(1, validator.cache["microsoft.storage"]["2015-05-01-preview"]["patch"].length)
assert.equal(4, validator.cache["microsoft.storage"]["2015-06-15"]["get"].length)
assert.equal(3, validator.cache["microsoft.storage"]["2016-01-01"]["post"].length)
assert.equal(4, validator.cache["microsoft.test"]["2016-01-01"]["post"].length)
2018-05-30 01:38:27 +03:00
done()
2018-05-26 03:18:04 +03:00
}).catch((err: any) => {
2018-05-30 01:38:27 +03:00
assert.ifError(err)
done()
}).catch(done)
})
})
2018-06-04 16:16:47 +03:00
describe("Initialize cache and search", function () {
it("should return one matched operation for arm-storage", function (done) {
let options = {
"directory": "./test/liveValidation/swaggers/"
2018-05-30 01:38:27 +03:00
}
let listRequestUrl =
"https://management.azure.com/subscriptions/subscriptionId/providers/Microsoft.Storage/storageAccounts?api-version=2015-06-15"
let postRequestUrl =
"https://management.azure.com/subscriptions/subscriptionId/providers/Microsoft.Storage/checkNameAvailability?api-version=2015-06-15"
let deleteRequestUrl =
"https://management.azure.com/subscriptions/subscriptionId/resourceGroups/myRG/providers/Microsoft.Storage/storageAccounts/accname?api-version=2015-06-15"
let validator = new LiveValidator(options)
validator.initialize().then(function () {
// Operations to match is StorageAccounts_List
2018-06-04 16:16:47 +03:00
let operations = validator.getPotentialOperations(listRequestUrl, "Get").operations
2018-05-30 01:38:27 +03:00
assert.equal(
1, operations.length)
assert.equal(
"/subscriptions/{subscriptionId}/providers/Microsoft.Storage/storageAccounts",
operations[0].pathObject.path)
// Operations to match is StorageAccounts_CheckNameAvailability
2018-06-04 16:16:47 +03:00
operations = validator.getPotentialOperations(postRequestUrl, "PoSt").operations
2018-05-30 01:38:27 +03:00
assert.equal(1, operations.length)
assert.equal(
"/subscriptions/{subscriptionId}/providers/Microsoft.Storage/checkNameAvailability",
operations[0].pathObject.path)
// Operations to match is StorageAccounts_Delete
2018-06-04 16:16:47 +03:00
operations = validator.getPotentialOperations(deleteRequestUrl, "delete").operations
2018-05-30 01:38:27 +03:00
assert.equal(1, operations.length)
assert.equal(
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}",
operations[0].pathObject.path)
done()
2018-05-26 03:18:04 +03:00
}).catch((err: any) => {
2018-05-30 01:38:27 +03:00
assert.ifError(err)
done()
}).catch(done)
})
2018-06-04 16:16:47 +03:00
it("should return reason for not matched operations", function (done) {
let options = {
"directory": "./test/liveValidation/swaggers/"
2018-05-30 01:38:27 +03:00
}
let nonCachedApiUrl =
"https://management.azure.com/subscriptions/subscriptionId/providers/Microsoft.Storage/storageAccounts?api-version=2015-08-15"
let nonCachedProviderUrl =
"https://management.azure.com/subscriptions/subscriptionId/providers/Hello.World/checkNameAvailability?api-version=2015-06-15"
let nonCachedVerbUrl =
"https://management.azure.com/subscriptions/subscriptionId/resourceGroups/myRG/providers/Microsoft.Storage/storageAccounts/accname?api-version=2015-06-15"
let nonCachedPath =
"https://management.azure.com/subscriptions/subscriptionId/providers/Microsoft.Storage/storageAccounts/accountName/properties?api-version=2015-06-15"
let validator = new LiveValidator(options)
validator.initialize().then(function () {
// Operations to match is StorageAccounts_List with api-version 2015-08-15 [non cached api version]
2018-06-04 16:16:47 +03:00
let result = validator.getPotentialOperations(nonCachedApiUrl, "Get")
2018-05-30 01:38:27 +03:00
let operations = result.operations
let reason = result.reason
assert.equal(0, operations.length)
assert.equal(Constants.ErrorCodes.OperationNotFoundInCacheWithApi.name, reason.code)
// Operations to match is StorageAccounts_CheckNameAvailability with provider "Hello.World" [non cached provider]
2018-06-04 16:16:47 +03:00
result = validator.getPotentialOperations(nonCachedProviderUrl, "PoSt")
2018-05-30 01:38:27 +03:00
operations = result.operations
reason = result.reason
assert.equal(0, operations.length)
assert.equal(Constants.ErrorCodes.OperationNotFoundInCacheWithProvider.name, reason.code)
// Operations to match is StorageAccounts_Delete with verb "head" [non cached http verb]
2018-06-04 16:16:47 +03:00
result = validator.getPotentialOperations(nonCachedVerbUrl, "head")
2018-05-30 01:38:27 +03:00
operations = result.operations
reason = result.reason
assert.equal(0, operations.length)
assert.equal(Constants.ErrorCodes.OperationNotFoundInCacheWithVerb.name, reason.code)
// Operations to match is with path "subscriptions/subscriptionId/providers/Microsoft.Storage/storageAccounts/storageAccounts/accountName/properties/" [non cached path]
2018-06-04 16:16:47 +03:00
result = validator.getPotentialOperations(nonCachedPath, "get")
2018-05-30 01:38:27 +03:00
operations = result.operations
reason = result.reason
assert.equal(0, operations.length)
assert.equal(Constants.ErrorCodes.OperationNotFoundInCache.name, reason.code)
done()
2018-05-26 03:18:04 +03:00
}).catch((err: any) => {
2018-05-30 01:38:27 +03:00
assert.ifError(err)
done()
}).catch(done)
})
2018-06-04 16:16:47 +03:00
it("it should create an implicit default response and find it", function (done) {
2018-03-16 00:53:01 +03:00
let options = {
"directory": "./test/liveValidation/swaggers/specification/scenarios",
"swaggerPathsPattern": "**/*.json",
"shouldModelImplicitDefaultResponse": true
2018-05-30 01:38:27 +03:00
}
let apiUrl =
"https://management.azure.com/subscriptions/subscriptionId/providers/Microsoft.Test/storageAccounts?api-version=2016-01-01"
2018-03-16 00:53:01 +03:00
2018-05-30 01:38:27 +03:00
let validator = new LiveValidator(options)
2018-03-16 00:53:01 +03:00
validator.initialize().then(() => {
// Operations to match is StorageAccounts_List
2018-06-04 16:16:47 +03:00
let operations = validator.cache["microsoft.test"]["2016-01-01"]["post"]
2018-03-16 00:53:01 +03:00
for (const operation of operations) {
2018-05-30 01:38:27 +03:00
assert(operation.responses.default)
assert.deepEqual(operation.responses.default.schema.properties.error, utils.CloudError)
2018-03-16 00:53:01 +03:00
}
2018-05-30 01:38:27 +03:00
done()
2018-05-26 03:18:04 +03:00
}).catch((err: any) => {
2018-05-30 01:38:27 +03:00
assert.ifError(err)
done()
}).catch(done)
})
})
2018-06-04 16:16:47 +03:00
describe("Initialize cache and validate", function () {
2018-05-26 03:18:04 +03:00
livePaths.forEach((livePath: any) => {
it(`should validate request and response for "${livePath}"`, function (done) {
let options = {
"directory": "./test/liveValidation/swaggers/specification/storage",
"swaggerPathsPattern": "**/*.json"
2018-05-30 01:38:27 +03:00
}
let validator = new LiveValidator(options)
validator.initialize().then(function () {
2018-05-30 01:38:27 +03:00
let reqRes = require(livePath)
let validationResult = validator.validateLiveRequestResponse(reqRes)
console.dir(validationResult, { depth: null, colors: true })
done()
2018-05-26 03:18:04 +03:00
}).catch((err: any) => {
2018-05-30 01:38:27 +03:00
assert.ifError(err)
done()
}).catch(done)
})
})
})
})