summaryrefslogtreecommitdiff
path: root/web-ui/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'web-ui/src/common')
-rw-r--r--web-ui/src/common/util.js21
-rw-r--r--web-ui/src/common/util.spec.js35
2 files changed, 55 insertions, 1 deletions
diff --git a/web-ui/src/common/util.js b/web-ui/src/common/util.js
index effb3d9c..c70a8444 100644
--- a/web-ui/src/common/util.js
+++ b/web-ui/src/common/util.js
@@ -1,8 +1,27 @@
+import browser from 'helpers/browser';
+
export const hasQueryParameter = (param) => {
const decodedUri = decodeURIComponent(window.location.search.substring(1));
return !(decodedUri.split('&').indexOf(param) < 0);
};
+export const submitForm = (event, url, body = {}) => {
+ event.preventDefault();
+
+ return fetch(url, {
+ credentials: 'same-origin',
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json'
+ },
+ body: JSON.stringify({
+ csrftoken: [browser.getCookie('XSRF-TOKEN')],
+ ...body
+ })
+ });
+};
+
export default {
- hasQueryParameter
+ hasQueryParameter,
+ submitForm
};
diff --git a/web-ui/src/common/util.spec.js b/web-ui/src/common/util.spec.js
index 805d9dd5..a79859a0 100644
--- a/web-ui/src/common/util.spec.js
+++ b/web-ui/src/common/util.spec.js
@@ -1,4 +1,7 @@
import expect from 'expect';
+import fetchMock from 'fetch-mock';
+
+import browser from 'helpers/browser';
import Util from 'src/common/util';
describe('Utils', () => {
@@ -17,4 +20,36 @@ describe('Utils', () => {
expect(Util.hasQueryParameter('error')).toBe(false);
});
});
+
+ describe('submitForm', () => {
+ const event = {};
+
+ beforeEach(() => {
+ event.preventDefault = expect.createSpy();
+ expect.spyOn(browser, 'getCookie').andReturn('abc123');
+
+ fetchMock.post('/some-url', 200);
+ Util.submitForm(event, '/some-url', { userCode: '123' });
+ });
+
+ it('sends csrftoken as content', () => {
+ expect(fetchMock.lastOptions('/some-url').body).toContain('"csrftoken":["abc123"]');
+ });
+
+ it('sends body as content', () => {
+ expect(fetchMock.lastOptions('/some-url').body).toContain('"userCode":"123"');
+ });
+
+ it('sends content-type header', () => {
+ expect(fetchMock.lastOptions('/some-url').headers['Content-Type']).toEqual('application/json');
+ });
+
+ it('sends same origin headers', () => {
+ expect(fetchMock.lastOptions('/some-url').credentials).toEqual('same-origin');
+ });
+
+ it('prevents default call to refresh page', () => {
+ expect(event.preventDefault).toHaveBeenCalled();
+ });
+ });
});