From 3536f2d51824b6e0887dc677aff7a91ea16903ae Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Mon, 6 Nov 2017 22:08:42 -0800 Subject: [PATCH] Add tests for modified selectors --- test/core/plugins/auth/selectors.js | 133 ++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 test/core/plugins/auth/selectors.js diff --git a/test/core/plugins/auth/selectors.js b/test/core/plugins/auth/selectors.js new file mode 100644 index 00000000..99ad9c50 --- /dev/null +++ b/test/core/plugins/auth/selectors.js @@ -0,0 +1,133 @@ +/* eslint-env mocha */ +import expect from "expect" +import { fromJS } from "immutable" +import { definitionsToAuthorize, definitionsForRequirements } from "corePlugins/auth/selectors" + +describe.only("auth plugin - selectors", () => { + describe("definitionsToAuthorize", () => { + it("should return securityDefinitions as a List", () => { + const securityDefinitions = { + "petstore_auth": { + "type": "oauth2", + "authorizationUrl": "http://petstore.swagger.io/oauth/dialog", + "flow": "implicit", + "scopes": { + "write:pets": "modify pets in your account", + "read:pets": "read your pets" + } + }, + "api_key": { + "type": "apiKey", + "name": "api_key", + "in": "header" + } + } + + const system = { + specSelectors: { + securityDefinitions() { + return fromJS(securityDefinitions) + } + } + } + + const res = definitionsToAuthorize({})(system) + + expect(res.toJS()).toEqual([ + { + "petstore_auth": securityDefinitions["petstore_auth"] + }, + { + "api_key": securityDefinitions["api_key"] + }, + ]) + }) + + it("should fail gracefully with bad data", () => { + const securityDefinitions = null + + const system = { + specSelectors: { + securityDefinitions() { + return fromJS(securityDefinitions) + } + } + } + + const res = definitionsToAuthorize({})(system) + + expect(res.toJS()).toEqual([]) + }) + }) + + describe("definitionsForRequirements", () => { + it("should return applicable securityDefinitions as a List", () => { + const securityDefinitions = { + "petstore_auth": { + "type": "oauth2", + "authorizationUrl": "http://petstore.swagger.io/oauth/dialog", + "flow": "implicit", + "scopes": { + "write:pets": "modify pets in your account", + "read:pets": "read your pets" + } + }, + "api_key": { + "type": "apiKey", + "name": "api_key", + "in": "header" + } + } + + const system = { + authSelectors: { + definitionsToAuthorize() { + return fromJS([ + { + "petstore_auth": securityDefinitions["petstore_auth"] + }, + { + "api_key": securityDefinitions["api_key"] + }, + ]) + } + } + } + + const securities = fromJS([ + { + "petstore_auth": [ + "write:pets", + "read:pets" + ] + } + ]) + + const res = definitionsForRequirements({}, securities)(system) + + expect(res.toJS()).toEqual([ + { + "petstore_auth": securityDefinitions["petstore_auth"] + } + ]) + }) + + it("should fail gracefully with bad data", () => { + const securityDefinitions = null + + const system = { + authSelectors: { + definitionsToAuthorize() { + return null + } + } + } + + const securities = null + + const res = definitionsForRequirements({}, securities)(system) + + expect(res.toJS()).toEqual([]) + }) + }) +})