-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathapi-keys.test.ts
37 lines (28 loc) · 1.05 KB
/
api-keys.test.ts
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
import { getApiKeys } from '../api-keys.js';
import * as fs from 'fs';
describe('getApiKeys()', () => {
afterEach(() => {
jest.resetAllMocks();
});
it('should return api key from environment variable', () => {
process.env.REDOCLY_AUTHORIZATION = 'test-api-key';
expect(getApiKeys('test-domain')).toEqual('test-api-key');
});
it('should return api key from credentials file', () => {
process.env.REDOCLY_AUTHORIZATION = '';
jest.spyOn(fs, 'existsSync').mockReturnValue(true);
jest.spyOn(fs, 'readFileSync').mockReturnValue(
JSON.stringify({
['test-domain']: 'test-api-key-from-credentials-file',
})
);
expect(getApiKeys('test-domain')).toEqual('test-api-key-from-credentials-file');
});
it('should throw an error if no api key provided', () => {
process.env.REDOCLY_AUTHORIZATION = '';
jest.spyOn(fs, 'existsSync').mockReturnValue(false);
expect(() => getApiKeys('test-domain')).toThrowError(
'No api key provided, please use environment variable REDOCLY_AUTHORIZATION.'
);
});
});