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

Commit 11b555a

Browse files
author
Dom Harrington
committed
Fix oauth2 test
This will need more testing against an actual oauth project
1 parent 2a4c308 commit 11b555a

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

packages/api-explorer-ui/__tests__/CodeSampleResponse.test.jsx

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ describe('Results body', () => {
117117
});
118118

119119
test('should not display responseBody if isBinary is true', async () => {
120-
const props2 = {
120+
const binaryResponse = {
121121
result: await parseResponse(
122122
{
123123
log: {
@@ -134,38 +134,39 @@ describe('Results body', () => {
134134
),
135135
operation: new Operation({}, '/pet', 'post'),
136136
};
137-
const codeSampleResponseTabs = shallow(<CodeSampleResponseTabs {...props2} oas={oas} />);
137+
const codeSampleResponseTabs = shallow(
138+
<CodeSampleResponseTabs {...binaryResponse} oas={oas} />,
139+
);
138140

139141
expect(
140-
codeSampleResponseTabs.containsMatchingElement(<div> A binary file was returned</div>),
142+
codeSampleResponseTabs.containsMatchingElement(<div>A binary file was returned</div>),
141143
).toEqual(true);
142144
});
143145

144-
test('should display message if OAuth is incorrect or expired ', () => {
145-
const props3 = {
146-
result: {
147-
method: 'POST',
148-
requestHeaders: 'Authorization : Bearer api-key',
149-
responseHeaders: 'content-disposition,application/json',
150-
statusCode: [401, 'Unauthorized', 'error'],
151-
responseBody: {
152-
id: 9205436248879918000,
153-
category: { id: 0 },
154-
name: '1',
155-
photoUrls: ['1'],
156-
tags: [],
146+
test('should display message if OAuth is incorrect or expired', async () => {
147+
const oauthInvalidResponse = {
148+
result: await parseResponse(
149+
{
150+
log: {
151+
entries: [
152+
{
153+
request: { url: 'http://petstore.swagger.io/v2/pet', method: 'POST', headers: [] },
154+
},
155+
],
156+
},
157157
},
158-
url: 'http://petstore.swagger.io/v2/pet',
159-
},
160-
operation: new Operation({}, '/pet', 'post'),
158+
new Response('{}', {
159+
headers: { 'content-disposition': 'attachment' },
160+
status: 401,
161+
}),
162+
),
163+
operation: oas.operation('/pet', 'post'),
161164
};
162-
const codeSampleResponseTabs = shallow(<CodeSampleResponseTabs {...props3} oas={oas} />);
165+
const codeSampleResponseTabs = shallow(
166+
<CodeSampleResponseTabs {...oauthInvalidResponse} oas={oas} />,
167+
);
163168

164-
expect(
165-
codeSampleResponseTabs.containsMatchingElement(
166-
<div className="text-center hub-expired-token" />,
167-
),
168-
).toEqual(true);
169+
expect(codeSampleResponseTabs.find('.hub-expired-token').length).toEqual(1);
169170
});
170171
});
171172

packages/api-explorer-ui/src/CodeSampleResponse.jsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ class CodeSampleResponse extends React.Component {
4040

4141
render() {
4242
const { result, oas, operation } = this.props;
43-
let allSecurities;
44-
try {
45-
allSecurities = operation.prepareSecurity();
46-
} catch (e) {} // eslint-disable-line no-empty
43+
let securities = operation.prepareSecurity();
4744

4845
return (
4946
<div
@@ -131,9 +128,9 @@ class CodeSampleResponse extends React.Component {
131128

132129
{result.statusCode[0] === 401 && (
133130
<div className="text-center hub-expired-token">
134-
{allSecurities.OAuth2 ? (
131+
{securities.OAuth2 ? (
135132
() => {
136-
if (allSecurities.OAuth2[0].flows.implicit.authorizationUrl) {
133+
if (securities.OAuth2[0].flows.implicit.authorizationUrl) {
137134
return (
138135
<div>
139136
<p>Your OAuth2 token has expired</p>

0 commit comments

Comments
 (0)