Skip to content

Commit

Permalink
Moving CSRFToken and fetchData to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Peralta committed May 22, 2019
1 parent 526b0b0 commit 8815ea4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 36 deletions.
14 changes: 1 addition & 13 deletions app/javascript/src/Policies/components/CustomPolicy.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { Policy, Schema } from 'Policies/types/Policies'
import type {InputEvent} from 'Policies/types'
import 'codemirror/mode/javascript/javascript'
import 'Policies/styles/policies.scss'
import {CSRFToken} from 'utilities/utils'

type OnChange = (InputEvent) => void

Expand All @@ -32,18 +33,6 @@ const POLICY_TEMPLATE: Policy = {
id: 0
}

function CSRFToken ({win = window}: {win?: Window}) {
const getMetaContent = meta => win.document.head.querySelector(`meta[name~=${meta}][content]`).content
const props = {
name: getMetaContent('csrf-param'),
value: getMetaContent('csrf-token'),
type: 'hidden'
}
return (
<input {...props} />
)
}

function CustomPolicyForm ({policy, onChange, win = window}: {policy: Policy, onChange: OnChange, win?: Window}) {
const isNewPolicy = !policy.id
const [method, setMethod] = useState(isNewPolicy ? 'post' : 'put')
Expand Down Expand Up @@ -145,7 +134,6 @@ function CustomPolicy ({policy = POLICY_TEMPLATE}: {policy: Policy}) {
export {
CustomPolicy,
CustomPolicyForm,
CSRFToken,
CustomPolicyEditor,
POLICY_TEMPLATE
}
43 changes: 43 additions & 0 deletions app/javascript/src/utilities/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// @flow
import {fetch as fetchPolyfill} from 'whatwg-fetch'
import 'core-js/es6/promise'
import React from 'react'

function CSRFToken ({win = window}: {win?: Window}) {
const getMetaContent = meta => win.document.head.querySelector(`meta[name~=${meta}][content]`).content

try {
return (
<input
name={getMetaContent('csrf-param')}
value={getMetaContent('csrf-token')}
type='hidden'
/>
)
} catch (error) {
console.error(error)
return (
<input
name={null}
value={null}
type='hidden'
/>
)
}
}

const fetchData = (url: string) => {
return fetchPolyfill(url)
.then((response) => {
return response.json()
})
.then(data => data)
.catch(error => {
console.error(error)
})
}

export {
CSRFToken,
fetchData
}
24 changes: 1 addition & 23 deletions spec/javascripts/Policies/components/CustomPolicy.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import React from 'react'
import Enzyme, { mount, shallow } from 'enzyme'
import Enzyme, { shallow } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import {JSDOM} from 'jsdom'

import {
CustomPolicy,
CustomPolicyEditor,
CustomPolicyForm,
CSRFToken,
POLICY_TEMPLATE
} from 'Policies/components/CustomPolicy'

Expand Down Expand Up @@ -92,23 +90,3 @@ describe('CustomPolicyForm', () => {
expect(wrapper.find('input[name="_method"]').prop('value')).toBe('delete')
})
})

describe('CSRFToken', () => {
function setup () {
const jsdom = new JSDOM(
`<!doctype html><html>
<head><meta name="csrf-param" content="authenticity_token"><meta name="csrf-token" content="=42="></head>
<body></body>
</html>`
)
const { window } = jsdom
return mount(<CSRFToken win={window} />)
}

it('should render itself correctly', () => {
const wrapper = setup()
expect(wrapper.find(CSRFToken).exists()).toBe(true)
expect(wrapper.find('input').prop('name')).toBe('authenticity_token')
expect(wrapper.find('input').prop('value')).toBe('=42=')
})
})
39 changes: 39 additions & 0 deletions spec/javascripts/utilities/utils.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from 'react'
import Enzyme, { mount } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import {CSRFToken} from 'utilities/utils'
import {JSDOM} from 'jsdom'
Enzyme.configure({ adapter: new Adapter() })

describe('CSRFToken', () => {
function setup () {
const jsdom = new JSDOM(
`<!doctype html><html>
<head><meta name="csrf-param" content="authenticity_token"><meta name="csrf-token" content="=42="></head>
<body></body>
</html>`
)
const { window } = jsdom
return mount(<CSRFToken win={window} />)
}

it('should render itself correctly', () => {
const wrapper = setup()
expect(wrapper.find(CSRFToken).exists()).toBe(true)
expect(wrapper.find('input').prop('name')).toBe('authenticity_token')
expect(wrapper.find('input').prop('value')).toBe('=42=')
})

it('should return null values when csrf-param meta tag is not present', () => {
const { window } = new JSDOM(
`<!doctype html><html>
<head></head>
<body></body>
</html>`
)
const wrapper = mount(<CSRFToken win={window} />)
expect(wrapper.find(CSRFToken).exists()).toBe(true)
expect(wrapper.find('input').prop('name')).toBe(null)
expect(wrapper.find('input').prop('value')).toBe(null)
})
})

0 comments on commit 8815ea4

Please sign in to comment.