Skip to content

Commit 2fb1f3c

Browse files
committed
test: increase tes coverage
1 parent 04d912e commit 2fb1f3c

2 files changed

Lines changed: 126 additions & 12 deletions

File tree

test/ConfigLoader.test.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,3 +682,70 @@ describe('Validation Helpers', () => {
682682
});
683683
});
684684
});
685+
686+
describe('ConfigLoader Error Handling', () => {
687+
let configLoader;
688+
let tempDir;
689+
let tempConfigFile;
690+
691+
beforeEach(() => {
692+
tempDir = fs.mkdtempSync('gitproxy-configloader-test-');
693+
tempConfigFile = path.join(tempDir, 'test-config.json');
694+
});
695+
696+
afterEach(() => {
697+
if (fs.existsSync(tempDir)) {
698+
fs.rmSync(tempDir, { recursive: true });
699+
}
700+
sinon.restore();
701+
configLoader?.stop();
702+
});
703+
704+
it('should handle invalid JSON in file source', async () => {
705+
fs.writeFileSync(tempConfigFile, 'invalid json content');
706+
707+
configLoader = new ConfigLoader({});
708+
try {
709+
await configLoader.loadFromFile({
710+
type: 'file',
711+
enabled: true,
712+
path: tempConfigFile,
713+
});
714+
throw new Error('Expected error was not thrown');
715+
} catch (error) {
716+
expect(error.message).to.contain('Invalid configuration file format');
717+
}
718+
});
719+
720+
it('should handle HTTP request errors', async () => {
721+
sinon.stub(axios, 'get').rejects(new Error('Network error'));
722+
723+
configLoader = new ConfigLoader({});
724+
try {
725+
await configLoader.loadFromHttp({
726+
type: 'http',
727+
enabled: true,
728+
url: 'http://config-service/config',
729+
});
730+
throw new Error('Expected error was not thrown');
731+
} catch (error) {
732+
expect(error.message).to.equal('Network error');
733+
}
734+
});
735+
736+
it('should handle invalid JSON from HTTP response', async () => {
737+
sinon.stub(axios, 'get').resolves({ data: 'invalid json response' });
738+
739+
configLoader = new ConfigLoader({});
740+
try {
741+
await configLoader.loadFromHttp({
742+
type: 'http',
743+
enabled: true,
744+
url: 'http://config-service/config',
745+
});
746+
throw new Error('Expected error was not thrown');
747+
} catch (error) {
748+
expect(error.message).to.contain('Invalid configuration format from HTTP source');
749+
}
750+
});
751+
});

test/testConfig.test.js

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('default configuration', function () {
1111
it('should use default values if no user-settings.json file exists', function () {
1212
const config = require('../src/config');
1313
config.logConfiguration();
14-
const enabledMethods = defaultSettings.authentication.filter(method => method.enabled);
14+
const enabledMethods = defaultSettings.authentication.filter((method) => method.enabled);
1515

1616
expect(config.getAuthMethods()).to.deep.equal(enabledMethods);
1717
expect(config.getDatabase()).to.be.eql(defaultSettings.sink[0]);
@@ -62,7 +62,7 @@ describe('user configuration', function () {
6262
// Invalidate cache to force reload
6363
const config = require('../src/config');
6464
config.invalidateCache();
65-
const enabledMethods = defaultSettings.authentication.filter(method => method.enabled);
65+
const enabledMethods = defaultSettings.authentication.filter((method) => method.enabled);
6666

6767
expect(config.getAuthorisedList()).to.be.eql(user.authorisedList);
6868
expect(config.getAuthMethods()).to.deep.equal(enabledMethods);
@@ -81,8 +81,8 @@ describe('user configuration', function () {
8181
clientID: 'test-client-id',
8282
clientSecret: 'test-client-secret',
8383
callbackURL: 'https://example.com/callback',
84-
scope: 'openid email profile'
85-
}
84+
scope: 'openid email profile',
85+
},
8686
},
8787
],
8888
};
@@ -92,7 +92,7 @@ describe('user configuration', function () {
9292
const config = require('../src/config');
9393
config.invalidateCache();
9494
const authMethods = config.getAuthMethods();
95-
const oidcAuth = authMethods.find(method => method.type === 'openidconnect');
95+
const oidcAuth = authMethods.find((method) => method.type === 'openidconnect');
9696

9797
expect(oidcAuth).to.not.be.undefined;
9898
expect(oidcAuth.enabled).to.be.true;
@@ -114,7 +114,7 @@ describe('user configuration', function () {
114114
fs.writeFileSync(tempUserFile, JSON.stringify(user));
115115

116116
const config = require('../src/config');
117-
const enabledMethods = defaultSettings.authentication.filter(method => method.enabled);
117+
const enabledMethods = defaultSettings.authentication.filter((method) => method.enabled);
118118

119119
expect(config.getDatabase()).to.be.eql(user.sink[0]);
120120
expect(config.getDatabase()).to.not.be.eql(defaultSettings.sink[0]);
@@ -217,7 +217,7 @@ describe('user configuration', function () {
217217
enabled: true,
218218
key: 'my-key.pem',
219219
cert: 'my-cert.pem',
220-
}
220+
},
221221
};
222222

223223
fs.writeFileSync(tempUserFile, JSON.stringify(user));
@@ -240,7 +240,7 @@ describe('user configuration', function () {
240240
sslCertPemPath: 'bad-cert.pem',
241241
};
242242
fs.writeFileSync(tempUserFile, JSON.stringify(user));
243-
243+
244244
// Invalidate cache to force reload
245245
const config = require('../src/config');
246246
config.invalidateCache();
@@ -275,7 +275,7 @@ describe('user configuration', function () {
275275
},
276276
};
277277
fs.writeFileSync(tempUserFile, JSON.stringify(user));
278-
278+
279279
// Invalidate cache to force reload
280280
const config = require('../src/config');
281281
config.invalidateCache();
@@ -285,7 +285,7 @@ describe('user configuration', function () {
285285

286286
it('should override default settings for cookieSecret if env var is used', function () {
287287
fs.writeFileSync(tempUserFile, '{}');
288-
process.env.GIT_PROXY_COOKIE_SECRET = 'test-cookie-secret'
288+
process.env.GIT_PROXY_COOKIE_SECRET = 'test-cookie-secret';
289289

290290
const config = require('../src/config');
291291
expect(config.getCookieSecret()).to.equal('test-cookie-secret');
@@ -297,8 +297,8 @@ describe('user configuration', function () {
297297
{
298298
type: 'mongo',
299299
enabled: true,
300-
}
301-
]
300+
},
301+
],
302302
};
303303
fs.writeFileSync(tempUserFile, JSON.stringify(user));
304304
process.env.GIT_PROXY_MONGO_CONNECTION_STRING = 'mongodb://example.com:27017/test';
@@ -307,6 +307,53 @@ describe('user configuration', function () {
307307
expect(config.getDatabase().connectionString).to.equal('mongodb://example.com:27017/test');
308308
});
309309

310+
it('should test cache invalidation function', function () {
311+
fs.writeFileSync(tempUserFile, '{}');
312+
313+
const config = require('../src/config');
314+
315+
// Load config first time
316+
const firstLoad = config.getAuthorisedList();
317+
318+
// Invalidate cache and load again
319+
config.invalidateCache();
320+
const secondLoad = config.getAuthorisedList();
321+
322+
expect(firstLoad).to.deep.equal(secondLoad);
323+
});
324+
325+
it('should test reloadConfiguration function', async function () {
326+
fs.writeFileSync(tempUserFile, '{}');
327+
328+
const config = require('../src/config');
329+
330+
// reloadConfiguration doesn't throw
331+
await config.reloadConfiguration();
332+
});
333+
334+
it('should handle configuration errors during initialization', function () {
335+
const user = {
336+
invalidConfig: 'this should cause validation error',
337+
};
338+
fs.writeFileSync(tempUserFile, JSON.stringify(user));
339+
340+
const config = require('../src/config');
341+
expect(() => config.getAuthorisedList()).to.not.throw();
342+
});
343+
344+
it('should test all getter functions for coverage', function () {
345+
fs.writeFileSync(tempUserFile, '{}');
346+
347+
const config = require('../src/config');
348+
349+
expect(() => config.getProxyUrl()).to.not.throw();
350+
expect(() => config.getCookieSecret()).to.not.throw();
351+
expect(() => config.getSessionMaxAgeHours()).to.not.throw();
352+
expect(() => config.getCommitConfig()).to.not.throw();
353+
expect(() => config.getPrivateOrganizations()).to.not.throw();
354+
expect(() => config.getUIRouteAuth()).to.not.throw();
355+
});
356+
310357
afterEach(function () {
311358
fs.rmSync(tempUserFile);
312359
fs.rmdirSync(tempDir);

0 commit comments

Comments
 (0)