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:
23
test/mocha/bugs/3199-sanitization-escaping.jsx
Normal file
23
test/mocha/bugs/3199-sanitization-escaping.jsx
Normal file
@@ -0,0 +1,23 @@
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import expect from "expect"
|
||||
import { render } from "enzyme"
|
||||
import Markdown from "components/providers/markdown"
|
||||
|
||||
describe("UI-3199: Sanitized Markdown causing code examples to be double escaped", function(){
|
||||
it("should single-escape quotes", function(){
|
||||
|
||||
let str = "" +
|
||||
"This is a test: \n\n" +
|
||||
" {\"abc\": \"def\"}\n"
|
||||
|
||||
let props = {
|
||||
source: str
|
||||
}
|
||||
|
||||
let el = render(<Markdown {...props}/>)
|
||||
|
||||
expect(el.find("code").first().text()).toEqual("{\"abc\": \"def\"}\n")
|
||||
expect(el.find("code").first().html()).toEqual("{"abc": "def"}\n")
|
||||
})
|
||||
})
|
||||
37
test/mocha/bugs/3279-empty-markdown-source.jsx
Normal file
37
test/mocha/bugs/3279-empty-markdown-source.jsx
Normal file
@@ -0,0 +1,37 @@
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import expect from "expect"
|
||||
import { render } from "enzyme"
|
||||
import Markdown from "components/providers/markdown"
|
||||
|
||||
describe("UI-3279: Empty Markdown inputs causing bare `undefined` in output", function(){
|
||||
it("should return no text for `null` as source input", function(){
|
||||
let props = {
|
||||
source: null
|
||||
}
|
||||
|
||||
let el = render(<Markdown {...props}/>)
|
||||
|
||||
expect(el.text()).toEqual("")
|
||||
})
|
||||
|
||||
it("should return no text for `undefined` as source input", function(){
|
||||
let props = {
|
||||
source: undefined
|
||||
}
|
||||
|
||||
let el = render(<Markdown {...props}/>)
|
||||
|
||||
expect(el.text()).toEqual("")
|
||||
})
|
||||
|
||||
it("should return no text for empty string as source input", function(){
|
||||
let props = {
|
||||
source: ""
|
||||
}
|
||||
|
||||
let el = render(<Markdown {...props}/>)
|
||||
|
||||
expect(el.text()).toEqual("")
|
||||
})
|
||||
})
|
||||
78
test/mocha/bugs/4557-default-parameter-values.jsx
Normal file
78
test/mocha/bugs/4557-default-parameter-values.jsx
Normal file
@@ -0,0 +1,78 @@
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import { List, fromJS } from "immutable"
|
||||
import expect, { createSpy } from "expect"
|
||||
import { render } from "enzyme"
|
||||
import ParameterRow from "components/parameter-row"
|
||||
|
||||
describe("bug #4557: default parameter values", function(){
|
||||
it("should apply a Swagger 2.0 default value", function(){
|
||||
|
||||
const paramValue = fromJS({
|
||||
description: "a pet",
|
||||
type: "string",
|
||||
default: "MyDefaultValue"
|
||||
})
|
||||
|
||||
let props = {
|
||||
getComponent: ()=> "div",
|
||||
specSelectors: {
|
||||
security(){},
|
||||
parameterWithMetaByIdentity(){ return paramValue },
|
||||
isOAS3(){ return false },
|
||||
isSwagger2(){ return true }
|
||||
},
|
||||
fn: {},
|
||||
operation: {get: ()=>{}},
|
||||
onChange: createSpy(),
|
||||
param: paramValue,
|
||||
rawParam: paramValue,
|
||||
onChangeConsumes: () => {},
|
||||
pathMethod: [],
|
||||
getConfigs: () => { return {} },
|
||||
specPath: List([])
|
||||
}
|
||||
|
||||
render(<ParameterRow {...props}/>)
|
||||
|
||||
expect(props.onChange).toHaveBeenCalled()
|
||||
expect(props.onChange).toHaveBeenCalledWith(paramValue, "MyDefaultValue", false)
|
||||
})
|
||||
it("should apply an OpenAPI 3.0 default value", function(){
|
||||
|
||||
const paramValue = fromJS({
|
||||
description: "a pet",
|
||||
schema: {
|
||||
type: "string",
|
||||
default: "MyDefaultValue"
|
||||
}
|
||||
})
|
||||
|
||||
let props = {
|
||||
getComponent: ()=> "div",
|
||||
specSelectors: {
|
||||
security(){},
|
||||
parameterWithMetaByIdentity(){ return paramValue },
|
||||
isOAS3(){ return true },
|
||||
isSwagger2() { return false }
|
||||
},
|
||||
oas3Selectors: {
|
||||
activeExamplesMember: () => null
|
||||
},
|
||||
fn: {},
|
||||
operation: {get: ()=>{}},
|
||||
onChange: createSpy(),
|
||||
param: paramValue,
|
||||
rawParam: paramValue,
|
||||
onChangeConsumes: () => {},
|
||||
pathMethod: [],
|
||||
getConfigs: () => { return {} },
|
||||
specPath: List([])
|
||||
}
|
||||
|
||||
render(<ParameterRow {...props}/>)
|
||||
|
||||
expect(props.onChange).toHaveBeenCalled()
|
||||
expect(props.onChange).toHaveBeenCalledWith(paramValue, "MyDefaultValue", false)
|
||||
})
|
||||
})
|
||||
65
test/mocha/components/filter.jsx
Normal file
65
test/mocha/components/filter.jsx
Normal file
@@ -0,0 +1,65 @@
|
||||
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import expect from "expect"
|
||||
import { mount } from "enzyme"
|
||||
import FilterContainer from "containers/filter"
|
||||
import { Col } from "components/layout-utils"
|
||||
|
||||
describe("<FilterContainer/>", function(){
|
||||
|
||||
const mockedProps = {
|
||||
specSelectors: {
|
||||
loadingStatus() {}
|
||||
},
|
||||
layoutSelectors: {
|
||||
currentFilter() {}
|
||||
},
|
||||
getComponent: () => {return Col}
|
||||
}
|
||||
|
||||
it("renders FilterContainer if filter is provided", function(){
|
||||
|
||||
// Given
|
||||
let props = {...mockedProps}
|
||||
props.layoutSelectors = {...mockedProps.specSelectors}
|
||||
props.layoutSelectors.currentFilter = function() {return true}
|
||||
|
||||
// When
|
||||
let wrapper = mount(<FilterContainer {...props}/>)
|
||||
|
||||
// Then
|
||||
const renderedColInsideFilter = wrapper.find(Col)
|
||||
expect(renderedColInsideFilter.length).toEqual(1)
|
||||
})
|
||||
|
||||
it("does not render FilterContainer if filter is null", function(){
|
||||
|
||||
// Given
|
||||
let props = {...mockedProps}
|
||||
props.layoutSelectors = {...mockedProps.specSelectors}
|
||||
props.layoutSelectors.currentFilter = function() {return null}
|
||||
|
||||
// When
|
||||
let wrapper = mount(<FilterContainer {...props}/>)
|
||||
|
||||
// Then
|
||||
const renderedColInsideFilter = wrapper.find(Col)
|
||||
expect(renderedColInsideFilter.length).toEqual(0)
|
||||
})
|
||||
|
||||
it("does not render FilterContainer if filter is false", function(){
|
||||
|
||||
// Given
|
||||
let props = {...mockedProps}
|
||||
props.layoutSelectors = {...mockedProps.specSelectors}
|
||||
props.layoutSelectors.currentFilter = function() {return false}
|
||||
|
||||
// When
|
||||
let wrapper = mount(<FilterContainer {...props}/>)
|
||||
|
||||
// Then
|
||||
const renderedColInsideFilter = wrapper.find(Col)
|
||||
expect(renderedColInsideFilter.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
67
test/mocha/components/info-wrapper.jsx
Normal file
67
test/mocha/components/info-wrapper.jsx
Normal file
@@ -0,0 +1,67 @@
|
||||
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import expect from "expect"
|
||||
import { mount } from "enzyme"
|
||||
import { fromJS } from "immutable"
|
||||
import InfoContainer from "containers/info"
|
||||
|
||||
describe("<InfoContainer/>", function () {
|
||||
|
||||
const components = {
|
||||
info: () => <span className="mocked-info"/>
|
||||
}
|
||||
const mockedProps = {
|
||||
specSelectors: {
|
||||
info () {},
|
||||
url () {},
|
||||
basePath () {},
|
||||
host () {},
|
||||
externalDocs () {}
|
||||
},
|
||||
getComponent: c => components[c]
|
||||
}
|
||||
|
||||
it("renders Info inside InfoContainer if info is provided", function () {
|
||||
|
||||
// Given
|
||||
let props = {...mockedProps}
|
||||
props.specSelectors = {...mockedProps.specSelectors}
|
||||
props.specSelectors.info = function () {return fromJS(["info1", "info2"])}
|
||||
|
||||
// When
|
||||
let wrapper = mount(<InfoContainer {...props}/>)
|
||||
|
||||
// Then
|
||||
const renderedInfo = wrapper.find("span.mocked-info")
|
||||
expect(renderedInfo.length).toEqual(1)
|
||||
})
|
||||
|
||||
it("does not render Info inside InfoContainer if no info is provided", function () {
|
||||
|
||||
// Given
|
||||
let props = {...mockedProps}
|
||||
props.specSelectors = {...mockedProps.specSelectors}
|
||||
props.specSelectors.info = function () {return fromJS([])}
|
||||
|
||||
// When
|
||||
let wrapper = mount(<InfoContainer {...props}/>)
|
||||
|
||||
// Then
|
||||
const renderedInfo = wrapper.find("span.mocked-info")
|
||||
expect(renderedInfo.length).toEqual(0)
|
||||
})
|
||||
|
||||
it("does not render Info inside InfoContainer if info is undefined", function () {
|
||||
|
||||
// Given
|
||||
let props = {...mockedProps}
|
||||
|
||||
// When
|
||||
let wrapper = mount(<InfoContainer {...props}/>)
|
||||
|
||||
// Then
|
||||
const renderedInfo = wrapper.find("span.mocked-info")
|
||||
expect(renderedInfo.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
236
test/mocha/components/json-schema-form.jsx
Normal file
236
test/mocha/components/json-schema-form.jsx
Normal file
@@ -0,0 +1,236 @@
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import { List } from "immutable"
|
||||
import expect, { createSpy } from "expect"
|
||||
import { Select, Input, TextArea } from "components/layout-utils"
|
||||
import { mount, render } from "enzyme"
|
||||
import * as JsonSchemaComponents from "core/json-schema-components"
|
||||
import { JsonSchemaForm } from "core/json-schema-components"
|
||||
|
||||
const components = {...JsonSchemaComponents, Select, Input, TextArea}
|
||||
|
||||
const getComponentStub = (name) => {
|
||||
if(components[name]) return components[name]
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
describe("<JsonSchemaForm/>", function(){
|
||||
describe("strings", function() {
|
||||
it("should render the correct options for a string enum parameter", function(){
|
||||
|
||||
let props = {
|
||||
getComponent: getComponentStub,
|
||||
value: "",
|
||||
onChange: () => {},
|
||||
keyName: "",
|
||||
fn: {},
|
||||
schema: {
|
||||
type: "string",
|
||||
enum: ["one", "two"]
|
||||
}
|
||||
}
|
||||
|
||||
let wrapper = render(<JsonSchemaForm {...props}/>)
|
||||
|
||||
expect(wrapper.find("select").length).toEqual(1)
|
||||
expect(wrapper.find("select option").length).toEqual(3)
|
||||
expect(wrapper.find("select option").eq(0).text()).toEqual("--")
|
||||
expect(wrapper.find("select option").eq(1).text()).toEqual("one")
|
||||
expect(wrapper.find("select option").eq(2).text()).toEqual("two")
|
||||
})
|
||||
|
||||
it("should render the correct options for a required string enum parameter", function(){
|
||||
|
||||
let props = {
|
||||
getComponent: getComponentStub,
|
||||
value: "",
|
||||
onChange: () => {},
|
||||
keyName: "",
|
||||
fn: {},
|
||||
required: true,
|
||||
schema: {
|
||||
type: "string",
|
||||
enum: ["one", "two"]
|
||||
}
|
||||
}
|
||||
|
||||
let wrapper = render(<JsonSchemaForm {...props}/>)
|
||||
|
||||
expect(wrapper.find("select").length).toEqual(1)
|
||||
expect(wrapper.find("select option").length).toEqual(2)
|
||||
expect(wrapper.find("select option").eq(0).text()).toEqual("one")
|
||||
expect(wrapper.find("select option").eq(1).text()).toEqual("two")
|
||||
})
|
||||
})
|
||||
describe("booleans", function() {
|
||||
it("should render the correct options for a boolean parameter", function(){
|
||||
|
||||
let props = {
|
||||
getComponent: getComponentStub,
|
||||
value: "",
|
||||
onChange: () => {},
|
||||
keyName: "",
|
||||
fn: {},
|
||||
schema: {
|
||||
type: "boolean"
|
||||
}
|
||||
}
|
||||
|
||||
let wrapper = render(<JsonSchemaForm {...props}/>)
|
||||
|
||||
expect(wrapper.find("select").length).toEqual(1)
|
||||
expect(wrapper.find("select option").length).toEqual(3)
|
||||
expect(wrapper.find("select option").eq(0).text()).toEqual("--")
|
||||
expect(wrapper.find("select option").eq(1).text()).toEqual("true")
|
||||
expect(wrapper.find("select option").eq(2).text()).toEqual("false")
|
||||
})
|
||||
|
||||
|
||||
it("should render the correct options for an enum boolean parameter", function(){
|
||||
|
||||
let props = {
|
||||
getComponent: getComponentStub,
|
||||
value: "",
|
||||
onChange: () => {},
|
||||
keyName: "",
|
||||
fn: {},
|
||||
schema: {
|
||||
type: "boolean",
|
||||
enum: ["true"]
|
||||
}
|
||||
}
|
||||
|
||||
let wrapper = render(<JsonSchemaForm {...props}/>)
|
||||
|
||||
expect(wrapper.find("select").length).toEqual(1)
|
||||
expect(wrapper.find("select option").length).toEqual(2)
|
||||
expect(wrapper.find("select option").eq(0).text()).toEqual("--")
|
||||
expect(wrapper.find("select option").eq(1).text()).toEqual("true")
|
||||
expect(wrapper.find("select option:checked").first().text()).toEqual("--")
|
||||
})
|
||||
|
||||
it("should render the correct options for a required boolean parameter", function(){
|
||||
|
||||
let props = {
|
||||
getComponent: getComponentStub,
|
||||
value: "",
|
||||
onChange: () => {},
|
||||
keyName: "",
|
||||
fn: {},
|
||||
schema: {
|
||||
type: "boolean",
|
||||
required: true
|
||||
}
|
||||
}
|
||||
|
||||
let wrapper = render(<JsonSchemaForm {...props}/>)
|
||||
|
||||
expect(wrapper.find("select").length).toEqual(1)
|
||||
expect(wrapper.find("select option").length).toEqual(3)
|
||||
expect(wrapper.find("select option").eq(0).text()).toEqual("--")
|
||||
expect(wrapper.find("select option").eq(1).text()).toEqual("true")
|
||||
expect(wrapper.find("select option").eq(2).text()).toEqual("false")
|
||||
expect(wrapper.find("select option:checked").first().text()).toEqual("--")
|
||||
})
|
||||
|
||||
it("should render the correct options for a required enum boolean parameter", function(){
|
||||
|
||||
let props = {
|
||||
getComponent: getComponentStub,
|
||||
value: "",
|
||||
onChange: () => {},
|
||||
keyName: "",
|
||||
fn: {},
|
||||
required: true,
|
||||
schema: {
|
||||
type: "boolean",
|
||||
enum: ["true"]
|
||||
}
|
||||
}
|
||||
|
||||
let wrapper = render(<JsonSchemaForm {...props}/>)
|
||||
|
||||
expect(wrapper.find("select").length).toEqual(1)
|
||||
expect(wrapper.find("select option").length).toEqual(1)
|
||||
expect(wrapper.find("select option").eq(0).text()).toEqual("true")
|
||||
expect(wrapper.find("select option:checked").first().text()).toEqual("true")
|
||||
})
|
||||
})
|
||||
describe("objects", function() {
|
||||
it("should render the correct editor for an OAS3 object parameter", function(){
|
||||
let updateQueue = []
|
||||
|
||||
let props = {
|
||||
getComponent: getComponentStub,
|
||||
value: `{\n "id": "abc123"\n}`,
|
||||
onChange: (value) => {
|
||||
updateQueue.push({ value })
|
||||
},
|
||||
keyName: "",
|
||||
fn: {},
|
||||
errors: List(),
|
||||
schema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
id: {
|
||||
type: "string",
|
||||
example: "abc123"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let wrapper = mount(<JsonSchemaForm {...props}/>)
|
||||
|
||||
updateQueue.forEach(newProps => wrapper.setProps(newProps))
|
||||
|
||||
expect(wrapper.find("textarea").length).toEqual(1)
|
||||
expect(wrapper.find("textarea").text()).toEqual(`{\n "id": "abc123"\n}`)
|
||||
})
|
||||
})
|
||||
describe("unknown types", function() {
|
||||
it("should render unknown types as strings", function(){
|
||||
|
||||
let props = {
|
||||
getComponent: getComponentStub,
|
||||
value: "yo",
|
||||
onChange: () => {},
|
||||
keyName: "",
|
||||
fn: {},
|
||||
schema: {
|
||||
type: "NotARealType"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let wrapper = render(<JsonSchemaForm {...props}/>)
|
||||
|
||||
expect(wrapper.find("input").length).toEqual(1)
|
||||
// expect(wrapper.find("select input").length).toEqual(1)
|
||||
// expect(wrapper.find("select option").first().text()).toEqual("true")
|
||||
})
|
||||
|
||||
it("should render unknown types as strings when a format is passed", function(){
|
||||
|
||||
let props = {
|
||||
getComponent: getComponentStub,
|
||||
value: "yo",
|
||||
onChange: () => {},
|
||||
keyName: "",
|
||||
fn: {},
|
||||
schema: {
|
||||
type: "NotARealType",
|
||||
format: "NotARealFormat"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let wrapper = render(<JsonSchemaForm {...props}/>)
|
||||
|
||||
expect(wrapper.find("input").length).toEqual(1)
|
||||
// expect(wrapper.find("select input").length).toEqual(1)
|
||||
// expect(wrapper.find("select option").first().text()).toEqual("true")
|
||||
})
|
||||
})
|
||||
})
|
||||
88
test/mocha/components/live-response.jsx
Normal file
88
test/mocha/components/live-response.jsx
Normal file
@@ -0,0 +1,88 @@
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import { fromJSOrdered } from "core/utils"
|
||||
import expect, { createSpy } from "expect"
|
||||
import { shallow } from "enzyme"
|
||||
import Curl from "components/curl"
|
||||
import LiveResponse from "components/live-response"
|
||||
import ResponseBody from "components/response-body"
|
||||
|
||||
describe("<LiveResponse/>", function(){
|
||||
let request = fromJSOrdered({
|
||||
credentials: "same-origin",
|
||||
headers: {
|
||||
accept: "application/xml"
|
||||
},
|
||||
url: "http://petstore.swagger.io/v2/pet/1"
|
||||
})
|
||||
|
||||
let mutatedRequest = fromJSOrdered({
|
||||
credentials: "same-origin",
|
||||
headers: {
|
||||
accept: "application/xml",
|
||||
mutated: "header"
|
||||
},
|
||||
url: "http://mutated.petstore.swagger.io/v2/pet/1"
|
||||
})
|
||||
|
||||
let requests = {
|
||||
request: request,
|
||||
mutatedRequest: mutatedRequest
|
||||
}
|
||||
|
||||
const tests = [
|
||||
{ showMutatedRequest: true, expected: { request: "mutatedRequest", requestForCalls: 0, mutatedRequestForCalls: 1 } },
|
||||
{ showMutatedRequest: false, expected: { request: "request", requestForCalls: 1, mutatedRequestForCalls: 0 } }
|
||||
]
|
||||
|
||||
tests.forEach(function(test) {
|
||||
it("passes " + test.expected.request + " to Curl when showMutatedRequest = " + test.showMutatedRequest, function() {
|
||||
|
||||
// Given
|
||||
|
||||
let response = fromJSOrdered({
|
||||
status: 200,
|
||||
url: "http://petstore.swagger.io/v2/pet/1",
|
||||
headers: {},
|
||||
text: "<response/>",
|
||||
})
|
||||
|
||||
let mutatedRequestForSpy = createSpy().andReturn(mutatedRequest)
|
||||
let requestForSpy = createSpy().andReturn(request)
|
||||
|
||||
let components = {
|
||||
curl: Curl,
|
||||
responseBody: ResponseBody
|
||||
}
|
||||
|
||||
let props = {
|
||||
response: response,
|
||||
specSelectors: {
|
||||
mutatedRequestFor: mutatedRequestForSpy,
|
||||
requestFor: requestForSpy,
|
||||
},
|
||||
pathMethod: [ "/one", "get" ],
|
||||
getComponent: (c) => {
|
||||
return components[c]
|
||||
},
|
||||
displayRequestDuration: true,
|
||||
getConfigs: () => ({ showMutatedRequest: test.showMutatedRequest })
|
||||
}
|
||||
|
||||
// When
|
||||
let wrapper = shallow(<LiveResponse {...props}/>)
|
||||
|
||||
// Then
|
||||
expect(mutatedRequestForSpy.calls.length).toEqual(test.expected.mutatedRequestForCalls)
|
||||
expect(requestForSpy.calls.length).toEqual(test.expected.requestForCalls)
|
||||
|
||||
const curl = wrapper.find(Curl)
|
||||
expect(curl.length).toEqual(1)
|
||||
expect(curl.props().request).toBe(requests[test.expected.request])
|
||||
|
||||
const expectedUrl = requests[test.expected.request].get("url")
|
||||
expect(wrapper.find("div.request-url pre").text()).toEqual(expectedUrl)
|
||||
|
||||
})
|
||||
})
|
||||
})
|
||||
90
test/mocha/components/markdown.jsx
Normal file
90
test/mocha/components/markdown.jsx
Normal file
@@ -0,0 +1,90 @@
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import expect from "expect"
|
||||
import { render } from "enzyme"
|
||||
import Markdown from "components/providers/markdown"
|
||||
import { Markdown as OAS3Markdown } from "corePlugins/oas3/wrap-components/markdown.jsx"
|
||||
|
||||
describe("Markdown component", function() {
|
||||
describe("Swagger 2.0", function() {
|
||||
it("allows span elements with class attrib", function() {
|
||||
const str = `<span class="method">ONE</span>`
|
||||
const el = render(<Markdown source={str} />)
|
||||
expect(el.html()).toEqual(`<div class="markdown"><p><span class="method">ONE</span></p>\n</div>`)
|
||||
})
|
||||
|
||||
it("allows td elements with colspan attrib", function() {
|
||||
const str = `<table><tr><td>ABC</td></tr></table>`
|
||||
const el = render(<Markdown source={str} />)
|
||||
expect(el.html()).toEqual(`<div class="markdown"><table><tbody><tr><td>ABC</td></tr></tbody></table></div>`)
|
||||
})
|
||||
|
||||
it("allows image elements", function() {
|
||||
const str = ``
|
||||
const el = render(<Markdown source={str} />)
|
||||
expect(el.html()).toEqual(`<div class="markdown"><p><img title="Image title" alt="Image alt text" src="http://image.source"></p>\n</div>`)
|
||||
})
|
||||
|
||||
it("allows image elements with https scheme", function() {
|
||||
const str = ``
|
||||
const el = render(<Markdown source={str} />)
|
||||
expect(el.html()).toEqual(`<div class="markdown"><p><img title="Image title" alt="Image alt text" src="https://image.source"></p>\n</div>`)
|
||||
})
|
||||
|
||||
it("allows image elements with data scheme", function() {
|
||||
const str = `<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==">`
|
||||
const el = render(<Markdown source={str} />)
|
||||
expect(el.html()).toEqual(`<div class="markdown"><p>` + str + `</p>\n</div>`)
|
||||
})
|
||||
|
||||
it("allows heading elements", function() {
|
||||
const str = `
|
||||
# h1
|
||||
## h2
|
||||
### h3
|
||||
#### h4
|
||||
##### h5
|
||||
###### h6`
|
||||
const el = render(<Markdown source={str} />)
|
||||
expect(el.html()).toEqual(`<div class="markdown"><h1>h1</h1>\n<h2>h2</h2>\n<h3>h3</h3>\n<h4>h4</h4>\n<h5>h5</h5>\n<h6>h6</h6>\n</div>`)
|
||||
})
|
||||
|
||||
it("allows links", function() {
|
||||
const str = `[Link](https://example.com/)`
|
||||
const el = render(<Markdown source={str} />)
|
||||
expect(el.html()).toEqual(`<div class="markdown"><p><a rel="noopener noreferrer" target="_blank" href="https://example.com/">Link</a></p>\n</div>`)
|
||||
})
|
||||
})
|
||||
|
||||
describe("OAS 3", function() {
|
||||
it("allows image elements", function() {
|
||||
const str = ``
|
||||
const el = render(<OAS3Markdown source={str} />)
|
||||
expect(el.html()).toEqual(`<div class="renderedMarkdown"><p><img title="Image title" alt="Image alt text" src="http://image.source"></p></div>`)
|
||||
})
|
||||
|
||||
it("allows image elements with https scheme", function() {
|
||||
const str = ``
|
||||
const el = render(<OAS3Markdown source={str} />)
|
||||
expect(el.html()).toEqual(`<div class="renderedMarkdown"><p><img title="Image title" alt="Image alt text" src="https://image.source"></p></div>`)
|
||||
})
|
||||
|
||||
it("allows image elements with data scheme", function() {
|
||||
const str = `<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==">`
|
||||
const el = render(<OAS3Markdown source={str} />)
|
||||
expect(el.html()).toEqual(`<div class="renderedMarkdown"><p>` + str + `</p></div>`)
|
||||
})
|
||||
|
||||
it("allows heading elements", function() {
|
||||
const str = `
|
||||
# h1
|
||||
## h2
|
||||
### h3
|
||||
#### h4
|
||||
##### h5
|
||||
###### h6`
|
||||
const el = render(<OAS3Markdown source={str} />)
|
||||
expect(el.html()).toEqual(`<div class="renderedMarkdown"><h1>h1</h1>\n<h2>h2</h2>\n<h3>h3</h3>\n<h4>h4</h4>\n<h5>h5</h5>\n<h6>h6</h6></div>`)
|
||||
})
|
||||
})
|
||||
})
|
||||
124
test/mocha/components/model-example.jsx
Normal file
124
test/mocha/components/model-example.jsx
Normal file
@@ -0,0 +1,124 @@
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import expect, { createSpy } from "expect"
|
||||
import { shallow } from "enzyme"
|
||||
import ModelExample from "components/model-example"
|
||||
import ModelComponent from "components/model-wrapper"
|
||||
|
||||
describe("<ModelExample/>", function(){
|
||||
let components, props
|
||||
|
||||
let exampleSelectedTestInputs = [
|
||||
{ defaultModelRendering: "model", isExecute: true },
|
||||
{ defaultModelRendering: "example", isExecute: true },
|
||||
{ defaultModelRendering: "example", isExecute: false },
|
||||
{ defaultModelRendering: "othervalue", isExecute: true },
|
||||
{ defaultModelRendering: "othervalue", isExecute: false }
|
||||
]
|
||||
|
||||
let modelSelectedTestInputs = [
|
||||
{ defaultModelRendering: "model", isExecute: false }
|
||||
]
|
||||
|
||||
beforeEach(() => {
|
||||
components = {
|
||||
ModelWrapper: ModelComponent
|
||||
}
|
||||
|
||||
props = {
|
||||
getComponent: (c) => {
|
||||
return components[c]
|
||||
},
|
||||
specSelectors: {
|
||||
isOAS3: () => false
|
||||
},
|
||||
schema: {},
|
||||
example: "{\"example\": \"value\"}",
|
||||
isExecute: false,
|
||||
getConfigs: () => ({
|
||||
defaultModelRendering: "model",
|
||||
defaultModelExpandDepth: 1
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
it("renders model and example tabs", function(){
|
||||
// When
|
||||
let wrapper = shallow(<ModelExample {...props}/>)
|
||||
|
||||
// Then should render tabs
|
||||
expect(wrapper.find("div > ul.tab").length).toEqual(1)
|
||||
|
||||
let tabs = wrapper.find("div > ul.tab").children()
|
||||
expect(tabs.length).toEqual(2)
|
||||
tabs.forEach((node) => {
|
||||
expect(node.length).toEqual(1)
|
||||
expect(node.name()).toEqual("li")
|
||||
expect(node.hasClass("tabitem")).toEqual(true)
|
||||
})
|
||||
expect(tabs.at(0).text()).toEqual("Example Value")
|
||||
expect(tabs.at(1).text()).toEqual("Model")
|
||||
})
|
||||
|
||||
exampleSelectedTestInputs.forEach(function(testInputs) {
|
||||
it("example tab is selected if isExecute = " + testInputs.isExecute + " and defaultModelRendering = " + testInputs.defaultModelRendering, function(){
|
||||
// When
|
||||
props.isExecute = testInputs.isExecute
|
||||
props.getConfigs = () => ({
|
||||
defaultModelRendering: testInputs.defaultModelRendering,
|
||||
defaultModelExpandDepth: 1
|
||||
})
|
||||
let wrapper = shallow(<ModelExample {...props}/>)
|
||||
|
||||
// Then
|
||||
let tabs = wrapper.find("div > ul.tab").children()
|
||||
|
||||
let exampleTab = tabs.at(0)
|
||||
expect(exampleTab.hasClass("active")).toEqual(true)
|
||||
let modelTab = tabs.at(1)
|
||||
expect(modelTab.hasClass("active")).toEqual(false)
|
||||
|
||||
expect(wrapper.find("div > div").length).toEqual(1)
|
||||
expect(wrapper.find("div > div").text()).toEqual(props.example)
|
||||
})
|
||||
})
|
||||
|
||||
modelSelectedTestInputs.forEach(function(testInputs) {
|
||||
it("model tab is selected if isExecute = " + testInputs.isExecute + " and defaultModelRendering = " + testInputs.defaultModelRendering, function(){
|
||||
// When
|
||||
props.isExecute = testInputs.isExecute
|
||||
props.getConfigs = () => ({
|
||||
defaultModelRendering: testInputs.defaultModelRendering,
|
||||
defaultModelExpandDepth: 1
|
||||
})
|
||||
let wrapper = shallow(<ModelExample {...props}/>)
|
||||
|
||||
// Then
|
||||
let tabs = wrapper.find("div > ul.tab").children()
|
||||
|
||||
let exampleTab = tabs.at(0)
|
||||
expect(exampleTab.hasClass("active")).toEqual(false)
|
||||
let modelTab = tabs.at(1)
|
||||
expect(modelTab.hasClass("active")).toEqual(true)
|
||||
|
||||
expect(wrapper.find("div > div").length).toEqual(1)
|
||||
expect(wrapper.find("div > div").find(ModelComponent).props().expandDepth).toBe(1)
|
||||
})
|
||||
})
|
||||
|
||||
it("passes defaultModelExpandDepth to ModelComponent", function(){
|
||||
// When
|
||||
let expandDepth = 0
|
||||
props.isExecute = false
|
||||
props.getConfigs = () => ({
|
||||
defaultModelRendering: "model",
|
||||
defaultModelExpandDepth: expandDepth
|
||||
})
|
||||
let wrapper = shallow(<ModelExample {...props}/>)
|
||||
|
||||
// Then
|
||||
expect(wrapper.find("div > div").find(ModelComponent).props().expandDepth).toBe(expandDepth)
|
||||
})
|
||||
|
||||
})
|
||||
54
test/mocha/components/models.jsx
Normal file
54
test/mocha/components/models.jsx
Normal file
@@ -0,0 +1,54 @@
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import expect, { createSpy } from "expect"
|
||||
import { shallow } from "enzyme"
|
||||
import { fromJS, Map } from "immutable"
|
||||
import Models from "components/models"
|
||||
import ModelCollpase from "components/model-collapse"
|
||||
import ModelComponent from "components/model-wrapper"
|
||||
|
||||
describe("<Models/>", function(){
|
||||
// Given
|
||||
let components = {
|
||||
Collapse: ModelCollpase,
|
||||
ModelWrapper: ModelComponent
|
||||
}
|
||||
let props = {
|
||||
getComponent: (c) => {
|
||||
return components[c]
|
||||
},
|
||||
specSelectors: {
|
||||
isOAS3: () => false,
|
||||
specJson: () => Map(),
|
||||
definitions: function() {
|
||||
return fromJS({
|
||||
def1: {},
|
||||
def2: {}
|
||||
})
|
||||
},
|
||||
specResolvedSubtree: () => {}
|
||||
},
|
||||
layoutSelectors: {
|
||||
isShown: createSpy()
|
||||
},
|
||||
layoutActions: {},
|
||||
getConfigs: () => ({
|
||||
docExpansion: "list",
|
||||
defaultModelsExpandDepth: 0
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
it("passes defaultModelsExpandDepth to ModelWrapper", function(){
|
||||
// When
|
||||
let wrapper = shallow(<Models {...props}/>)
|
||||
|
||||
// Then should render tabs
|
||||
expect(wrapper.find("ModelCollapse").length).toEqual(1)
|
||||
expect(wrapper.find("ModelWrapper").length).toBeGreaterThan(0)
|
||||
wrapper.find("ModelComponent").forEach((modelWrapper) => {
|
||||
expect(modelWrapper.props().expandDepth).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
69
test/mocha/components/object-model.jsx
Normal file
69
test/mocha/components/object-model.jsx
Normal file
@@ -0,0 +1,69 @@
|
||||
import React from "react"
|
||||
import expect from "expect"
|
||||
import { shallow } from "enzyme"
|
||||
import { fromJS, List } from "immutable"
|
||||
import ObjectModel from "components/object-model"
|
||||
import ModelExample from "components/model-example"
|
||||
import Immutable from "immutable"
|
||||
import Model from "components/model"
|
||||
import ModelCollapse from "components/model-collapse"
|
||||
import { inferSchema } from "corePlugins/samples/fn"
|
||||
|
||||
describe("<ObjectModel />", function() {
|
||||
const dummyComponent = () => null
|
||||
const components = {
|
||||
"JumpToPath" : dummyComponent,
|
||||
"Markdown" : dummyComponent,
|
||||
"Model" : Model,
|
||||
"ModelCollapse" : ModelCollapse
|
||||
}
|
||||
const props = {
|
||||
getComponent: c => components[c],
|
||||
getConfigs: () => {
|
||||
return {
|
||||
showExtensions: true
|
||||
}
|
||||
},
|
||||
isRef : false,
|
||||
specPath: List(),
|
||||
schema: Immutable.fromJS(
|
||||
{
|
||||
"properties": {
|
||||
// Note reverse order: c, b, a
|
||||
c: {
|
||||
type: "integer",
|
||||
name: "c"
|
||||
},
|
||||
b: {
|
||||
type: "boolean",
|
||||
name: "b"
|
||||
},
|
||||
a: {
|
||||
type: "string",
|
||||
name: "a"
|
||||
}
|
||||
}
|
||||
}
|
||||
),
|
||||
specSelectors: {
|
||||
isOAS3(){
|
||||
return false
|
||||
}
|
||||
},
|
||||
className: "for-test"
|
||||
}
|
||||
it("renders a collapsible header", function(){
|
||||
const wrapper = shallow(<ObjectModel {...props}/>)
|
||||
const renderedModelCollapse = wrapper.find(ModelCollapse)
|
||||
expect(renderedModelCollapse.length).toEqual(1)
|
||||
})
|
||||
|
||||
it("renders the object properties in order", function() {
|
||||
const wrapper = shallow(<ObjectModel {...props}/>)
|
||||
const renderedModel = wrapper.find(Model)
|
||||
expect(renderedModel.length).toEqual(3)
|
||||
expect(renderedModel.get(0).props.schema.get("name")).toEqual("c")
|
||||
expect(renderedModel.get(1).props.schema.get("name")).toEqual("b")
|
||||
expect(renderedModel.get(2).props.schema.get("name")).toEqual("a")
|
||||
})
|
||||
})
|
||||
79
test/mocha/components/online-validator-badge.jsx
Normal file
79
test/mocha/components/online-validator-badge.jsx
Normal file
@@ -0,0 +1,79 @@
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import expect, { createSpy } from "expect"
|
||||
import { mount } from "enzyme"
|
||||
import { fromJS, Map } from "immutable"
|
||||
import OnlineValidatorBadge from "components/online-validator-badge"
|
||||
|
||||
describe("<OnlineValidatorBadge/>", function () {
|
||||
it("should render a validator link and image correctly for the default validator", function () {
|
||||
// When
|
||||
const props = {
|
||||
getConfigs: () => ({}),
|
||||
getComponent: () => null,
|
||||
specSelectors: {
|
||||
url: () => "swagger.json"
|
||||
}
|
||||
}
|
||||
const wrapper = mount(
|
||||
<OnlineValidatorBadge {...props} />
|
||||
)
|
||||
|
||||
// Then
|
||||
expect(wrapper.find("a").props().href).toEqual(
|
||||
"https://validator.swagger.io/validator/debug?url=swagger.json"
|
||||
)
|
||||
expect(wrapper.find("ValidatorImage").length).toEqual(1)
|
||||
expect(wrapper.find("ValidatorImage").props().src).toEqual(
|
||||
"https://validator.swagger.io/validator?url=swagger.json"
|
||||
)
|
||||
})
|
||||
it("should encode a definition URL correctly", function () {
|
||||
// When
|
||||
const props = {
|
||||
getConfigs: () => ({}),
|
||||
getComponent: () => null,
|
||||
specSelectors: {
|
||||
url: () => "http://google.com/swagger.json"
|
||||
}
|
||||
}
|
||||
const wrapper = mount(
|
||||
<OnlineValidatorBadge {...props} />
|
||||
)
|
||||
|
||||
// Then
|
||||
expect(wrapper.find("a").props().href).toEqual(
|
||||
"https://validator.swagger.io/validator/debug?url=http%3A%2F%2Fgoogle.com%2Fswagger.json"
|
||||
)
|
||||
expect(wrapper.find("ValidatorImage").length).toEqual(1)
|
||||
expect(wrapper.find("ValidatorImage").props().src).toEqual(
|
||||
"https://validator.swagger.io/validator?url=http%3A%2F%2Fgoogle.com%2Fswagger.json"
|
||||
)
|
||||
})
|
||||
it.skip("should resolve a definition URL against the browser's location", function () {
|
||||
// TODO: mock `window`
|
||||
// When
|
||||
|
||||
const props = {
|
||||
getConfigs: () => ({}),
|
||||
getComponent: () => null,
|
||||
specSelectors: {
|
||||
url: () => "http://google.com/swagger.json"
|
||||
}
|
||||
}
|
||||
const wrapper = mount(
|
||||
<OnlineValidatorBadge {...props} />
|
||||
)
|
||||
|
||||
// Then
|
||||
expect(wrapper.find("a").props().href).toEqual(
|
||||
"https://validator.swagger.io/validator/debug?url=http%3A%2F%2Fgoogle.com%2Fswagger.json"
|
||||
)
|
||||
expect(wrapper.find("ValidatorImage").length).toEqual(1)
|
||||
expect(wrapper.find("ValidatorImage").props().src).toEqual(
|
||||
"https://validator.swagger.io/validator?url=http%3A%2F%2Fgoogle.com%2Fswagger.json"
|
||||
)
|
||||
})
|
||||
// should resolve a definition URL against the browser's location
|
||||
|
||||
})
|
||||
32
test/mocha/components/operation.jsx
Normal file
32
test/mocha/components/operation.jsx
Normal file
@@ -0,0 +1,32 @@
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import expect, { createSpy } from "expect"
|
||||
import { shallow } from "enzyme"
|
||||
import Operation from "components/operation"
|
||||
|
||||
describe("<Operation/>", function(){
|
||||
it.skip("blanket tests", function(){
|
||||
|
||||
let props = {
|
||||
operation: {get: ()=>{}},
|
||||
getComponent: ()=> "div",
|
||||
specSelectors: { security(){} },
|
||||
path: "/one",
|
||||
method: "get",
|
||||
shown: true,
|
||||
showOpId: "",
|
||||
showOpIdPrefix: "",
|
||||
toggleCollapse: createSpy()
|
||||
}
|
||||
|
||||
let wrapper = shallow(<Operation {...props}/>)
|
||||
|
||||
expect(wrapper.find(".opblock").length).toEqual(1)
|
||||
expect(wrapper.find(".opblock-summary-method").text()).toEqual("GET")
|
||||
expect(wrapper.find(".opblock-summary-path").text().trim()).toEqual("/one")
|
||||
expect(wrapper.find("[isOpened]").prop("isOpened")).toEqual(true)
|
||||
|
||||
wrapper.find(".opblock-summary").simulate("click")
|
||||
expect(props.toggleCollapse).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
126
test/mocha/components/operations.jsx
Normal file
126
test/mocha/components/operations.jsx
Normal file
@@ -0,0 +1,126 @@
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import expect from "expect"
|
||||
import { render } from "enzyme"
|
||||
import { fromJS } from "immutable"
|
||||
import DeepLink from "components/deep-link"
|
||||
import Operations from "components/operations"
|
||||
import {Collapse} from "components/layout-utils"
|
||||
|
||||
const components = {
|
||||
Collapse,
|
||||
DeepLink,
|
||||
OperationContainer: ({ path, method }) => <span className="mocked-op" id={`${path}-${method}`} />,
|
||||
OperationTag: "div",
|
||||
}
|
||||
|
||||
describe("<Operations/>", function(){
|
||||
it("should render a Swagger2 `get` method, but not a `trace` or `foo` method", function(){
|
||||
|
||||
let props = {
|
||||
fn: {},
|
||||
specActions: {},
|
||||
layoutActions: {},
|
||||
getComponent: (name)=> {
|
||||
return components[name] || null
|
||||
},
|
||||
getConfigs: () => {
|
||||
return {}
|
||||
},
|
||||
specSelectors: {
|
||||
isOAS3() { return false },
|
||||
taggedOperations() {
|
||||
return fromJS({
|
||||
"default": {
|
||||
"operations": [
|
||||
{
|
||||
"path": "/pets/{id}",
|
||||
"method": "get"
|
||||
},
|
||||
{
|
||||
"path": "/pets/{id}",
|
||||
"method": "trace"
|
||||
},
|
||||
{
|
||||
"path": "/pets/{id}",
|
||||
"method": "foo"
|
||||
},
|
||||
]
|
||||
}
|
||||
})
|
||||
},
|
||||
},
|
||||
layoutSelectors: {
|
||||
currentFilter() {
|
||||
return null
|
||||
},
|
||||
isShown() {
|
||||
return true
|
||||
},
|
||||
show() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let wrapper = render(<Operations {...props}/>)
|
||||
|
||||
expect(wrapper.find("span.mocked-op").length).toEqual(1)
|
||||
expect(wrapper.find("span.mocked-op").eq(0).attr("id")).toEqual("/pets/{id}-get")
|
||||
})
|
||||
|
||||
it("should render an OAS3 `get` and `trace` method, but not a `foo` method", function(){
|
||||
|
||||
let props = {
|
||||
fn: {},
|
||||
specActions: {},
|
||||
layoutActions: {},
|
||||
getComponent: (name)=> {
|
||||
return components[name] || null
|
||||
},
|
||||
getConfigs: () => {
|
||||
return {}
|
||||
},
|
||||
specSelectors: {
|
||||
isOAS3() { return true },
|
||||
taggedOperations() {
|
||||
return fromJS({
|
||||
"default": {
|
||||
"operations": [
|
||||
{
|
||||
"path": "/pets/{id}",
|
||||
"method": "get"
|
||||
},
|
||||
{
|
||||
"path": "/pets/{id}",
|
||||
"method": "trace"
|
||||
},
|
||||
{
|
||||
"path": "/pets/{id}",
|
||||
"method": "foo"
|
||||
},
|
||||
]
|
||||
}
|
||||
})
|
||||
},
|
||||
},
|
||||
layoutSelectors: {
|
||||
currentFilter() {
|
||||
return null
|
||||
},
|
||||
isShown() {
|
||||
return true
|
||||
},
|
||||
show() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let wrapper = render(<Operations {...props}/>)
|
||||
|
||||
expect(wrapper.find("span.mocked-op").length).toEqual(2)
|
||||
expect(wrapper.find("span.mocked-op").eq(0).attr("id")).toEqual("/pets/{id}-get")
|
||||
expect(wrapper.find("span.mocked-op").eq(1).attr("id")).toEqual("/pets/{id}-trace")
|
||||
})
|
||||
})
|
||||
52
test/mocha/components/primitive-model.jsx
Normal file
52
test/mocha/components/primitive-model.jsx
Normal file
@@ -0,0 +1,52 @@
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import expect from "expect"
|
||||
import { shallow } from "enzyme"
|
||||
import { fromJS } from "immutable"
|
||||
import PrimitiveModel from "components/primitive-model"
|
||||
|
||||
describe("<PrimitiveModel/>", function() {
|
||||
describe("Model name", function() {
|
||||
const dummyComponent = () => null
|
||||
const components = {
|
||||
Markdown: dummyComponent,
|
||||
EnumModel: dummyComponent
|
||||
}
|
||||
const props = {
|
||||
getComponent: c => components[c],
|
||||
getConfigs: () => ({
|
||||
showExtensions: false
|
||||
}),
|
||||
name: "Name from props",
|
||||
depth: 1,
|
||||
schema: fromJS({
|
||||
type: "string",
|
||||
title: "Custom model title"
|
||||
})
|
||||
}
|
||||
|
||||
it("renders the schema's title", function() {
|
||||
// When
|
||||
const wrapper = shallow(<PrimitiveModel {...props}/>)
|
||||
const modelTitleEl = wrapper.find("span.model-title")
|
||||
expect(modelTitleEl.length).toEqual(1)
|
||||
|
||||
// Then
|
||||
expect( modelTitleEl.text() ).toEqual( "Custom model title" )
|
||||
})
|
||||
|
||||
it("falls back to the passed-in `name` prop for the title", function() {
|
||||
// When
|
||||
props.schema = fromJS({
|
||||
type: "string"
|
||||
})
|
||||
const wrapper = shallow(<PrimitiveModel {...props}/>)
|
||||
const modelTitleEl = wrapper.find("span.model-title")
|
||||
expect(modelTitleEl.length).toEqual(1)
|
||||
|
||||
// Then
|
||||
expect( modelTitleEl.text() ).toEqual( "Name from props" )
|
||||
})
|
||||
|
||||
})
|
||||
} )
|
||||
36
test/mocha/components/response-body.jsx
Normal file
36
test/mocha/components/response-body.jsx
Normal file
@@ -0,0 +1,36 @@
|
||||
import React from "react"
|
||||
import expect from "expect"
|
||||
import { shallow } from "enzyme"
|
||||
import ResponseBody from "components/response-body"
|
||||
import { inferSchema } from "corePlugins/samples/fn"
|
||||
|
||||
describe("<ResponseBody />", function() {
|
||||
const highlightCodeComponent = () => null
|
||||
const components = {
|
||||
highlightCode: highlightCodeComponent
|
||||
}
|
||||
const props = {
|
||||
getComponent: c => components[c],
|
||||
}
|
||||
|
||||
it("renders ResponseBody as 'application/json'", function() {
|
||||
props.contentType = "application/json"
|
||||
props.content = "{\"key\": \"a test value\"}"
|
||||
const wrapper = shallow(<ResponseBody {...props}/>)
|
||||
expect(wrapper.find("highlightCodeComponent").length).toEqual(1)
|
||||
})
|
||||
|
||||
it("renders ResponseBody as 'text/html'", function() {
|
||||
props.contentType = "application/json"
|
||||
props.content = "<b>Result</b>"
|
||||
const wrapper = shallow(<ResponseBody {...props}/>)
|
||||
expect(wrapper.find("highlightCodeComponent").length).toEqual(1)
|
||||
})
|
||||
|
||||
it("renders ResponseBody as 'image/svg'", function() {
|
||||
props.contentType = "image/svg"
|
||||
const wrapper = shallow(<ResponseBody {...props}/>)
|
||||
console.warn(wrapper.debug())
|
||||
expect(wrapper.find("highlightCodeComponent").length).toEqual(0)
|
||||
})
|
||||
})
|
||||
61
test/mocha/components/response.jsx
Normal file
61
test/mocha/components/response.jsx
Normal file
@@ -0,0 +1,61 @@
|
||||
import React from "react"
|
||||
import expect from "expect"
|
||||
import { shallow } from "enzyme"
|
||||
import { fromJS, List } from "immutable"
|
||||
import Response from "components/response"
|
||||
import ModelExample from "components/model-example"
|
||||
import { inferSchema } from "corePlugins/samples/fn"
|
||||
|
||||
describe("<Response />", function() {
|
||||
const dummyComponent = () => null
|
||||
const components = {
|
||||
headers: dummyComponent,
|
||||
highlightCode: dummyComponent,
|
||||
modelExample: ModelExample,
|
||||
Markdown: dummyComponent,
|
||||
operationLink: dummyComponent,
|
||||
contentType: dummyComponent
|
||||
}
|
||||
const props = {
|
||||
getComponent: c => components[c],
|
||||
specSelectors: {
|
||||
isOAS3() {
|
||||
return false
|
||||
}
|
||||
},
|
||||
fn: {
|
||||
inferSchema
|
||||
},
|
||||
contentType: "application/json",
|
||||
className: "for-test",
|
||||
specPath: List(),
|
||||
response: fromJS({
|
||||
schema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
// Note reverse order: c, b, a
|
||||
"c": {
|
||||
type: "integer"
|
||||
},
|
||||
"b": {
|
||||
type: "boolean"
|
||||
},
|
||||
"a": {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}),
|
||||
code: "200"
|
||||
}
|
||||
|
||||
it("renders the model-example schema properties in order", function() {
|
||||
const wrapper = shallow(<Response {...props}/>)
|
||||
const renderedModelExample = wrapper.find(ModelExample)
|
||||
expect(renderedModelExample.length).toEqual(1)
|
||||
|
||||
// Assert the schema's properties have maintained their order
|
||||
const modelExampleSchemaProperties = renderedModelExample.props().schema.toJS().properties
|
||||
expect( Object.keys(modelExampleSchemaProperties) ).toEqual(["c", "b", "a"])
|
||||
})
|
||||
})
|
||||
91
test/mocha/components/schemes-wrapper.jsx
Normal file
91
test/mocha/components/schemes-wrapper.jsx
Normal file
@@ -0,0 +1,91 @@
|
||||
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import expect from "expect"
|
||||
import { mount, render } from "enzyme"
|
||||
import { fromJS } from "immutable"
|
||||
import SchemesContainer from "containers/schemes"
|
||||
import Schemes from "components/schemes"
|
||||
import { Col } from "components/layout-utils"
|
||||
|
||||
describe("<SchemesContainer/>", function(){
|
||||
|
||||
const components = {
|
||||
schemes: Schemes,
|
||||
Col,
|
||||
authorizeBtn: () => <span className="mocked-button" id="mocked-button" />
|
||||
}
|
||||
const mockedProps = {
|
||||
specSelectors: {
|
||||
securityDefinitions() {},
|
||||
operationScheme() {},
|
||||
schemes() {}
|
||||
},
|
||||
specActions: {
|
||||
setScheme() {}
|
||||
},
|
||||
getComponent: c => components[c]
|
||||
}
|
||||
const twoSecurityDefinitions = {
|
||||
"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"
|
||||
}
|
||||
}
|
||||
|
||||
it("renders Schemes inside SchemesContainer if schemes are provided", function(){
|
||||
|
||||
// Given
|
||||
let props = {...mockedProps}
|
||||
props.specSelectors = {...mockedProps.specSelectors}
|
||||
props.specSelectors.operationScheme = function() {return "http"}
|
||||
props.specSelectors.schemes = function() {return fromJS(["http", "https"])}
|
||||
|
||||
// When
|
||||
let wrapper = mount(<SchemesContainer {...props}/>)
|
||||
|
||||
// Then
|
||||
const renderedSchemes = wrapper.find(Schemes)
|
||||
expect(renderedSchemes.length).toEqual(1)
|
||||
})
|
||||
|
||||
it("does not render Schemes inside SchemeWrapper if empty array of schemes is provided", function(){
|
||||
|
||||
// Given
|
||||
let props = {...mockedProps}
|
||||
props.specSelectors = {...mockedProps.specSelectors}
|
||||
props.specSelectors.schemes = function() {return fromJS([])}
|
||||
|
||||
// When
|
||||
let wrapper = mount(<SchemesContainer {...props}/>)
|
||||
|
||||
// Then
|
||||
const renderedSchemes = wrapper.find(Schemes)
|
||||
expect(renderedSchemes.length).toEqual(0)
|
||||
})
|
||||
|
||||
it("does not render Schemes inside SchemeWrapper if provided schemes are undefined", function(){
|
||||
|
||||
// Given
|
||||
let props = {...mockedProps}
|
||||
props.specSelectors = {...mockedProps.specSelectors}
|
||||
props.specSelectors.schemes = function() {return undefined}
|
||||
|
||||
// When
|
||||
let wrapper = mount(<SchemesContainer {...props}/>)
|
||||
|
||||
// Then
|
||||
const renderedSchemes = wrapper.find(Schemes)
|
||||
expect(renderedSchemes.length).toEqual(0)
|
||||
})
|
||||
})
|
||||
72
test/mocha/components/schemes.jsx
Normal file
72
test/mocha/components/schemes.jsx
Normal file
@@ -0,0 +1,72 @@
|
||||
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import expect, { createSpy } from "expect"
|
||||
import { shallow } from "enzyme"
|
||||
import { fromJS } from "immutable"
|
||||
import Schemes from "components/schemes"
|
||||
|
||||
describe("<Schemes/>", function(){
|
||||
it("calls props.specActions.setScheme() when no currentScheme is selected", function(){
|
||||
|
||||
let setSchemeSpy = createSpy()
|
||||
|
||||
// Given
|
||||
let props = {
|
||||
specActions: {
|
||||
setScheme: setSchemeSpy
|
||||
},
|
||||
schemes: fromJS([
|
||||
"http",
|
||||
"https"
|
||||
]),
|
||||
currentScheme: undefined,
|
||||
path: "/test",
|
||||
method: "get"
|
||||
}
|
||||
|
||||
// When
|
||||
let wrapper = shallow(<Schemes {...props}/>)
|
||||
|
||||
// Then currentScheme should default to first scheme in options list
|
||||
expect(props.specActions.setScheme).toHaveBeenCalledWith("http", "/test" , "get")
|
||||
|
||||
// When the currentScheme is no longer in the list of options
|
||||
props.schemes = fromJS([
|
||||
"https"
|
||||
])
|
||||
wrapper.setProps(props)
|
||||
|
||||
// Then currentScheme should default to first scheme in options list, again
|
||||
expect(props.specActions.setScheme).toHaveBeenCalledWith("https", "/test", "get")
|
||||
})
|
||||
|
||||
it("doesn't call props.specActions.setScheme() when schemes hasn't changed", function(){
|
||||
|
||||
let setSchemeSpy = createSpy()
|
||||
|
||||
// Given
|
||||
let props = {
|
||||
specActions: {
|
||||
setScheme: setSchemeSpy
|
||||
},
|
||||
schemes: fromJS([
|
||||
"http",
|
||||
"https"
|
||||
]),
|
||||
currentScheme: "https"
|
||||
}
|
||||
|
||||
// When
|
||||
let wrapper = shallow(<Schemes {...props}/>)
|
||||
|
||||
// Should be called initially, to set the global state
|
||||
expect(setSchemeSpy.calls.length).toEqual(1)
|
||||
|
||||
// After an update
|
||||
wrapper.instance().componentWillReceiveProps(props)
|
||||
|
||||
// Should not be called again, since `currentScheme` is in schemes
|
||||
expect(setSchemeSpy.calls.length).toEqual(1)
|
||||
})
|
||||
})
|
||||
70
test/mocha/components/version-pragma-filter.jsx
Normal file
70
test/mocha/components/version-pragma-filter.jsx
Normal file
@@ -0,0 +1,70 @@
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import expect, { createSpy } from "expect"
|
||||
import { shallow } from "enzyme"
|
||||
import { fromJS, Map } from "immutable"
|
||||
import VersionPragmaFilter from "components/version-pragma-filter"
|
||||
|
||||
describe("<VersionPragmaFilter/>", function(){
|
||||
it("renders children for a Swagger 2 definition", function(){
|
||||
// When
|
||||
let wrapper = shallow(
|
||||
<VersionPragmaFilter isSwagger2={true} isOAS3={false}>
|
||||
hello!
|
||||
</VersionPragmaFilter>
|
||||
)
|
||||
|
||||
// Then
|
||||
expect(wrapper.find("div").length).toEqual(1)
|
||||
expect(wrapper.find("div").text()).toEqual("hello!")
|
||||
})
|
||||
it("renders children for an OpenAPI 3 definition", function(){
|
||||
// When
|
||||
let wrapper = shallow(
|
||||
<VersionPragmaFilter isSwagger2={false} isOAS3={true}>
|
||||
hello!
|
||||
</VersionPragmaFilter>
|
||||
)
|
||||
|
||||
// Then
|
||||
expect(wrapper.find("div").length).toEqual(1)
|
||||
expect(wrapper.find("div").text()).toEqual("hello!")
|
||||
})
|
||||
it("renders children when a bypass prop is set", function(){
|
||||
// When
|
||||
let wrapper = shallow(
|
||||
<VersionPragmaFilter bypass>
|
||||
hello!
|
||||
</VersionPragmaFilter>
|
||||
)
|
||||
|
||||
// Then
|
||||
expect(wrapper.find("div").length).toEqual(1)
|
||||
expect(wrapper.find("div").text()).toEqual("hello!")
|
||||
})
|
||||
it("renders the correct message for an ambiguous-version definition", function(){
|
||||
// When
|
||||
let wrapper = shallow(
|
||||
<VersionPragmaFilter isSwagger2={true} isOAS3={true}>
|
||||
hello!
|
||||
</VersionPragmaFilter>
|
||||
)
|
||||
|
||||
// Then
|
||||
expect(wrapper.find("div.version-pragma__message--ambiguous").length).toEqual(1)
|
||||
expect(wrapper.find("div.version-pragma__message--missing").length).toEqual(0)
|
||||
})
|
||||
it("renders the correct message for a missing-version definition", function(){
|
||||
// When
|
||||
let wrapper = shallow(
|
||||
<VersionPragmaFilter isSwagger2={false} isOAS3={false}>
|
||||
hello!
|
||||
</VersionPragmaFilter>
|
||||
)
|
||||
|
||||
// Then
|
||||
expect(wrapper.find("div.version-pragma__message--missing").length).toEqual(1)
|
||||
expect(wrapper.find("div.version-pragma__message--ambiguous").length).toEqual(0)
|
||||
})
|
||||
|
||||
})
|
||||
218
test/mocha/core/curlify.js
Normal file
218
test/mocha/core/curlify.js
Normal file
@@ -0,0 +1,218 @@
|
||||
import expect from "expect"
|
||||
import Im from "immutable"
|
||||
import curl from "core/curlify"
|
||||
import win from "core/window"
|
||||
|
||||
describe("curlify", function() {
|
||||
|
||||
it("prints a curl statement with custom content-type", function() {
|
||||
var req = {
|
||||
url: "http://example.com",
|
||||
method: "POST",
|
||||
body: {
|
||||
id: 0,
|
||||
name: "doggie",
|
||||
status: "available"
|
||||
},
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
"content-type": "application/json"
|
||||
}
|
||||
}
|
||||
|
||||
let curlified = curl(Im.fromJS(req))
|
||||
|
||||
expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"Accept: application/json\" -H \"content-type: application/json\" -d {\"id\":0,\"name\":\"doggie\",\"status\":\"available\"}")
|
||||
})
|
||||
|
||||
it("does not change the case of header in curl", function() {
|
||||
var req = {
|
||||
url: "http://example.com",
|
||||
method: "POST",
|
||||
headers: {
|
||||
"conTenT Type": "application/Moar"
|
||||
}
|
||||
}
|
||||
|
||||
let curlified = curl(Im.fromJS(req))
|
||||
|
||||
expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"conTenT Type: application/Moar\"")
|
||||
})
|
||||
|
||||
it("prints a curl statement with an array of query params", function() {
|
||||
var req = {
|
||||
url: "http://swaggerhub.com/v1/one?name=john|smith",
|
||||
method: "GET"
|
||||
}
|
||||
|
||||
let curlified = curl(Im.fromJS(req))
|
||||
|
||||
expect(curlified).toEqual("curl -X GET \"http://swaggerhub.com/v1/one?name=john|smith\"")
|
||||
})
|
||||
|
||||
it("prints a curl statement with an array of query params and auth", function() {
|
||||
var req = {
|
||||
url: "http://swaggerhub.com/v1/one?name=john|smith",
|
||||
method: "GET",
|
||||
headers: {
|
||||
authorization: "Basic Zm9vOmJhcg=="
|
||||
}
|
||||
}
|
||||
|
||||
let curlified = curl(Im.fromJS(req))
|
||||
|
||||
expect(curlified).toEqual("curl -X GET \"http://swaggerhub.com/v1/one?name=john|smith\" -H \"authorization: Basic Zm9vOmJhcg==\"")
|
||||
})
|
||||
|
||||
it("prints a curl statement with html", function() {
|
||||
var req = {
|
||||
url: "http://swaggerhub.com/v1/one?name=john|smith",
|
||||
method: "GET",
|
||||
headers: {
|
||||
accept: "application/json"
|
||||
},
|
||||
body: {
|
||||
description: "<b>Test</b>"
|
||||
}
|
||||
}
|
||||
|
||||
let curlified = curl(Im.fromJS(req))
|
||||
|
||||
expect(curlified).toEqual("curl -X GET \"http://swaggerhub.com/v1/one?name=john|smith\" -H \"accept: application/json\" -d {\"description\":\"<b>Test</b>\"}")
|
||||
})
|
||||
|
||||
it("handles post body with html", function() {
|
||||
var req = {
|
||||
url: "http://swaggerhub.com/v1/one?name=john|smith",
|
||||
method: "POST",
|
||||
headers: {
|
||||
accept: "application/json"
|
||||
},
|
||||
body: {
|
||||
description: "<b>Test</b>"
|
||||
}
|
||||
}
|
||||
|
||||
let curlified = curl(Im.fromJS(req))
|
||||
|
||||
expect(curlified).toEqual("curl -X POST \"http://swaggerhub.com/v1/one?name=john|smith\" -H \"accept: application/json\" -d {\"description\":\"<b>Test</b>\"}")
|
||||
})
|
||||
|
||||
it("handles post body with special chars", function() {
|
||||
var req = {
|
||||
url: "http://swaggerhub.com/v1/one?name=john|smith",
|
||||
method: "POST",
|
||||
body: {
|
||||
description: "@prefix nif:<http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#> .\n" +
|
||||
"@prefix itsrdf: <http://www.w3.org/2005/11/its/rdf#> ."
|
||||
}
|
||||
}
|
||||
|
||||
let curlified = curl(Im.fromJS(req))
|
||||
|
||||
expect(curlified).toEqual("curl -X POST \"http://swaggerhub.com/v1/one?name=john|smith\" -d {\"description\":\"@prefix nif:<http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#> .@prefix itsrdf: <http://www.w3.org/2005/11/its/rdf#> .\"}")
|
||||
})
|
||||
|
||||
it("handles delete form with parameters", function() {
|
||||
var req = {
|
||||
url: "http://example.com",
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
accept: "application/x-www-form-urlencoded"
|
||||
}
|
||||
}
|
||||
|
||||
let curlified = curl(Im.fromJS(req))
|
||||
|
||||
expect(curlified).toEqual("curl -X DELETE \"http://example.com\" -H \"accept: application/x-www-form-urlencoded\"")
|
||||
})
|
||||
|
||||
it("should print a curl with formData", function() {
|
||||
var req = {
|
||||
url: "http://example.com",
|
||||
method: "POST",
|
||||
headers: { "content-type": "multipart/form-data" },
|
||||
body: {
|
||||
id: "123",
|
||||
name: "Sahar"
|
||||
}
|
||||
}
|
||||
|
||||
let curlified = curl(Im.fromJS(req))
|
||||
|
||||
expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"content-type: multipart/form-data\" -F \"id=123\" -F \"name=Sahar\"")
|
||||
})
|
||||
|
||||
it("should print a curl with formData and file", function() {
|
||||
var file = new win.File()
|
||||
file.name = "file.txt"
|
||||
file.type = "text/plain"
|
||||
|
||||
var req = {
|
||||
url: "http://example.com",
|
||||
method: "POST",
|
||||
headers: { "content-type": "multipart/form-data" },
|
||||
body: {
|
||||
id: "123",
|
||||
file
|
||||
}
|
||||
}
|
||||
|
||||
let curlified = curl(Im.fromJS(req))
|
||||
|
||||
expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"content-type: multipart/form-data\" -F \"id=123\" -F \"file=@file.txt;type=text/plain\"")
|
||||
})
|
||||
|
||||
it("should print a curl without form data type if type is unknown", function() {
|
||||
var file = new win.File()
|
||||
file.name = "file.txt"
|
||||
file.type = ""
|
||||
|
||||
var req = {
|
||||
url: "http://example.com",
|
||||
method: "POST",
|
||||
headers: { "content-type": "multipart/form-data" },
|
||||
body: {
|
||||
id: "123",
|
||||
file
|
||||
}
|
||||
}
|
||||
|
||||
let curlified = curl(Im.fromJS(req))
|
||||
|
||||
expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"content-type: multipart/form-data\" -F \"id=123\" -F \"file=@file.txt\"")
|
||||
})
|
||||
|
||||
it("prints a curl post statement from an object", function() {
|
||||
var req = {
|
||||
url: "http://example.com",
|
||||
method: "POST",
|
||||
headers: {
|
||||
accept: "application/json"
|
||||
},
|
||||
body: {
|
||||
id: 10101
|
||||
}
|
||||
}
|
||||
|
||||
let curlified = curl(Im.fromJS(req))
|
||||
|
||||
expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"accept: application/json\" -d {\"id\":10101}")
|
||||
})
|
||||
|
||||
it("prints a curl post statement from a string containing a single quote", function() {
|
||||
var req = {
|
||||
url: "http://example.com",
|
||||
method: "POST",
|
||||
headers: {
|
||||
accept: "application/json"
|
||||
},
|
||||
body: "{\"id\":\"foo'bar\"}"
|
||||
}
|
||||
|
||||
let curlified = curl(Im.fromJS(req))
|
||||
|
||||
expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"accept: application/json\" -d \"{\\\"id\\\":\\\"foo'bar\\\"}\"")
|
||||
})
|
||||
|
||||
})
|
||||
142
test/mocha/core/helpers/get-parameter-schema.js
Normal file
142
test/mocha/core/helpers/get-parameter-schema.js
Normal file
@@ -0,0 +1,142 @@
|
||||
/**
|
||||
* @prettier
|
||||
*/
|
||||
|
||||
import expect from "expect"
|
||||
import Im, { fromJS } from "immutable"
|
||||
import getParameterSchema from "../../../../src/helpers/get-parameter-schema"
|
||||
|
||||
describe("getParameterSchema", () => {
|
||||
it("should return an empty Map when given no parameters", () => {
|
||||
const result = getParameterSchema()
|
||||
|
||||
expect(result).toEqual(fromJS({}))
|
||||
})
|
||||
|
||||
it("should return an empty Map when given an empty Map", () => {
|
||||
const result = getParameterSchema(fromJS({}))
|
||||
|
||||
expect(result).toEqual(fromJS({}))
|
||||
})
|
||||
|
||||
it("should return a schema for a Swagger 2.0 query parameter", () => {
|
||||
const result = getParameterSchema(
|
||||
fromJS({
|
||||
name: "id",
|
||||
in: "query",
|
||||
description: "ID of the object to fetch",
|
||||
required: false,
|
||||
type: "array",
|
||||
items: {
|
||||
type: "string",
|
||||
},
|
||||
collectionFormat: "multi",
|
||||
})
|
||||
)
|
||||
|
||||
expect(result.toJS()).toEqual({
|
||||
type: "array",
|
||||
items: {
|
||||
type: "string",
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
it("should return a schema for a Swagger 2.0 body parameter", () => {
|
||||
const result = getParameterSchema(
|
||||
fromJS({
|
||||
name: "user",
|
||||
in: "body",
|
||||
description: "user to add to the system",
|
||||
required: true,
|
||||
schema: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "string",
|
||||
},
|
||||
},
|
||||
})
|
||||
)
|
||||
|
||||
expect(result.toJS()).toEqual({
|
||||
type: "array",
|
||||
items: {
|
||||
type: "string",
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
it("should return a schema for an OpenAPI 3.0 query parameter", () => {
|
||||
const result = getParameterSchema(
|
||||
fromJS({
|
||||
name: "id",
|
||||
in: "query",
|
||||
description: "ID of the object to fetch",
|
||||
required: false,
|
||||
schema: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "string",
|
||||
},
|
||||
},
|
||||
style: "form",
|
||||
explode: true,
|
||||
}),
|
||||
{
|
||||
isOAS3: true,
|
||||
}
|
||||
)
|
||||
|
||||
expect(result.toJS()).toEqual({
|
||||
type: "array",
|
||||
items: {
|
||||
type: "string",
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
it("should return a schema for an OpenAPI 3.0 query parameter with `content`", () => {
|
||||
const result = getParameterSchema(
|
||||
fromJS({
|
||||
in: "query",
|
||||
name: "coordinates",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
type: "object",
|
||||
required: ["lat", "long"],
|
||||
properties: {
|
||||
lat: {
|
||||
type: "number",
|
||||
},
|
||||
long: {
|
||||
type: "number",
|
||||
},
|
||||
},
|
||||
},
|
||||
"should-ignore/the-second-media-type": {
|
||||
type: "string",
|
||||
default: "this shouldn't be returned",
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
{
|
||||
isOAS3: true,
|
||||
}
|
||||
)
|
||||
|
||||
expect(result.toJS()).toEqual({
|
||||
type: "object",
|
||||
required: ["lat", "long"],
|
||||
properties: {
|
||||
lat: {
|
||||
type: "number",
|
||||
},
|
||||
long: {
|
||||
type: "number",
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
39
test/mocha/core/oauth2-authorize.js
Normal file
39
test/mocha/core/oauth2-authorize.js
Normal file
@@ -0,0 +1,39 @@
|
||||
/* eslint-env mocha */
|
||||
import expect, { createSpy } from "expect"
|
||||
import { fromJS } from "immutable"
|
||||
import win from "core/window"
|
||||
import oauth2Authorize from "core/oauth2-authorize"
|
||||
|
||||
describe("oauth2", function () {
|
||||
|
||||
let mockSchema = {
|
||||
flow: "accessCode",
|
||||
authorizationUrl: "https://testAuthorizationUrl"
|
||||
}
|
||||
|
||||
let authConfig = {
|
||||
auth: { schema: { get: (key)=> mockSchema[key] } },
|
||||
authActions: {},
|
||||
errActions: {},
|
||||
configs: { oauth2RedirectUrl: "" },
|
||||
authConfigs: {}
|
||||
}
|
||||
|
||||
describe("authorize redirect", function () {
|
||||
|
||||
it("should build authorize url", function() {
|
||||
win.open = createSpy()
|
||||
oauth2Authorize(authConfig)
|
||||
expect(win.open.calls.length).toEqual(1)
|
||||
expect(win.open.calls[0].arguments[0]).toMatch("https://testAuthorizationUrl?response_type=code&redirect_uri=&state=")
|
||||
})
|
||||
|
||||
it("should append query parameters to authorizeUrl with query parameters", function() {
|
||||
win.open = createSpy()
|
||||
mockSchema.authorizationUrl = "https://testAuthorizationUrl?param=1"
|
||||
oauth2Authorize(authConfig)
|
||||
expect(win.open.calls.length).toEqual(1)
|
||||
expect(win.open.calls[0].arguments[0]).toMatch("https://testAuthorizationUrl?param=1&response_type=code&redirect_uri=&state=")
|
||||
})
|
||||
})
|
||||
})
|
||||
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)
|
||||
})
|
||||
})
|
||||
1028
test/mocha/core/system/system.jsx
Normal file
1028
test/mocha/core/system/system.jsx
Normal file
File diff suppressed because it is too large
Load Diff
247
test/mocha/core/system/wrapComponent.jsx
Normal file
247
test/mocha/core/system/wrapComponent.jsx
Normal file
@@ -0,0 +1,247 @@
|
||||
import React from "react"
|
||||
import expect from "expect"
|
||||
import { render } from "enzyme"
|
||||
import System from "core/system"
|
||||
|
||||
describe("wrapComponents", () => {
|
||||
describe("should wrap a component and provide a reference to the original", () => {
|
||||
it("with stateless components", function(){
|
||||
// Given
|
||||
const system = new System({
|
||||
plugins: [
|
||||
{
|
||||
components: {
|
||||
wow: ({ name }) => <div>{name} component</div>
|
||||
}
|
||||
},
|
||||
{
|
||||
wrapComponents: {
|
||||
wow: (OriginalComponent) => (props) => {
|
||||
return <container>
|
||||
<OriginalComponent {...props}></OriginalComponent>
|
||||
<OriginalComponent name="Wrapped"></OriginalComponent>
|
||||
</container>
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
// When
|
||||
var Component = system.getSystem().getComponents("wow")
|
||||
const wrapper = render(<Component name="Normal" />)
|
||||
|
||||
const container = wrapper.children().first()
|
||||
expect(container[0].name).toEqual("container")
|
||||
|
||||
const children = container.children()
|
||||
expect(children.length).toEqual(2)
|
||||
expect(children.eq(0).text()).toEqual("Normal component")
|
||||
expect(children.eq(1).text()).toEqual("Wrapped component")
|
||||
})
|
||||
|
||||
it("with React classes", function(){
|
||||
class MyComponent extends React.Component {
|
||||
render() {
|
||||
return <div>{this.props.name} component</div>
|
||||
}
|
||||
}
|
||||
|
||||
// Given
|
||||
const system = new System({
|
||||
plugins: [
|
||||
{
|
||||
components: {
|
||||
wow: MyComponent
|
||||
}
|
||||
},
|
||||
{
|
||||
wrapComponents: {
|
||||
wow: (OriginalComponent) => {
|
||||
return class WrapperComponent extends React.Component {
|
||||
render() {
|
||||
return <container>
|
||||
<OriginalComponent {...this.props}></OriginalComponent>
|
||||
<OriginalComponent name="Wrapped"></OriginalComponent>
|
||||
</container>
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
// When
|
||||
var Component = system.getSystem().getComponents("wow")
|
||||
const wrapper = render(<Component name="Normal" />)
|
||||
|
||||
const container = wrapper.children().first()
|
||||
expect(container[0].name).toEqual("container")
|
||||
|
||||
const children = container.children()
|
||||
expect(children.length).toEqual(2)
|
||||
expect(children.eq(0).text()).toEqual("Normal component")
|
||||
expect(children.eq(1).text()).toEqual("Wrapped component")
|
||||
})
|
||||
})
|
||||
|
||||
it("should provide a reference to the system to the wrapper", function(){
|
||||
|
||||
// Given
|
||||
|
||||
const mySystem = new System({
|
||||
plugins: [
|
||||
{
|
||||
// Make a selector
|
||||
statePlugins: {
|
||||
doge: {
|
||||
selectors: {
|
||||
wow: () => () => {
|
||||
return "WOW much data"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
// Create a component
|
||||
components: {
|
||||
wow: () => <div>Original component</div>
|
||||
}
|
||||
},
|
||||
{
|
||||
// Wrap the component and use the system
|
||||
wrapComponents: {
|
||||
wow: (OriginalComponent, system) => (props) => {
|
||||
return <container>
|
||||
<OriginalComponent {...props}></OriginalComponent>
|
||||
<div>{system.dogeSelectors.wow()}</div>
|
||||
</container>
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
// Then
|
||||
var Component = mySystem.getSystem().getComponents("wow")
|
||||
const wrapper = render(<Component name="Normal" />)
|
||||
|
||||
const container = wrapper.children().first()
|
||||
expect(container[0].name).toEqual("container")
|
||||
|
||||
const children = container.children()
|
||||
expect(children.length).toEqual(2)
|
||||
expect(children.eq(0).text()).toEqual("Original component")
|
||||
expect(children.eq(1).text()).toEqual("WOW much data")
|
||||
})
|
||||
|
||||
it("should wrap correctly when registering more plugins", function(){
|
||||
|
||||
// Given
|
||||
|
||||
const mySystem = new System({
|
||||
plugins: [
|
||||
() => {
|
||||
return {
|
||||
statePlugins: {
|
||||
doge: {
|
||||
selectors: {
|
||||
wow: () => () => {
|
||||
return "WOW much data"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
wow: () => <div>Original component</div>
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
mySystem.register([
|
||||
function() {
|
||||
return {
|
||||
// Wrap the component and use the system
|
||||
wrapComponents: {
|
||||
wow: (OriginalComponent, system) => (props) => {
|
||||
return <container>
|
||||
<OriginalComponent {...props}></OriginalComponent>
|
||||
<div>{system.dogeSelectors.wow()}</div>
|
||||
</container>
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
])
|
||||
|
||||
// Then
|
||||
var Component = mySystem.getSystem().getComponents("wow")
|
||||
const wrapper = render(<Component name="Normal" />)
|
||||
|
||||
const container = wrapper.children().first()
|
||||
expect(container[0].name).toEqual("container")
|
||||
|
||||
const children = container.children()
|
||||
expect(children.length).toEqual(2)
|
||||
expect(children.eq(0).text()).toEqual("Original component")
|
||||
expect(children.eq(1).text()).toEqual("WOW much data")
|
||||
})
|
||||
|
||||
it("should wrap correctly when building a system twice", function(){
|
||||
|
||||
// Given
|
||||
|
||||
const pluginOne = {
|
||||
statePlugins: {
|
||||
doge: {
|
||||
selectors: {
|
||||
wow: () => () => {
|
||||
return "WOW much data"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
components: {
|
||||
wow: () => <div>Original component</div>
|
||||
}
|
||||
}
|
||||
|
||||
const pluginTwo = {
|
||||
// Wrap the component and use the system
|
||||
wrapComponents: {
|
||||
wow: (OriginalComponent, system) => (props) => {
|
||||
return <container>
|
||||
<OriginalComponent {...props}></OriginalComponent>
|
||||
<div>{system.dogeSelectors.wow()}</div>
|
||||
</container>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const bothPlugins = () => [pluginOne, pluginTwo]
|
||||
|
||||
new System({
|
||||
plugins: bothPlugins
|
||||
})
|
||||
|
||||
const secondSystem = new System({
|
||||
plugins: bothPlugins
|
||||
})
|
||||
|
||||
// Then
|
||||
var Component = secondSystem.getSystem().getComponents("wow")
|
||||
const wrapper = render(<Component name="Normal" />)
|
||||
|
||||
const container = wrapper.children().first()
|
||||
expect(container[0].name).toEqual("container")
|
||||
|
||||
const children = container.children()
|
||||
expect(children.length).toEqual(2)
|
||||
expect(children.eq(0).text()).toEqual("Original component")
|
||||
expect(children.eq(1).text()).toEqual("WOW much data")
|
||||
})
|
||||
})
|
||||
1389
test/mocha/core/utils.js
Normal file
1389
test/mocha/core/utils.js
Normal file
File diff suppressed because it is too large
Load Diff
58
test/mocha/docker/oauth.js
Normal file
58
test/mocha/docker/oauth.js
Normal file
@@ -0,0 +1,58 @@
|
||||
const expect = require("expect")
|
||||
const oauthBlockBuilder = require("../../../docker/configurator/oauth")
|
||||
const dedent = require("dedent")
|
||||
|
||||
describe("docker: env translator - oauth block", function() {
|
||||
it("should omit the block if there are no valid keys", function () {
|
||||
const input = {}
|
||||
|
||||
expect(oauthBlockBuilder(input)).toEqual(``)
|
||||
})
|
||||
it("should omit the block if there are no valid keys", function () {
|
||||
const input = {
|
||||
NOT_A_VALID_KEY: "asdf1234"
|
||||
}
|
||||
|
||||
expect(oauthBlockBuilder(input)).toEqual(``)
|
||||
})
|
||||
it("should generate a block from empty values", function() {
|
||||
const input = {
|
||||
OAUTH_CLIENT_ID: ``,
|
||||
OAUTH_CLIENT_SECRET: ``,
|
||||
OAUTH_REALM: ``,
|
||||
OAUTH_APP_NAME: ``,
|
||||
OAUTH_SCOPE_SEPARATOR: "",
|
||||
OAUTH_ADDITIONAL_PARAMS: ``,
|
||||
}
|
||||
|
||||
expect(oauthBlockBuilder(input)).toEqual(dedent(`
|
||||
ui.initOAuth({
|
||||
clientId: "",
|
||||
clientSecret: "",
|
||||
realm: "",
|
||||
appName: "",
|
||||
scopeSeparator: "",
|
||||
additionalQueryStringParams: undefined,
|
||||
})`))
|
||||
})
|
||||
it("should generate a full block", function() {
|
||||
const input = {
|
||||
OAUTH_CLIENT_ID: `myId`,
|
||||
OAUTH_CLIENT_SECRET: `mySecret`,
|
||||
OAUTH_REALM: `myRealm`,
|
||||
OAUTH_APP_NAME: `myAppName`,
|
||||
OAUTH_SCOPE_SEPARATOR: "%21",
|
||||
OAUTH_ADDITIONAL_PARAMS: `{ "a": 1234, "b": "stuff" }`,
|
||||
}
|
||||
|
||||
expect(oauthBlockBuilder(input)).toEqual(dedent(`
|
||||
ui.initOAuth({
|
||||
clientId: "myId",
|
||||
clientSecret: "mySecret",
|
||||
realm: "myRealm",
|
||||
appName: "myAppName",
|
||||
scopeSeparator: "%21",
|
||||
additionalQueryStringParams: { "a": 1234, "b": "stuff" },
|
||||
})`))
|
||||
})
|
||||
})
|
||||
339
test/mocha/docker/translator.js
Normal file
339
test/mocha/docker/translator.js
Normal file
@@ -0,0 +1,339 @@
|
||||
const expect = require("expect")
|
||||
const translator = require("../../../docker/configurator/translator")
|
||||
const dedent = require("dedent")
|
||||
|
||||
describe("docker: env translator", function() {
|
||||
describe("fundamentals", function() {
|
||||
it("should generate an empty baseline config", function () {
|
||||
const input = {}
|
||||
|
||||
expect(translator(input)).toEqual(``)
|
||||
})
|
||||
|
||||
it("should call an onFound callback", function () {
|
||||
const input = {
|
||||
MY_THING: "hey"
|
||||
}
|
||||
|
||||
const onFoundSpy = expect.createSpy()
|
||||
|
||||
const schema = {
|
||||
MY_THING: {
|
||||
type: "string",
|
||||
name: "myThing",
|
||||
onFound: onFoundSpy
|
||||
}
|
||||
}
|
||||
|
||||
const res = translator(input, {
|
||||
schema
|
||||
})
|
||||
expect(res).toEqual(`myThing: "hey",`)
|
||||
expect(onFoundSpy.calls.length).toEqual(1)
|
||||
|
||||
})
|
||||
|
||||
it("should use a regular value over a legacy one, regardless of order", function () {
|
||||
const schema = {
|
||||
MY_THING: {
|
||||
type: "string",
|
||||
name: "myThing"
|
||||
},
|
||||
MY_OTHER_THING: {
|
||||
type: "string",
|
||||
name: "myThing",
|
||||
legacy: true
|
||||
}
|
||||
}
|
||||
|
||||
// Regular value provided first
|
||||
expect(translator({
|
||||
MY_THING: "hey",
|
||||
MY_OTHER_THING: "hello"
|
||||
}, {
|
||||
schema
|
||||
})).toEqual(`myThing: "hey",`)
|
||||
|
||||
// Legacy value provided first
|
||||
expect(translator({
|
||||
MY_OTHER_THING: "hello",
|
||||
MY_THING: "hey"
|
||||
}, {
|
||||
schema
|
||||
})).toEqual(`myThing: "hey",`)
|
||||
})
|
||||
|
||||
it("should use a legacy value over a base one, regardless of order", function () {
|
||||
const schema = {
|
||||
MY_THING: {
|
||||
type: "string",
|
||||
name: "myThing",
|
||||
legacy: true
|
||||
}
|
||||
}
|
||||
|
||||
const baseConfig = {
|
||||
myThing: {
|
||||
value: "base",
|
||||
schema: {
|
||||
type: "string",
|
||||
base: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Regular value provided first
|
||||
expect(translator({
|
||||
MY_THING: "legacy"
|
||||
}, {
|
||||
injectBaseConfig: true,
|
||||
schema,
|
||||
baseConfig
|
||||
})).toEqual(`myThing: "legacy",`)
|
||||
})
|
||||
})
|
||||
describe("Swagger UI configuration", function() {
|
||||
it("should generate a base config including the base content", function () {
|
||||
const input = {}
|
||||
|
||||
expect(translator(input, {
|
||||
injectBaseConfig: true
|
||||
})).toEqual(dedent(`
|
||||
url: "https://petstore.swagger.io/v2/swagger.json",
|
||||
"dom_id": "#swagger-ui",
|
||||
deepLinking: true,
|
||||
presets: [
|
||||
SwaggerUIBundle.presets.apis,
|
||||
SwaggerUIStandalonePreset
|
||||
],
|
||||
plugins: [
|
||||
SwaggerUIBundle.plugins.DownloadUrl
|
||||
],
|
||||
layout: "StandaloneLayout",
|
||||
`))
|
||||
})
|
||||
|
||||
it("should ignore an unknown config", function () {
|
||||
const input = {
|
||||
ASDF1234: "wow hello"
|
||||
}
|
||||
|
||||
expect(translator(input)).toEqual(dedent(``))
|
||||
})
|
||||
|
||||
it("should generate a string config", function () {
|
||||
const input = {
|
||||
URL: "http://petstore.swagger.io/v2/swagger.json",
|
||||
FILTER: ""
|
||||
}
|
||||
|
||||
expect(translator(input)).toEqual(dedent(`
|
||||
url: "http://petstore.swagger.io/v2/swagger.json",
|
||||
filter: "",`
|
||||
).trim())
|
||||
})
|
||||
|
||||
it("should generate a boolean config", function () {
|
||||
const input = {
|
||||
DEEP_LINKING: "true",
|
||||
SHOW_EXTENSIONS: "false",
|
||||
SHOW_COMMON_EXTENSIONS: ""
|
||||
}
|
||||
|
||||
expect(translator(input)).toEqual(dedent(`
|
||||
deepLinking: true,
|
||||
showExtensions: false,
|
||||
showCommonExtensions: undefined,`
|
||||
))
|
||||
})
|
||||
|
||||
it("should generate an object config", function () {
|
||||
const input = {
|
||||
SPEC: `{ swagger: "2.0" }`
|
||||
}
|
||||
|
||||
expect(translator(input)).toEqual(dedent(`
|
||||
spec: { swagger: "2.0" },`
|
||||
).trim())
|
||||
})
|
||||
|
||||
it("should generate an array config", function () {
|
||||
const input = {
|
||||
URLS: `["/one", "/two"]`,
|
||||
SUPPORTED_SUBMIT_METHODS: ""
|
||||
}
|
||||
|
||||
expect(translator(input)).toEqual(dedent(`
|
||||
urls: ["/one", "/two"],
|
||||
supportedSubmitMethods: undefined,`
|
||||
).trim())
|
||||
})
|
||||
|
||||
it("should properly escape key names when necessary", function () {
|
||||
const input = {
|
||||
URLS: `["/one", "/two"]`,
|
||||
URLS_PRIMARY_NAME: "one",
|
||||
}
|
||||
|
||||
expect(translator(input)).toEqual(dedent(`
|
||||
urls: ["/one", "/two"],
|
||||
"urls.primaryName": "one",`
|
||||
).trim())
|
||||
})
|
||||
|
||||
it("should disregard a legacy variable in favor of a regular one", function () {
|
||||
const input = {
|
||||
// Order is important to this test... legacy vars should be
|
||||
// superseded regardless of what is fed in first.
|
||||
API_URL: "/old.json",
|
||||
URL: "/swagger.json",
|
||||
URLS: `["/one", "/two"]`,
|
||||
API_URLS: `["/three", "/four"]`,
|
||||
}
|
||||
|
||||
expect(translator(input)).toEqual(dedent(`
|
||||
url: "/swagger.json",
|
||||
urls: ["/one", "/two"],`
|
||||
).trim())
|
||||
})
|
||||
|
||||
|
||||
it("should pick up legacy variables when using base config", function () {
|
||||
const input = {
|
||||
API_URL: "/swagger.json",
|
||||
API_URLS: `["/one", "/two"]`,
|
||||
}
|
||||
|
||||
expect(translator(input, { injectBaseConfig: true })).toEqual(dedent(`
|
||||
"dom_id": "#swagger-ui",
|
||||
deepLinking: true,
|
||||
presets: [
|
||||
SwaggerUIBundle.presets.apis,
|
||||
SwaggerUIStandalonePreset
|
||||
],
|
||||
plugins: [
|
||||
SwaggerUIBundle.plugins.DownloadUrl
|
||||
],
|
||||
layout: "StandaloneLayout",
|
||||
url: "/swagger.json",
|
||||
urls: ["/one", "/two"],`
|
||||
|
||||
).trim())
|
||||
})
|
||||
|
||||
it("should generate a full config k:v string", function () {
|
||||
const input = {
|
||||
API_URL: "/old.yaml",
|
||||
API_URLS: `["/old", "/older"]`,
|
||||
CONFIG_URL: "/wow",
|
||||
DOM_ID: "#swagger_ui",
|
||||
SPEC: `{ swagger: "2.0" }`,
|
||||
URL: "/swagger.json",
|
||||
URLS: `["/one", "/two"]`,
|
||||
URLS_PRIMARY_NAME: "one",
|
||||
LAYOUT: "BaseLayout",
|
||||
DEEP_LINKING: "false",
|
||||
DISPLAY_OPERATION_ID: "true",
|
||||
DEFAULT_MODELS_EXPAND_DEPTH: "0",
|
||||
DEFAULT_MODEL_EXPAND_DEPTH: "1",
|
||||
DEFAULT_MODEL_RENDERING: "example",
|
||||
DISPLAY_REQUEST_DURATION: "true",
|
||||
DOC_EXPANSION: "full",
|
||||
FILTER: "wowee",
|
||||
MAX_DISPLAYED_TAGS: "4",
|
||||
SHOW_EXTENSIONS: "true",
|
||||
SHOW_COMMON_EXTENSIONS: "false",
|
||||
OAUTH2_REDIRECT_URL: "http://google.com/",
|
||||
SHOW_MUTATED_REQUEST: "true",
|
||||
SUPPORTED_SUBMIT_METHODS: `["get", "post"]`,
|
||||
VALIDATOR_URL: "http://smartbear.com/"
|
||||
}
|
||||
|
||||
expect(translator(input)).toEqual(dedent(`
|
||||
configUrl: "/wow",
|
||||
"dom_id": "#swagger_ui",
|
||||
spec: { swagger: "2.0" },
|
||||
url: "/swagger.json",
|
||||
urls: ["/one", "/two"],
|
||||
"urls.primaryName": "one",
|
||||
layout: "BaseLayout",
|
||||
deepLinking: false,
|
||||
displayOperationId: true,
|
||||
defaultModelsExpandDepth: 0,
|
||||
defaultModelExpandDepth: 1,
|
||||
defaultModelRendering: "example",
|
||||
displayRequestDuration: true,
|
||||
docExpansion: "full",
|
||||
filter: "wowee",
|
||||
maxDisplayedTags: 4,
|
||||
showExtensions: true,
|
||||
showCommonExtensions: false,
|
||||
oauth2RedirectUrl: "http://google.com/",
|
||||
showMutatedRequest: true,
|
||||
supportedSubmitMethods: ["get", "post"],
|
||||
validatorUrl: "http://smartbear.com/",`
|
||||
).trim())
|
||||
})
|
||||
|
||||
it("should generate a full config k:v string including base config", function () {
|
||||
const input = {
|
||||
API_URL: "/old.yaml",
|
||||
API_URLS: `["/old", "/older"]`,
|
||||
CONFIG_URL: "/wow",
|
||||
DOM_ID: "#swagger_ui",
|
||||
SPEC: `{ swagger: "2.0" }`,
|
||||
URL: "/swagger.json",
|
||||
URLS: `["/one", "/two"]`,
|
||||
URLS_PRIMARY_NAME: "one",
|
||||
LAYOUT: "BaseLayout",
|
||||
DEEP_LINKING: "false",
|
||||
DISPLAY_OPERATION_ID: "true",
|
||||
DEFAULT_MODELS_EXPAND_DEPTH: "0",
|
||||
DEFAULT_MODEL_EXPAND_DEPTH: "1",
|
||||
DEFAULT_MODEL_RENDERING: "example",
|
||||
DISPLAY_REQUEST_DURATION: "true",
|
||||
DOC_EXPANSION: "full",
|
||||
FILTER: "wowee",
|
||||
MAX_DISPLAYED_TAGS: "4",
|
||||
SHOW_EXTENSIONS: "true",
|
||||
SHOW_COMMON_EXTENSIONS: "false",
|
||||
OAUTH2_REDIRECT_URL: "http://google.com/",
|
||||
SHOW_MUTATED_REQUEST: "true",
|
||||
SUPPORTED_SUBMIT_METHODS: `["get", "post"]`,
|
||||
VALIDATOR_URL: "http://smartbear.com/"
|
||||
}
|
||||
|
||||
expect(translator(input, { injectBaseConfig: true })).toEqual(dedent(`
|
||||
presets: [
|
||||
SwaggerUIBundle.presets.apis,
|
||||
SwaggerUIStandalonePreset
|
||||
],
|
||||
plugins: [
|
||||
SwaggerUIBundle.plugins.DownloadUrl
|
||||
],
|
||||
configUrl: "/wow",
|
||||
"dom_id": "#swagger_ui",
|
||||
spec: { swagger: "2.0" },
|
||||
url: "/swagger.json",
|
||||
urls: ["/one", "/two"],
|
||||
"urls.primaryName": "one",
|
||||
layout: "BaseLayout",
|
||||
deepLinking: false,
|
||||
displayOperationId: true,
|
||||
defaultModelsExpandDepth: 0,
|
||||
defaultModelExpandDepth: 1,
|
||||
defaultModelRendering: "example",
|
||||
displayRequestDuration: true,
|
||||
docExpansion: "full",
|
||||
filter: "wowee",
|
||||
maxDisplayedTags: 4,
|
||||
showExtensions: true,
|
||||
showCommonExtensions: false,
|
||||
oauth2RedirectUrl: "http://google.com/",
|
||||
showMutatedRequest: true,
|
||||
supportedSubmitMethods: ["get", "post"],
|
||||
validatorUrl: "http://smartbear.com/",`
|
||||
).trim())
|
||||
})
|
||||
})
|
||||
})
|
||||
23
test/mocha/setup.js
Normal file
23
test/mocha/setup.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const { JSDOM } = require("jsdom")
|
||||
const win = require("core/window")
|
||||
|
||||
const jsdom = new JSDOM("<!doctype html><html><body></body></html>")
|
||||
const { window } = jsdom
|
||||
|
||||
function copyProps(src, target) {
|
||||
const props = Object.getOwnPropertyNames(src)
|
||||
.filter(prop => typeof target[prop] === "undefined")
|
||||
.reduce((result, prop) => ({
|
||||
...result,
|
||||
[prop]: Object.getOwnPropertyDescriptor(src, prop),
|
||||
}), {})
|
||||
Object.defineProperties(target, props)
|
||||
}
|
||||
|
||||
global.window = window
|
||||
global.document = window.document
|
||||
global.navigator = {
|
||||
userAgent: "node.js",
|
||||
}
|
||||
copyProps(win, window) // use UI's built-in window wrapper
|
||||
copyProps(window, global)
|
||||
13
test/mocha/swagger-ui-dist-package/absolute-path.js
Normal file
13
test/mocha/swagger-ui-dist-package/absolute-path.js
Normal file
@@ -0,0 +1,13 @@
|
||||
/* eslint-env mocha */
|
||||
import expect from "expect"
|
||||
import path from "path"
|
||||
import getAbsoluteFSPath from "../../../swagger-ui-dist-package/absolute-path"
|
||||
|
||||
describe("swagger-ui-dist", function(){
|
||||
describe("getAbsoluteFSPath", function(){
|
||||
it("returns absolute path", function(){
|
||||
const expectedPath = path.resolve(__dirname, "../../../swagger-ui-dist-package")
|
||||
expect(getAbsoluteFSPath()).toEqual(expectedPath)
|
||||
})
|
||||
})
|
||||
})
|
||||
108
test/mocha/xss/anchor-target-rel/info.jsx
Normal file
108
test/mocha/xss/anchor-target-rel/info.jsx
Normal file
@@ -0,0 +1,108 @@
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import expect from "expect"
|
||||
import { render } from "enzyme"
|
||||
import { fromJS } from "immutable"
|
||||
import Info, { InfoUrl } from "components/info"
|
||||
import { Link } from "components/layout-utils"
|
||||
import Markdown from "components/providers/markdown"
|
||||
|
||||
describe("<Info/> Anchor Target Safety", function(){
|
||||
const dummyComponent = () => null
|
||||
const components = {
|
||||
Markdown,
|
||||
InfoUrl,
|
||||
Link
|
||||
}
|
||||
const baseProps = {
|
||||
getComponent: c => components[c] || dummyComponent,
|
||||
host: "example.test",
|
||||
basePath: "/api",
|
||||
info: fromJS({
|
||||
title: "Hello World"
|
||||
})
|
||||
}
|
||||
|
||||
it("renders externalDocs links with safe `rel` attributes", function () {
|
||||
const props = {
|
||||
...baseProps,
|
||||
externalDocs: fromJS({
|
||||
url: "http://google.com/"
|
||||
})
|
||||
}
|
||||
let wrapper = render(<Info {...props} />)
|
||||
const anchor = wrapper.find("a")
|
||||
|
||||
expect(anchor.html()).toEqual("http://google.com/")
|
||||
expect(anchor.attr("target")).toEqual("_blank")
|
||||
expect(anchor.attr("rel") || "").toInclude("noopener")
|
||||
expect(anchor.attr("rel") || "").toInclude("noreferrer")
|
||||
})
|
||||
|
||||
it("renders Contact links with safe `rel` attributes", function () {
|
||||
const props = {
|
||||
...baseProps,
|
||||
info: fromJS({
|
||||
contact: {
|
||||
url: "http://google.com/",
|
||||
name: "My Site"
|
||||
}
|
||||
})
|
||||
}
|
||||
let wrapper = render(<Info {...props} />)
|
||||
const anchor = wrapper.find("a")
|
||||
|
||||
expect(anchor.attr("href")).toEqual("http://google.com/")
|
||||
expect(anchor.attr("target")).toEqual("_blank")
|
||||
expect(anchor.attr("rel") || "").toInclude("noopener")
|
||||
expect(anchor.attr("rel") || "").toInclude("noreferrer")
|
||||
})
|
||||
|
||||
it("renders License links with safe `rel` attributes", function () {
|
||||
const props = {
|
||||
...baseProps,
|
||||
info: fromJS({
|
||||
license: {
|
||||
url: "http://mit.edu/"
|
||||
}
|
||||
})
|
||||
}
|
||||
let wrapper = render(<Info {...props} />)
|
||||
const anchor = wrapper.find("a")
|
||||
|
||||
expect(anchor.attr("href")).toEqual("http://mit.edu/")
|
||||
expect(anchor.attr("target")).toEqual("_blank")
|
||||
expect(anchor.attr("rel") || "").toInclude("noopener")
|
||||
expect(anchor.attr("rel") || "").toInclude("noreferrer")
|
||||
})
|
||||
|
||||
it("renders termsOfService links with safe `rel` attributes", function () {
|
||||
const props = {
|
||||
...baseProps,
|
||||
info: fromJS({
|
||||
termsOfService: "http://smartbear.com/"
|
||||
})
|
||||
}
|
||||
let wrapper = render(<Info {...props} />)
|
||||
const anchor = wrapper.find("a")
|
||||
|
||||
expect(anchor.attr("href")).toEqual("http://smartbear.com/")
|
||||
expect(anchor.attr("target")).toEqual("_blank")
|
||||
expect(anchor.attr("rel") || "").toInclude("noopener")
|
||||
expect(anchor.attr("rel") || "").toInclude("noreferrer")
|
||||
})
|
||||
|
||||
it("renders definition URL links with safe `rel` attributes", function () {
|
||||
const props = {
|
||||
...baseProps,
|
||||
url: "http://petstore.swagger.io/v2/petstore.json"
|
||||
}
|
||||
let wrapper = render(<Info {...props} />)
|
||||
const anchor = wrapper.find("a")
|
||||
|
||||
expect(anchor.attr("href")).toEqual("http://petstore.swagger.io/v2/petstore.json")
|
||||
expect(anchor.attr("target")).toEqual("_blank")
|
||||
expect(anchor.attr("rel") || "").toInclude("noopener")
|
||||
expect(anchor.attr("rel") || "").toInclude("noreferrer")
|
||||
})
|
||||
})
|
||||
44
test/mocha/xss/anchor-target-rel/link.jsx
Normal file
44
test/mocha/xss/anchor-target-rel/link.jsx
Normal file
@@ -0,0 +1,44 @@
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import expect from "expect"
|
||||
import { render } from "enzyme"
|
||||
import { fromJS } from "immutable"
|
||||
import { Link } from "components/layout-utils"
|
||||
|
||||
describe("<Link/> Anchor Target Safety", function () {
|
||||
const dummyComponent = () => null
|
||||
const components = {
|
||||
Link
|
||||
}
|
||||
const baseProps = {
|
||||
getComponent: c => components[c] || dummyComponent
|
||||
}
|
||||
|
||||
it("renders regular links with `noreferrer` and `noopener`", function () {
|
||||
const props = {
|
||||
...baseProps,
|
||||
href: "http://google.com/"
|
||||
}
|
||||
let wrapper = render(<Link {...props} />)
|
||||
const anchor = wrapper.find("a")
|
||||
|
||||
expect(anchor.attr("href")).toEqual("http://google.com/")
|
||||
expect(anchor.attr("rel") || "").toInclude("noopener")
|
||||
expect(anchor.attr("rel") || "").toInclude("noreferrer")
|
||||
})
|
||||
|
||||
it("enforces `noreferrer` and `noopener` on target=_blank links", function () {
|
||||
const props = {
|
||||
...baseProps,
|
||||
href: "http://google.com/",
|
||||
target: "_blank"
|
||||
}
|
||||
let wrapper = render(<Link {...props} />)
|
||||
const anchor = wrapper.find("a")
|
||||
|
||||
expect(anchor.attr("href")).toEqual("http://google.com/")
|
||||
expect(anchor.attr("target")).toEqual("_blank")
|
||||
expect(anchor.attr("rel") || "").toInclude("noopener")
|
||||
expect(anchor.attr("rel") || "").toInclude("noreferrer")
|
||||
})
|
||||
})
|
||||
66
test/mocha/xss/anchor-target-rel/markdown.jsx
Normal file
66
test/mocha/xss/anchor-target-rel/markdown.jsx
Normal file
@@ -0,0 +1,66 @@
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import expect from "expect"
|
||||
import { render } from "enzyme"
|
||||
import Markdown from "components/providers/markdown"
|
||||
import { Markdown as OAS3Markdown } from "corePlugins/oas3/wrap-components/markdown.jsx"
|
||||
|
||||
describe("Markdown Link Anchor Safety", function () {
|
||||
describe("Swagger 2.0", function () {
|
||||
it("sanitizes Markdown links", function () {
|
||||
const str = `Hello, [here](http://google.com/) is my link`
|
||||
const wrapper = render(<Markdown source={str} />)
|
||||
|
||||
const anchor = wrapper.find("a")
|
||||
|
||||
expect(anchor.attr("href")).toEqual("http://google.com/")
|
||||
expect(anchor.attr("target")).toEqual("_blank")
|
||||
expect(anchor.attr("rel") || "").toInclude("noopener")
|
||||
expect(anchor.attr("rel") || "").toInclude("noreferrer")
|
||||
})
|
||||
|
||||
it("sanitizes raw HTML links", function () {
|
||||
const str = `Hello, <a href="http://google.com/">here</a> is my link`
|
||||
const wrapper = render(<Markdown source={str} />)
|
||||
|
||||
const anchor = wrapper.find("a")
|
||||
|
||||
expect(anchor.attr("href")).toEqual("http://google.com/")
|
||||
expect(anchor.attr("rel") || "").toInclude("noopener")
|
||||
expect(anchor.attr("rel") || "").toInclude("noreferrer")
|
||||
})
|
||||
})
|
||||
|
||||
describe("OAS 3", function () {
|
||||
it("sanitizes Markdown links", function () {
|
||||
const str = `Hello, [here](http://google.com/) is my link`
|
||||
const wrapper = render(<OAS3Markdown source={str} />)
|
||||
|
||||
const anchor = wrapper.find("a")
|
||||
|
||||
expect(anchor.attr("href")).toEqual("http://google.com/")
|
||||
expect(anchor.attr("target")).toEqual("_blank")
|
||||
expect(anchor.attr("rel") || "").toInclude("noopener")
|
||||
expect(anchor.attr("rel") || "").toInclude("noreferrer")
|
||||
})
|
||||
|
||||
it("sanitizes raw HTML links", function () {
|
||||
const str = `Hello, <a href="http://google.com/">here</a> is my link`
|
||||
const wrapper = render(<OAS3Markdown source={str} />)
|
||||
|
||||
const anchor = wrapper.find("a")
|
||||
|
||||
expect(anchor.attr("href")).toEqual("http://google.com/")
|
||||
expect(anchor.attr("rel") || "").toInclude("noopener")
|
||||
expect(anchor.attr("rel") || "").toInclude("noreferrer")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
function withMarkdownWrapper(str, { isOAS3 = false } = {}) {
|
||||
if(isOAS3) {
|
||||
return `<div class="renderedMarkdown"><p>${str}</p></div>`
|
||||
}
|
||||
|
||||
return `<div class="markdown"><p>${str}</p>\n</div>`
|
||||
}
|
||||
32
test/mocha/xss/anchor-target-rel/online-validator-badge.jsx
Normal file
32
test/mocha/xss/anchor-target-rel/online-validator-badge.jsx
Normal file
@@ -0,0 +1,32 @@
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import expect, { createSpy } from "expect"
|
||||
import { mount } from "enzyme"
|
||||
import { fromJS, Map } from "immutable"
|
||||
import OnlineValidatorBadge from "components/online-validator-badge"
|
||||
|
||||
describe("<OnlineValidatorBadge/> Anchor Target Safety", function () {
|
||||
it("should render a validator link with safe `rel` attributes", function () {
|
||||
// When
|
||||
const props = {
|
||||
getConfigs: () => ({}),
|
||||
getComponent: () => null,
|
||||
specSelectors: {
|
||||
url: () => "swagger.json"
|
||||
}
|
||||
}
|
||||
const wrapper = mount(
|
||||
<OnlineValidatorBadge {...props} />
|
||||
)
|
||||
|
||||
const anchor = wrapper.find("a")
|
||||
|
||||
// Then
|
||||
expect(anchor.props().href).toEqual(
|
||||
"https://validator.swagger.io/validator/debug?url=swagger.json"
|
||||
)
|
||||
expect(anchor.props().target).toEqual("_blank")
|
||||
expect(anchor.props().rel || "").toInclude("noopener")
|
||||
expect(anchor.props().rel || "").toInclude("noreferrer")
|
||||
})
|
||||
})
|
||||
33
test/mocha/xss/info-sanitization.jsx
Normal file
33
test/mocha/xss/info-sanitization.jsx
Normal file
@@ -0,0 +1,33 @@
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import expect from "expect"
|
||||
import { render } from "enzyme"
|
||||
import { fromJS } from "immutable"
|
||||
import Info from "components/info"
|
||||
import Markdown from "components/providers/markdown"
|
||||
|
||||
describe("<Info/> Sanitization", function(){
|
||||
const dummyComponent = () => null
|
||||
const components = {
|
||||
Markdown
|
||||
}
|
||||
const props = {
|
||||
getComponent: c => components[c] || dummyComponent,
|
||||
info: fromJS({
|
||||
title: "Test Title **strong** <script>alert(1)</script>",
|
||||
description: "Description *with* <script>Markdown</script>"
|
||||
}),
|
||||
host: "example.test",
|
||||
basePath: "/api"
|
||||
}
|
||||
|
||||
it("renders sanitized .title content", function(){
|
||||
let wrapper = render(<Info {...props}/>)
|
||||
expect(wrapper.find(".title").html()).toEqual("Test Title **strong** <script>alert(1)</script>")
|
||||
})
|
||||
|
||||
it("renders sanitized .description content", function() {
|
||||
let wrapper = render(<Info {...props}/>)
|
||||
expect(wrapper.find(".description").html()).toEqual("<div class=\"markdown\"><p>Description <em>with</em> </p>\n</div>")
|
||||
})
|
||||
})
|
||||
36
test/mocha/xss/markdown-script-sanitization.jsx
Normal file
36
test/mocha/xss/markdown-script-sanitization.jsx
Normal file
@@ -0,0 +1,36 @@
|
||||
/* eslint-env mocha */
|
||||
import React from "react"
|
||||
import expect from "expect"
|
||||
import { render } from "enzyme"
|
||||
import Markdown from "components/providers/markdown"
|
||||
import { Markdown as OAS3Markdown } from "corePlugins/oas3/wrap-components/markdown.jsx"
|
||||
|
||||
describe("Markdown Script Sanitization", function() {
|
||||
describe("Swagger 2.0", function() {
|
||||
it("sanitizes <script> elements", function() {
|
||||
const str = `script <script>alert(1)</script>`
|
||||
const el = render(<Markdown source={str} />)
|
||||
expect(el.html()).toEqual(`<div class="markdown"><p>script </p>\n</div>`)
|
||||
})
|
||||
|
||||
it("sanitizes <img> elements", function() {
|
||||
const str = `<img src=x onerror="alert('img-in-description')">`
|
||||
const el = render(<Markdown source={str} />)
|
||||
expect(el.html()).toEqual(`<div class="markdown"><p><img src="x"></p>\n</div>`)
|
||||
})
|
||||
})
|
||||
|
||||
describe("OAS 3", function() {
|
||||
it("sanitizes <script> elements", function() {
|
||||
const str = `script <script>alert(1)</script>`
|
||||
const el = render(<OAS3Markdown source={str} />)
|
||||
expect(el.html()).toEqual(`<div class="renderedMarkdown"><p>script </p></div>`)
|
||||
})
|
||||
|
||||
it("sanitizes <img> elements", function() {
|
||||
const str = `<img src=x onerror="alert('img-in-description')">`
|
||||
const el = render(<OAS3Markdown source={str} />)
|
||||
expect(el.html()).toEqual(`<div class="renderedMarkdown"><p><img src="x"></p></div>`)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user