housekeeping: reorganize and rewire Mocha tests (#5600)
* move Mocha-run tests to `test/mocha` * fix relative paths * fix JSX test paths * update stagnated JSX tests * `test/setup.js` -> `test/mocha/setup.js` * use regex+globstar for test matching * remove `console.log`
This commit is contained in:
147
test/mocha/core/plugins/auth/actions.js
Normal file
147
test/mocha/core/plugins/auth/actions.js
Normal file
@@ -0,0 +1,147 @@
|
||||
/* eslint-env mocha */
|
||||
import expect, { createSpy } from "expect"
|
||||
import { authorizeRequest } from "corePlugins/auth/actions"
|
||||
|
||||
describe("auth plugin - actions", () => {
|
||||
|
||||
describe("authorizeRequest", () => {
|
||||
|
||||
[
|
||||
[
|
||||
{
|
||||
oas3: true,
|
||||
server: "https://host/resource",
|
||||
scheme: "http",
|
||||
host: null,
|
||||
url: "http://specs/file",
|
||||
},
|
||||
"https://host/authorize"
|
||||
],
|
||||
[
|
||||
{
|
||||
oas3: false,
|
||||
server: null,
|
||||
scheme: "https",
|
||||
host: undefined,
|
||||
url: "https://specs/file",
|
||||
},
|
||||
"https://specs/authorize"
|
||||
],
|
||||
[
|
||||
{
|
||||
oas3: false,
|
||||
server: null,
|
||||
scheme: "https",
|
||||
host: "host",
|
||||
url: "http://specs/file",
|
||||
},
|
||||
"http://specs/authorize"
|
||||
],
|
||||
].forEach(([{oas3, server, scheme, host, url}, expectedFetchUrl]) => {
|
||||
it("should resolve authorization endpoint against the server URL", () => {
|
||||
|
||||
// Given
|
||||
const data = {
|
||||
url: "/authorize"
|
||||
}
|
||||
const system = {
|
||||
fn: {
|
||||
fetch: createSpy().andReturn(Promise.resolve())
|
||||
},
|
||||
getConfigs: () => ({}),
|
||||
authSelectors: {
|
||||
getConfigs: () => ({})
|
||||
},
|
||||
oas3Selectors: {
|
||||
selectedServer: () => server
|
||||
},
|
||||
specSelectors: {
|
||||
isOAS3: () => oas3,
|
||||
operationScheme: () => scheme,
|
||||
host: () => host,
|
||||
url: () => url
|
||||
}
|
||||
}
|
||||
|
||||
// When
|
||||
authorizeRequest(data)(system)
|
||||
|
||||
// Then
|
||||
expect(system.fn.fetch.calls.length).toEqual(1)
|
||||
expect(system.fn.fetch.calls[0].arguments[0]).toInclude({url: expectedFetchUrl})
|
||||
})
|
||||
})
|
||||
|
||||
it("should add additionalQueryStringParams to Swagger 2.0 authorization and token URLs", () => {
|
||||
|
||||
// Given
|
||||
const data = {
|
||||
url: "/authorize?q=1"
|
||||
}
|
||||
const system = {
|
||||
fn: {
|
||||
fetch: createSpy().andReturn(Promise.resolve())
|
||||
},
|
||||
getConfigs: () => ({}),
|
||||
authSelectors: {
|
||||
getConfigs: () => ({
|
||||
additionalQueryStringParams: {
|
||||
myCustomParam: "abc123"
|
||||
}
|
||||
})
|
||||
},
|
||||
specSelectors: {
|
||||
isOAS3: () => false,
|
||||
operationScheme: () => "https",
|
||||
host: () => "http://google.com",
|
||||
url: () => "http://google.com/swagger.json"
|
||||
}
|
||||
}
|
||||
|
||||
// When
|
||||
authorizeRequest(data)(system)
|
||||
|
||||
// Then
|
||||
expect(system.fn.fetch.calls.length).toEqual(1)
|
||||
|
||||
expect(system.fn.fetch.calls[0].arguments[0].url)
|
||||
.toEqual("http://google.com/authorize?q=1&myCustomParam=abc123")
|
||||
})
|
||||
|
||||
it("should add additionalQueryStringParams to OpenAPI 3.0 authorization and token URLs", () => {
|
||||
|
||||
// Given
|
||||
const data = {
|
||||
url: "/authorize?q=1"
|
||||
}
|
||||
const system = {
|
||||
fn: {
|
||||
fetch: createSpy().andReturn(Promise.resolve())
|
||||
},
|
||||
getConfigs: () => ({}),
|
||||
authSelectors: {
|
||||
getConfigs: () => ({
|
||||
additionalQueryStringParams: {
|
||||
myCustomParam: "abc123"
|
||||
}
|
||||
})
|
||||
},
|
||||
oas3Selectors: {
|
||||
selectedServer: () => "http://google.com"
|
||||
},
|
||||
specSelectors: {
|
||||
isOAS3: () => true,
|
||||
}
|
||||
}
|
||||
|
||||
// When
|
||||
authorizeRequest(data)(system)
|
||||
|
||||
// Then
|
||||
expect(system.fn.fetch.calls.length).toEqual(1)
|
||||
|
||||
expect(system.fn.fetch.calls[0].arguments[0].url)
|
||||
.toEqual("http://google.com/authorize?q=1&myCustomParam=abc123")
|
||||
})
|
||||
})
|
||||
})
|
||||
154
test/mocha/core/plugins/auth/preauthorize.js
Normal file
154
test/mocha/core/plugins/auth/preauthorize.js
Normal file
@@ -0,0 +1,154 @@
|
||||
/* eslint-env mocha */
|
||||
import expect from "expect"
|
||||
import { fromJS } from "immutable"
|
||||
import { preauthorizeBasic, preauthorizeApiKey } from "corePlugins/auth"
|
||||
import { authorize } from "corePlugins/auth/actions"
|
||||
|
||||
const S2_SYSTEM = {
|
||||
authActions: {
|
||||
authorize
|
||||
},
|
||||
specSelectors: {
|
||||
isOAS3: () => false,
|
||||
specJson: () => {
|
||||
return fromJS({
|
||||
swagger: "2.0",
|
||||
securityDefinitions: {
|
||||
"APIKeyHeader": {
|
||||
"type": "apiKey",
|
||||
"in": "header",
|
||||
"name": "X-API-Key"
|
||||
},
|
||||
"basicAuth": {
|
||||
"type": "basic"
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const OAI3_SYSTEM = {
|
||||
authActions: {
|
||||
authorize
|
||||
},
|
||||
specSelectors: {
|
||||
isOAS3: () => true,
|
||||
specJson: () => {
|
||||
return fromJS({
|
||||
openapi: "3.0.0",
|
||||
components: {
|
||||
securitySchemes: {
|
||||
basicAuth: {
|
||||
type: "http",
|
||||
scheme: "basic"
|
||||
},
|
||||
APIKeyHeader: {
|
||||
type: "apiKey",
|
||||
in: "header",
|
||||
name: "X-API-Key"
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
describe("auth plugin - preauthorizers", () => {
|
||||
describe("preauthorizeBasic", () => {
|
||||
it("should return a valid authorize action in Swagger 2", () => {
|
||||
const res = preauthorizeBasic(S2_SYSTEM, "basicAuth", "user", "pass")
|
||||
|
||||
expect(res).toEqual({
|
||||
type: "authorize",
|
||||
payload: {
|
||||
basicAuth: {
|
||||
schema: {
|
||||
type: "basic"
|
||||
},
|
||||
value: {
|
||||
username: "user",
|
||||
password: "pass"
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
it("should return a valid authorize action in OpenAPI 3", () => {
|
||||
const res = preauthorizeBasic(OAI3_SYSTEM, "basicAuth", "user", "pass")
|
||||
|
||||
expect(res).toEqual({
|
||||
type: "authorize",
|
||||
payload: {
|
||||
basicAuth: {
|
||||
schema: {
|
||||
type: "http",
|
||||
scheme: "basic"
|
||||
},
|
||||
value: {
|
||||
username: "user",
|
||||
password: "pass"
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
it("should return null when the authorization name is invalid in Swagger 2", () => {
|
||||
const res = preauthorizeBasic(S2_SYSTEM, "fakeBasicAuth", "user", "pass")
|
||||
|
||||
expect(res).toEqual(null)
|
||||
})
|
||||
it("should return null when the authorization name is invalid in OpenAPI 3", () => {
|
||||
const res = preauthorizeBasic(OAI3_SYSTEM, "fakeBasicAuth", "user", "pass")
|
||||
|
||||
expect(res).toEqual(null)
|
||||
})
|
||||
})
|
||||
describe("preauthorizeApiKey", () => {
|
||||
it("should return a valid authorize action in Swagger 2", () => {
|
||||
const res = preauthorizeApiKey(S2_SYSTEM, "APIKeyHeader", "Asdf1234")
|
||||
|
||||
expect(res).toEqual({
|
||||
type: "authorize",
|
||||
payload: {
|
||||
APIKeyHeader: {
|
||||
schema: {
|
||||
type: "apiKey",
|
||||
name: "X-API-Key",
|
||||
"in": "header"
|
||||
},
|
||||
value: "Asdf1234"
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
it("should return a valid authorize action in OpenAPI 3", () => {
|
||||
const res = preauthorizeApiKey(OAI3_SYSTEM, "APIKeyHeader", "Asdf1234")
|
||||
|
||||
expect(res).toEqual({
|
||||
type: "authorize",
|
||||
payload: {
|
||||
APIKeyHeader: {
|
||||
schema: {
|
||||
type: "apiKey",
|
||||
"in": "header",
|
||||
name: "X-API-Key"
|
||||
},
|
||||
value: "Asdf1234"
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
it("should return null when the authorization name is invalid in Swagger 2", () => {
|
||||
const res = preauthorizeApiKey(S2_SYSTEM, "FakeAPIKeyHeader", "Asdf1234")
|
||||
|
||||
expect(res).toEqual(null)
|
||||
})
|
||||
it("should return null when the authorization name is invalid in OpenAPI 3", () => {
|
||||
const res = preauthorizeApiKey(OAI3_SYSTEM, "FakeAPIKeyHeader", "Asdf1234")
|
||||
|
||||
expect(res).toEqual(null)
|
||||
})
|
||||
})
|
||||
})
|
||||
133
test/mocha/core/plugins/auth/selectors.js
Normal file
133
test/mocha/core/plugins/auth/selectors.js
Normal file
@@ -0,0 +1,133 @@
|
||||
/* eslint-env mocha */
|
||||
import expect from "expect"
|
||||
import { fromJS } from "immutable"
|
||||
import { definitionsToAuthorize, definitionsForRequirements } from "corePlugins/auth/selectors"
|
||||
|
||||
describe("auth plugin - selectors", () => {
|
||||
describe("definitionsToAuthorize", () => {
|
||||
it("should return securityDefinitions as a List", () => {
|
||||
const securityDefinitions = {
|
||||
"petstore_auth": {
|
||||
"type": "oauth2",
|
||||
"authorizationUrl": "http://petstore.swagger.io/oauth/dialog",
|
||||
"flow": "implicit",
|
||||
"scopes": {
|
||||
"write:pets": "modify pets in your account",
|
||||
"read:pets": "read your pets"
|
||||
}
|
||||
},
|
||||
"api_key": {
|
||||
"type": "apiKey",
|
||||
"name": "api_key",
|
||||
"in": "header"
|
||||
}
|
||||
}
|
||||
|
||||
const system = {
|
||||
specSelectors: {
|
||||
securityDefinitions() {
|
||||
return fromJS(securityDefinitions)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const res = definitionsToAuthorize({})(system)
|
||||
|
||||
expect(res.toJS()).toEqual([
|
||||
{
|
||||
"petstore_auth": securityDefinitions["petstore_auth"]
|
||||
},
|
||||
{
|
||||
"api_key": securityDefinitions["api_key"]
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should fail gracefully with bad data", () => {
|
||||
const securityDefinitions = null
|
||||
|
||||
const system = {
|
||||
specSelectors: {
|
||||
securityDefinitions() {
|
||||
return fromJS(securityDefinitions)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const res = definitionsToAuthorize({})(system)
|
||||
|
||||
expect(res.toJS()).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe("definitionsForRequirements", () => {
|
||||
it("should return applicable securityDefinitions as a List", () => {
|
||||
const securityDefinitions = {
|
||||
"petstore_auth": {
|
||||
"type": "oauth2",
|
||||
"authorizationUrl": "http://petstore.swagger.io/oauth/dialog",
|
||||
"flow": "implicit",
|
||||
"scopes": {
|
||||
"write:pets": "modify pets in your account",
|
||||
"read:pets": "read your pets"
|
||||
}
|
||||
},
|
||||
"api_key": {
|
||||
"type": "apiKey",
|
||||
"name": "api_key",
|
||||
"in": "header"
|
||||
}
|
||||
}
|
||||
|
||||
const system = {
|
||||
authSelectors: {
|
||||
definitionsToAuthorize() {
|
||||
return fromJS([
|
||||
{
|
||||
"petstore_auth": securityDefinitions["petstore_auth"]
|
||||
},
|
||||
{
|
||||
"api_key": securityDefinitions["api_key"]
|
||||
},
|
||||
])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const securities = fromJS([
|
||||
{
|
||||
"petstore_auth": [
|
||||
"write:pets",
|
||||
"read:pets"
|
||||
]
|
||||
}
|
||||
])
|
||||
|
||||
const res = definitionsForRequirements({}, securities)(system)
|
||||
|
||||
expect(res.toJS()).toEqual([
|
||||
{
|
||||
"petstore_auth": securityDefinitions["petstore_auth"]
|
||||
}
|
||||
])
|
||||
})
|
||||
|
||||
it("should fail gracefully with bad data", () => {
|
||||
const securityDefinitions = null
|
||||
|
||||
const system = {
|
||||
authSelectors: {
|
||||
definitionsToAuthorize() {
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const securities = null
|
||||
|
||||
const res = definitionsForRequirements({}, securities)(system)
|
||||
|
||||
expect(res.toJS()).toEqual([])
|
||||
})
|
||||
})
|
||||
})
|
||||
39
test/mocha/core/plugins/auth/wrap-spec-actions.js
Normal file
39
test/mocha/core/plugins/auth/wrap-spec-actions.js
Normal file
@@ -0,0 +1,39 @@
|
||||
/* eslint-env mocha */
|
||||
import expect, { createSpy } from "expect"
|
||||
import { execute } from "corePlugins/auth/spec-wrap-actions"
|
||||
|
||||
describe("spec plugin - actions", function(){
|
||||
|
||||
describe("execute", function(){
|
||||
|
||||
xit("should add `securities` to the oriAction call", function(){
|
||||
// Given
|
||||
const system = {
|
||||
authSelectors: {
|
||||
authorized: createSpy().andReturn({some: "security"})
|
||||
}
|
||||
}
|
||||
const oriExecute = createSpy()
|
||||
|
||||
// When
|
||||
let executeFn = execute(oriExecute, system)
|
||||
executeFn({})
|
||||
|
||||
// Then
|
||||
expect(oriExecute.calls.length).toEqual(1)
|
||||
expect(oriExecute.calls[0].arguments[0]).toEqual({
|
||||
extras: {
|
||||
security: {
|
||||
some: "security"
|
||||
}
|
||||
},
|
||||
method: undefined,
|
||||
path: undefined,
|
||||
operation: undefined
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
})
|
||||
27
test/mocha/core/plugins/configs/actions.js
Normal file
27
test/mocha/core/plugins/configs/actions.js
Normal file
@@ -0,0 +1,27 @@
|
||||
/* eslint-env mocha */
|
||||
import expect, { createSpy } from "expect"
|
||||
import { downloadConfig } from "corePlugins/configs/spec-actions"
|
||||
|
||||
describe("configs plugin - actions", () => {
|
||||
|
||||
describe("downloadConfig", () => {
|
||||
it("should call the system fetch helper with a provided request", () => {
|
||||
const fetchSpy = createSpy(async () => {}).andCallThrough()
|
||||
const system = {
|
||||
fn: {
|
||||
fetch: fetchSpy
|
||||
}
|
||||
}
|
||||
|
||||
const req = {
|
||||
url: "http://swagger.io/one",
|
||||
requestInterceptor: a => a,
|
||||
responseInterceptor: a => a,
|
||||
}
|
||||
|
||||
downloadConfig(req)(system)
|
||||
|
||||
expect(fetchSpy).toHaveBeenCalledWith(req)
|
||||
})
|
||||
})
|
||||
})
|
||||
55
test/mocha/core/plugins/err/transformers/not-of-type.js
Normal file
55
test/mocha/core/plugins/err/transformers/not-of-type.js
Normal file
@@ -0,0 +1,55 @@
|
||||
import expect from "expect"
|
||||
import { Map, List } from "immutable"
|
||||
import { transform } from "corePlugins/err/error-transformers/transformers/not-of-type"
|
||||
|
||||
describe("err plugin - tranformers - not of type", () => {
|
||||
|
||||
it("should transform a singular not of type(s) error without an inline path", () => {
|
||||
let ori = List([
|
||||
Map({
|
||||
path: "info.version",
|
||||
message: "is not of a type(s) string"
|
||||
})
|
||||
])
|
||||
|
||||
let res = transform(ori).toJS()
|
||||
|
||||
expect(res).toEqual([{
|
||||
path: "info.version",
|
||||
message: "should be a string"
|
||||
}])
|
||||
})
|
||||
|
||||
it("should transform a plural (2) not of type(s) error without an inline path", () => {
|
||||
let ori = List([
|
||||
Map({
|
||||
path: "info.version",
|
||||
message: "is not of a type(s) string,array"
|
||||
})
|
||||
])
|
||||
|
||||
let res = transform(ori).toJS()
|
||||
|
||||
expect(res).toEqual([{
|
||||
path: "info.version",
|
||||
message: "should be a string or array"
|
||||
}])
|
||||
})
|
||||
|
||||
it("should transform a plural (3+) not of type(s) error without an inline path", () => {
|
||||
let ori = List([
|
||||
Map({
|
||||
path: "info.version",
|
||||
message: "is not of a type(s) string,array,number"
|
||||
})
|
||||
])
|
||||
|
||||
let res = transform(ori).toJS()
|
||||
|
||||
expect(res).toEqual([{
|
||||
path: "info.version",
|
||||
message: "should be a string, array, or number"
|
||||
}])
|
||||
})
|
||||
|
||||
})
|
||||
132
test/mocha/core/plugins/err/transformers/parameter-oneof.js
Normal file
132
test/mocha/core/plugins/err/transformers/parameter-oneof.js
Normal file
@@ -0,0 +1,132 @@
|
||||
/* eslint-disable no-useless-escape */
|
||||
import expect from "expect"
|
||||
import { fromJS } from "immutable"
|
||||
import { transform } from "corePlugins/err/error-transformers/transformers/parameter-oneof"
|
||||
|
||||
describe.skip("err plugin - tranformers - parameter oneof", () => {
|
||||
|
||||
describe("parameter.in misuse transformation to fixed value error", () => {
|
||||
|
||||
it("should return a helpful error for invalid 'in' values", () => {
|
||||
const jsSpec = {
|
||||
paths: {
|
||||
"/CoolPath/": {
|
||||
get: {
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
in: "heder"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const jsonSchemaError = {
|
||||
"level": "error",
|
||||
"path": "paths.\/CoolPath\/.get.parameters[0]",
|
||||
"message": "is not exactly one from <#\/definitions\/parameter>,<#\/definitions\/jsonReference>",
|
||||
"source": "schema",
|
||||
"type": "spec"
|
||||
}
|
||||
|
||||
let res = transform(fromJS([jsonSchemaError]), { jsSpec })
|
||||
|
||||
expect(res.toJS()).toEqual([{
|
||||
path: "paths./CoolPath/.get.parameters[0].in",
|
||||
message: "Wrong value for the \"in\" keyword. Expected one of: path, query, header, body, formData.",
|
||||
level: "error",
|
||||
source: "schema",
|
||||
type: "spec"
|
||||
}])
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe("parameter.collectionFormat misuse transformation to fixed value error", () => {
|
||||
it("should return a helpful error for invalid 'collectionFormat' values", () => {
|
||||
const jsSpec = {
|
||||
paths: {
|
||||
"/CoolPath/": {
|
||||
get: {
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
in: "query",
|
||||
collectionFormat: "asdf"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const jsonSchemaError = {
|
||||
"level": "error",
|
||||
"path": "paths.\/CoolPath\/.get.parameters[0]",
|
||||
"message": "is not exactly one from <#\/definitions\/parameter>,<#\/definitions\/jsonReference>",
|
||||
"source": "schema",
|
||||
"type": "spec"
|
||||
}
|
||||
|
||||
let res = transform(fromJS([jsonSchemaError]), { jsSpec })
|
||||
|
||||
expect(res.toJS()).toEqual([{
|
||||
path: "paths./CoolPath/.get.parameters[0].collectionFormat",
|
||||
message: "Wrong value for the \"collectionFormat\" keyword. Expected one of: csv, ssv, tsv, pipes, multi.",
|
||||
level: "error",
|
||||
source: "schema",
|
||||
type: "spec"
|
||||
}])
|
||||
})
|
||||
})
|
||||
|
||||
describe("Integrations", () => {
|
||||
it("should return the correct errors when both 'in' and 'collectionFormat' are incorrect", () => {
|
||||
const jsSpec = {
|
||||
paths: {
|
||||
"/CoolPath/": {
|
||||
get: {
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
in: "blah",
|
||||
collectionFormat: "asdf"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const jsonSchemaError = {
|
||||
"level": "error",
|
||||
"path": "paths.\/CoolPath\/.get.parameters[0]",
|
||||
"message": "is not exactly one from <#\/definitions\/parameter>,<#\/definitions\/jsonReference>",
|
||||
"source": "schema",
|
||||
"type": "spec"
|
||||
}
|
||||
|
||||
let res = transform(fromJS([jsonSchemaError]), { jsSpec })
|
||||
|
||||
expect(res.toJS()).toEqual([
|
||||
{
|
||||
path: "paths./CoolPath/.get.parameters[0].in",
|
||||
message: "Wrong value for the \"in\" keyword. Expected one of: path, query, header, body, formData.",
|
||||
level: "error",
|
||||
source: "schema",
|
||||
type: "spec"
|
||||
},
|
||||
{
|
||||
path: "paths./CoolPath/.get.parameters[0].collectionFormat",
|
||||
message: "Wrong value for the \"collectionFormat\" keyword. Expected one of: csv, ssv, tsv, pipes, multi.",
|
||||
level: "error",
|
||||
source: "schema",
|
||||
type: "spec"
|
||||
}
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
25
test/mocha/core/plugins/filter/opsFilter.js
Normal file
25
test/mocha/core/plugins/filter/opsFilter.js
Normal file
@@ -0,0 +1,25 @@
|
||||
import { Map } from "immutable"
|
||||
import opsFilter from "corePlugins/filter/opsFilter"
|
||||
import expect from "expect"
|
||||
|
||||
describe("opsFilter", function() {
|
||||
const taggedOps = Map([["pet"], ["store"], ["user"]])
|
||||
|
||||
it("should filter taggedOps by tag name", function () {
|
||||
const filtered = opsFilter(taggedOps, "sto")
|
||||
|
||||
expect(filtered.size).toEqual(1)
|
||||
})
|
||||
|
||||
it("should return all taggedOps when search phrase is empty", function () {
|
||||
const filtered = opsFilter(taggedOps, "")
|
||||
|
||||
expect(filtered.size).toEqual(taggedOps.size)
|
||||
})
|
||||
|
||||
it("should return empty result when there is no match", function () {
|
||||
const filtered = opsFilter(taggedOps, "NoMatch")
|
||||
|
||||
expect(filtered.size).toEqual(0)
|
||||
})
|
||||
})
|
||||
68
test/mocha/core/plugins/oas3/helpers.js
Normal file
68
test/mocha/core/plugins/oas3/helpers.js
Normal file
@@ -0,0 +1,68 @@
|
||||
import { fromJS } from "immutable"
|
||||
import { isOAS3, isSwagger2 } from "corePlugins/oas3/helpers"
|
||||
import expect from "expect"
|
||||
|
||||
const isOAS3Shorthand = (version) => isOAS3(fromJS({
|
||||
openapi: version
|
||||
}))
|
||||
|
||||
const isSwagger2Shorthand = (version) => isSwagger2(fromJS({
|
||||
swagger: version
|
||||
}))
|
||||
|
||||
describe("isOAS3", function () {
|
||||
it("should recognize valid OAS3 version values", function () {
|
||||
expect(isOAS3Shorthand("3.0.0")).toEqual(true)
|
||||
expect(isOAS3Shorthand("3.0.1")).toEqual(true)
|
||||
expect(isOAS3Shorthand("3.0.11111")).toEqual(true)
|
||||
expect(isOAS3Shorthand("3.0.0-rc0")).toEqual(true)
|
||||
})
|
||||
|
||||
it("should fail for invalid OAS3 version values", function () {
|
||||
expect(isOAS3Shorthand("3.0")).toEqual(false)
|
||||
expect(isOAS3Shorthand("3.0.")).toEqual(false)
|
||||
expect(isOAS3Shorthand("2.0")).toEqual(false)
|
||||
})
|
||||
|
||||
it("should gracefully fail for non-string values", function () {
|
||||
expect(isOAS3Shorthand(3.0)).toEqual(false)
|
||||
expect(isOAS3Shorthand(3)).toEqual(false)
|
||||
expect(isOAS3Shorthand({})).toEqual(false)
|
||||
expect(isOAS3Shorthand(null)).toEqual(false)
|
||||
})
|
||||
|
||||
it("should gracefully fail when `openapi` field is missing", function () {
|
||||
expect(isOAS3(fromJS({
|
||||
openApi: "3.0.0"
|
||||
}))).toEqual(false)
|
||||
expect(isOAS3Shorthand(null)).toEqual(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("isSwagger2", function () {
|
||||
it("should recognize valid Swagger 2.0 version values", function () {
|
||||
expect(isSwagger2Shorthand("2.0")).toEqual(true)
|
||||
expect(isSwagger2Shorthand("2.0-rc0")).toEqual(true)
|
||||
})
|
||||
|
||||
it("should fail for invalid Swagger 2.0 version values", function () {
|
||||
expect(isSwagger2Shorthand("3.0")).toEqual(false)
|
||||
expect(isSwagger2Shorthand("3.0.")).toEqual(false)
|
||||
expect(isSwagger2Shorthand("2.1")).toEqual(false)
|
||||
expect(isSwagger2Shorthand("1.2")).toEqual(false)
|
||||
expect(isSwagger2Shorthand("2")).toEqual(false)
|
||||
})
|
||||
|
||||
it("should gracefully fail for non-string values", function () {
|
||||
expect(isSwagger2Shorthand(2.0)).toEqual(false)
|
||||
expect(isSwagger2Shorthand(2)).toEqual(false)
|
||||
expect(isSwagger2Shorthand({})).toEqual(false)
|
||||
expect(isSwagger2Shorthand(null)).toEqual(false)
|
||||
})
|
||||
|
||||
it("should gracefully fail when `swagger` field is missing", function () {
|
||||
expect(isSwagger2(fromJS({
|
||||
Swagger: "2.0"
|
||||
}))).toEqual(false)
|
||||
})
|
||||
})
|
||||
77
test/mocha/core/plugins/oas3/servers-wrapper.jsx
Normal file
77
test/mocha/core/plugins/oas3/servers-wrapper.jsx
Normal file
@@ -0,0 +1,77 @@
|
||||
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import expect from "expect"
|
||||
import { mount } from "enzyme"
|
||||
import { fromJS } from "immutable"
|
||||
import ServersContainer from "core/plugins/oas3/components/servers-container"
|
||||
import Servers from "core/plugins/oas3/components/servers"
|
||||
import { Col } from "components/layout-utils"
|
||||
|
||||
describe("<ServersContainer/>", function(){
|
||||
|
||||
const components = {
|
||||
Servers,
|
||||
Col
|
||||
}
|
||||
const mockedProps = {
|
||||
specSelectors: {
|
||||
servers() {}
|
||||
},
|
||||
oas3Selectors: {
|
||||
selectedServer() {},
|
||||
serverVariableValue() {},
|
||||
serverEffectiveValue() {}
|
||||
},
|
||||
oas3Actions: {
|
||||
setSelectedServer() {},
|
||||
setServerVariableValue() {}
|
||||
},
|
||||
getComponent: c => components[c]
|
||||
}
|
||||
|
||||
it("renders Servers inside ServersContainer if servers are provided", function(){
|
||||
|
||||
// Given
|
||||
let props = {...mockedProps}
|
||||
props.specSelectors = {...mockedProps.specSelectors}
|
||||
props.specSelectors.servers = function() {return fromJS([{url: "http://server1.com"}])}
|
||||
props.oas3Selectors = {...mockedProps.oas3Selectors}
|
||||
props.oas3Selectors.selectedServer = function() {return "http://server1.com"}
|
||||
|
||||
// When
|
||||
let wrapper = mount(<ServersContainer {...props}/>)
|
||||
|
||||
// Then
|
||||
const renderedServers = wrapper.find(Servers)
|
||||
expect(renderedServers.length).toEqual(1)
|
||||
})
|
||||
|
||||
it("does not render Servers inside ServersContainer if servers are empty", function(){
|
||||
|
||||
// Given
|
||||
let props = {...mockedProps}
|
||||
props.specSelectors = {...mockedProps.specSelectors}
|
||||
props.specSelectors.servers = function() {return fromJS([])}
|
||||
|
||||
// When
|
||||
let wrapper = mount(<ServersContainer {...props}/>)
|
||||
|
||||
// Then
|
||||
const renderedServers = wrapper.find(Servers)
|
||||
expect(renderedServers.length).toEqual(0)
|
||||
})
|
||||
|
||||
it("does not render Servers inside ServersContainer if servers are undefined", function(){
|
||||
|
||||
// Given
|
||||
let props = {...mockedProps}
|
||||
|
||||
// When
|
||||
let wrapper = mount(<ServersContainer {...props}/>)
|
||||
|
||||
// Then
|
||||
const renderedServers = wrapper.find(Servers)
|
||||
expect(renderedServers.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
359
test/mocha/core/plugins/oas3/state-integration.js
Normal file
359
test/mocha/core/plugins/oas3/state-integration.js
Normal file
@@ -0,0 +1,359 @@
|
||||
import expect from "expect"
|
||||
import { fromJS, OrderedMap } from "immutable"
|
||||
|
||||
import {
|
||||
selectedServer,
|
||||
serverVariableValue,
|
||||
serverVariables,
|
||||
serverEffectiveValue
|
||||
} from "corePlugins/oas3/selectors"
|
||||
|
||||
import reducers from "corePlugins/oas3/reducers"
|
||||
|
||||
import {
|
||||
setSelectedServer,
|
||||
setServerVariableValue,
|
||||
} from "corePlugins/oas3/actions"
|
||||
|
||||
describe("OAS3 plugin - state", function() {
|
||||
describe("action + reducer + selector integration", function() {
|
||||
describe("selectedServer", function() {
|
||||
it("should set and get a global selectedServer", function() {
|
||||
const state = new OrderedMap()
|
||||
const system = {
|
||||
// needed to handle `onlyOAS3` wrapper
|
||||
getSystem() {
|
||||
return {
|
||||
specSelectors: {
|
||||
specJson: () => {
|
||||
return fromJS({ openapi: "3.0.0" })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create the action
|
||||
const action = setSelectedServer("http://google.com")
|
||||
|
||||
// Collect the new state
|
||||
const newState = reducers["oas3_set_servers"](state, action)
|
||||
|
||||
// Get the value with the selector
|
||||
const res = selectedServer(newState)(system)
|
||||
|
||||
expect(res).toEqual("http://google.com")
|
||||
})
|
||||
|
||||
it("should set and get a namespaced selectedServer", function() {
|
||||
const state = fromJS({
|
||||
selectedServer: "http://yahoo.com"
|
||||
})
|
||||
const system = {
|
||||
// needed to handle `onlyOAS3` wrapper
|
||||
getSystem() {
|
||||
return {
|
||||
specSelectors: {
|
||||
specJson: () => {
|
||||
return fromJS({ openapi: "3.0.0" })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create the action
|
||||
const action = setSelectedServer("http://google.com", "myOperation")
|
||||
|
||||
// Collect the new state
|
||||
const newState = reducers["oas3_set_servers"](state, action)
|
||||
|
||||
// Get the value with the selector
|
||||
const res = selectedServer(newState, "myOperation")(system)
|
||||
|
||||
// Get the global selected server
|
||||
const globalRes = selectedServer(newState)(system)
|
||||
|
||||
expect(res).toEqual("http://google.com")
|
||||
expect(globalRes).toEqual("http://yahoo.com")
|
||||
})
|
||||
})
|
||||
|
||||
describe("serverVariableValue", function() {
|
||||
it("should set and get a global serverVariableValue", function() {
|
||||
const state = new OrderedMap()
|
||||
const system = {
|
||||
// needed to handle `onlyOAS3` wrapper
|
||||
getSystem() {
|
||||
return {
|
||||
specSelectors: {
|
||||
specJson: () => {
|
||||
return fromJS({ openapi: "3.0.0" })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create the action
|
||||
const action = setServerVariableValue({
|
||||
server: "google.com",
|
||||
key: "foo",
|
||||
val: "bar"
|
||||
})
|
||||
|
||||
// Collect the new state
|
||||
const newState = reducers["oas3_set_server_variable_value"](state, action)
|
||||
|
||||
// Get the value with the selector
|
||||
const res = serverVariableValue(newState, "google.com", "foo")(system)
|
||||
|
||||
expect(res).toEqual("bar")
|
||||
})
|
||||
it("should set and get a namespaced serverVariableValue", function() {
|
||||
const state = fromJS({
|
||||
serverVariableValues: {
|
||||
"google.com": {
|
||||
foo: "123"
|
||||
}
|
||||
}
|
||||
})
|
||||
const system = {
|
||||
// needed to handle `onlyOAS3` wrapper
|
||||
getSystem() {
|
||||
return {
|
||||
specSelectors: {
|
||||
specJson: () => {
|
||||
return fromJS({ openapi: "3.0.0" })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create the action
|
||||
const action = setServerVariableValue({
|
||||
namespace: "myOperation",
|
||||
server: "google.com",
|
||||
key: "foo",
|
||||
val: "bar"
|
||||
})
|
||||
|
||||
// Collect the new state
|
||||
const newState = reducers["oas3_set_server_variable_value"](state, action)
|
||||
|
||||
// Get the value with the selector
|
||||
const res = serverVariableValue(newState, {
|
||||
namespace: "myOperation",
|
||||
server: "google.com"
|
||||
}, "foo")(system)
|
||||
|
||||
// Get the global value, to cross-check
|
||||
const globalRes = serverVariableValue(newState, {
|
||||
server: "google.com"
|
||||
}, "foo")(system)
|
||||
|
||||
expect(res).toEqual("bar")
|
||||
expect(globalRes).toEqual("123")
|
||||
})
|
||||
})
|
||||
|
||||
describe("serverVariables", function() {
|
||||
it("should set and get global serverVariables", function() {
|
||||
const state = new OrderedMap()
|
||||
const system = {
|
||||
// needed to handle `onlyOAS3` wrapper
|
||||
getSystem() {
|
||||
return {
|
||||
specSelectors: {
|
||||
specJson: () => {
|
||||
return fromJS({ openapi: "3.0.0" })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create the action
|
||||
const action = setServerVariableValue({
|
||||
server: "google.com",
|
||||
key: "foo",
|
||||
val: "bar"
|
||||
})
|
||||
|
||||
// Collect the new state
|
||||
const newState = reducers["oas3_set_server_variable_value"](state, action)
|
||||
|
||||
// Get the value with the selector
|
||||
const res = serverVariables(newState, "google.com", "foo")(system)
|
||||
|
||||
expect(res.toJS()).toEqual({
|
||||
foo: "bar"
|
||||
})
|
||||
})
|
||||
|
||||
it("should set and get namespaced serverVariables", function() {
|
||||
const state = fromJS({
|
||||
serverVariableValues: {
|
||||
"google.com": {
|
||||
foo: "123"
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const system = {
|
||||
// needed to handle `onlyOAS3` wrapper
|
||||
getSystem() {
|
||||
return {
|
||||
specSelectors: {
|
||||
specJson: () => {
|
||||
return fromJS({ openapi: "3.0.0" })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create the action
|
||||
const action = setServerVariableValue({
|
||||
namespace: "myOperation",
|
||||
server: "google.com",
|
||||
key: "foo",
|
||||
val: "bar"
|
||||
})
|
||||
|
||||
// Collect the new state
|
||||
const newState = reducers["oas3_set_server_variable_value"](state, action)
|
||||
|
||||
// Get the value with the selector
|
||||
const res = serverVariables(newState, {
|
||||
namespace: "myOperation",
|
||||
server: "google.com"
|
||||
}, "foo")(system)
|
||||
|
||||
// Get the global value, to cross-check
|
||||
const globalRes = serverVariables(newState, {
|
||||
server: "google.com"
|
||||
}, "foo")(system)
|
||||
|
||||
expect(res.toJS()).toEqual({
|
||||
foo: "bar"
|
||||
})
|
||||
|
||||
expect(globalRes.toJS()).toEqual({
|
||||
foo: "123"
|
||||
})
|
||||
})
|
||||
})
|
||||
describe("serverEffectiveValue", function() {
|
||||
it("should set variable values and compute a URL for a namespaced server", function() {
|
||||
const state = fromJS({
|
||||
serverVariableValues: {
|
||||
"google.com/{foo}": {
|
||||
foo: "123"
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const system = {
|
||||
// needed to handle `onlyOAS3` wrapper
|
||||
getSystem() {
|
||||
return {
|
||||
specSelectors: {
|
||||
specJson: () => {
|
||||
return fromJS({ openapi: "3.0.0" })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create the action
|
||||
const action = setServerVariableValue({
|
||||
namespace: "myOperation",
|
||||
server: "google.com/{foo}",
|
||||
key: "foo",
|
||||
val: "bar"
|
||||
})
|
||||
|
||||
// Collect the new state
|
||||
const newState = reducers["oas3_set_server_variable_value"](state, action)
|
||||
|
||||
// Get the value with the selector
|
||||
const res = serverEffectiveValue(newState, {
|
||||
namespace: "myOperation",
|
||||
server: "google.com/{foo}"
|
||||
})(system)
|
||||
|
||||
// Get the global value, to cross-check
|
||||
const globalRes = serverEffectiveValue(newState, {
|
||||
server: "google.com/{foo}"
|
||||
})(system)
|
||||
|
||||
expect(res).toEqual("google.com/bar")
|
||||
|
||||
expect(globalRes).toEqual("google.com/123")
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
describe("selectors", function() {
|
||||
describe("serverEffectiveValue", function() {
|
||||
it("should compute global serverEffectiveValues", function() {
|
||||
const state = fromJS({
|
||||
serverVariableValues: {
|
||||
"google.com/{foo}/{bar}": {
|
||||
foo: "123",
|
||||
bar: "456"
|
||||
}
|
||||
}
|
||||
})
|
||||
const system = {
|
||||
// needed to handle `onlyOAS3` wrapper
|
||||
getSystem() {
|
||||
return {
|
||||
specSelectors: {
|
||||
specJson: () => {
|
||||
return fromJS({ openapi: "3.0.0" })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get the value with the selector
|
||||
const res = serverEffectiveValue(state, "google.com/{foo}/{bar}")(system)
|
||||
|
||||
expect(res).toEqual("google.com/123/456")
|
||||
})
|
||||
|
||||
it("should handle multiple variable instances", function() {
|
||||
const state = fromJS({
|
||||
serverVariableValues: {
|
||||
"google.com/{foo}/{foo}/{bar}": {
|
||||
foo: "123",
|
||||
bar: "456"
|
||||
}
|
||||
}
|
||||
})
|
||||
const system = {
|
||||
// needed to handle `onlyOAS3` wrapper
|
||||
getSystem() {
|
||||
return {
|
||||
specSelectors: {
|
||||
specJson: () => {
|
||||
return fromJS({ openapi: "3.0.0" })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get the value with the selector
|
||||
const res = serverEffectiveValue(state, "google.com/{foo}/{foo}/{bar}")(system)
|
||||
|
||||
expect(res).toEqual("google.com/123/123/456")
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
116
test/mocha/core/plugins/oas3/wrap-auth-selectors.js
Normal file
116
test/mocha/core/plugins/oas3/wrap-auth-selectors.js
Normal file
@@ -0,0 +1,116 @@
|
||||
/* eslint-env mocha */
|
||||
import expect, { createSpy } from "expect"
|
||||
import { Map, fromJS } from "immutable"
|
||||
import {
|
||||
definitionsToAuthorize
|
||||
} from "corePlugins/oas3/auth-extensions/wrap-selectors"
|
||||
|
||||
describe("oas3 plugin - auth extensions - wrapSelectors", function(){
|
||||
|
||||
describe("execute", function(){
|
||||
|
||||
it("should add `securities` to the oriAction call", function(){
|
||||
// Given
|
||||
const system = {
|
||||
getSystem: () => system,
|
||||
specSelectors: {
|
||||
specJson: () => fromJS({
|
||||
openapi: "3.0.0"
|
||||
}),
|
||||
securityDefinitions: () => {
|
||||
return fromJS({
|
||||
"oauth2AuthorizationCode": {
|
||||
"type": "oauth2",
|
||||
"flows": {
|
||||
"authorizationCode": {
|
||||
"authorizationUrl": "http://google.com/",
|
||||
"tokenUrl": "http://google.com/",
|
||||
"scopes": {
|
||||
"myScope": "our only scope"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"oauth2Multiflow": {
|
||||
"type": "oauth2",
|
||||
"flows": {
|
||||
"clientCredentials": {
|
||||
"tokenUrl": "http://google.com/",
|
||||
"scopes": {
|
||||
"myScope": "our only scope"
|
||||
}
|
||||
},
|
||||
"password": {
|
||||
"tokenUrl": "http://google.com/",
|
||||
"scopes": {
|
||||
"myScope": "our only scope"
|
||||
}
|
||||
},
|
||||
"authorizationCode": {
|
||||
"authorizationUrl": "http://google.com/",
|
||||
"tokenUrl": "http://google.com/",
|
||||
"scopes": {
|
||||
"myScope": "our only scope"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// When
|
||||
let res = definitionsToAuthorize(() => null, system)()
|
||||
|
||||
// Then
|
||||
expect(res.toJS()).toEqual([
|
||||
{
|
||||
oauth2AuthorizationCode: {
|
||||
flow: "authorizationCode",
|
||||
authorizationUrl: "http://google.com/",
|
||||
tokenUrl: "http://google.com/",
|
||||
scopes: {
|
||||
"myScope": "our only scope"
|
||||
},
|
||||
type: "oauth2"
|
||||
}
|
||||
},
|
||||
{
|
||||
oauth2Multiflow: {
|
||||
flow: "clientCredentials",
|
||||
tokenUrl: "http://google.com/",
|
||||
scopes: {
|
||||
"myScope": "our only scope"
|
||||
},
|
||||
type: "oauth2"
|
||||
}
|
||||
},
|
||||
{
|
||||
oauth2Multiflow: {
|
||||
flow: "password",
|
||||
tokenUrl: "http://google.com/",
|
||||
scopes: {
|
||||
"myScope": "our only scope"
|
||||
},
|
||||
type: "oauth2"
|
||||
}
|
||||
},
|
||||
{
|
||||
oauth2Multiflow: {
|
||||
flow: "authorizationCode",
|
||||
authorizationUrl: "http://google.com/",
|
||||
tokenUrl: "http://google.com/",
|
||||
scopes: {
|
||||
"myScope": "our only scope"
|
||||
},
|
||||
type: "oauth2"
|
||||
}
|
||||
},
|
||||
])
|
||||
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
})
|
||||
99
test/mocha/core/plugins/oas3/wrap-spec-selectors.js
Normal file
99
test/mocha/core/plugins/oas3/wrap-spec-selectors.js
Normal file
@@ -0,0 +1,99 @@
|
||||
/* eslint-env mocha */
|
||||
import expect, { createSpy } from "expect"
|
||||
import { Map, fromJS } from "immutable"
|
||||
import {
|
||||
definitions
|
||||
} from "corePlugins/oas3/spec-extensions/wrap-selectors"
|
||||
|
||||
describe("oas3 plugin - spec extensions - wrapSelectors", function(){
|
||||
|
||||
describe("definitions", function(){
|
||||
it("should return definitions by default", function () {
|
||||
|
||||
// Given
|
||||
const spec = fromJS({
|
||||
openapi: "3.0.0",
|
||||
components: {
|
||||
schemas: {
|
||||
a: {
|
||||
type: "string"
|
||||
},
|
||||
b: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const system = {
|
||||
getSystem: () => system,
|
||||
specSelectors: {
|
||||
specJson: () => spec,
|
||||
}
|
||||
}
|
||||
|
||||
// When
|
||||
let res = definitions(() => null, system)(fromJS({
|
||||
json: spec
|
||||
}))
|
||||
|
||||
// Then
|
||||
expect(res.toJS()).toEqual({
|
||||
a: {
|
||||
type: "string"
|
||||
},
|
||||
b: {
|
||||
type: "string"
|
||||
}
|
||||
})
|
||||
})
|
||||
it("should return an empty Map when missing definitions", function () {
|
||||
|
||||
// Given
|
||||
const spec = fromJS({
|
||||
openapi: "3.0.0"
|
||||
})
|
||||
|
||||
const system = {
|
||||
getSystem: () => system,
|
||||
specSelectors: {
|
||||
specJson: () => spec,
|
||||
}
|
||||
}
|
||||
|
||||
// When
|
||||
let res = definitions(() => null, system)(fromJS({
|
||||
json: spec
|
||||
}))
|
||||
|
||||
// Then
|
||||
expect(res.toJS()).toEqual({})
|
||||
})
|
||||
it("should return an empty Map when given non-object definitions", function () {
|
||||
|
||||
// Given
|
||||
const spec = fromJS({
|
||||
openapi: "3.0.0",
|
||||
components: {
|
||||
schemas: "..."
|
||||
}
|
||||
})
|
||||
|
||||
const system = {
|
||||
getSystem: () => system,
|
||||
specSelectors: {
|
||||
specJson: () => spec,
|
||||
}
|
||||
}
|
||||
|
||||
// When
|
||||
let res = definitions(() => null, system)(fromJS({
|
||||
json: spec
|
||||
}))
|
||||
|
||||
// Then
|
||||
expect(res.toJS()).toEqual({})
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
1411
test/mocha/core/plugins/samples/fn.js
Normal file
1411
test/mocha/core/plugins/samples/fn.js
Normal file
File diff suppressed because it is too large
Load Diff
228
test/mocha/core/plugins/spec/actions.js
Normal file
228
test/mocha/core/plugins/spec/actions.js
Normal file
@@ -0,0 +1,228 @@
|
||||
/* eslint-env mocha */
|
||||
import expect, { createSpy } from "expect"
|
||||
import { fromJS } from "immutable"
|
||||
import { execute, executeRequest, changeParamByIdentity, updateEmptyParamInclusion } from "corePlugins/spec/actions"
|
||||
|
||||
describe("spec plugin - actions", function(){
|
||||
|
||||
describe("execute", function(){
|
||||
|
||||
xit("should collect a full request and call fn.executeRequest", function(){
|
||||
// Given
|
||||
const system = {
|
||||
fn: {
|
||||
fetch: 1
|
||||
},
|
||||
specActions: {
|
||||
executeRequest: createSpy()
|
||||
},
|
||||
specSelectors: {
|
||||
spec: () => fromJS({spec: 1}),
|
||||
parameterValues: () => fromJS({values: 2}),
|
||||
contentTypeValues: () => fromJS({requestContentType: "one", responseContentType: "two"})
|
||||
}
|
||||
}
|
||||
|
||||
// When
|
||||
let executeFn = execute({ path: "/one", method: "get"})
|
||||
executeFn(system)
|
||||
|
||||
// Then
|
||||
expect(system.specActions.executeRequest.calls[0].arguments[0]).toEqual({
|
||||
fetch: 1,
|
||||
method: "get",
|
||||
pathName: "/one",
|
||||
parameters: {
|
||||
values: 2
|
||||
},
|
||||
requestContentType: "one",
|
||||
responseContentType: "two",
|
||||
spec: {
|
||||
spec: 1
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
xit("should allow passing _extra_ properties to executeRequest", function(){
|
||||
|
||||
// Given
|
||||
const system = {
|
||||
fn: {},
|
||||
specActions: {
|
||||
executeRequest: createSpy()
|
||||
},
|
||||
specSelectors: {
|
||||
spec: () => fromJS({}),
|
||||
parameterValues: () => fromJS({}),
|
||||
contentTypeValues: () => fromJS({})
|
||||
}
|
||||
}
|
||||
|
||||
// When
|
||||
let executeFn = execute({ hi: "hello" })
|
||||
executeFn(system)
|
||||
|
||||
// Then
|
||||
expect(system.specActions.executeRequest.calls[0].arguments[0]).toInclude({hi: "hello"})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe("executeRequest", function(){
|
||||
|
||||
xit("should call fn.execute with arg ", function(){
|
||||
|
||||
const system = {
|
||||
fn: {
|
||||
execute: createSpy().andReturn(Promise.resolve())
|
||||
},
|
||||
specActions: {
|
||||
setResponse: createSpy()
|
||||
}
|
||||
}
|
||||
|
||||
// When
|
||||
let executeFn = executeRequest({one: 1})
|
||||
let res = executeFn(system)
|
||||
|
||||
// Then
|
||||
expect(res).toBeA(Promise)
|
||||
expect(system.fn.execute.calls.length).toEqual(1)
|
||||
expect(system.fn.execute.calls[0].arguments[0]).toEqual({
|
||||
one: 1
|
||||
})
|
||||
})
|
||||
|
||||
it("should pass requestInterceptor/responseInterceptor to fn.execute", function(){
|
||||
// Given
|
||||
let configs = {
|
||||
requestInterceptor: createSpy(),
|
||||
responseInterceptor: createSpy()
|
||||
}
|
||||
const system = {
|
||||
fn: {
|
||||
buildRequest: createSpy(),
|
||||
execute: createSpy().andReturn(Promise.resolve())
|
||||
},
|
||||
specActions: {
|
||||
executeRequest: createSpy(),
|
||||
setMutatedRequest: createSpy(),
|
||||
setRequest: createSpy()
|
||||
},
|
||||
specSelectors: {
|
||||
spec: () => fromJS({}),
|
||||
parameterValues: () => fromJS({}),
|
||||
contentTypeValues: () => fromJS({}),
|
||||
url: () => fromJS({}),
|
||||
isOAS3: () => false
|
||||
},
|
||||
getConfigs: () => configs
|
||||
}
|
||||
// When
|
||||
let executeFn = executeRequest({
|
||||
pathName: "/one",
|
||||
method: "GET",
|
||||
operation: fromJS({operationId: "getOne"})
|
||||
})
|
||||
let res = executeFn(system)
|
||||
|
||||
// Then
|
||||
expect(system.fn.execute.calls.length).toEqual(1)
|
||||
expect(system.fn.execute.calls[0].arguments[0]).toIncludeKey("requestInterceptor")
|
||||
expect(system.fn.execute.calls[0].arguments[0]).toInclude({
|
||||
responseInterceptor: configs.responseInterceptor
|
||||
})
|
||||
expect(system.specActions.setMutatedRequest.calls.length).toEqual(0)
|
||||
expect(system.specActions.setRequest.calls.length).toEqual(1)
|
||||
|
||||
|
||||
let wrappedRequestInterceptor = system.fn.execute.calls[0].arguments[0].requestInterceptor
|
||||
wrappedRequestInterceptor(system.fn.execute.calls[0].arguments[0])
|
||||
expect(configs.requestInterceptor.calls.length).toEqual(1)
|
||||
expect(system.specActions.setMutatedRequest.calls.length).toEqual(1)
|
||||
expect(system.specActions.setRequest.calls.length).toEqual(1)
|
||||
})
|
||||
})
|
||||
|
||||
xit("should call specActions.setResponse, when fn.execute resolves", function(){
|
||||
|
||||
const response = {serverResponse: true}
|
||||
const system = {
|
||||
fn: {
|
||||
execute: createSpy().andReturn(Promise.resolve(response))
|
||||
},
|
||||
specActions: {
|
||||
setResponse: createSpy()
|
||||
},
|
||||
errActions: {
|
||||
newSpecErr: createSpy()
|
||||
}
|
||||
}
|
||||
|
||||
// When
|
||||
let executeFn = executeRequest({
|
||||
pathName: "/one",
|
||||
method: "GET"
|
||||
})
|
||||
let executePromise = executeFn(system)
|
||||
|
||||
// Then
|
||||
return executePromise.then( () => {
|
||||
expect(system.specActions.setResponse.calls.length).toEqual(1)
|
||||
expect(system.specActions.setResponse.calls[0].arguments).toEqual([
|
||||
"/one",
|
||||
"GET",
|
||||
response
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("requestResolvedSubtree", () => {
|
||||
it("should return a promise ")
|
||||
})
|
||||
|
||||
it.skip("should call errActions.newErr, if the fn.execute rejects", function(){
|
||||
})
|
||||
|
||||
describe("changeParamByIdentity", function () {
|
||||
it("should map its arguments to a payload", function () {
|
||||
const pathMethod = ["/one", "get"]
|
||||
const param = fromJS({
|
||||
name: "body",
|
||||
in: "body"
|
||||
})
|
||||
const value = "my value"
|
||||
const isXml = false
|
||||
|
||||
const result = changeParamByIdentity(pathMethod, param, value, isXml)
|
||||
|
||||
expect(result).toEqual({
|
||||
type: "spec_update_param",
|
||||
payload: {
|
||||
path: pathMethod,
|
||||
param,
|
||||
value,
|
||||
isXml
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("updateEmptyParamInclusion", function () {
|
||||
it("should map its arguments to a payload", function () {
|
||||
const pathMethod = ["/one", "get"]
|
||||
|
||||
const result = updateEmptyParamInclusion(pathMethod, "param", "query", true)
|
||||
|
||||
expect(result).toEqual({
|
||||
type: "spec_update_empty_param_inclusion",
|
||||
payload: {
|
||||
pathMethod,
|
||||
paramName: "param",
|
||||
paramIn: "query",
|
||||
includeEmptyValue: true
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
1035
test/mocha/core/plugins/spec/assets/petstore.json
Normal file
1035
test/mocha/core/plugins/spec/assets/petstore.json
Normal file
File diff suppressed because it is too large
Load Diff
203
test/mocha/core/plugins/spec/reducer.js
Normal file
203
test/mocha/core/plugins/spec/reducer.js
Normal file
@@ -0,0 +1,203 @@
|
||||
/* eslint-env mocha */
|
||||
import expect from "expect"
|
||||
import { fromJS } from "immutable"
|
||||
import reducer from "corePlugins/spec/reducers"
|
||||
|
||||
describe("spec plugin - reducer", function(){
|
||||
|
||||
describe("update operation meta value", function() {
|
||||
it("should update the operation metadata at the specified key", () => {
|
||||
const updateOperationValue = reducer["spec_update_operation_meta_value"]
|
||||
|
||||
const state = fromJS({
|
||||
resolved: {
|
||||
"paths": {
|
||||
"/pet": {
|
||||
"post": {
|
||||
"description": "my operation"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
let result = updateOperationValue(state, {
|
||||
payload: {
|
||||
path: ["/pet", "post"],
|
||||
value: "application/json",
|
||||
key: "consumes_value"
|
||||
}
|
||||
})
|
||||
|
||||
let expectedResult = {
|
||||
resolved: {
|
||||
"paths": {
|
||||
"/pet": {
|
||||
"post": {
|
||||
"description": "my operation"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
meta: {
|
||||
paths: {
|
||||
"/pet": {
|
||||
post: {
|
||||
"consumes_value": "application/json"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(result.toJS()).toEqual(expectedResult)
|
||||
})
|
||||
|
||||
it("shouldn't throw an error if we try to update the consumes_value of a null operation", () => {
|
||||
const updateOperationValue = reducer["spec_update_operation_meta_value"]
|
||||
|
||||
const state = fromJS({
|
||||
resolved: {
|
||||
"paths": {
|
||||
"/pet": {
|
||||
"post": null
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
let result = updateOperationValue(state, {
|
||||
payload: {
|
||||
path: ["/pet", "post"],
|
||||
value: "application/json",
|
||||
key: "consumes_value"
|
||||
}
|
||||
})
|
||||
|
||||
expect(result.toJS()).toEqual(state.toJS())
|
||||
})
|
||||
})
|
||||
|
||||
describe("set response value", function() {
|
||||
it("should combine the response and error objects", () => {
|
||||
const setResponse = reducer["spec_set_response"]
|
||||
|
||||
const path = "/pet/post"
|
||||
const method = "POST"
|
||||
|
||||
const state = fromJS({})
|
||||
const result = setResponse(state, {
|
||||
payload: {
|
||||
path: path,
|
||||
method: method,
|
||||
res: {
|
||||
error: true,
|
||||
err: {
|
||||
message: "Not Found",
|
||||
name: "Error",
|
||||
response: {
|
||||
data: "response data",
|
||||
headers: {
|
||||
key: "value"
|
||||
},
|
||||
ok: false,
|
||||
status: 404,
|
||||
statusText: "Not Found"
|
||||
},
|
||||
status: 404,
|
||||
statusCode: 404
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
let expectedResult = {
|
||||
error: true,
|
||||
message: "Not Found",
|
||||
name: "Error",
|
||||
data: "response data",
|
||||
headers: {
|
||||
key: "value"
|
||||
},
|
||||
ok: false,
|
||||
status: 404,
|
||||
statusCode: 404,
|
||||
statusText: "Not Found"
|
||||
}
|
||||
|
||||
const response = result.getIn(["responses", path, method]).toJS()
|
||||
expect(response).toEqual(expectedResult)
|
||||
})
|
||||
})
|
||||
describe("SPEC_UPDATE_PARAM", function() {
|
||||
it("should store parameter values by {in}.{name}", () => {
|
||||
const updateParam = reducer["spec_update_param"]
|
||||
|
||||
const path = "/pet/post"
|
||||
const method = "POST"
|
||||
|
||||
const state = fromJS({})
|
||||
const result = updateParam(state, {
|
||||
payload: {
|
||||
path: [path, method],
|
||||
paramName: "myBody",
|
||||
paramIn: "body",
|
||||
value: `{ "a": 123 }`,
|
||||
isXml: false
|
||||
}
|
||||
})
|
||||
|
||||
const response = result.getIn(["meta", "paths", path, method, "parameters", "body.myBody", "value"])
|
||||
expect(response).toEqual(`{ "a": 123 }`)
|
||||
})
|
||||
it("should store parameter values by identity", () => {
|
||||
const updateParam = reducer["spec_update_param"]
|
||||
|
||||
const path = "/pet/post"
|
||||
const method = "POST"
|
||||
|
||||
const param = fromJS({
|
||||
name: "myBody",
|
||||
in: "body",
|
||||
schema: {
|
||||
type: "string"
|
||||
}
|
||||
})
|
||||
|
||||
const state = fromJS({})
|
||||
const result = updateParam(state, {
|
||||
payload: {
|
||||
param,
|
||||
path: [path, method],
|
||||
value: `{ "a": 123 }`,
|
||||
isXml: false
|
||||
}
|
||||
})
|
||||
|
||||
const value = result.getIn(["meta", "paths", path, method, "parameters", `body.myBody.hash-${param.hashCode()}`, "value"])
|
||||
expect(value).toEqual(`{ "a": 123 }`)
|
||||
})
|
||||
})
|
||||
describe("SPEC_UPDATE_EMPTY_PARAM_INCLUSION", function() {
|
||||
it("should store parameter values by {in}.{name}", () => {
|
||||
const updateParam = reducer["spec_update_empty_param_inclusion"]
|
||||
|
||||
const path = "/pet/post"
|
||||
const method = "POST"
|
||||
|
||||
const state = fromJS({})
|
||||
|
||||
const result = updateParam(state, {
|
||||
payload: {
|
||||
pathMethod: [path, method],
|
||||
paramName: "param",
|
||||
paramIn: "query",
|
||||
includeEmptyValue: true
|
||||
}
|
||||
})
|
||||
|
||||
const response = result.getIn(["meta", "paths", path, method, "parameter_inclusions", "query.param"])
|
||||
expect(response).toEqual(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
1214
test/mocha/core/plugins/spec/selectors.js
Normal file
1214
test/mocha/core/plugins/spec/selectors.js
Normal file
File diff suppressed because it is too large
Load Diff
94
test/mocha/core/plugins/swagger-js/withCredentials.js
Normal file
94
test/mocha/core/plugins/swagger-js/withCredentials.js
Normal file
@@ -0,0 +1,94 @@
|
||||
import expect, { createSpy } from "expect"
|
||||
import { loaded } from "corePlugins/swagger-js/configs-wrap-actions"
|
||||
|
||||
describe("swagger-js plugin - withCredentials", () => {
|
||||
it("should have no effect by default", () => {
|
||||
const system = {
|
||||
fn: {
|
||||
fetch: createSpy().andReturn(Promise.resolve())
|
||||
},
|
||||
getConfigs: () => ({})
|
||||
}
|
||||
const oriExecute = createSpy()
|
||||
|
||||
const loadedFn = loaded(oriExecute, system)
|
||||
loadedFn()
|
||||
|
||||
expect(oriExecute.calls.length).toBe(1)
|
||||
expect(system.fn.fetch.withCredentials).toBe(undefined)
|
||||
})
|
||||
|
||||
it("should allow setting flag to true via config", () => {
|
||||
const system = {
|
||||
fn: {
|
||||
fetch: createSpy().andReturn(Promise.resolve())
|
||||
},
|
||||
getConfigs: () => ({
|
||||
withCredentials: true
|
||||
})
|
||||
}
|
||||
const oriExecute = createSpy()
|
||||
|
||||
const loadedFn = loaded(oriExecute, system)
|
||||
loadedFn()
|
||||
|
||||
expect(oriExecute.calls.length).toBe(1)
|
||||
expect(system.fn.fetch.withCredentials).toBe(true)
|
||||
})
|
||||
|
||||
it("should allow setting flag to false via config", () => {
|
||||
const system = {
|
||||
fn: {
|
||||
fetch: createSpy().andReturn(Promise.resolve())
|
||||
},
|
||||
getConfigs: () => ({
|
||||
withCredentials: false
|
||||
})
|
||||
}
|
||||
const oriExecute = createSpy()
|
||||
|
||||
const loadedFn = loaded(oriExecute, system)
|
||||
loadedFn()
|
||||
|
||||
expect(oriExecute.calls.length).toBe(1)
|
||||
expect(system.fn.fetch.withCredentials).toBe(false)
|
||||
})
|
||||
|
||||
it("should allow setting flag to true via config as string", () => {
|
||||
// for query string config
|
||||
const system = {
|
||||
fn: {
|
||||
fetch: createSpy().andReturn(Promise.resolve())
|
||||
},
|
||||
getConfigs: () => ({
|
||||
withCredentials: "true"
|
||||
})
|
||||
}
|
||||
const oriExecute = createSpy()
|
||||
|
||||
const loadedFn = loaded(oriExecute, system)
|
||||
loadedFn()
|
||||
|
||||
expect(oriExecute.calls.length).toBe(1)
|
||||
expect(system.fn.fetch.withCredentials).toBe(true)
|
||||
})
|
||||
|
||||
it("should allow setting flag to false via config as string", () => {
|
||||
// for query string config
|
||||
const system = {
|
||||
fn: {
|
||||
fetch: createSpy().andReturn(Promise.resolve())
|
||||
},
|
||||
getConfigs: () => ({
|
||||
withCredentials: "false"
|
||||
})
|
||||
}
|
||||
const oriExecute = createSpy()
|
||||
|
||||
const loadedFn = loaded(oriExecute, system)
|
||||
loadedFn()
|
||||
|
||||
expect(oriExecute.calls.length).toBe(1)
|
||||
expect(system.fn.fetch.withCredentials).toBe(false)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user