UNPKG

@mailprotector/haraka-plugin-secrets-manager

Version:

A Haraka plugin for parsing config files with EJS to inject environment variables into config files on initialize

70 lines (57 loc) 2.11 kB
const { readFileSync, writeFileSync } = require('fs'); const { load_secrets, load_secrets_test, register } = require('../index'); describe('register', () => { test('registers plugin and loads config', () => { const registerHookMock = jest.fn(() => {}); class TestClass { constructor() { this.register_hook = registerHookMock; } }; testFunc = new TestClass(); testFunc.register = register; testFunc.register(); expect(registerHookMock.mock.calls[0][0]).toEqual('init_master'); expect(registerHookMock.mock.calls[0][1]).toEqual('load_secrets'); expect(registerHookMock.mock.calls[0][3]).toEqual(undefined); expect(registerHookMock.mock.calls[1]).toEqual(undefined); }); }); describe('load_secrets', () => { test('modifies the files with the correct values', testComplete => { process.env.TESTING = 'testah'; process.env.CONFIG_PATH = './test/config' const logerror = msg => { // console.log(msg); }; const connection = { logerror }; const fsMock = { writeFileSync: jest.fn((path, contents) => ('new-contents')), readdirSync: jest.fn((path, settings) => ([ { name: 'filename1', isFile: () => (true) }, { name: 'filename2', isFile: () => (true) } ])) }; const ejsMock = { renderFile: jest.fn((filePath, model, cb) => { cb(null, 'ejs-contents'); }) }; const next = () => { expect(fsMock.readdirSync.mock.calls[0][0]).toEqual(`./test/config`); expect(fsMock.writeFileSync.mock.calls[0][0]).toEqual(`./test/config/filename1`); expect(fsMock.writeFileSync.mock.calls[0][1]).toEqual('test1'); expect(fsMock.writeFileSync.mock.calls[1][0]).toEqual(`./test/config/filename2`); expect(fsMock.writeFileSync.mock.calls[1][1]).toEqual('test2'); testComplete(); } class TestClass { loginfo(msg) { // console.log(msg); } }; testFunc = new TestClass(); testFunc.load_secrets = load_secrets_test(fsMock, ejsMock); testFunc.load_secrets(next, connection); }); });