forked from SmartDropLabs/smartdrop-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartupBanner.test.js
More file actions
86 lines (67 loc) · 2.71 KB
/
Copy pathstartupBanner.test.js
File metadata and controls
86 lines (67 loc) · 2.71 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
'use strict';
/**
* Startup banner with version and config summary (issue #236).
*/
const { createCacheMock } = require('./helpers/cacheMock');
const mockHelper = createCacheMock();
jest.mock('../src/services/cache', () => mockHelper.cacheMock);
const mockLogger = {
info: jest.fn(), warn: jest.fn(), error: jest.fn(), debug: jest.fn(),
};
jest.mock('../src/logger', () => mockLogger);
const { logStartupBanner, sanitizeUrl } = require('../src/index');
const config = require('../src/config');
const { version: appVersion } = require('../package.json');
beforeEach(() => {
Object.values(mockLogger).forEach((fn) => fn.mockClear());
});
describe('sanitizeUrl', () => {
test('redacts the password from a connection URL', () => {
expect(sanitizeUrl('redis://admin:hunter2@redis.internal:6379'))
.not.toContain('hunter2');
});
test('redacts the username as well', () => {
expect(sanitizeUrl('postgres://smartdrop:secret@db:5432/smartdrop'))
.not.toContain('smartdrop:secret');
});
test('keeps host, port, and path so the target is still identifiable', () => {
const sanitized = sanitizeUrl('postgres://user:pw@db.internal:5432/smartdrop');
expect(sanitized).toContain('db.internal');
expect(sanitized).toContain('5432');
expect(sanitized).toContain('smartdrop');
});
test('passes credential-free URLs through unchanged in substance', () => {
expect(sanitizeUrl('redis://localhost:6379')).toContain('localhost:6379');
});
test('returns null when no URL is configured', () => {
expect(sanitizeUrl(undefined)).toBeNull();
expect(sanitizeUrl('')).toBeNull();
});
test('never returns an unparseable value verbatim', () => {
expect(sanitizeUrl('not a url at all')).toBe('[unparseable]');
});
});
describe('startup banner', () => {
test('logs app version, Node version, sanitized Redis URL, and watched asset count', () => {
logStartupBanner();
expect(mockLogger.info).toHaveBeenCalledTimes(1);
const [message, meta] = mockLogger.info.mock.calls[0];
expect(message).toContain(String(config.port));
expect(meta).toEqual(expect.objectContaining({
app_version: appVersion,
node_version: process.version,
node_env: config.nodeEnv,
port: config.port,
watched_assets_count: config.watchedAssets.length,
}));
expect(meta.redis_url).toContain('redis');
});
test('never logs Redis or database credentials', () => {
process.env.DATABASE_URL = 'postgres://dbuser:dbpassword@db:5432/smartdrop';
logStartupBanner();
const [, meta] = mockLogger.info.mock.calls[0];
const serialized = JSON.stringify(meta);
expect(serialized).not.toContain('dbpassword');
expect(serialized).not.toContain('dbuser');
});
});