2021-03-29 19:58:11 +02:00
|
|
|
import dbInit from '../helpers/database-init';
|
|
|
|
import getLogger from '../../fixtures/no-logger';
|
|
|
|
import { ApiTokenService } from '../../../lib/services/api-token-service';
|
2021-04-22 15:04:08 +02:00
|
|
|
import { createTestConfig } from '../../config/test-config';
|
2021-09-15 20:28:10 +02:00
|
|
|
import { ApiTokenType, IApiToken } from '../../../lib/types/models/api-token';
|
2021-09-24 13:55:00 +02:00
|
|
|
import { DEFAULT_ENV } from '../../../lib/util/constants';
|
2021-03-29 19:58:11 +02:00
|
|
|
|
|
|
|
let db;
|
|
|
|
let stores;
|
|
|
|
let apiTokenService: ApiTokenService;
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
beforeAll(async () => {
|
2021-04-22 15:04:08 +02:00
|
|
|
const config = createTestConfig({
|
2021-04-22 10:07:10 +02:00
|
|
|
server: { baseUriPath: '/test' },
|
|
|
|
});
|
2021-04-09 13:46:53 +02:00
|
|
|
db = await dbInit('api_token_service_serial', getLogger);
|
2021-03-29 19:58:11 +02:00
|
|
|
stores = db.stores;
|
|
|
|
// projectStore = stores.projectStore;
|
2021-04-22 10:07:10 +02:00
|
|
|
apiTokenService = new ApiTokenService(stores, config);
|
2021-03-29 19:58:11 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
afterAll(async () => {
|
|
|
|
if (db) {
|
|
|
|
await db.destroy();
|
|
|
|
}
|
2021-03-29 19:58:11 +02:00
|
|
|
});
|
2021-05-28 11:10:24 +02:00
|
|
|
afterEach(async () => {
|
2021-03-29 19:58:11 +02:00
|
|
|
const tokens = await stores.apiTokenStore.getAll();
|
|
|
|
const deleteAll = tokens.map((t: IApiToken) =>
|
|
|
|
stores.apiTokenStore.delete(t.secret),
|
|
|
|
);
|
|
|
|
await Promise.all(deleteAll);
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should have empty list of tokens', async () => {
|
2021-03-29 19:58:11 +02:00
|
|
|
const allTokens = await apiTokenService.getAllTokens();
|
|
|
|
const activeTokens = await apiTokenService.getAllTokens();
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(allTokens.length).toBe(0);
|
|
|
|
expect(activeTokens.length).toBe(0);
|
2021-03-29 19:58:11 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should create client token', async () => {
|
2021-09-15 20:28:10 +02:00
|
|
|
const token = await apiTokenService.createApiToken({
|
2021-03-29 19:58:11 +02:00
|
|
|
username: 'default-client',
|
|
|
|
type: ApiTokenType.CLIENT,
|
2021-09-15 20:28:10 +02:00
|
|
|
project: '*',
|
2021-09-24 13:55:00 +02:00
|
|
|
environment: DEFAULT_ENV,
|
2021-03-29 19:58:11 +02:00
|
|
|
});
|
|
|
|
const allTokens = await apiTokenService.getAllTokens();
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(allTokens.length).toBe(1);
|
|
|
|
expect(token.secret.length > 32).toBe(true);
|
|
|
|
expect(token.type).toBe(ApiTokenType.CLIENT);
|
|
|
|
expect(token.username).toBe('default-client');
|
|
|
|
expect(allTokens[0].secret).toBe(token.secret);
|
2021-03-29 19:58:11 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should create admin token', async () => {
|
2021-09-15 20:28:10 +02:00
|
|
|
const token = await apiTokenService.createApiToken({
|
2021-03-29 19:58:11 +02:00
|
|
|
username: 'admin',
|
|
|
|
type: ApiTokenType.ADMIN,
|
2021-09-15 20:28:10 +02:00
|
|
|
project: '*',
|
|
|
|
environment: '*',
|
2021-03-29 19:58:11 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(token.secret.length > 32).toBe(true);
|
|
|
|
expect(token.type).toBe(ApiTokenType.ADMIN);
|
2021-03-29 19:58:11 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should set expiry of token', async () => {
|
2021-03-29 19:58:11 +02:00
|
|
|
const time = new Date('2022-01-01');
|
2021-09-15 20:28:10 +02:00
|
|
|
await apiTokenService.createApiToken({
|
2021-03-29 19:58:11 +02:00
|
|
|
username: 'default-client',
|
|
|
|
type: ApiTokenType.CLIENT,
|
|
|
|
expiresAt: time,
|
2021-09-15 20:28:10 +02:00
|
|
|
project: '*',
|
2021-09-24 13:55:00 +02:00
|
|
|
environment: DEFAULT_ENV,
|
2021-03-29 19:58:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const [token] = await apiTokenService.getAllTokens();
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(token.expiresAt).toEqual(time);
|
2021-03-29 19:58:11 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should update expiry of token', async () => {
|
2021-03-29 19:58:11 +02:00
|
|
|
const time = new Date('2022-01-01');
|
|
|
|
const newTime = new Date('2023-01-01');
|
|
|
|
|
2021-09-15 20:28:10 +02:00
|
|
|
const token = await apiTokenService.createApiToken({
|
2021-03-29 19:58:11 +02:00
|
|
|
username: 'default-client',
|
|
|
|
type: ApiTokenType.CLIENT,
|
|
|
|
expiresAt: time,
|
2021-09-15 20:28:10 +02:00
|
|
|
project: '*',
|
2021-09-24 13:55:00 +02:00
|
|
|
environment: DEFAULT_ENV,
|
2021-03-29 19:58:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
await apiTokenService.updateExpiry(token.secret, newTime);
|
|
|
|
|
|
|
|
const [updatedToken] = await apiTokenService.getAllTokens();
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(updatedToken.expiresAt).toEqual(newTime);
|
2021-03-29 19:58:11 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should only return valid tokens', async () => {
|
2021-03-29 19:58:11 +02:00
|
|
|
const today = new Date();
|
|
|
|
const tomorrow = new Date(today.getTime() + 24 * 60 * 60 * 1000);
|
|
|
|
|
2021-09-15 20:28:10 +02:00
|
|
|
await apiTokenService.createApiToken({
|
2021-03-29 19:58:11 +02:00
|
|
|
username: 'default-expired',
|
|
|
|
type: ApiTokenType.CLIENT,
|
|
|
|
expiresAt: new Date('2021-01-01'),
|
2021-09-15 20:28:10 +02:00
|
|
|
project: '*',
|
2021-09-24 13:55:00 +02:00
|
|
|
environment: DEFAULT_ENV,
|
2021-03-29 19:58:11 +02:00
|
|
|
});
|
|
|
|
|
2021-09-15 20:28:10 +02:00
|
|
|
const activeToken = await apiTokenService.createApiToken({
|
2021-03-29 19:58:11 +02:00
|
|
|
username: 'default-valid',
|
|
|
|
type: ApiTokenType.CLIENT,
|
|
|
|
expiresAt: tomorrow,
|
2021-09-15 20:28:10 +02:00
|
|
|
project: '*',
|
2021-09-24 13:55:00 +02:00
|
|
|
environment: DEFAULT_ENV,
|
2021-03-29 19:58:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const tokens = await apiTokenService.getAllActiveTokens();
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(tokens.length).toBe(1);
|
|
|
|
expect(activeToken.secret).toBe(tokens[0].secret);
|
2021-03-29 19:58:11 +02:00
|
|
|
});
|