-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving CSRFToken and fetchData to utils
- Loading branch information
Damian Peralta
committed
May 22, 2019
1 parent
526b0b0
commit 8815ea4
Showing
4 changed files
with
84 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |