forked from guacsec/trustify-da-javascript-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexhort-backend-utils.test.js
More file actions
81 lines (69 loc) · 2.95 KB
/
Copy pathexhort-backend-utils.test.js
File metadata and controls
81 lines (69 loc) · 2.95 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { expect } from 'chai'
import * as chai from 'chai'
import * as sinon from 'sinon'
import sinonChai from 'sinon-chai'
import { getTokenHeaders } from '../src/analysis.js';
import { selectTrustifyDABackend } from '../src/index.js'
chai.use(sinonChai)
suite('testing Select Trustify DA Backend function', () => {
const testUrl = 'https://trustify-da.example.com';
const testUrl2 = 'https://dev.trustify-da.example.com';
test('When TRUSTIFY_DA_BACKEND_URL is set in environment variable, should return that value', () => {
process.env['TRUSTIFY_DA_BACKEND_URL'] = testUrl;
let selectedUrl = selectTrustifyDABackend({});
expect(selectedUrl).to.be.equals(testUrl);
delete process.env['TRUSTIFY_DA_BACKEND_URL'];
});
test('When TRUSTIFY_DA_BACKEND_URL is set in opts (but not in env), should return that value', () => {
delete process.env['TRUSTIFY_DA_BACKEND_URL'];
let testOpts = {
'TRUSTIFY_DA_BACKEND_URL': testUrl
};
let selectedUrl = selectTrustifyDABackend(testOpts);
expect(selectedUrl).to.be.equals(testUrl);
});
test('When TRUSTIFY_DA_BACKEND_URL is set in both environment and opts, environment variable should take precedence', () => {
process.env['TRUSTIFY_DA_BACKEND_URL'] = testUrl;
let testOpts = {
'TRUSTIFY_DA_BACKEND_URL': testUrl2
};
let selectedUrl = selectTrustifyDABackend(testOpts);
expect(selectedUrl).to.be.equals(testUrl);
delete process.env['TRUSTIFY_DA_BACKEND_URL'];
});
test('When TRUSTIFY_DA_BACKEND_URL is not set, should throw error', () => {
delete process.env['TRUSTIFY_DA_BACKEND_URL'];
expect(() => selectTrustifyDABackend({})).to.throw('TRUSTIFY_DA_BACKEND_URL is unset');
});
test('When TRUSTIFY_DA_BACKEND_URL is empty string in environment, should throw error', () => {
process.env['TRUSTIFY_DA_BACKEND_URL'] = '';
expect(() => selectTrustifyDABackend({})).to.throw('TRUSTIFY_DA_BACKEND_URL is unset');
delete process.env['TRUSTIFY_DA_BACKEND_URL'];
});
test('When TRUSTIFY_DA_BACKEND_URL is empty string in opts, should throw error', () => {
delete process.env['TRUSTIFY_DA_BACKEND_URL'];
let testOpts = {
'TRUSTIFY_DA_BACKEND_URL': ''
};
expect(() => selectTrustifyDABackend(testOpts)).to.throw('TRUSTIFY_DA_BACKEND_URL is unset');
});
test('When TRUSTIFY_DA_BACKEND_URL is null in opts, should throw error', () => {
delete process.env['TRUSTIFY_DA_BACKEND_URL'];
let testOpts = {
'TRUSTIFY_DA_BACKEND_URL': null
};
expect(() => selectTrustifyDABackend(testOpts)).to.throw('TRUSTIFY_DA_BACKEND_URL is unset');
});
}).afterAll(() => {
delete process.env['TRUSTIFY_DA_BACKEND_URL'];
});
suite('verify token header logging', () => {
test('don\'t log the token header', () => {
getTokenHeaders({
'TRUSTIFY_DA_TOKEN': 'banana',
'TRUSTIFY_DA_DEBUG': 'true'
})
// Should only be called once with "Headers Values to be sent to Trustify DA backend:"
expect(console.log).to.be.calledOnce
})
}).beforeAll(() => sinon.spy(console, 'log')).afterAll(() => console.log.restore())