Skip to content

Commit

Permalink
Added task installation and state checks tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKriegler committed Dec 29, 2018
1 parent 6604ddd commit ec7e98c
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 31 deletions.
14 changes: 7 additions & 7 deletions lib/common/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ export function convertToPlatformError(e: any) {

export function sanitizeError(e: any) {
if (!e) { return e; }
let r: any = {};
if (e.statusCode) { r.statusCode = e.statusCode; }
if (e.status) { r.status = e.status; }
if (e.message) { r.message = e.message; }
if (e.reason) { r.reason = sanitizeError(e.reason); }
if (e.meta) { r.meta = e.meta; }
return r;
return {
...(e.status) && {status: e.status},
...(e.message) && {message: e.message},
...(e.reason) && {reason: e.reason},
...(e.meta) && {meta: e.meta},
...(e.statusCode) && {statusCode: e.statusCode}
};
}
2 changes: 1 addition & 1 deletion src/states/scheduled/ScheduledDeinstallationTaskState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class ScheduledDeinstallationTaskState extends TaskState {

public async update(data: TaskData): Promise<void> {
if (data.userId !== this.task.userId) {
throwError(ERRORS.EINVALID, 'Assigned user required');
this.task.userId = data.userId;
}

return await super.update(data);
Expand Down
2 changes: 1 addition & 1 deletion src/states/scheduled/ScheduledInstallationTaskState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class ScheduledInstallationTaskState extends TaskState {

public async update(data: TaskData): Promise<void> {
if (data.userId !== this.task.userId) {
throwError(ERRORS.EINVALID, 'Assigned user required');
this.task.userId = data.userId;
}

return await super.update(data);
Expand Down
2 changes: 1 addition & 1 deletion src/states/scheduled/ScheduledQuoteTaskState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class ScheduledQuoteTaskState extends TaskState {

public async update(data: TaskData): Promise<void> {
if (data.userId !== this.task.userId) {
throwError(ERRORS.EINVALID, 'Assigned user required');
this.task.userId = data.userId;
}

return await super.update(data);
Expand Down
2 changes: 1 addition & 1 deletion src/states/scheduled/ScheduledRepairTaskState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class ScheduledRepairTaskState extends TaskState {

public async update(data: TaskData): Promise<void> {
if (data.userId !== this.task.userId) {
throwError(ERRORS.EINVALID, 'Assigned user required');
this.task.userId = data.userId;
}

return await super.update(data);
Expand Down
2 changes: 1 addition & 1 deletion src/stores/TaskStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class TaskStore {
.updateOne(query, { $set: task });

if (result.modifiedCount === 0) {
throwError(ERRORS.ECONFLICT, 'Task has been updated by a concurrent process, please refresh');
throwError(ERRORS.ECONFLICT, 'Task has not been updated, this means a concurrent process has updated it.');
}

return task;
Expand Down
3 changes: 2 additions & 1 deletion tests/consts.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const TEST_TIMEOUT = 10000;
export const TEST_TIMEOUT = 10000;
export const TEST_INSTALLER = 'user:id:1';
14 changes: 5 additions & 9 deletions tests/steps/system-test-steps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Chance from 'chance';
import { expect, AssertionError } from 'chai';
import { IGlobalTestStateHolder } from './base-test-steps';
import { Response } from 'superagent';
import { sleep } from '@matilda/lib/common'

const chance = new Chance();
Expand Down Expand Up @@ -56,15 +57,10 @@ export class SystemTestSteps {
.forEach(key => expect(e[key]).to.eq(validate[key]));
}

public async runRequest<T>(fn: () => Promise<T>): Promise<T | undefined> {
try {
this.state.lastError = null;
return await fn();
} catch (e) {
this.state.lastError = e;
console.log(e);
return undefined;
}
public async runRequest(fn: () => Promise<Response>) {
const response: Response = await fn();
this.state.lastError = response.error || null;
return response.body.data;
}

public async waitUntilAssertionsSucceed<T>(timeout: number,
Expand Down
47 changes: 41 additions & 6 deletions tests/steps/task-test-steps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TaskData } from '@matilda/src/types';
import { IGlobalTestStateHolder } from './base-test-steps';
import { Response } from 'superagent';
import { deepClone } from '@matilda/lib/common';
import { TEST_INSTALLER } from '../consts';
import * as chai from "chai";

chai.use(require("chai-http"));
Expand All @@ -22,7 +23,6 @@ export class TaskTestSteps {
}

public async new_task_is_created(template: TaskTemplate = { type: 'installation' }) {

template.expectedStatus = template.expectedStatus || 'new';

let taskData: TaskData = {
Expand All @@ -42,10 +42,7 @@ export class TaskTestSteps {
}

const createdTask = await this.state.system.runRequest(
async () =>{
const response: Response = await request(app).post('/api/v1/tasks').send(taskData)
return response.body.data;
}
async () => await request(app).post('/api/v1/tasks').send(taskData)
);

expect(createdTask).to.be.an('object');
Expand All @@ -65,6 +62,44 @@ export class TaskTestSteps {
expect(newTask).to.deep.eq(taskData);
}

public async task_is_scheduled() {
return await this.task_is_updated('scheduled');
}

public async task_is_closed() {
return await this.task_is_updated('closed');
}

public async task_is_updated(status: TaskTemplate['expectedStatus']) {
expect(this.state.currentTask.id).to.exist;

let taskData = deepClone(this.state.currentTask.task);
taskData.status = status;
taskData.userId = TEST_INSTALLER;

const updatedTask = await this.state.system.runRequest(
async () => await request(app).put(`/api/v1/tasks/${this.state.currentTask.id}`).send(taskData)
);

if (this.state.lastError) {
return;
}

expect(updatedTask).to.be.an('object');

const newTask: TaskData = <any> updatedTask;
expect(newTask).to.exist;
expect(newTask.status).to.eq(status);
expect(newTask.version).to.eq(taskData.version + 1);

this.state.currentTask.task.version = newTask.version;
this.state.currentTask.task.status = status;
this.state.currentTask.task.userId = taskData.userId;

delete (<any> newTask)._id;
expect(newTask).to.deep.eq(this.state.currentTask.task);
}

public async close() {
return 0;
}
Expand Down
13 changes: 13 additions & 0 deletions tests/tasks/tasks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,18 @@ describe('Tasks', function() {
/* When */ await utils().taskTest.new_task_is_created({ type: 'installation' });
/* Then */ await utils().system.server_returned_success();
});

it('should be possible to schedule an installation task', async function() {
/* When */ await utils().taskTest.new_task_is_created({ type: 'installation' });
/* When */ await utils().taskTest.task_is_scheduled();
/* Then */ await utils().system.server_returned_success();
});

it('should not be possible to close a scheduled installation task', async function() {
/* When */ await utils().taskTest.new_task_is_created({ type: 'installation' });
/* When */ await utils().taskTest.task_is_scheduled();
/* When */ await utils().taskTest.task_is_closed();
/* Then */ await utils().system.server_returned_error();
})
});
});
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./dist/@matilda", /* Redirect output structure to the directory. */
"rootDir": "src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
Expand All @@ -22,8 +22,8 @@

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
"noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */
"strictNullChecks": false, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
"strictPropertyInitialization": false, /* Enable strict checking of property initialization in classes. */
Expand Down

0 comments on commit ec7e98c

Please sign in to comment.