debian-mirror-gitlab/spec/frontend/lib/utils/csrf_token_spec.js

58 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
import csrf from '~/lib/utils/csrf';
2020-05-24 23:13:21 +05:30
import { setHTMLFixture } from 'helpers/fixtures';
describe('csrf', () => {
let testContext;
beforeEach(() => {
testContext = {};
});
2018-03-17 18:26:18 +05:30
beforeEach(() => {
2020-05-24 23:13:21 +05:30
testContext.tokenKey = 'X-CSRF-Token';
testContext.token =
2018-12-13 13:39:08 +05:30
'pH1cvjnP9grx2oKlhWEDvUZnJ8x2eXsIs1qzyHkF3DugSG5yTxR76CWeEZRhML2D1IeVB7NEW0t5l/axE4iJpQ==';
2018-03-17 18:26:18 +05:30
});
it('returns the correct headerKey', () => {
2020-05-24 23:13:21 +05:30
expect(csrf.headerKey).toBe(testContext.tokenKey);
2018-03-17 18:26:18 +05:30
});
describe('when csrf token is in the DOM', () => {
beforeEach(() => {
2020-05-24 23:13:21 +05:30
setHTMLFixture(`
<meta name="csrf-token" content="${testContext.token}">
2018-03-17 18:26:18 +05:30
`);
csrf.init();
});
it('returns the csrf token', () => {
2020-05-24 23:13:21 +05:30
expect(csrf.token).toBe(testContext.token);
2018-03-17 18:26:18 +05:30
});
it('returns the csrf headers object', () => {
2020-05-24 23:13:21 +05:30
expect(csrf.headers[testContext.tokenKey]).toBe(testContext.token);
2018-03-17 18:26:18 +05:30
});
});
describe('when csrf token is not in the DOM', () => {
beforeEach(() => {
2020-05-24 23:13:21 +05:30
setHTMLFixture(`
2018-03-17 18:26:18 +05:30
<meta name="some-other-token">
`);
csrf.init();
});
it('returns null for token', () => {
expect(csrf.token).toBeNull();
});
it('returns empty object for headers', () => {
expect(typeof csrf.headers).toBe('object');
expect(Object.keys(csrf.headers).length).toBe(0);
});
});
});