mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 13:20:31 +00:00
WIP Financial accounting module.
This commit is contained in:
@@ -1,42 +1,93 @@
|
||||
import { create, expect, request } from '~/testInit';
|
||||
import {
|
||||
create,
|
||||
expect,
|
||||
request,
|
||||
login,
|
||||
} from '~/testInit';
|
||||
import knex from '@/database/knex';
|
||||
import ResourceField from '@/models/ResourceField';
|
||||
import e from 'express';
|
||||
import Fields from '../../src/http/controllers/Fields';
|
||||
|
||||
let loginRes;
|
||||
|
||||
describe('route: `/fields`', () => {
|
||||
beforeEach(async () => {
|
||||
loginRes = await login();
|
||||
});
|
||||
afterEach(() => {
|
||||
loginRes = null;
|
||||
});
|
||||
describe('POST: `/fields/:resource_id`', () => {
|
||||
it('Should `label` be required.', async () => {
|
||||
const resource = await create('resource');
|
||||
const res = await request().post(`/api/fields/resource/${resource.resource_id}`).send();
|
||||
it('Should response unauthorized in case the user was not authorized.', async () => {
|
||||
const res = await request()
|
||||
.post('/api/fields/resource/items')
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(422);
|
||||
expect(res.body.code).equals('VALIDATION_ERROR');
|
||||
expect(res.body.code).equals('validation_error');
|
||||
});
|
||||
|
||||
const paramsErrors = res.body.errors.map((er) => er.param);
|
||||
expect(paramsErrors).to.include('label');
|
||||
it('Should response bad request in case resource name was not exist.', async () => {
|
||||
const res = await request()
|
||||
.post('/api/fields/resource/not_found_resource')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
label: 'Extra Field',
|
||||
data_type: 'text',
|
||||
});
|
||||
|
||||
expect(res.status).equals(404);
|
||||
expect(res.body.errors).include.something.deep.equals({
|
||||
type: 'RESOURCE_NOT_FOUND', code: 100,
|
||||
});
|
||||
});
|
||||
|
||||
it('Should `label` be required.', async () => {
|
||||
const resource = await create('resource');
|
||||
const res = await request()
|
||||
.post(`/api/fields/resource/${resource.resource_name}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(422);
|
||||
expect(res.body.code).equals('validation_error');
|
||||
expect(res.body.errors).include.something.deep.equals({
|
||||
msg: 'Invalid value', param: 'label', location: 'body',
|
||||
});
|
||||
});
|
||||
|
||||
it('Should `data_type` be required.', async () => {
|
||||
const resource = await create('resource');
|
||||
const res = await request().post(`/api/fields/resource/${resource.resource_id}`);
|
||||
const res = await request()
|
||||
.post(`/api/fields/resource/${resource.resource_id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
label: 'Field label',
|
||||
});
|
||||
|
||||
expect(res.status).equals(422);
|
||||
expect(res.body.code).equals('VALIDATION_ERROR');
|
||||
|
||||
const paramsErrors = res.body.errors.map((er) => er.param);
|
||||
expect(paramsErrors).to.include('data_type');
|
||||
expect(res.body.code).equals('validation_error');
|
||||
expect(res.body.errors).include.something.deep.equals({
|
||||
msg: 'Invalid value', param: 'data_type', location: 'body',
|
||||
});
|
||||
});
|
||||
|
||||
it('Should `data_type` be one in the list.', async () => {
|
||||
const resource = await create('resource');
|
||||
const res = await request().post(`/api/fields/resource/${resource.resource_id}`).send({
|
||||
label: 'Field label',
|
||||
data_type: 'invalid_type',
|
||||
});
|
||||
const res = await request()
|
||||
.post(`/api/fields/resource/${resource.resource_id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
label: 'Field label',
|
||||
data_type: 'invalid_type',
|
||||
});
|
||||
|
||||
expect(res.status).equals(422);
|
||||
expect(res.body.code).equals('VALIDATION_ERROR');
|
||||
|
||||
const paramsErrors = res.body.errors.map((er) => er.param);
|
||||
expect(paramsErrors).to.include('data_type');
|
||||
expect(res.body.code).equals('validation_error');
|
||||
expect(res.body.errors).include.something.deep.equals({
|
||||
msg: 'Invalid value', param: 'data_type', location: 'body', value: 'invalid_type',
|
||||
});
|
||||
});
|
||||
|
||||
it('Should `value` be boolean valid value in case `data_type` was `boolean`.', () => {
|
||||
@@ -63,13 +114,16 @@ describe('route: `/fields`', () => {
|
||||
|
||||
});
|
||||
|
||||
it('Should response not found in case resource id was not exist.', async () => {
|
||||
const res = await request().post('/api/fields/resource/100').send({
|
||||
label: 'Field label',
|
||||
data_type: 'text',
|
||||
default: 'default value',
|
||||
help_text: 'help text',
|
||||
});
|
||||
it('Should response not found in case resource name was not exist.', async () => {
|
||||
const res = await request()
|
||||
.post('/api/fields/resource/resource_not_found')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
label: 'Field label',
|
||||
data_type: 'text',
|
||||
default: 'default value',
|
||||
help_text: 'help text',
|
||||
});
|
||||
|
||||
expect(res.status).equals(404);
|
||||
expect(res.body.errors).include.something.that.deep.equals({
|
||||
@@ -77,63 +131,81 @@ describe('route: `/fields`', () => {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('Should response success with valid data.', async () => {
|
||||
const resource = await create('resource');
|
||||
const res = await request().post(`/api/fields/resource/${resource.id}`).send({
|
||||
label: 'Field label',
|
||||
data_type: 'text',
|
||||
default: 'default value',
|
||||
help_text: 'help text',
|
||||
});
|
||||
const res = await request()
|
||||
.post(`/api/fields/resource/${resource.name}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
label: 'Field label',
|
||||
data_type: 'text',
|
||||
default: 'default value',
|
||||
help_text: 'help text',
|
||||
});
|
||||
|
||||
expect(res.status).equals(200);
|
||||
});
|
||||
|
||||
it('Should store the given field details to the storage.', async () => {
|
||||
const resource = await create('resource');
|
||||
await request().post(`/api/fields/resource/${resource.id}`).send({
|
||||
label: 'Field label',
|
||||
data_type: 'text',
|
||||
default: 'default value',
|
||||
help_text: 'help text',
|
||||
options: ['option 1', 'option 2'],
|
||||
});
|
||||
const res = await request()
|
||||
.post(`/api/fields/resource/${resource.name}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
label: 'Field label',
|
||||
data_type: 'text',
|
||||
default: 'default value',
|
||||
help_text: 'help text',
|
||||
options: ['option 1', 'option 2'],
|
||||
});
|
||||
|
||||
const foundField = await knex('resource_fields').first();
|
||||
const foundField = await ResourceField.query().findById(res.body.id);
|
||||
|
||||
expect(foundField.label_name).equals('Field label');
|
||||
expect(foundField.data_type).equals('text');
|
||||
expect(foundField.labelName).equals('Field label');
|
||||
expect(foundField.dataType).equals('text');
|
||||
expect(foundField.default).equals('default value');
|
||||
expect(foundField.help_text).equals('help text');
|
||||
expect(foundField.options).equals.deep([
|
||||
{ key: 1, value: 'option 1' },
|
||||
{ key: 2, value: 'option 2' },
|
||||
]);
|
||||
expect(foundField.helpText).equals('help text');
|
||||
expect(foundField.options.length).equals(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST: `/fields/:field_id`', () => {
|
||||
it('Should `label` be required.', async () => {
|
||||
it('Should response unauthorized in case the user was not authorized.', async () => {
|
||||
const field = await create('resource_field');
|
||||
const res = await request().post(`/api/fields/${field.id}`).send();
|
||||
const res = await request()
|
||||
.post(`/api/fields/${field.id}`)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(422);
|
||||
expect(res.body.code).equals('VALIDATION_ERROR');
|
||||
expect(res.body.code).equals('validation_error');
|
||||
});
|
||||
|
||||
const paramsErrors = res.body.errors.map((er) => er.param);
|
||||
expect(paramsErrors).to.include('label');
|
||||
it('Should `label` be required.', async () => {
|
||||
const field = await create('resource_field');
|
||||
const res = await request()
|
||||
.post(`/api/fields/${field.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(422);
|
||||
expect(res.body.code).equals('validation_error');
|
||||
expect(res.body.errors).include.something.deep.equals({
|
||||
msg: 'Invalid value', param: 'label', location: 'body',
|
||||
})
|
||||
});
|
||||
|
||||
it('Should `data_type` be required.', async () => {
|
||||
const field = await create('resource_field');
|
||||
const res = await request().post(`/api/fields/${field.id}`);
|
||||
const res = await request()
|
||||
.post(`/api/fields/${field.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(422);
|
||||
expect(res.body.code).equals('VALIDATION_ERROR');
|
||||
|
||||
const paramsErrors = res.body.errors.map((er) => er.param);
|
||||
expect(paramsErrors).to.include('data_type');
|
||||
expect(res.body.code).equals('validation_error');
|
||||
expect(res.body.errors).include.something.deep.equals({
|
||||
msg: 'Invalid value', param: 'data_type', location: 'body',
|
||||
});
|
||||
});
|
||||
|
||||
it('Should `data_type` be one in the list.', async () => {
|
||||
@@ -144,28 +216,78 @@ describe('route: `/fields`', () => {
|
||||
});
|
||||
|
||||
expect(res.status).equals(422);
|
||||
expect(res.body.code).equals('VALIDATION_ERROR');
|
||||
|
||||
const paramsErrors = res.body.errors.map((er) => er.param);
|
||||
expect(paramsErrors).to.include('data_type');
|
||||
expect(res.body.code).equals('validation_error');
|
||||
expect(res.body.errors).include.something.deep.equals({
|
||||
value: 'invalid_type',
|
||||
msg: 'Invalid value',
|
||||
param: 'data_type',
|
||||
location: 'body',
|
||||
});
|
||||
});
|
||||
|
||||
it('Should response not found in case resource id was not exist.', async () => {
|
||||
const res = await request().post('/api/fields/100').send({
|
||||
label: 'Field label',
|
||||
data_type: 'text',
|
||||
default: 'default value',
|
||||
help_text: 'help text',
|
||||
});
|
||||
it('Should response not found in case resource field id was not exist.', async () => {
|
||||
const res = await request()
|
||||
.post('/api/fields/100')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
label: 'Field label',
|
||||
data_type: 'text',
|
||||
default: 'default value',
|
||||
help_text: 'help text',
|
||||
});
|
||||
|
||||
expect(res.status).equals(404);
|
||||
expect(res.body.errors).include.something.that.deep.equals({
|
||||
type: 'FIELD_NOT_FOUND', code: 100,
|
||||
});
|
||||
});
|
||||
|
||||
it('Should update details of the given resource field.', async () => {
|
||||
const field = await create('resource_field');
|
||||
const res = await request()
|
||||
.post(`/api/fields/${field.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
label: 'Field label',
|
||||
data_type: 'text',
|
||||
default: 'default value',
|
||||
help_text: 'help text',
|
||||
});
|
||||
|
||||
it('Should save the new options of the field in the storage.', async () => {
|
||||
const updateField = await ResourceField.query().findById(res.body.id);
|
||||
|
||||
expect(updateField.labelName).equals('Field label');
|
||||
expect(updateField.dataType).equals('text');
|
||||
expect(updateField.default).equals('default value');
|
||||
expect(updateField.helpText).equals('help text');
|
||||
});
|
||||
|
||||
it('Should save the new options of the field with exist ones in the storage.', async () => {
|
||||
const field = await create('resource_field', {
|
||||
options: JSON.stringify([{ key: 1, value: 'Option 1' }]),
|
||||
});
|
||||
const res = await request()
|
||||
.post(`/api/fields/${field.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
label: 'Field label',
|
||||
data_type: 'text',
|
||||
default: 'default value',
|
||||
help_text: 'help text',
|
||||
options: [
|
||||
{ key: 1, value: 'Value Key 1' },
|
||||
{ key: 2, value: 'Value Key 2' },
|
||||
],
|
||||
});
|
||||
|
||||
const updateField = await ResourceField.query().findById(res.body.id);
|
||||
|
||||
expect(updateField.options.length).equals(2);
|
||||
expect(updateField.options[0].key).equals(1);
|
||||
expect(updateField.options[1].key).equals(2);
|
||||
|
||||
expect(updateField.options[0].value).equals('Value Key 1');
|
||||
expect(updateField.options[1].value).equals('Value Key 2');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -178,10 +300,12 @@ describe('route: `/fields`', () => {
|
||||
|
||||
it('Should change status activation of the given field.', async () => {
|
||||
const field = await create('resource_field');
|
||||
await request().post(`/api/fields/status/${field.id}`).send({
|
||||
active: false,
|
||||
});
|
||||
|
||||
const res = await request()
|
||||
.post(`/api/fields/status/${field.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
active: false,
|
||||
});
|
||||
const storedField = await knex('resource_fields').where('id', field.id).first();
|
||||
expect(storedField.active).equals(0);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user