Repository navigation
Expand file tree
/
Copy pathUtilFunc.test.js
More file actions
161 lines (154 loc) · 6.52 KB
/
Copy pathUtilFunc.test.js
File metadata and controls
161 lines (154 loc) · 6.52 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const helpers = require('../utils/helpers');
const path = require('path');
const invalidFilePath = path.resolve(__dirname, '../../testKeyFiles/missingfile.key');
const filePathInvKey = path.resolve(__dirname, '../../testKeyFiles/jwtRS256.key.pub');
const filePathValidKey = path.resolve(__dirname, '../../testKeyFiles/jwtRS256.pub');
const sampleBinaryKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApXd6RqV95G7+alU1PmA49n9IG8mCT27vpCpTJz3MGH+pqBEp6gLYDkP6lxK4ix5dy9NrOcKnaIWJ3xAc/JU+rVt6CiEyqJo4rchrNnRQsn4+P+efuVlsL959MqjzQC98qcVdf44C3wrxsOHE823zRACsJylOFkf7KkXd9c8L8vIj9x29q5K7NkGRKtOLKY7k4QPhlCVFDkMgAidHvi8HD7HDI6KYljguuhHUtRdrmC4i0NuwpSdqsavUJ9ASQu9Cr0QhpzOFJeZQ91ZkLoSDAkpSXAfBS+lvGtEnWLh7q3JczJOb3Tz8YolUTGfBlJ9iXiHDcY8PXdRTrvUVqeTe3wIDAQAB';
const samplePKCS8Key = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApXd6RqV95G7+alU1PmA4
9n9IG8mCT27vpCpTJz3MGH+pqBEp6gLYDkP6lxK4ix5dy9NrOcKnaIWJ3xAc/JU+
rVt6CiEyqJo4rchrNnRQsn4+P+efuVlsL959MqjzQC98qcVdf44C3wrxsOHE823z
RACsJylOFkf7KkXd9c8L8vIj9x29q5K7NkGRKtOLKY7k4QPhlCVFDkMgAidHvi8H
D7HDI6KYljguuhHUtRdrmC4i0NuwpSdqsavUJ9ASQu9Cr0QhpzOFJeZQ91ZkLoSD
AkpSXAfBS+lvGtEnWLh7q3JczJOb3Tz8YolUTGfBlJ9iXiHDcY8PXdRTrvUVqeTe
3wIDAQAB
-----END PUBLIC KEY-----`;
// This one contains newlines but no key headers
const sampleWrongKey = `MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApXd6RqV95G7+alU1PmA4
9n9IG8mCT27vpCpTJz3MGH+pqBEp6gLYDkP6lxK4ix5dy9NrOcKnaIWJ3xAc/JU+
rVt6CiEyqJo4rchrNnRQsn4+P+efuVlsL959MqjzQC98qcVdf44C3wrxsOHE823z
RACsJylOFkf7KkXd9c8L8vIj9x29q5K7NkGRKtOLKY7k4QPhlCVFDkMgAidHvi8H
D7HDI6KYljguuhHUtRdrmC4i0NuwpSdqsavUJ9ASQu9Cr0QhpzOFJeZQ91ZkLoSD
AkpSXAfBS+lvGtEnWLh7q3JczJOb3Tz8YolUTGfBlJ9iXiHDcY8PXdRTrvUVqeTe
3wIDAQAB`;
describe('Testing Utilitiy functions', () => {
describe('Testing readKeyFile', () => {
describe('testing sync calls', () => {
test('test empty filePath string', () => {
expect( () => {
helpers.readKeyFile('');
}).toThrow('Path not specified or empty');
});
test('test null filePath string', () => {
expect( () => {
helpers.readKeyFile(null);
}).toThrow('Path not specified or empty');
});
test('test invalid filepath', () => {
expect( () => {
helpers.readKeyFile(invalidFilePath);
}).toThrow('Invalid file path');
});
test('test reading invalid public key file', () => {
expect( () => {
helpers.readKeyFile(filePathInvKey);
}).toThrow('Invalid key file');
});
test('test reading valid public key file', () => {
expect( () => {
const key = helpers.readKeyFile(filePathValidKey);
// @TODO find regexp for PKCS8 Key format
// expect(key).toMatch(/-----BEGIN PUBLIC KEY-----*-----END PUBLIC KEY-----/)
// console.log('GOT KEY:', key);
expect(key).toBeDefined();
}).not.toThrow();
});
});
describe('testing cb pattern', () => {
test('test emptry filePath string', (done) => {
helpers.readKeyFile('', (err, res) => {
expect(err).toMatch(/Path not specified or empty/);
done();
});
});
test('test null filePath string', (done) => {
helpers.readKeyFile(null, (err, res) => {
expect(err).toMatch(/Path not specified or empty/);
done();
});
});
test('test invalid filepath', (done) => {
helpers.readKeyFile(invalidFilePath, (err, res) => {
expect(err).toMatch(/Invalid file path/);
done();
});
});
test('test reading invalid public key file', (done) => {
helpers.readKeyFile(filePathInvKey, (err, res) => {
expect(err).toMatch(/Invalid key file/);
done();
});
});
test('test reading valid public key file', (done) => {
helpers.readKeyFile(filePathValidKey, (err, key) => {
// console.log('GOT KEY:', key);
expect(err).toBeFalsy();
expect(key).toBeDefined();
done();
});
});
});
});
describe('testing transformKeyToFormat', () => {
test('test transforming null secret / key', () => {
expect( () => {
helpers.transformKeyToFormat();
}).toThrow('No Secret Specified');
});
test('test transforming a non string secret / key', () => {
expect( () => {
helpers.transformKeyToFormat({key: 'asdasdasd'});
}).toThrow('Key can only be a string value');
});
test('test transforming empty secret / key', () => {
expect( () => {
helpers.transformKeyToFormat('');
}).toThrow('Secret cannot be empty string');
});
test('test transforming from Binary to PKCS8 format', () => {
expect( () => {
const formattedSecret = helpers.transformKeyToFormat(sampleBinaryKey);
expect(formattedSecret.indexOf('-----BEGIN PUBLIC KEY-----')).not.toEqual(-1);
expect(formattedSecret.indexOf('-----END PUBLIC KEY-----')).not.toEqual(-1);
}).not.toThrow();
});
test('test tranforming key in PKCS8 format', () => {
expect( () => {
const formattedSecret = helpers.transformKeyToFormat(samplePKCS8Key);
expect(formattedSecret).toEqual(samplePKCS8Key);
}).not.toThrow();
});
test('test transforming a key in wrong format', () => {
expect( () => {
helpers.transformKeyToFormat(sampleWrongKey);
}).toThrow('Secret in Unsupported Format');
});
});
});
describe('Testing readKeyFile error edge cases', () => {
afterEach(() => {
jest.restoreAllMocks();
});
test('sync: should rethrow unexpected errors (not ENOENT or encoding too long)', () => {
const fs = require('node:fs');
jest.spyOn(fs, 'readFileSync').mockImplementationOnce(() => Buffer.from(''));
expect(() => helpers.readKeyFile('/mocked/path')).toThrow('Empty key given');
});
test('async: should pass non-ENOENT fs.readFile errors to callback', (done) => {
const fs = require('node:fs');
const permError = new Error('EPERM: operation not permitted, open \'/some/path\'');
jest.spyOn(fs, 'readFile').mockImplementationOnce((path, cb) => cb(permError, null));
helpers.readKeyFile('/mocked/path', (err) => {
expect(err).toBe(permError);
done();
});
});
test('async: should pass unexpected NodeRSA errors to callback', (done) => {
const fs = require('node:fs');
jest.spyOn(fs, 'readFile').mockImplementationOnce((path, cb) => cb(null, Buffer.from('')));
helpers.readKeyFile('/mocked/path', (err) => {
expect(err.message).toBe('Empty key given');
done();
});
});
});