From 11b555a2dfa68a728fc7a6a22f1706d5ff77e8ba Mon Sep 17 00:00:00 2001 From: Dom Harrington Date: Fri, 27 Oct 2017 16:17:17 -0700 Subject: [PATCH] Fix oauth2 test This will need more testing against an actual oauth project --- .../__tests__/CodeSampleResponse.test.jsx | 51 ++++++++++--------- .../src/CodeSampleResponse.jsx | 9 ++-- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/packages/api-explorer-ui/__tests__/CodeSampleResponse.test.jsx b/packages/api-explorer-ui/__tests__/CodeSampleResponse.test.jsx index 0eb90efcd..60f05cc77 100644 --- a/packages/api-explorer-ui/__tests__/CodeSampleResponse.test.jsx +++ b/packages/api-explorer-ui/__tests__/CodeSampleResponse.test.jsx @@ -117,7 +117,7 @@ describe('Results body', () => { }); test('should not display responseBody if isBinary is true', async () => { - const props2 = { + const binaryResponse = { result: await parseResponse( { log: { @@ -134,38 +134,39 @@ describe('Results body', () => { ), operation: new Operation({}, '/pet', 'post'), }; - const codeSampleResponseTabs = shallow(); + const codeSampleResponseTabs = shallow( + , + ); expect( - codeSampleResponseTabs.containsMatchingElement(
A binary file was returned
), + codeSampleResponseTabs.containsMatchingElement(
A binary file was returned
), ).toEqual(true); }); - test('should display message if OAuth is incorrect or expired ', () => { - const props3 = { - result: { - method: 'POST', - requestHeaders: 'Authorization : Bearer api-key', - responseHeaders: 'content-disposition,application/json', - statusCode: [401, 'Unauthorized', 'error'], - responseBody: { - id: 9205436248879918000, - category: { id: 0 }, - name: '1', - photoUrls: ['1'], - tags: [], + test('should display message if OAuth is incorrect or expired', async () => { + const oauthInvalidResponse = { + result: await parseResponse( + { + log: { + entries: [ + { + request: { url: 'http://petstore.swagger.io/v2/pet', method: 'POST', headers: [] }, + }, + ], + }, }, - url: 'http://petstore.swagger.io/v2/pet', - }, - operation: new Operation({}, '/pet', 'post'), + new Response('{}', { + headers: { 'content-disposition': 'attachment' }, + status: 401, + }), + ), + operation: oas.operation('/pet', 'post'), }; - const codeSampleResponseTabs = shallow(); + const codeSampleResponseTabs = shallow( + , + ); - expect( - codeSampleResponseTabs.containsMatchingElement( -
, - ), - ).toEqual(true); + expect(codeSampleResponseTabs.find('.hub-expired-token').length).toEqual(1); }); }); diff --git a/packages/api-explorer-ui/src/CodeSampleResponse.jsx b/packages/api-explorer-ui/src/CodeSampleResponse.jsx index 2b0e1e61d..a3100639f 100644 --- a/packages/api-explorer-ui/src/CodeSampleResponse.jsx +++ b/packages/api-explorer-ui/src/CodeSampleResponse.jsx @@ -40,10 +40,7 @@ class CodeSampleResponse extends React.Component { render() { const { result, oas, operation } = this.props; - let allSecurities; - try { - allSecurities = operation.prepareSecurity(); - } catch (e) {} // eslint-disable-line no-empty + let securities = operation.prepareSecurity(); return (
- {allSecurities.OAuth2 ? ( + {securities.OAuth2 ? ( () => { - if (allSecurities.OAuth2[0].flows.implicit.authorizationUrl) { + if (securities.OAuth2[0].flows.implicit.authorizationUrl) { return (

Your OAuth2 token has expired