summaryrefslogtreecommitdiff
path: root/spec/javascripts/lib/utils/csrf_token_spec.js
blob: c484213df8e61d89cc91ebca245d2f48e7cef20a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import csrf from '~/lib/utils/csrf';

describe('csrf', () => {
  beforeEach(() => {
    this.tokenKey = 'X-CSRF-Token';
    this.token = 'pH1cvjnP9grx2oKlhWEDvUZnJ8x2eXsIs1qzyHkF3DugSG5yTxR76CWeEZRhML2D1IeVB7NEW0t5l/axE4iJpQ==';
  });

  it('returns the correct headerKey', () => {
    expect(csrf.headerKey).toBe(this.tokenKey);
  });

  describe('when csrf token is in the DOM', () => {
    beforeEach(() => {
      setFixtures(`
        <meta name="csrf-token" content="${this.token}">
      `);

      csrf.init();
    });

    it('returns the csrf token', () => {
      expect(csrf.token).toBe(this.token);
    });

    it('returns the csrf headers object', () => {
      expect(csrf.headers[this.tokenKey]).toBe(this.token);
    });
  });

  describe('when csrf token is not in the DOM', () => {
    beforeEach(() => {
      setFixtures(`
        <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);
    });
  });
});