2017-04-13 06:54:39 +03:00
// 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"
2017-04-13 06:54:39 +03:00
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 ( ) {
2017-04-13 09:49:14 +03:00
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" )
2017-04-13 09:49:14 +03:00
} ;
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" ]
2017-04-13 09:49:14 +03:00
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" ]
2017-04-13 09:49:14 +03:00
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" ]
2017-04-13 09:49:14 +03:00
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"
2017-04-13 09:49:14 +03:00
}
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" ]
2017-04-13 09:49:14 +03:00
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"
2017-04-13 09:49:14 +03:00
}
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 ( ) {
2017-04-13 09:49:14 +03:00
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"/ )
2017-04-13 09:49:14 +03:00
assert . throws ( ( ) = > {
2018-05-30 01:38:27 +03:00
new LiveValidator ( { "swaggerPaths" : "should be array" } )
} , /must be of type "array"/ )
2017-04-13 09:49:14 +03:00
assert . throws ( ( ) = > {
2018-05-30 01:38:27 +03:00
new LiveValidator ( { "git" : 1 } )
} , /must be of type "object"/ )
2017-04-13 09:49:14 +03:00
assert . throws ( ( ) = > {
2018-05-30 01:38:27 +03:00
new LiveValidator ( { "git" : { "url" : [ ] } } )
} , /must be of type "string"/ )
2017-04-13 09:49:14 +03:00
assert . throws ( ( ) = > {
2018-05-30 01:38:27 +03:00
new LiveValidator ( { "git" : { "url" : "url" , "shouldClone" : "no" } } )
} , /must be of type "boolean"/ )
} )
} )
2017-04-18 01:20:08 +03:00
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"
2017-04-13 09:49:14 +03:00
let options = {
2017-10-18 23:40:55 +03:00
"directory" : "./test/liveValidation/swaggers/"
2018-05-30 01:38:27 +03:00
}
let validator = new LiveValidator ( options )
2017-04-13 09:49:14 +03:00
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"
2017-04-13 09:49:14 +03:00
let options = {
2017-10-18 23:40:55 +03:00
"directory" : "./test/liveValidation/swaggers/"
2018-05-30 01:38:27 +03:00
}
let validator = new LiveValidator ( options )
2017-04-13 09:49:14 +03:00
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 )
2017-04-14 01:14:58 +03:00
// '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 )
2017-04-14 01:14:58 +03:00
// '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 ) {
2017-04-13 09:49:14 +03:00
let options = {
2017-10-18 23:40:55 +03:00
"directory" : "./test/liveValidation/swaggers/"
2018-05-30 01:38:27 +03:00
}
let validator = new LiveValidator ( options )
2017-04-13 09:49:14 +03:00
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 )
} )
} )
2017-04-18 01:20:08 +03:00
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 ) {
2017-04-13 09:49:14 +03:00
let options = {
2017-10-18 23:40:55 +03:00
"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 )
2017-04-13 09:49:14 +03:00
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 )
2017-04-13 09:42:48 +03:00
2017-04-13 09:49:14 +03:00
// 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 )
2017-04-13 09:42:48 +03:00
2017-04-13 09:49:14 +03:00
// 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 ) {
2017-04-18 01:20:08 +03:00
let options = {
2017-10-18 23:40:55 +03:00
"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 )
2017-04-18 01:20:08 +03:00
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 )
2017-04-18 01:20:08 +03:00
// 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 )
2017-04-18 01:20:08 +03:00
// 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 )
2017-04-18 01:20:08 +03:00
// 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 )
} )
} )
2017-04-18 01:20:08 +03:00
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 ) = > {
2017-04-16 08:59:25 +03:00
it ( ` should validate request and response for " ${ livePath } " ` , function ( done ) {
let options = {
2018-03-01 22:16:13 +03:00
"directory" : "./test/liveValidation/swaggers/specification/storage" ,
"swaggerPathsPattern" : "**/*.json"
2018-05-30 01:38:27 +03:00
}
let validator = new LiveValidator ( options )
2017-04-16 08:59:25 +03:00
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 )
} )
} )
} )
} )