Skip to content

Commit 1aabdde

Browse files
authored
Merge branch 'master' into bug/3658-add-enzyme-test-for-objectmodel
2 parents bcad5b5 + 076f32b commit 1aabdde

File tree

6 files changed

+168
-22
lines changed

6 files changed

+168
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ node_modules
22
.idea
33
.deps_check
44
.DS_Store
5+
.nyc_output
56
npm-debug.log*
67
.eslintcache
78
package-lock.json

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"test-in-node": "npm run lint-errors && npm run just-test-in-node",
3434
"just-test": "karma start --config karma.conf.js",
3535
"just-test-in-node": "mocha --recursive --compilers js:babel-core/register test/core test/components test/bugs test/swagger-ui-dist-package test/xss",
36+
"just-check-coverage": "nyc npm run just-test-in-node",
3637
"test-e2e": "sleep 3 && nightwatch test/e2e/scenarios/ --config test/e2e/nightwatch.json",
3738
"e2e-initial-render": "nightwatch test/e2e/scenarios/ --config test/e2e/nightwatch.json --group initial-render",
3839
"mock-api": "json-server --watch test/e2e/db.json --port 3204",
@@ -127,6 +128,7 @@
127128
"node-sass": "^4.5.0",
128129
"npm-run-all": "4.0.2",
129130
"null-loader": "0.1.1",
131+
"nyc": "^11.3.0",
130132
"open": "0.0.5",
131133
"postcss-loader": "2.0.6",
132134
"raw-loader": "0.5.1",
@@ -152,5 +154,11 @@
152154
],
153155
"optionalDependencies": {
154156
"webpack-dev-server": "2.5.0"
157+
},
158+
"nyc": {
159+
"all": true,
160+
"include": [
161+
"**/src/core/plugins/**.js"
162+
]
155163
}
156164
}
Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
import React from "react"
22
import PropTypes from "prop-types"
3-
import ImPropTypes from "react-immutable-proptypes"
43

54
export default class AuthorizeOperationBtn extends React.Component {
5+
static propTypes = {
6+
isAuthorized: PropTypes.bool.isRequired,
7+
onClick: PropTypes.func
8+
}
9+
610
onClick =(e) => {
711
e.stopPropagation()
12+
let { onClick } = this.props
813

9-
let { security, authActions, authSelectors } = this.props
10-
let definitions = authSelectors.getDefinitionsByNames(security)
11-
12-
authActions.showDefinitions(definitions)
14+
if(onClick) {
15+
onClick()
16+
}
1317
}
1418

1519
render() {
16-
let { security, authSelectors } = this.props
17-
18-
let isAuthorized = authSelectors.isAuthorized(security)
19-
20-
if(isAuthorized === null) {
21-
return null
22-
}
20+
let { isAuthorized } = this.props
2321

2422
return (
2523
<button className={isAuthorized ? "authorization__btn locked" : "authorization__btn unlocked"} onClick={ this.onClick }>
@@ -30,10 +28,4 @@ export default class AuthorizeOperationBtn extends React.Component {
3028

3129
)
3230
}
33-
34-
static propTypes = {
35-
authSelectors: PropTypes.object.isRequired,
36-
authActions: PropTypes.object.isRequired,
37-
security: ImPropTypes.iterable.isRequired
38-
}
3931
}

src/core/components/operation.jsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,13 @@ export default class Operation extends PureComponent {
183183

184184
{
185185
(!security || !security.count()) ? null :
186-
<AuthorizeOperationBtn authActions={ authActions }
187-
security={ security }
188-
authSelectors={ authSelectors }/>
186+
<AuthorizeOperationBtn
187+
isAuthorized={ authSelectors.isAuthorized(security) }
188+
onClick={() => {
189+
const applicableDefinitions = authSelectors.definitionsForRequirements(security)
190+
authActions.showDefinitions(applicableDefinitions)
191+
}}
192+
/>
189193
}
190194
</div>
191195

src/core/plugins/auth/selectors.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const shownDefinitions = createSelector(
1111
export const definitionsToAuthorize = createSelector(
1212
state,
1313
() => ( { specSelectors } ) => {
14-
let definitions = specSelectors.securityDefinitions()
14+
let definitions = specSelectors.securityDefinitions() || Map({})
1515
let list = List()
1616

1717
//todo refactor
@@ -28,6 +28,7 @@ export const definitionsToAuthorize = createSelector(
2828

2929

3030
export const getDefinitionsByNames = ( state, securities ) => ( { specSelectors } ) => {
31+
console.warn("WARNING: getDefinitionsByNames is deprecated and will be removed in the next major version.")
3132
let securityDefinitions = specSelectors.securityDefinitions()
3233
let result = List()
3334

@@ -58,6 +59,13 @@ export const getDefinitionsByNames = ( state, securities ) => ( { specSelectors
5859
return result
5960
}
6061

62+
export const definitionsForRequirements = (state, securities = List()) => ({ authSelectors }) => {
63+
const allDefinitions = authSelectors.definitionsToAuthorize() || List()
64+
return allDefinitions.filter((def) => {
65+
return securities.some(sec => sec.get(def.keySeq().first()))
66+
})
67+
}
68+
6169
export const authorized = createSelector(
6270
state,
6371
auth => auth.get("authorized") || Map()
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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

Comments
 (0)