diff --git a/index.js b/index.js old mode 100644 new mode 100755 index 51d572850..36bcfa570 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +#!/usr/bin/env node const proxy = require('./src/proxy'); const service = require('./src/service'); diff --git a/package.json b/package.json index a2813e762..304a105a1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@finos/git-proxy", - "version": "1.0.0", + "version": "1.0.1", "description": "Deploy custom push protections and policies on top of Git.", "scripts": { "client": "vite --config vite.config.js", @@ -15,6 +15,7 @@ "prepare": "node ./scripts/prepare.js", "lint": "eslint --fix . --ext .js,.jsx" }, + "bin": "./index.js", "author": "Paul Groves", "license": "Apache-2.0", "dependencies": { diff --git a/src/config/index.js b/src/config/index.js index b39bbd0e4..1435bd16a 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -1,24 +1,22 @@ const fs = require('fs'); -const proxySettings = JSON.parse(fs.readFileSync('./resources/config.json')); +const path = require('path'); -let _userSettings = null; -let _authorisedList = proxySettings.authorisedList; -let _database = proxySettings.sink; -let _authentication = proxySettings.authentication; -let _tempPassword = proxySettings.tempPassword; +const defaultSettings = require('../../resources/config.json'); +const userSettingsPath = path.join(process.cwd(), './user-settings.json'); -const userSettings = () => { - const path = './user-settings.json'; - if (_userSettings === null && fs.existsSync(path)) { - _userSettings = JSON.parse(fs.readFileSync(path)); - } - return _userSettings; -}; +let _userSettings = null; +if (fs.existsSync(userSettingsPath)) { + _userSettings = JSON.parse(fs.readFileSync(userSettingsPath)); +} +let _authorisedList = defaultSettings.authorisedList; +let _database = defaultSettings.sink; +let _authentication = defaultSettings.authentication; +let _tempPassword = defaultSettings.tempPassword; // Gets a list of authorised repositories const getAuthorisedList = () => { - if (userSettings !== null && userSettings.authorisedList) { - _authorisedList = userSettings.authorisedList; + if (_userSettings !== null && _userSettings.authorisedList) { + _authorisedList = _userSettings.authorisedList; } return _authorisedList; @@ -26,8 +24,8 @@ const getAuthorisedList = () => { // Gets a list of authorised repositories const getTempPasswordConfig = () => { - if (userSettings !== null && userSettings.tempPassword) { - _tempPassword = userSettings.tempPassword; + if (_userSettings !== null && _userSettings.tempPassword) { + _tempPassword = _userSettings.tempPassword; } return _tempPassword; @@ -35,8 +33,8 @@ const getTempPasswordConfig = () => { // Gets the configuared data sink, defaults to filesystem const getDatabase = () => { - if (userSettings !== null && userSettings.sink) { - _database = userSettings.database; + if (_userSettings !== null && _userSettings.sink) { + _database = _userSettings.sink; } for (const ix in _database) { if (ix) { @@ -52,8 +50,8 @@ const getDatabase = () => { // Gets the configuared data sink, defaults to filesystem const getAuthentication = () => { - if (userSettings !== null && userSettings.sink) { - _authentication = userSettings.authentication; + if (_userSettings !== null && _userSettings.authentication) { + _authentication = _userSettings.authentication; } for (const ix in _authentication) { if (!ix) continue; diff --git a/test/testConfig.js b/test/testConfig.js new file mode 100644 index 000000000..1a8091214 --- /dev/null +++ b/test/testConfig.js @@ -0,0 +1,118 @@ +/* eslint-disable max-len */ +const chai = require('chai'); +const fs = require('fs'); +const path = require('path'); +const defaultSettings = require('../resources/config.json'); + +chai.should(); +const expect = chai.expect; + +describe('default configuration', function () { + it('should use default values if no user-settings.json file exists', function () { + const config = require('../src/config'); + + expect(config.getAuthentication()).to.be.eql( + defaultSettings.authentication[0], + ); + expect(config.getDatabase()).to.be.eql(defaultSettings.sink[0]); + expect(config.getTempPasswordConfig()).to.be.eql( + defaultSettings.tempPassword, + ); + expect(config.getAuthorisedList()).to.be.eql( + defaultSettings.authorisedList, + ); + }); + after(function () { + delete require.cache[require.resolve('../src/config')]; + }); +}); + +describe('user configuration', function () { + let tempDir; + let tempUserFile; + let curDirUserFile; + + beforeEach(function () { + tempDir = fs.mkdtempSync('gitproxy-test'); + tempUserFile = path.join(tempDir, 'user-settings.json'); + curDirUserFile = path.join(process.cwd(), 'user-settings.json'); + fs.symlinkSync(tempUserFile, curDirUserFile); + }); + + it('should override default settings for authorisedList', function () { + const user = { + authorisedList: [ + { + project: 'foo', + name: 'bar', + url: 'https://github.com/foo/bar.git', + }, + ], + }; + fs.writeFileSync(tempUserFile, JSON.stringify(user)); + + const config = require('../src/config'); + + expect(config.getAuthorisedList()).to.be.eql(user.authorisedList); + expect(config.getAuthentication()).to.be.eql( + defaultSettings.authentication[0], + ); + expect(config.getDatabase()).to.be.eql(defaultSettings.sink[0]); + expect(config.getTempPasswordConfig()).to.be.eql( + defaultSettings.tempPassword, + ); + }); + + it('should override default settings for authentication', function () { + const user = { + authentication: [ + { + type: 'google', + enabled: true, + }, + ], + }; + fs.writeFileSync(tempUserFile, JSON.stringify(user)); + + const config = require('../src/config'); + + expect(config.getAuthentication()).to.be.eql(user.authentication[0]); + expect(config.getAuthentication()).to.not.be.eql( + defaultSettings.authentication[0], + ); + expect(config.getDatabase()).to.be.eql(defaultSettings.sink[0]); + expect(config.getTempPasswordConfig()).to.be.eql( + defaultSettings.tempPassword, + ); + }); + + it('should override default settings for database', function () { + const user = { + sink: [ + { + type: 'postgres', + enabled: true, + }, + ], + }; + fs.writeFileSync(tempUserFile, JSON.stringify(user)); + + const config = require('../src/config'); + + expect(config.getDatabase()).to.be.eql(user.sink[0]); + expect(config.getDatabase()).to.not.be.eql(defaultSettings.sink[0]); + expect(config.getAuthentication()).to.be.eql( + defaultSettings.authentication[0], + ); + expect(config.getTempPasswordConfig()).to.be.eql( + defaultSettings.tempPassword, + ); + }); + + afterEach(function () { + fs.unlinkSync(curDirUserFile); + fs.rmSync(tempUserFile); + fs.rmdirSync(tempDir); + delete require.cache[require.resolve('../src/config')]; + }); +});