2022-01-05 10:00:59 +01:00
|
|
|
import { ApiTokenService } from './api-token-service';
|
|
|
|
import { createTestConfig } from '../../test/config/test-config';
|
|
|
|
import { IUnleashConfig } from '../server-impl';
|
2022-08-18 10:20:51 +02:00
|
|
|
import { ApiTokenType, IApiTokenCreate } from '../types/models/api-token';
|
2022-01-05 10:00:59 +01:00
|
|
|
import FakeApiTokenStore from '../../test/fixtures/fake-api-token-store';
|
2022-03-24 11:26:00 +01:00
|
|
|
import FakeEnvironmentStore from '../../test/fixtures/fake-environment-store';
|
2022-11-28 10:56:34 +01:00
|
|
|
import FakeEventStore from '../../test/fixtures/fake-event-store';
|
|
|
|
import {
|
|
|
|
API_TOKEN_CREATED,
|
|
|
|
API_TOKEN_DELETED,
|
|
|
|
API_TOKEN_UPDATED,
|
|
|
|
} from '../types';
|
|
|
|
import { addDays } from 'date-fns';
|
2022-01-05 10:00:59 +01:00
|
|
|
|
|
|
|
test('Should init api token', async () => {
|
|
|
|
const token = {
|
|
|
|
environment: '*',
|
|
|
|
project: '*',
|
|
|
|
secret: '*:*:some-random-string',
|
|
|
|
type: ApiTokenType.ADMIN,
|
|
|
|
username: 'admin',
|
|
|
|
};
|
|
|
|
|
|
|
|
const config: IUnleashConfig = createTestConfig({
|
|
|
|
authentication: {
|
|
|
|
initApiTokens: [token],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const apiTokenStore = new FakeApiTokenStore();
|
2022-03-24 11:26:00 +01:00
|
|
|
const environmentStore = new FakeEnvironmentStore();
|
2022-11-28 10:56:34 +01:00
|
|
|
const eventStore = new FakeEventStore();
|
2022-01-05 10:00:59 +01:00
|
|
|
const insertCalled = new Promise((resolve) => {
|
|
|
|
apiTokenStore.on('insert', resolve);
|
|
|
|
});
|
|
|
|
|
2022-11-28 10:56:34 +01:00
|
|
|
new ApiTokenService(
|
|
|
|
{ apiTokenStore, environmentStore, eventStore },
|
|
|
|
config,
|
|
|
|
);
|
2022-01-05 10:00:59 +01:00
|
|
|
|
|
|
|
await insertCalled;
|
|
|
|
|
|
|
|
const tokens = await apiTokenStore.getAll();
|
|
|
|
|
|
|
|
expect(tokens).toHaveLength(1);
|
|
|
|
});
|
2022-08-18 10:20:51 +02:00
|
|
|
|
|
|
|
test("Shouldn't return frontend token when secret is undefined", async () => {
|
|
|
|
const token: IApiTokenCreate = {
|
|
|
|
environment: 'default',
|
|
|
|
projects: ['*'],
|
|
|
|
secret: '*:*:some-random-string',
|
|
|
|
type: ApiTokenType.FRONTEND,
|
|
|
|
username: 'front',
|
|
|
|
expiresAt: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
const config: IUnleashConfig = createTestConfig({});
|
|
|
|
const apiTokenStore = new FakeApiTokenStore();
|
|
|
|
const environmentStore = new FakeEnvironmentStore();
|
2022-11-28 10:56:34 +01:00
|
|
|
const eventStore = new FakeEventStore();
|
2022-08-18 10:20:51 +02:00
|
|
|
|
|
|
|
await environmentStore.create({
|
|
|
|
name: 'default',
|
|
|
|
enabled: true,
|
|
|
|
protected: true,
|
|
|
|
type: 'test',
|
|
|
|
sortOrder: 1,
|
|
|
|
});
|
|
|
|
|
|
|
|
const apiTokenService = new ApiTokenService(
|
2022-11-28 10:56:34 +01:00
|
|
|
{ apiTokenStore, environmentStore, eventStore },
|
2022-08-18 10:20:51 +02:00
|
|
|
config,
|
|
|
|
);
|
|
|
|
|
|
|
|
await apiTokenService.createApiTokenWithProjects(token);
|
|
|
|
await apiTokenService.fetchActiveTokens();
|
|
|
|
|
|
|
|
expect(apiTokenService.getUserForToken(undefined)).toEqual(undefined);
|
|
|
|
expect(apiTokenService.getUserForToken('')).toEqual(undefined);
|
|
|
|
});
|
2022-11-28 10:56:34 +01:00
|
|
|
|
|
|
|
test('Api token operations should all have events attached', async () => {
|
|
|
|
const token: IApiTokenCreate = {
|
|
|
|
environment: 'default',
|
|
|
|
projects: ['*'],
|
|
|
|
secret: '*:*:some-random-string',
|
|
|
|
type: ApiTokenType.FRONTEND,
|
|
|
|
username: 'front',
|
|
|
|
expiresAt: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
const config: IUnleashConfig = createTestConfig({});
|
|
|
|
const apiTokenStore = new FakeApiTokenStore();
|
|
|
|
const environmentStore = new FakeEnvironmentStore();
|
|
|
|
const eventStore = new FakeEventStore();
|
|
|
|
|
|
|
|
await environmentStore.create({
|
|
|
|
name: 'default',
|
|
|
|
enabled: true,
|
|
|
|
protected: true,
|
|
|
|
type: 'test',
|
|
|
|
sortOrder: 1,
|
|
|
|
});
|
|
|
|
|
|
|
|
const apiTokenService = new ApiTokenService(
|
|
|
|
{ apiTokenStore, environmentStore, eventStore },
|
|
|
|
config,
|
|
|
|
);
|
|
|
|
let saved = await apiTokenService.createApiTokenWithProjects(token);
|
|
|
|
let newExpiry = addDays(new Date(), 30);
|
|
|
|
await apiTokenService.updateExpiry(saved.secret, newExpiry, 'test');
|
|
|
|
await apiTokenService.delete(saved.secret, 'test');
|
|
|
|
const events = await eventStore.getEvents();
|
|
|
|
const createdApiTokenEvents = events.filter(
|
|
|
|
(e) => e.type === API_TOKEN_CREATED,
|
|
|
|
);
|
|
|
|
expect(createdApiTokenEvents).toHaveLength(1);
|
|
|
|
expect(createdApiTokenEvents[0].preData).toBeUndefined();
|
|
|
|
expect(createdApiTokenEvents[0].data.secret).toBeUndefined();
|
|
|
|
|
|
|
|
const updatedApiTokenEvents = events.filter(
|
|
|
|
(e) => e.type === API_TOKEN_UPDATED,
|
|
|
|
);
|
|
|
|
expect(updatedApiTokenEvents).toHaveLength(1);
|
|
|
|
expect(updatedApiTokenEvents[0].preData.expiresAt).toBeDefined();
|
|
|
|
expect(updatedApiTokenEvents[0].preData.secret).toBeUndefined();
|
|
|
|
expect(updatedApiTokenEvents[0].data.secret).toBeUndefined();
|
|
|
|
expect(updatedApiTokenEvents[0].data.expiresAt).toBe(newExpiry);
|
|
|
|
|
|
|
|
const deletedApiTokenEvents = events.filter(
|
|
|
|
(e) => e.type === API_TOKEN_DELETED,
|
|
|
|
);
|
|
|
|
expect(deletedApiTokenEvents).toHaveLength(1);
|
|
|
|
expect(deletedApiTokenEvents[0].data).toBeUndefined();
|
|
|
|
expect(deletedApiTokenEvents[0].preData).toBeDefined();
|
|
|
|
expect(deletedApiTokenEvents[0].preData.secret).toBeUndefined();
|
|
|
|
});
|