Completed
Push — master ( 77afd5...a883f6 )
by Cyril
01:05
created

test/test-cookieAuthentication.js   D

Complexity

Total Complexity 62
Complexity/F 1

Size

Lines of Code 114
Function Count 62

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 0
wmc 62
c 3
b 0
f 1
nc 1
mnd 0
bc 12
fnc 62
dl 0
loc 114
rs 4.8235
bpm 0.1935
cpm 1
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A test-cookieAuthentication.js ➔ ??? 0 1 1

How to fix   Complexity   

Complexity

Complex classes like test/test-cookieAuthentication.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
const test = require('unit.js');
2
const testThat = test.promise;
3
const sinon = require('sinon');
4
const chai = require('chai');
5
const chaiAsPromised = require('chai-as-promised');
6
const expect = chai.expect;
7
const rewire = require('rewire');
8
const cookie = rewire('../lib/cookieAuthentication.js');
9
const requestify = require('requestify');
10
const getBaseUrl = cookie.__get__('getBaseUrl');
11
const extract = cookie.__get__('extractSessionCookie');
12
let sandbox = null;
13
14
before(() => chai.use(chaiAsPromised));
15
16
beforeEach(() => sandbox = sinon.sandbox.create());
17
18
afterEach(() => sandbox.restore());
19
20
21
describe('getBaseUrl', () => {
22
  it('getBaseUrl() returns base url if valid url', () => testThat
23
    .given(() => 'http://www.dummyserver:23456/rest/api/2/issue/ID-5')
24
    .when((url) => getBaseUrl(url))
25
    .then((baseUrl) => expect(baseUrl).to.equal('http://www.dummyserver:23456/rest/'))
26
  );
27
  it('getBaseUrl() throw error if missing url', () => testThat
28
    .given()
29
    .when(() => () => getBaseUrl())
30
    .then((func) => expect(func).to.throw(Error))
31
  );
32
  it('getBaseUrl() throw error if invalid url', () => testThat
33
      .given(() => 'http://www.dummyserver:23456/bla/bla/bla')
34
      .when((url) => () => getBaseUrl(url))
35
      .then((func) => expect(func).to.throw(Error))
36
  );
37
});
38
39
describe('extractSessionCookie', () => {
40
  it('extractSessionCookie() throw error if cookie name not specified', () => testThat
41
    .given(() => 'fake="KHG8768"')
42
    .when((cookie) => () => extract(cookie))
43
    .then((func) => expect(func).to.throw(Error))
44
  );
45
  it('extractSessionCookie() throw error if cookies not specified', () => testThat
46
    .given()
47
    .when(() => () => extract())
48
    .then((func) => expect(func).to.throw(Error))
49
  );
50
  it('extractSessionCookie() throw error if cookies is not an array', () => testThat
51
    .given(() => ({cookies: 'fake="KHG987J"', name: 'fake'}))
52
    .then((data) => () => extract(data.cookies, data.name))
53
    .then((func) => expect(func).to.throw(Error))
54
  );
55
  it('extractSessionCookie() returns sessionCookie when only one cookie', () => testThat
56
    .given(() => ({cookies: ['cookie1="LKJHLKJ8768H"'], name: 'cookie1'}))
57
    .when((data) => extract(data.cookies, data.name))
58
    .then((cookie) => expect(cookie).to.equal('cookie1="LKJHLKJ8768H"'))
59
  );
60
  it('extractSessionCookie() throw error if cookie name not found in cookies', () => testThat
61
    .given(() => ({cookies: ['cookie1="LKJHLKJ8768H"'], name: 'cookie2'}))
62
    .when((data) => () => extract(data.cookies, data.name))
63
    .then((func) => expect(func).to.throw(Error))
64
  );
65
  it('extractSessionCookie() returns sessionCookie when multiple cookies', () => testThat
66
    .given(() => ({cookies: ['cookie1="LKJHLKJ8768H"', 'cookie2="KJHG76KHJB"', 'cookie3="JRS8MLKJKJF"', 'cookie4="JH8976HGFCJ"'], name: 'cookie3'}))
67
    .when((data) => extract(data.cookies, data.name))
68
    .then((cookie) => expect(cookie).to.equal('cookie3="JRS8MLKJKJF"'))
69
  );
70
  it('extractSessionCookie() returns sessionCookie when multiple cookies and empty cookie with cookie name ignored', () => testThat
71
    .given(() => ({cookies: ['cookie1="LKJHLKJ8768H"', 'cookie2="KJHG76KHJB"', 'cookie3=""', 'cookie3="JH8976HGFCJ"'], name: 'cookie3'}))
72
    .when((data) => extract(data.cookies, data.name))
73
    .then((cookie) => expect(cookie).to.equal('cookie3="JH8976HGFCJ"'))
74
  );
75
});
76
77
describe('getHeader', () => {
78
  it('getHeaders() builds header including cookie', () => testThat
79
    .given(() => 'cookiename=cookievalue')
80
    .when((sessionCookie) => cookie.getHeader(sessionCookie))
81
    .then((header) => {
82
      expect(header).to.have.property('cookie', 'cookiename=cookievalue');
83
      expect(header).to.have.property('Cache-Control', 'public, max-age=60');
84
    })
85
  );
86
});
87
88
describe('login', () => {
89
  it('login() rejects with if url not set', () => {
90
    return expect(cookie.login('user', 'password')).to.eventually.be.rejected
91
      .then((error) => {
92
        expect(error).to.be.an.instanceof(Error);
93
      });
94
  });
95
  it('login() rejects if no cookie is in header', () => {
96
    sandbox.stub(requestify, 'post', () => {
97
      let headers = {'set-cookie': ''};
98
      let response = '{"session": {"name": "studio.crowd.tokenkey"}}';
99
      return Promise.resolve({code: 200, headers: headers, body: response});
100
    });
101
    expect(cookie.login('dummy-user', 'dummy-password', 'http://www.dummyurl/rest/api/2/issue/ID-78')).to.eventually.be.rejected;
102
  });
103
  it('login() returns session Cookie', () => {
104
    sandbox.stub(requestify, 'post', () => {
105
      let headers = {'set-cookie': ['atlassian.xsrf.token=BGJJ-I70H-EYI8-6QPB|2ae8e3125acff97369f184a4530b59f9d983c12d|lout; Path=/; Secure', 'JSESSIONID=913F47DAFCA6D7FF09A65537D5BD3C5C; Path=/; Secure; HttpOnly', 'studio.crowd.tokenkey=""; Domain=.ulyssjira2.atlassian.net; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; Secure; HttpOnly', 'studio.crowd.tokenkey=gW34EFQfK8Kbwpp6HkHmng00; Domain=.ulyssjira2.atlassian.net; Path=/; Secure; HttpOnly']};
106
      let response = '{"session": {"name": "studio.crowd.tokenkey"}, "loginInfo": {"failedLoginCount": 1, "loginCount": 230, "lastFailedLoginTime": "2017-01-17T10:20:43.467+0100", "previousLoginTime": "2017-01-17T17:11:46.798+0100"}}';
107
      return Promise.resolve({code: 200, headers: headers, body: response});
108
    });
109
    return expect(cookie.login('dummy-user', 'dummy-password', 'http://www.dummyurl/rest/api/2/issue/ID-78')).to.eventually.be.fulfilled
110
      .then((cookie) => {
111
        expect(cookie).to.equal('studio.crowd.tokenkey=gW34EFQfK8Kbwpp6HkHmng00');
112
      });
113
  });
114
});
115