Skip to content

Commit 5e61f2b

Browse files
committed
chore: added unit tests
1 parent 9406f84 commit 5e61f2b

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright 2021 Adobe. All rights reserved.
3+
This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License. You may obtain a copy
5+
of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
Unless required by applicable law or agreed to in writing, software distributed under
8+
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
OF ANY KIND, either express or implied. See the License for the specific language
10+
governing permissions and limitations under the License.
11+
*/
12+
13+
const fs = require('fs');
14+
const os = require('os');
15+
const path = require('path');
16+
const { readSecretsFile } = require('../../../serverUtils');
17+
const { loadMeshSecrets } = require('../../../secrets');
18+
19+
describe('readSecretsFile', () => {
20+
let tmp;
21+
let prevCwd;
22+
23+
afterEach(() => {
24+
if (prevCwd !== undefined) {
25+
process.chdir(prevCwd);
26+
prevCwd = undefined;
27+
}
28+
if (tmp) {
29+
fs.rmSync(tmp, { recursive: true, force: true });
30+
tmp = undefined;
31+
}
32+
});
33+
34+
test('parses JSON object strings in secrets.yaml like the tenant worker', () => {
35+
tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'readSecretsFile-test-'));
36+
const meshDir = path.join(tmp, '.mesh');
37+
fs.mkdirSync(meshDir, { recursive: true });
38+
fs.writeFileSync(
39+
path.join(meshDir, 'secrets.yaml'),
40+
`TOKEN: '{"COMMERCE": "dummy-value"}'\nPLAIN: not-json\n`,
41+
'utf8',
42+
);
43+
prevCwd = process.cwd();
44+
process.chdir(tmp);
45+
46+
const secrets = readSecretsFile('.mesh');
47+
48+
expect(secrets.TOKEN).toEqual({ COMMERCE: 'dummy-value' });
49+
expect(secrets.PLAIN).toBe('not-json');
50+
51+
const mockLogger = { error: jest.fn() };
52+
const asWorkerSees = loadMeshSecrets(mockLogger, JSON.stringify(secrets));
53+
expect(asWorkerSees.TOKEN.COMMERCE).toBe('dummy-value');
54+
expect(asWorkerSees.PLAIN).toBe('not-json');
55+
expect(mockLogger.error).not.toHaveBeenCalled();
56+
});
57+
});

0 commit comments

Comments
 (0)