@facets-cloud/facetsctlv3
Version:
103 lines (102 loc) • 4.43 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const sinon = __importStar(require("sinon"));
const proxyquire_1 = __importDefault(require("proxyquire"));
describe('ConfigService', () => {
const config = {
ControlPlaneURL: 'https://control-plane.example.com',
Username: 'test-user',
AccessToken: 'test-token',
};
const configFilePath = path.join(os.homedir(), '.facets.login.json');
let fsStub;
let cryptoStub;
let ConfigService;
beforeEach(() => {
fsStub = {
existsSync: sinon.stub(),
readFileSync: sinon.stub(),
createWriteStream: sinon.stub(),
};
cryptoStub = {
randomBytes: sinon.stub(),
createCipheriv: sinon.stub(),
createDecipheriv: sinon.stub(),
};
ConfigService = (0, proxyquire_1.default)('../../src/services/config-service', {
'node:fs': fsStub,
'node:crypto': cryptoStub,
}).ConfigService;
});
afterEach(() => {
sinon.restore();
});
describe('findConfigFile', () => {
it('should return the home directory config path if the file exists', () => {
fsStub.existsSync.withArgs(configFilePath).returns(true);
const result = ConfigService.findConfigFile();
(0, chai_1.expect)(result).to.equal(configFilePath);
});
it('should return the current working directory config path if the file exists', () => {
const cwdConfigPath = path.join(process.cwd(), '.facets.login.json');
fsStub.existsSync.withArgs(configFilePath).returns(false);
fsStub.existsSync.withArgs(cwdConfigPath).returns(true);
const result = ConfigService.findConfigFile();
(0, chai_1.expect)(result).to.equal(cwdConfigPath);
});
it('should return null if the config file does not exist', () => {
fsStub.existsSync.returns(false);
const result = ConfigService.findConfigFile();
(0, chai_1.expect)(result).to.be.null;
});
});
describe('saveConfig', () => {
it('should write the config to the file with the correct permissions', () => {
const cipherStub = {
update: sinon.stub().returns('encrypted-'),
final: sinon.stub().returns('data')
};
const writeStreamStub = {
write: sinon.stub(),
end: sinon.stub()
};
fsStub.createWriteStream.returns(writeStreamStub);
cryptoStub.randomBytes.onCall(0).returns(Buffer.from('test-key'));
cryptoStub.randomBytes.onCall(1).returns(Buffer.from('test-iv'));
cryptoStub.createCipheriv.returns(cipherStub);
ConfigService.saveConfig(config);
(0, chai_1.expect)(writeStreamStub.write.calledOnce).to.be.true;
(0, chai_1.expect)(writeStreamStub.end.calledOnce).to.be.true;
(0, chai_1.expect)(fsStub.createWriteStream.calledOnceWith(configFilePath, { mode: 0o600 })).to.be.true;
});
});
});