|
| 1 | +/* eslint-env mocha */ |
| 2 | +import expect from "expect" |
| 3 | +import { fromJS } from "immutable" |
| 4 | +import { definitionsToAuthorize, definitionsForRequirements } from "corePlugins/auth/selectors" |
| 5 | + |
| 6 | +describe("auth plugin - selectors", () => { |
| 7 | + describe("definitionsToAuthorize", () => { |
| 8 | + it("should return securityDefinitions as a List", () => { |
| 9 | + const securityDefinitions = { |
| 10 | + "petstore_auth": { |
| 11 | + "type": "oauth2", |
| 12 | + "authorizationUrl": "http://petstore.swagger.io/oauth/dialog", |
| 13 | + "flow": "implicit", |
| 14 | + "scopes": { |
| 15 | + "write:pets": "modify pets in your account", |
| 16 | + "read:pets": "read your pets" |
| 17 | + } |
| 18 | + }, |
| 19 | + "api_key": { |
| 20 | + "type": "apiKey", |
| 21 | + "name": "api_key", |
| 22 | + "in": "header" |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + const system = { |
| 27 | + specSelectors: { |
| 28 | + securityDefinitions() { |
| 29 | + return fromJS(securityDefinitions) |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + const res = definitionsToAuthorize({})(system) |
| 35 | + |
| 36 | + expect(res.toJS()).toEqual([ |
| 37 | + { |
| 38 | + "petstore_auth": securityDefinitions["petstore_auth"] |
| 39 | + }, |
| 40 | + { |
| 41 | + "api_key": securityDefinitions["api_key"] |
| 42 | + }, |
| 43 | + ]) |
| 44 | + }) |
| 45 | + |
| 46 | + it("should fail gracefully with bad data", () => { |
| 47 | + const securityDefinitions = null |
| 48 | + |
| 49 | + const system = { |
| 50 | + specSelectors: { |
| 51 | + securityDefinitions() { |
| 52 | + return fromJS(securityDefinitions) |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + const res = definitionsToAuthorize({})(system) |
| 58 | + |
| 59 | + expect(res.toJS()).toEqual([]) |
| 60 | + }) |
| 61 | + }) |
| 62 | + |
| 63 | + describe("definitionsForRequirements", () => { |
| 64 | + it("should return applicable securityDefinitions as a List", () => { |
| 65 | + const securityDefinitions = { |
| 66 | + "petstore_auth": { |
| 67 | + "type": "oauth2", |
| 68 | + "authorizationUrl": "http://petstore.swagger.io/oauth/dialog", |
| 69 | + "flow": "implicit", |
| 70 | + "scopes": { |
| 71 | + "write:pets": "modify pets in your account", |
| 72 | + "read:pets": "read your pets" |
| 73 | + } |
| 74 | + }, |
| 75 | + "api_key": { |
| 76 | + "type": "apiKey", |
| 77 | + "name": "api_key", |
| 78 | + "in": "header" |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + const system = { |
| 83 | + authSelectors: { |
| 84 | + definitionsToAuthorize() { |
| 85 | + return fromJS([ |
| 86 | + { |
| 87 | + "petstore_auth": securityDefinitions["petstore_auth"] |
| 88 | + }, |
| 89 | + { |
| 90 | + "api_key": securityDefinitions["api_key"] |
| 91 | + }, |
| 92 | + ]) |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + const securities = fromJS([ |
| 98 | + { |
| 99 | + "petstore_auth": [ |
| 100 | + "write:pets", |
| 101 | + "read:pets" |
| 102 | + ] |
| 103 | + } |
| 104 | + ]) |
| 105 | + |
| 106 | + const res = definitionsForRequirements({}, securities)(system) |
| 107 | + |
| 108 | + expect(res.toJS()).toEqual([ |
| 109 | + { |
| 110 | + "petstore_auth": securityDefinitions["petstore_auth"] |
| 111 | + } |
| 112 | + ]) |
| 113 | + }) |
| 114 | + |
| 115 | + it("should fail gracefully with bad data", () => { |
| 116 | + const securityDefinitions = null |
| 117 | + |
| 118 | + const system = { |
| 119 | + authSelectors: { |
| 120 | + definitionsToAuthorize() { |
| 121 | + return null |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + const securities = null |
| 127 | + |
| 128 | + const res = definitionsForRequirements({}, securities)(system) |
| 129 | + |
| 130 | + expect(res.toJS()).toEqual([]) |
| 131 | + }) |
| 132 | + }) |
| 133 | +}) |
0 commit comments