From 3efdf1223e32dbb34466c497ce846c885bd9476a Mon Sep 17 00:00:00 2001 From: kyle Date: Fri, 6 Apr 2018 20:00:10 -0700 Subject: [PATCH] fix: add additionalQueryStringParams to auth requests (#4419) * tests: add failing unit tests * fix: add additionalQueryStringParams to auth requests --- src/core/plugins/auth/actions.js | 16 +++++-- test/core/plugins/auth/actions.js | 72 +++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 4 deletions(-) diff --git a/src/core/plugins/auth/actions.js b/src/core/plugins/auth/actions.js index f578f73d..7a6b7d0b 100644 --- a/src/core/plugins/auth/actions.js +++ b/src/core/plugins/auth/actions.js @@ -140,17 +140,25 @@ export const authorizeAccessCodeWithBasicAuthentication = ( { auth, redirectUrl return authActions.authorizeRequest({body: buildFormData(form), name, url: schema.get("tokenUrl"), auth, headers}) } -export const authorizeRequest = ( data ) => ( { fn, getConfigs, authActions, errActions, oas3Selectors, specSelectors } ) => { +export const authorizeRequest = ( data ) => ( { fn, getConfigs, authActions, errActions, oas3Selectors, specSelectors, authSelectors } ) => { let { body, query={}, headers={}, name, url, auth } = data - let fetchUrl + let { additionalQueryStringParams } = authSelectors.getConfigs() || {} + + let parsedUrl if (specSelectors.isOAS3()) { - fetchUrl = parseUrl(url, oas3Selectors.selectedServer()).toString() + parsedUrl = parseUrl(url, oas3Selectors.selectedServer(), true) } else { - fetchUrl = parseUrl(url, specSelectors.url()).toString() + parsedUrl = parseUrl(url, specSelectors.url(), true) } + if(typeof additionalQueryStringParams === "object") { + parsedUrl.query = Object.assign({}, parsedUrl.query, additionalQueryStringParams) + } + + const fetchUrl = parsedUrl.toString() + let _headers = Object.assign({ "Accept":"application/json, text/plain, */*", "Content-Type": "application/x-www-form-urlencoded" diff --git a/test/core/plugins/auth/actions.js b/test/core/plugins/auth/actions.js index bce850ba..f1ad6b68 100644 --- a/test/core/plugins/auth/actions.js +++ b/test/core/plugins/auth/actions.js @@ -71,5 +71,77 @@ describe("auth plugin - actions", () => { 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") + }) }) })