|
| 1 | +const chai = require('chai'); |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | +const { expect } = chai; |
| 5 | +const ConfigLoader = require('../src/config/ConfigLoader'); |
| 6 | +const sinon = require('sinon'); |
| 7 | +const axios = require('axios'); |
| 8 | + |
| 9 | +describe('ConfigLoader', () => { |
| 10 | + let configLoader; |
| 11 | + let tempDir; |
| 12 | + let tempConfigFile; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + // Create temp directory for test files |
| 16 | + tempDir = fs.mkdtempSync('gitproxy-configloader-test-'); |
| 17 | + tempConfigFile = path.join(tempDir, 'test-config.json'); |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + // Clean up temp files |
| 22 | + if (fs.existsSync(tempDir)) { |
| 23 | + fs.rmSync(tempDir, { recursive: true }); |
| 24 | + } |
| 25 | + sinon.restore(); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('loadFromFile', () => { |
| 29 | + it('should load configuration from file', async () => { |
| 30 | + const testConfig = { |
| 31 | + proxyUrl: 'https://test.com', |
| 32 | + cookieSecret: 'test-secret', |
| 33 | + }; |
| 34 | + fs.writeFileSync(tempConfigFile, JSON.stringify(testConfig)); |
| 35 | + |
| 36 | + configLoader = new ConfigLoader({}); |
| 37 | + const result = await configLoader.loadFromFile({ path: tempConfigFile }); |
| 38 | + |
| 39 | + expect(result).to.deep.equal(testConfig); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('loadFromHttp', () => { |
| 44 | + it('should load configuration from HTTP endpoint', async () => { |
| 45 | + const testConfig = { |
| 46 | + proxyUrl: 'https://test.com', |
| 47 | + cookieSecret: 'test-secret', |
| 48 | + }; |
| 49 | + |
| 50 | + sinon.stub(axios, 'get').resolves({ data: testConfig }); |
| 51 | + |
| 52 | + configLoader = new ConfigLoader({}); |
| 53 | + const result = await configLoader.loadFromHttp({ |
| 54 | + url: 'http://config-service/config', |
| 55 | + headers: {}, |
| 56 | + }); |
| 57 | + |
| 58 | + expect(result).to.deep.equal(testConfig); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should include bearer token if provided', async () => { |
| 62 | + const axiosStub = sinon.stub(axios, 'get').resolves({ data: {} }); |
| 63 | + |
| 64 | + configLoader = new ConfigLoader({}); |
| 65 | + await configLoader.loadFromHttp({ |
| 66 | + url: 'http://config-service/config', |
| 67 | + auth: { |
| 68 | + type: 'bearer', |
| 69 | + token: 'test-token', |
| 70 | + }, |
| 71 | + }); |
| 72 | + |
| 73 | + expect( |
| 74 | + axiosStub.calledWith('http://config-service/config', { |
| 75 | + headers: { Authorization: 'Bearer test-token' }, |
| 76 | + }), |
| 77 | + ).to.be.true; |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('reloadConfiguration', () => { |
| 82 | + it('should emit configurationChanged event when config changes', async () => { |
| 83 | + const initialConfig = { |
| 84 | + configurationSources: { |
| 85 | + enabled: true, |
| 86 | + sources: [ |
| 87 | + { |
| 88 | + type: 'file', |
| 89 | + enabled: true, |
| 90 | + path: tempConfigFile, |
| 91 | + }, |
| 92 | + ], |
| 93 | + }, |
| 94 | + }; |
| 95 | + |
| 96 | + const newConfig = { |
| 97 | + proxyUrl: 'https://new-test.com', |
| 98 | + }; |
| 99 | + |
| 100 | + fs.writeFileSync(tempConfigFile, JSON.stringify(newConfig)); |
| 101 | + |
| 102 | + configLoader = new ConfigLoader(initialConfig); |
| 103 | + const spy = sinon.spy(); |
| 104 | + configLoader.on('configurationChanged', spy); |
| 105 | + |
| 106 | + await configLoader.reloadConfiguration(); |
| 107 | + |
| 108 | + expect(spy.calledOnce).to.be.true; |
| 109 | + expect(spy.firstCall.args[0]).to.deep.include(newConfig); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should not emit event if config has not changed', async () => { |
| 113 | + const testConfig = { |
| 114 | + proxyUrl: 'https://test.com', |
| 115 | + }; |
| 116 | + |
| 117 | + const config = { |
| 118 | + configurationSources: { |
| 119 | + enabled: true, |
| 120 | + sources: [ |
| 121 | + { |
| 122 | + type: 'file', |
| 123 | + enabled: true, |
| 124 | + path: tempConfigFile, |
| 125 | + }, |
| 126 | + ], |
| 127 | + }, |
| 128 | + }; |
| 129 | + |
| 130 | + fs.writeFileSync(tempConfigFile, JSON.stringify(testConfig)); |
| 131 | + |
| 132 | + configLoader = new ConfigLoader(config); |
| 133 | + const spy = sinon.spy(); |
| 134 | + configLoader.on('configurationChanged', spy); |
| 135 | + |
| 136 | + await configLoader.reloadConfiguration(); // First reload should emit |
| 137 | + await configLoader.reloadConfiguration(); // Second reload should not emit since config hasn't changed |
| 138 | + |
| 139 | + expect(spy.calledOnce).to.be.true; // Should only emit once |
| 140 | + }); |
| 141 | + }); |
| 142 | +}); |
0 commit comments