Completed
Push — cookie-based-auth ( 27d120...34cabe )
by Cyril
01:17
created

➔ it(ꞌexecJiraQuery returns error on 500ꞌ)   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 9.4285

1 Function

Rating   Name   Duplication   Size   Complexity  
A test-jira.js ➔ ... ➔ ??? 0 3 1
1
const test = require('unit.js');
2
const sinon = require('sinon');
3
const chai = require('chai');
4
const expect = chai.expect;
5
const chaiAsPromised = require('chai-as-promised');
6
const rewire = require('rewire');
7
const requestify = require('requestify');
8
const jira = rewire('../lib/jira');
9
const cookieAuth = require('../lib/cookieAuthentication');
10
const testThat = test.promise;
11
let sandbox = null;
12
13
before(() => {
14
  chai.use(chaiAsPromised);
15
  jira.config.retryTimeout = 10;
16
  jira.config.authentication = 'basic';
17
});
18
19
beforeEach(() => {
20
  sandbox = sinon.sandbox.create();
21
  jira.__set__('sessionCookie', null);
22
});
23
24
afterEach(() => sandbox.restore());
25
26
describe('buildUrlToGetNextPage', () => {
27
  let buildUrl = jira.__get__('buildUrlToGetNextPage');
28
  it('buildUrlToGetNextPage() appends &startAt=50 to base url', () => testThat
29
      .given(() => 'http://host/any')
30
      .when((oldUrl) => buildUrl(oldUrl, 50))
31
      .then((newUrl) => expect(newUrl).to.be.equal('http://host/any&startAt=50'))
32
  );
33
  it('buildUrlToGetNextPage() replace existing startAt with new one', () => testThat
34
      .given(() => 'http://host/any&startAt=100')
35
      .when((oldUrl) => buildUrl(oldUrl, 150))
36
      .then((newUrl) => expect(newUrl).to.equal('http://host/any&startAt=150'))
37
  );
38
});
39
40
describe('areJiraCredentialsMissing', () => {
41
  it('areJiraCredentialsMissing() returns true without JIRA_USER and JIRA_PASSWORD', () => {
42
    delete process.env.JIRA_USER;
43
    delete process.env.JIRA_PASSWORD;
44
    testThat
45
      .given()
46
      .when(() =>jira.areJiraCredentialsMissing())
47
      .then((status) => expect(status).to.be.true);
48
  });
49
  it('areJiraCredentialsMissing() returns true with only JIRA_PASSWORD missing', () => {
50
    process.env.JIRA_USER = 'user';
51
    delete process.env.JIRA_PASSWORD;
52
    testThat
53
      .given()
54
      .when(() =>jira.areJiraCredentialsMissing())
55
      .then((status) => expect(status).to.be.true);
56
  });
57
  it('areJiraCredentialsMissing() returns true with only JIRA_USER missing', () => {
58
    process.env.JIRA_PASSWORD = 'pwd';
59
    delete process.env.JIRA_USER;
60
    testThat
61
      .given()
62
      .when(() =>jira.areJiraCredentialsMissing())
63
      .then((status) => expect(status).to.be.true);
64
  });
65
  it('areJiraCredentialsMissing() returns false if JIRA_USER and JIRA_PASSWORD are set', () => {
66
    process.env.JIRA_PASSWORD = 'pwd';
67
    process.env.JIRA_USER = 'user';
68
    testThat
69
      .given()
70
      .when(() =>jira.areJiraCredentialsMissing())
71
      .then((status) => expect(status).to.be.false);
72
  });
73
});
74
75
describe('execJiraQuery', () => {
76
  it('execJiraQuery returns parsed response on 200', () => {
77
    sandbox.stub(requestify, 'get', (url, option) => {
78
      return Promise.resolve({code: 200, body: '{"message": "successful!!"}'});
79
    });
80
    return expect(jira.execJiraQuery('', false)).to.eventually.have.property('message', 'successful!!');
81
  });
82
  it('execJiraQuery returns error on 400', () => {
83
    sandbox.stub(requestify, 'get', (url, option) => {
84
      return Promise.reject({code: 400, body: 'Failed!'});
85
    });
86
    return expect(jira.execJiraQuery('', false)).to.eventually.be.rejected;
87
  });
88
  it('execJiraQuery returns error on 500', function() {
89
    // this.timeout(8000); // need to increase Mocha timeout to give enough time to the retries
90
    sandbox.stub(requestify, 'get', (url, option) => {
91
      return Promise.reject({code: 500, body: 'Failed!'});
92
    });
93
    return expect(jira.execJiraQuery('', false)).to.eventually.be.rejected;
94
  });
95
  it('execJiraQuery returns error on 502', function() {
96
    // this.timeout(8000); // need to increase Mocha timeout to give enough time to the retries
97
    sandbox.stub(requestify, 'get', (url, option) => {
98
      return Promise.reject({code: 502, body: 'Failed!'});
99
    });
100
    return expect(jira.execJiraQuery('', false)).to.eventually.be.rejected;
101
  });
102
  it('execJiraQuery fails with a not JSON response', () => {
103
    sandbox.stub(requestify, 'get', (url, option) => {
104
      return Promise.resolve({code: 200, body: 'Not JSON'});
105
    });
106
    return expect(jira.execJiraQuery('', false)).to.eventually.be.rejected
107
      .then((error) => {
108
        expect(error).to.have.property('code', -1);
109
        expect(error).to.have.property('message', 'Unexpected token N in JSON at position 0');
110
      });
111
  });
112
  it('execJiraQuery aggregates results  if retrieveAllPages = true and if response specifies multiple pages', () => {
113
    let first = '{"resultset": "first"}';
114
    let second = '{"resultset": "second"}';
115
    let last = '{"resultset": "last"}';
116
    sandbox.stub(requestify, 'get', (url, option) => {
117
      if (url === '') {
118
        return Promise.resolve({code: 200, body: '{"startAt": 0,"total": 12,"maxResults": 5,"issues": [' + first + ']}'});
119
      } else if (url === '&startAt=5') {
120
        return Promise.resolve({code: 200, body: '{"startAt": 5,"total": 12,"maxResults": 5,"issues": [' + second + ']}'});
121
      } else {
122
        return Promise.resolve({code: 200, body: '{"startAt": 10,"total": 12,"maxResults": 5,"issues": [' + last + ']}'});
123
      }
124
    });
125
    return expect(jira.execJiraQuery('', true)).to.eventually.be.fulfilled
126
      .then((response) => {
127
        expect(response).to.have.property('startAt', 0);
128
        expect(response).to.have.property('total', 12);
129
        expect(response).to.have.property('maxResults', 12);
130
        expect(response).to.have.property('issues');
131
        expect(response.issues).to.be.an('array');
132
        expect(response.issues).to.have.length(3);
133
        expect(response.issues[0]).to.have.property('resultset', 'first');
134
        expect(response.issues[1]).to.have.property('resultset', 'second');
135
        expect(response.issues[2]).to.have.property('resultset', 'last');
136
      });
137
  });
138
  it('execJiraQuery doesn\'t aggregate results if retrieveAllPages = false', () => {
139
    let first = '{"resultset": "first"}';
140
    let second = '{"resultset": "second"}';
141
    let last = '{"resultset": "last"}';
142
    sandbox.stub(requestify, 'get', (url, option) => {
143
      if (url === '') {
144
        return Promise.resolve({code: 200, body: '{"startAt": 0,"total": 12,"maxResults": 5,"issues": [' + first + ']}'});
145
      } else if (url === '&startAt=5') {
146
        return Promise.resolve({code: 200, body: '{"startAt": 5,"total": 12,"maxResults": 5,"issues": [' + second + ']}'});
147
      } else {
148
        return Promise.resolve({code: 200, body: '{"startAt": 10,"total": 12,"maxResults": 5,"issues": [' + last + ']}'});
149
      }
150
    });
151
    return expect(jira.execJiraQuery('', false)).to.eventually.be.fulfilled
152
      .then((response) => {
153
        expect(response).to.have.property('startAt', 0);
154
        expect(response).to.have.property('total', 12);
155
        expect(response).to.have.property('maxResults', 5);
156
        expect(response).to.have.property('issues');
157
        expect(response.issues).to.be.an('array');
158
        expect(response.issues).to.have.length(1);
159
        expect(response.issues[0]).to.have.property('resultset', 'first');
160
      });
161
  });
162
  it('execJiraQuery is rejected if failure occus during aggregation', () => {
163
    let first = '{"resultset": "first"}';
164
    sandbox.stub(requestify, 'get', (url, option) => {
165
      if (url === '') {
166
        return Promise.resolve({code: 200, body: '{"startAt": 0,"total": 8,"maxResults": 5,"issues": [' + first + ']}'});
167
      } else if (url === '&startAt=5') {
168
        return Promise.reject({code: 400, body: 'Failed!'});
169
      }
170
    });
171
    return expect(jira.execJiraQuery('', true)).to.eventually.be.rejected;
172
  });
173
174
  describe('execJiraQuery with CBA', () => {
175
    it('execJiraQuery returns parsed response on 200 with CBA', () => {
176
      jira.config.authentication = 'cookie';
177
      sandbox.stub(cookieAuth, 'login', (user, password, url) => {
0 ignored issues
show
Unused Code introduced by
The parameter password is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter user is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
178
        let header = {'set-cookie': ['mycookie=RUTN87766HG']};
179
        let body = '{"session": {"name": "mycookie"}}';
180
        return Promise.resolve({code: 200, headers: header, body: body});
181
      });
182
      sandbox.stub(requestify, 'get', (url, option) => {
0 ignored issues
show
Unused Code introduced by
The parameter option is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
183
        return Promise.resolve({code: 200, body: '{"message": "successful!!"}'});
184
      });
185
      return expect(jira.execJiraQuery('', true)).to.eventually.be.fulfilled;
186
    });
187
    it('execJiraQuery rejects if can\'t get cookie', () => {
188
      jira.config.authentication = 'cookie';
189
      sandbox.stub(cookieAuth, 'login', (user, password, url) => {
0 ignored issues
show
Unused Code introduced by
The parameter user is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter password is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
190
        return Promise.reject(new Error('can\'t get cookie'));
191
      });
192
      return expect(jira.execJiraQuery('', false)).to.eventually.be.rejected
193
        .then((error) => expect(error.message).to.equal('can\'t get cookie'));
194
    });
195
    it('execJiraQuery returns parsed response on 200 with CBA with expired cookie in the middle', () => {
196
      jira.config.authentication = 'cookie';
197
      let first = '{"resultset": "first"}';
198
      let second = '{"resultset": "second"}';
199
      let last = '{"resultset": "last"}';
200
      let expired = true;
201
      sandbox.stub(requestify, 'get', (url, option) => {
0 ignored issues
show
Unused Code introduced by
The parameter option is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
202
        if (url === '') {
203
          return Promise.resolve({code: 200, body: '{"startAt": 0,"total": 12,"maxResults": 5,"issues": [' + first + ']}'});
204
        } else if (url === '&startAt=5') {
205
          if ( expired === true ) {
206
            expired = false;
207
            return Promise.reject({code: 401, body: '{"message": "cookie expired!!"}'});
208
          } else {
209
            return Promise.resolve({code: 200, body: '{"startAt": 5,"total": 12,"maxResults": 5,"issues": [' + second + ']}'});
210
          }
211
        } else {
212
          return Promise.resolve({code: 200, body: '{"startAt": 10,"total": 12,"maxResults": 5,"issues": [' + last + ']}'});
213
        }
214
      });
215
      sandbox.stub(cookieAuth, 'login', (user, password, url) => {
0 ignored issues
show
Unused Code introduced by
The parameter user is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter password is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
216
        let header = {'set-cookie': ['mycookie=RUTN87766HG']};
217
        let body = '{"session": {"name": "mycookie"}}';
218
        return Promise.resolve({code: 200, headers: header, body: body});
219
      });
220
      return expect(jira.execJiraQuery('', true)).to.eventually.be.fulfilled
221
        .then((response) => {
222
          expect(response).to.have.property('startAt', 0);
223
          expect(response).to.have.property('total', 12);
224
          expect(response).to.have.property('maxResults', 12);
225
          expect(response).to.have.property('issues');
226
          expect(response.issues).to.be.an('array');
227
          expect(response.issues).to.have.length(3);
228
        });
229
    });
230
  });
231
});
232