2021-03-29 19:58:11 +02:00
|
|
|
import test from 'ava';
|
|
|
|
|
|
|
|
import sinon from 'sinon';
|
|
|
|
|
|
|
|
import apiTokenMiddleware from './api-token-middleware';
|
|
|
|
import getLogger from '../../test/fixtures/no-logger';
|
|
|
|
import { CLIENT } from '../permissions';
|
2021-04-22 15:04:08 +02:00
|
|
|
import { createTestConfig } from '../../test/config/test-config';
|
2021-04-22 23:40:52 +02:00
|
|
|
import ApiUser from '../types/api-user';
|
2021-03-29 19:58:11 +02:00
|
|
|
|
|
|
|
let config: any;
|
|
|
|
|
|
|
|
test.beforeEach(() => {
|
|
|
|
config = {
|
|
|
|
getLogger,
|
|
|
|
authentication: {
|
|
|
|
enableApiToken: true,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not do anything if request does not contain a authorization', async t => {
|
|
|
|
const apiTokenService = {
|
|
|
|
getUserForToken: sinon.fake(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const func = apiTokenMiddleware(config, { apiTokenService });
|
|
|
|
|
|
|
|
const cb = sinon.fake();
|
|
|
|
|
|
|
|
const req = {
|
|
|
|
header: sinon.fake(),
|
|
|
|
};
|
|
|
|
|
|
|
|
await func(req, undefined, cb);
|
|
|
|
|
|
|
|
t.true(req.header.calledOnce);
|
|
|
|
t.true(cb.calledOnce);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not add user if unknown token', async t => {
|
|
|
|
const apiTokenService = {
|
|
|
|
getUserForToken: sinon.fake(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const func = apiTokenMiddleware(config, { apiTokenService });
|
|
|
|
|
|
|
|
const cb = sinon.fake();
|
|
|
|
|
|
|
|
const req = {
|
|
|
|
header: sinon.fake.returns('some-token'),
|
|
|
|
user: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
await func(req, undefined, cb);
|
|
|
|
|
|
|
|
t.true(cb.called);
|
|
|
|
t.true(req.header.called);
|
|
|
|
t.falsy(req.user);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should add user if unknown token', async t => {
|
2021-04-22 23:40:52 +02:00
|
|
|
const apiUser = new ApiUser({
|
2021-03-29 19:58:11 +02:00
|
|
|
username: 'default',
|
|
|
|
permissions: [CLIENT],
|
|
|
|
});
|
|
|
|
const apiTokenService = {
|
|
|
|
getUserForToken: sinon.fake.returns(apiUser),
|
|
|
|
};
|
|
|
|
|
|
|
|
const func = apiTokenMiddleware(config, { apiTokenService });
|
|
|
|
|
|
|
|
const cb = sinon.fake();
|
|
|
|
|
|
|
|
const req = {
|
|
|
|
header: sinon.fake.returns('some-known-token'),
|
|
|
|
user: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
await func(req, undefined, cb);
|
|
|
|
|
|
|
|
t.true(cb.called);
|
|
|
|
t.true(req.header.called);
|
|
|
|
t.is(req.user, apiUser);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not add user if disabled', async t => {
|
2021-04-22 23:40:52 +02:00
|
|
|
const apiUser = new ApiUser({
|
2021-03-29 19:58:11 +02:00
|
|
|
username: 'default',
|
|
|
|
permissions: [CLIENT],
|
|
|
|
});
|
|
|
|
const apiTokenService = {
|
|
|
|
getUserForToken: sinon.fake.returns(apiUser),
|
|
|
|
};
|
|
|
|
|
2021-04-22 15:04:08 +02:00
|
|
|
const disabledConfig = createTestConfig({
|
2021-03-29 19:58:11 +02:00
|
|
|
getLogger,
|
|
|
|
authentication: {
|
|
|
|
enableApiToken: false,
|
2021-04-09 13:46:53 +02:00
|
|
|
createAdminUser: false,
|
2021-03-29 19:58:11 +02:00
|
|
|
},
|
2021-04-22 10:07:10 +02:00
|
|
|
});
|
2021-03-29 19:58:11 +02:00
|
|
|
|
|
|
|
const func = apiTokenMiddleware(disabledConfig, { apiTokenService });
|
|
|
|
|
|
|
|
const cb = sinon.fake();
|
|
|
|
|
|
|
|
const req = {
|
|
|
|
header: sinon.fake.returns('some-known-token'),
|
|
|
|
user: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
await func(req, undefined, cb);
|
|
|
|
|
|
|
|
t.true(cb.called);
|
|
|
|
t.falsy(req.user);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should call next if apiTokenService throws', async t => {
|
|
|
|
getLogger.setMuteError(true);
|
|
|
|
const apiTokenService = {
|
|
|
|
getUserForToken: () => {
|
|
|
|
throw new Error('hi there, i am stupid');
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const func = apiTokenMiddleware(config, { apiTokenService });
|
|
|
|
|
|
|
|
const cb = sinon.fake();
|
|
|
|
|
|
|
|
const req = {
|
|
|
|
header: sinon.fake.returns('some-token'),
|
|
|
|
user: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
await func(req, undefined, cb);
|
|
|
|
|
|
|
|
t.true(cb.called);
|
|
|
|
getLogger.setMuteError(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should call next if apiTokenService throws x2', async t => {
|
|
|
|
const apiTokenService = {
|
|
|
|
getUserForToken: () => {
|
|
|
|
throw new Error('hi there, i am stupid');
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const func = apiTokenMiddleware(config, { apiTokenService });
|
|
|
|
|
|
|
|
const cb = sinon.fake();
|
|
|
|
|
|
|
|
const req = {
|
|
|
|
header: sinon.fake.returns('some-token'),
|
|
|
|
user: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
await func(req, undefined, cb);
|
|
|
|
|
|
|
|
t.true(cb.called);
|
|
|
|
});
|