Skip to content

Commit ee8f2c1

Browse files
committed
test(auth): add extra tests and fix linter issues
1 parent 678d932 commit ee8f2c1

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

test/testAuthMethods.test.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const chai = require('chai');
2-
const service = require('../src/service');
32
const config = require('../src/config');
43
const sinon = require('sinon');
54
const proxyquire = require('proxyquire');
@@ -8,12 +7,6 @@ chai.should();
87
const expect = chai.expect;
98

109
describe('auth methods', async () => {
11-
let app;
12-
13-
before(async function () {
14-
app = await service.start();
15-
});
16-
1710
it('should return a local auth method by default', async function () {
1811
const authMethods = config.getAuthMethods();
1912
expect(authMethods).to.have.lengthOf(1);
@@ -41,7 +34,28 @@ describe('auth methods', async () => {
4134
expect(() => config.getAuthMethods()).to.throw(Error, 'No authentication method enabled');
4235
});
4336

44-
after(async function () {
45-
await service.httpServer.close();
46-
});
37+
it('should return an array of enabled auth methods when overridden', async function () {
38+
const newConfig = JSON.stringify({
39+
authentication: [
40+
{ type: 'local', enabled: true },
41+
{ type: 'ActiveDirectory', enabled: true },
42+
{ type: 'openidconnect', enabled: true },
43+
],
44+
});
45+
46+
const fsStub = {
47+
existsSync: sinon.stub().returns(true),
48+
readFileSync: sinon.stub().returns(newConfig),
49+
};
50+
51+
const config = proxyquire('../src/config', {
52+
fs: fsStub,
53+
});
54+
55+
const authMethods = config.getAuthMethods();
56+
expect(authMethods).to.have.lengthOf(3);
57+
expect(authMethods[0].type).to.equal('local');
58+
expect(authMethods[1].type).to.equal('ActiveDirectory');
59+
expect(authMethods[2].type).to.equal('openidconnect');
60+
})
4761
});

test/testLogin.test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,15 @@ describe('auth', async () => {
6363
res.should.have.status(401);
6464
});
6565

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

0 commit comments

Comments
 (0)