Skip to content

Commit 71e7e52

Browse files
committed
test(auth): improve test coverage
1 parent 70dd346 commit 71e7e52

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

test/testAuthMethods.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const chai = require('chai');
2+
const service = require('../src/service');
3+
const config = require('../src/config');
4+
const sinon = require('sinon');
5+
const proxyquire = require('proxyquire');
6+
7+
chai.should();
8+
const expect = chai.expect;
9+
10+
describe('auth methods', async () => {
11+
let app;
12+
13+
before(async function () {
14+
app = await service.start();
15+
});
16+
17+
it('should return a local auth method by default', async function () {
18+
const authMethods = config.getAuthMethods();
19+
expect(authMethods).to.have.lengthOf(1);
20+
expect(authMethods[0].type).to.equal('local');
21+
});
22+
23+
it('should return an error if no auth methods are enabled', async function () {
24+
const newConfig = JSON.stringify({
25+
authentication: [
26+
{ type: 'local', enabled: false },
27+
{ type: 'ActiveDirectory', enabled: false },
28+
{ type: 'openidconnect', enabled: false },
29+
],
30+
});
31+
32+
const fsStub = {
33+
existsSync: sinon.stub().returns(true),
34+
readFileSync: sinon.stub().returns(newConfig),
35+
};
36+
37+
const config = proxyquire('../src/config', {
38+
fs: fsStub,
39+
});
40+
41+
expect(() => config.getAuthMethods()).to.throw(Error, 'No authentication method enabled');
42+
});
43+
});

test/testLogin.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ describe('auth', async () => {
6262

6363
res.should.have.status(401);
6464
});
65+
66+
it('should fail to login with invalid credentials', async function () {
67+
const res = await chai.request(app).post('/api/auth/login').send({
68+
username: 'admin',
69+
password: 'invalid',
70+
});
71+
res.should.have.status(401);
72+
});
6573
});
6674

6775
after(async function () {

0 commit comments

Comments
 (0)