Skip to content

Commit

Permalink
Add more tests to pass 90% test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
uppal101 committed Sep 21, 2017
1 parent 728b601 commit d00eae9
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 6 deletions.
8 changes: 8 additions & 0 deletions packages/api-explorer-ui/__tests__/AuthBox.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ test('should display authentication box if try it now button is selected without

expect(authBox.state('open')).toBe(true);
});

test('should not display authentication warning if authData is passed', () => {
const authBox = shallow(<AuthBox {...props} operation={oas.operation('/single-auth', 'post')} />);

authBox.setProps({ needsAuth: false });

expect(authBox.state('open')).toBe(false);
});
21 changes: 20 additions & 1 deletion packages/api-explorer-ui/__tests__/Doc.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,31 @@ describe('state.dirty', () => {
});

describe('onSubmit', () => {
test('should switch to true if auth is required and correct security isn not passed', () => {
test('should switch to true if auth is required and correct security is not passed', () => {
const doc = shallow(<Doc {...props} />);
doc.instance().onSubmit();

expect(doc.state('showAuthBox')).toBe(true);
});

xtest('should return true if auth is not required', () => {
const props2 = {
doc: {
title: 'Title',
slug: 'slug',
type: 'endpoint',
swagger: { path: '/store/order' },
api: { method: 'post' },
},
oas,
setLanguage: () => {},
};
const doc = shallow(<Doc {...props2} />);

doc.instance().onSubmit();

expect(doc).toBe(true);
});
});

describe('state.loading', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/api-explorer-ui/__tests__/Params.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const props = {
operation,
formData: {},
onChange: () => {},
onSubmit: () => {},
};

describe('form id attribute', () => {
Expand Down
39 changes: 39 additions & 0 deletions packages/api-explorer-ui/__tests__/block-types/Code.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,34 @@ const block = {
},
};

const block3 = {
type: 'code',
sidebar: undefined,
data: {
codes: [
{
code: 'whjdwhjwejhkwhjk',
language: 'text',
status: 400,
},
],
},
};

const badBlock = {
type: 'code',
sidebar: undefined,
data: {
codes: {
code: {
code: 'whjdwhjwejhkwhjk',
language: 'text',
status: 400,
},
},
},
};

const block2 = {
type: 'code',
sidebar: undefined,
Expand All @@ -45,6 +73,17 @@ describe('Code', () => {
expect(codeInput.find('em').text()).toBe('test');
});

test('Code will render status code within em tag', () => {
const codeInput = mount(<Code block={block3} />);
expect(codeInput.find('em').text()).toBe('Bad Request');
});

test('If codes array is not passed as an array expect empty array', () => {
const codeInput = mount(<Code block={badBlock} />);

expect(codeInput.find('span').text()).toBe('');
});

test('Code will render language if name or status is not provided within a tag if codes has a status', () => {
const codeInput = mount(<Code block={block2} />);
expect(codeInput.find('a').text()).toBe('JavaScript');
Expand Down
88 changes: 88 additions & 0 deletions packages/api-explorer-ui/__tests__/fixtures/auth-types/oas.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"openapi": "3.0.0-rc2",
"info": {
"version": "1.0.0",
"title": "Multiple Securities"
},
"paths": {
"/things": {
"post": {
"security": [
{
"oauth": ["write:things"]
},
{
"apiKey": []
}
]
}
},
"/api-key": {
"post": {
"security": [
{
"apiKey": []
}
]
}
},
"/basic": {
"post": {
"security": [
{
"basic": []
}
]
}
},
"/no-auth": {
"post": {}
},
"/multiple-oauths": {
"post": {
"security": [
{
"oauth": ["write:things", "read:things"]
},
{
"oauthDiff": ["write:things", "read:things"]
}
]
}
}
},
"components": {
"securitySchemes": {
"oauth": {
"type": "oauth2",
"flows": {
"implicit": {
"authorizationUrl": "http://example.com/oauth/dialog",
"scopes": {
"write:things": "Add things to your account"
}
}
}
},
"oauthDiff": {
"type": "oauth2",
"flows": {
"implicit": {
"authorizationUrl": "http://example.com/oauth/dialog",
"scopes": {
"write:things": "Add things to your account"
}
}
}
},
"apiKey": {
"type": "apiKey",
"name": "apiKey",
"in": "header"
},
"basic": {
"type": "basic"
}
}
}
}
3 changes: 3 additions & 0 deletions packages/api-explorer-ui/__tests__/fixtures/petstore/oas.json
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,9 @@
"type": "apiKey",
"name": "api_key",
"in": "header"
},
"basic": {
"type": "basic"
}
},
"headers": {}
Expand Down
38 changes: 38 additions & 0 deletions packages/api-explorer-ui/__tests__/lib/authRequired.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const AuthRequired = require('../../src/lib/authRequired');
const Oas = require('../../src/lib/Oas.js');
const petstore = require('../fixtures/petstore/oas');
const auth = require('../fixtures/auth-types/oas');

const oas = new Oas(petstore);
const oas2 = new Oas(auth);

describe('AuthRequired', () => {
xit('should return false if auth data is not passed in for api key condition', () => {
const operation = oas.operation('/pet/{petId}', 'patch');

expect(AuthRequired(operation)).toBe(null);
});
it('should return false if auth data is not passed in for api key condition', () => {
const operation = oas.operation('/pet/{petId}', 'get');

expect(AuthRequired(operation, { api_key: '' })).toBe(false);
});

it('should return false if auth data is not passed in for oaut condition', () => {
const operation = oas.operation('/pet/{petId}', 'delete');

expect(AuthRequired(operation, { petstore_auth: '' })).toBe(false);
});

it('should return false if auth data is not passed in for basic condition', () => {
const operation = oas2.operation('/basic', 'post');

expect(AuthRequired(operation, { basic: { username: '', password: '' } })).toBe(false);
});

it('should return true if endpoint does not need auth or passed in auth is correct', () => {
const operation = oas.operation('/store/order/{orderId}', 'get');

expect(AuthRequired(operation)).toBe(true);
});
});
3 changes: 2 additions & 1 deletion packages/api-explorer-ui/src/Doc.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const React = require('react');
const PropTypes = require('prop-types');
const authRequired = require('./lib/endpoint');
const authRequired = require('./lib/authRequired');
const extensions = require('../../readme-oas-extensions');

const PathUrl = require('./PathUrl');
Expand All @@ -24,6 +24,7 @@ class Doc extends React.Component {
this.onChange = this.onChange.bind(this);
this.oas = new Oas(this.props.oas);
this.onSubmit = this.onSubmit.bind(this);
// console.log('auth', this.state.formData.auth);
}

onChange(formData) {
Expand Down
5 changes: 4 additions & 1 deletion packages/api-explorer-ui/src/Params.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ Params.propTypes = {
operation: PropTypes.instanceOf(Operation).isRequired,
formData: PropTypes.shape({}).isRequired,
onChange: PropTypes.func.isRequired,
onSubmit: PropTypes.func.isRequired,
onSubmit: PropTypes.func,
};

Params.defaultProps = {
onSubmit: true,
};
module.exports = Params;
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
function authRequired(operation, authData) {
let ready = true;

const authInputData = authData === undefined ? {} : authData;
const securitySettings = operation.getSecurity();
if (!securitySettings) return ready;
// console.log(securitySettings);
securitySettings.forEach(sec => {
const key = Object.keys(sec)[0];

if (!operation.oas.components.securitySchemes[key]) return;
const security = operation.oas.components.securitySchemes[key];
const auth = authInputData[key];

if (security.type === 'basic') {
if (!auth || !auth.user) {
if (!auth || !auth.usern) {
ready = false;
}
}
Expand Down

0 comments on commit d00eae9

Please sign in to comment.