Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Tidy up a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed Aug 16, 2017
1 parent bfd28a2 commit d954b6b
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 8 deletions.
23 changes: 23 additions & 0 deletions packages/api-explorer-ui/__tests__/Doc.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const React = require('react');
const { shallow } = require('enzyme');
const Doc = require('../src/Doc');

const oas = require('./fixtures/petstore/oas');

test('should output a div', () => {
const doc = shallow(
<Doc
doc={{
title: 'Title',
slug: 'slug',
type: 'endpoint',
swagger: { path: '/pet/{petId}' },
api: { method: 'get' },
}}
oas={oas}
setLanguage={() => {}}
/>,
);

expect(doc.find('.hub-reference').length).toBe(1);
});
Original file line number Diff line number Diff line change
Expand Up @@ -938,5 +938,4 @@
"x-explorer-enabled": true,
"x-samples-enabled": true,
"x-samples-languages": ["curl", "node", "ruby", "javascript", "python"]

}
61 changes: 61 additions & 0 deletions packages/api-explorer-ui/__tests__/lib/Oas.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const Oas = require('../../src/lib/Oas');

describe('hasAuth()', () => {
test('should return true if there is a top level security object', () => {
expect(new Oas({ security: [{ 'security-scheme': [] }] }).hasAuth()).toBe(true);
expect(new Oas({ security: [{ 'security-scheme': ['scope'] }] }).hasAuth()).toBe(true);
});

test('should return true if a path has security', () => {
expect(new Oas({
paths: {
'/path': {
get: {
security: [{ 'security-scheme': [] }],
},
},
},
}).hasAuth('/path', 'get')).toBe(true);
});

test('should return false if there is no top level security object', () => {
expect(new Oas({}).hasAuth()).toBe(false);
expect(new Oas({ security: [] }).hasAuth()).toBe(false);
});

test('should return false if the path has no security', () => {
expect(new Oas({
paths: {
'/path': {
get: {},
},
},
}).hasAuth('/path', 'get')).toBe(false);
});
});

describe('getPathOperation()', () => {
test('should return a stub if there is no operation', () => {
expect(new Oas({}).getPathOperation({
swagger: { path: '/path' },
api: { method: 'get' },
})).toEqual({ parameters: [] });
});

test('should return the operation if there is one', () => {
const operation = { a: 1 };
expect(new Oas({
paths: {
'/path': {
get: operation,
},
},
}).getPathOperation({ swagger: { path: '/path' }, api: { method: 'get' } })).toBe(operation);
});
});

test('should be able to access properties on oas', () => {
expect(new Oas({
info: { version: '1.0' },
}).info.version).toBe('1.0');
});
12 changes: 6 additions & 6 deletions packages/api-explorer-ui/src/Doc.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ const PathUrl = require('./PathUrl');
const Params = require('./Params');
const CodeSample = require('./CodeSample');

const getPath = require('./lib/get-path');
const getPathOperation = require('./lib/get-path-operation');
const Oas = require('./lib/Oas');
const showCode = require('./lib/show-code');

class Doc extends React.Component {
constructor(props) {
super(props);
this.state = { formData: {} };
this.onChange = this.onChange.bind(this);
this.oas = new Oas(this.props.oas);
}

onChange(formData) {
this.setState({ formData });
}

render() {
const { doc, oas, setLanguage } = this.props;
const path = getPath(oas, doc);
const pathOperation = getPathOperation(oas, doc);
const { doc, setLanguage } = this.props;
const oas = this.oas;
const pathOperation = oas.getPathOperation(doc);

return (
<div className="hub-reference" id={`page-${doc.slug}`}>
Expand Down Expand Up @@ -62,7 +62,7 @@ class Doc extends React.Component {

<div className="hub-reference-section">
<div className="hub-reference-left">
<Params swagger={oas} path={path} pathOperation={pathOperation} formData={this.state.formData} onChange={this.onChange} />
<Params swagger={oas} pathOperation={pathOperation} formData={this.state.formData} onChange={this.onChange} />
</div>
<div className="hub-reference-right switcher">
</div>
Expand Down
3 changes: 2 additions & 1 deletion packages/api-explorer-ui/src/PathUrl.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const PropTypes = require('prop-types');
const extensions = require('../../readme-oas-extensions');

function splitPath(path) {
return path.split(/({\w.+})/).filter(Boolean).map(part => {
return path.split(/({\w.+})/).filter(Boolean).map((part) => {
return { type: part.match(/[{}]/) ? 'variable' : 'text', value: part.replace(/[{}]/g, '') };
});
}
Expand Down Expand Up @@ -55,6 +55,7 @@ PathUrl.propTypes = {
oas: PropTypes.shape({
servers: PropTypes.array.isRequired,
}).isRequired,
path: PropTypes.string.isRequired,
method: PropTypes.string.isRequired,
};

Expand Down
20 changes: 20 additions & 0 deletions packages/api-explorer-ui/src/lib/Oas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const getPathOperation = require('./get-path-operation');

class Oas {
constructor(oas) {
Object.assign(this, oas);
}

hasAuth(path, method) {
const operation = getPathOperation(this, { swagger: { path }, api: { method } });

const security = operation.security || this.security;
return !!(security && security.length);
}

getPathOperation(doc) {
return getPathOperation(this, doc);
}
}

module.exports = Oas;

0 comments on commit d954b6b

Please sign in to comment.