1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/test/e2e/services/project-service.e2e.test.js
Christopher Kolstad c17a1980a2
Add service layer
This simplifies stores to just be storage interaction, they no longer react to events.

Controllers now call services and awaits the result from the call.

When the service calls are returned the database is updated.
This simplifies testing dramatically, cause you know that your state is
updated when returned from a call, rather than hoping the store has
picked up the event (which really was a command) and reacted to it.

Events are still emitted from eventStore, so other parts of the app can
react to events as they're being sent out.

As part of the move to services, we now also emit an application-created
event when we see a new client application.

Fixes: #685
Fixes: #595
2021-01-21 10:59:19 +01:00

149 lines
4.1 KiB
JavaScript

const test = require('ava');
const dbInit = require('../helpers/database-init');
const getLogger = require('../../fixtures/no-logger');
const ProjectService = require('../../../lib/services/project-service');
let stores;
// let projectStore;
let projectService;
test.before(async () => {
const db = await dbInit('project_service_serial', getLogger);
stores = db.stores;
// projectStore = stores.projectStore;
projectService = new ProjectService(stores, { getLogger });
});
test.after(async () => {
await stores.db.destroy();
});
test.serial('should have default project', async t => {
const project = await projectService.getProject('default');
t.assert(project);
t.is(project.id, 'default');
});
test.serial('should list all projects', async t => {
const project = {
id: 'test-list',
name: 'New project',
description: 'Blah',
};
await projectService.createProject(project, 'someUser');
const projects = await projectService.getProjects();
t.is(projects.length, 2);
});
test.serial('should create new project', async t => {
const project = {
id: 'test',
name: 'New project',
description: 'Blah',
};
await projectService.createProject(project, 'someUser');
const ret = await projectService.getProject('test');
t.deepEqual(project.id, ret.id);
t.deepEqual(project.name, ret.name);
t.deepEqual(project.description, ret.description);
t.truthy(ret.createdAt);
});
test.serial('should delete project', async t => {
const project = {
id: 'test-delete',
name: 'New project',
description: 'Blah',
};
await projectService.createProject(project, 'some-user');
await projectService.deleteProject(project.id, 'some-user');
try {
await projectService.getProject(project.id);
} catch (err) {
t.is(err.message, 'No project found');
}
});
test.serial('should not be able to delete project with toggles', async t => {
const project = {
id: 'test-delete-with-toggles',
name: 'New project',
description: 'Blah',
};
await projectService.createProject(project, 'some-user');
await stores.featureToggleStore.createFeature({
name: 'test-project-delete',
project: project.id,
enabled: false,
});
try {
await projectService.deleteProject(project.id, 'some-user');
} catch (err) {
t.is(
err.message,
'You can not delete as project with active feature toggles',
);
}
});
test.serial('should not delete "default" project', async t => {
try {
await projectService.deleteProject('default', 'some-user');
} catch (err) {
t.is(err.message, 'You can not delete the default project!');
}
});
test.serial('should validate name, legal', async t => {
const result = await projectService.validateId('new_name');
t.true(result);
});
test.serial('should require URL friendly ID', async t => {
try {
await projectService.validateId('new name øæå');
} catch (err) {
t.is(err.message, '"value" must be URL friendly');
}
});
test.serial('should require unique ID', async t => {
try {
await projectService.validateId('default');
} catch (err) {
t.is(err.message, 'A project with this id already exists.');
}
});
test.serial('should update project', async t => {
const project = {
id: 'test-update',
name: 'New project',
description: 'Blah',
};
const updatedProject = {
id: 'test-update',
name: 'New name',
description: 'Blah longer desc',
};
await projectService.createProject(project, 'some-user');
await projectService.updateProject(updatedProject, 'some-user');
const readProject = await projectService.getProject(project.id);
t.is(updatedProject.name, readProject.name);
t.is(updatedProject.description, readProject.description);
});
test.serial('should give error when getting unkown project', async t => {
try {
await projectService.getProject('unknown');
} catch (err) {
t.is(err.message, 'No project found');
}
});