Custom fields feature.

This commit is contained in:
Ahmed Bouhuolia
2019-09-13 20:24:09 +02:00
parent cba17739d6
commit ed4d37c8fb
64 changed files with 2307 additions and 121 deletions

View File

@@ -0,0 +1,25 @@
import { create, expect } from '~/testInit';
import Resource from '@/models/Resource';
import '@/models/View';
import '@/models/ResourceField';
describe('Model: Resource', () => {
it('Resource model may has many associated views.', async () => {
const view = await create('view');
await create('view', { resource_id: view.resource_id });
const resourceModel = await Resource.where('id', view.resource_id).fetch();
const resourceViews = await resourceModel.views().fetch();
expect(resourceViews).to.have.lengthOf(2);
});
it('Resource model may has many fields.', async () => {
const resourceField = await create('resource_field');
const resourceModel = await Resource.where('id', resourceField.resource_id).fetch();
const resourceFields = await resourceModel.fields().fetch();
expect(resourceFields).to.have.lengthOf(1);
});
});

View File

@@ -0,0 +1,18 @@
import { create, expect } from '~/testInit';
import Resource from '@/models/Resource';
import ResourceField from '@/models/ResourceField';
import '@/models/View';
describe('Model: ResourceField', () => {
it('Resource field model may belongs to associated resource.', async () => {
const resourceField = await create('resource_field');
const resourceFieldModel = await ResourceField.where('id', resourceField.id).fetch();
const resourceModel = resourceFieldModel.resource().fetch();
const foundResource = await Resource.where('id', resourceField.resource_id).fetch();
expect(resourceModel.attributes.id).equals(foundResource.id);
expect(resourceModel.attributes.name).equals(foundResource.name);
});
});

View File

@@ -0,0 +1,42 @@
import { create, expect } from '~/testInit';
import View from '@/models/View';
import Resource from '@/models/Resource';
import '@/models/ResourceField';
import '@/models/ViewRole';
describe('Model: View', () => {
it('View model may has many associated resource.', async () => {
const view = await create('view');
const viewModel = await View.where('id', view.id).fetch();
const viewResource = await viewModel.resource().fetch();
const foundResource = await Resource.where('id', view.resource_id).fetch();
expect(viewResource.attributes.id).equals(foundResource.id);
expect(viewResource.attributes.name).equals(foundResource.attributes.name);
});
it('View model may has many associated view roles.', async () => {
const view = await create('view');
await create('view_role', { view_id: view.id });
await create('view_role', { view_id: view.id });
const viewModel = await View.where('id', view.id).fetch();
const viewRoles = await viewModel.viewRoles().fetch();
expect(viewRoles).to.have.lengthOf(2);
});
it('View model may has many associated view columns', async () => {
const view = await create('view');
await create('view_has_columns', { view_id: view.id });
await create('view_has_columns', { view_id: view.id });
const viewModel = await View.where('id', view.id).fetch();
const viewColumns = await viewModel.columns().fetch();
expect(viewColumns).to.have.lengthOf(2);
});
});

View File

@@ -0,0 +1,215 @@
import { create, expect, request } from '~/testInit';
import knex from '@/database/knex';
describe('route: `/fields`', () => {
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();
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('label');
});
it('Should `data_type` be required.', async () => {
const resource = await create('resource');
const res = await request().post(`/api/fields/resource/${resource.resource_id}`);
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');
});
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',
});
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');
});
it('Should `value` be boolean valid value in case `data_type` was `boolean`.', () => {
});
it('Should `value` be URL valid value in case `data_type` was `url`.', () => {
});
it('Should `value` be integer valid value in case `data_type` was `number`.', () => {
});
it('Should `value` be decimal valid value in case `data_type` was `decimal`.', () => {
});
it('Should `value` be email valid value in case `data_type` was `email`.', () => {
});
it('Should `value` be boolean valid value in case `data_type` was `checkbox`.', () => {
});
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',
});
expect(res.status).equals(404);
expect(res.body.errors).include.something.that.deep.equals({
type: 'RESOURCE_NOT_FOUND', code: 100,
});
});
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',
});
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 foundField = await knex('resource_fields').first();
expect(foundField.label_name).equals('Field label');
expect(foundField.data_type).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' },
]);
});
});
describe('POST: `/fields/:field_id`', () => {
it('Should `label` be required.', async () => {
const field = await create('resource_field');
const res = await request().post(`/api/fields/${field.id}`).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('label');
});
it('Should `data_type` be required.', async () => {
const field = await create('resource_field');
const res = await request().post(`/api/fields/${field.id}`);
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');
});
it('Should `data_type` be one in the list.', async () => {
const field = await create('resource_field');
const res = await request().post(`/api/fields/${field.id}`).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');
});
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',
});
expect(res.status).equals(404);
expect(res.body.errors).include.something.that.deep.equals({
type: 'FIELD_NOT_FOUND', code: 100,
});
});
it('Should save the new options of the field in the storage.', async () => {
});
});
describe('POST: `/fields/status/:field_id`', () => {
it('Should response not found in case field id was not exist.', async () => {
const res = await request().post('/api/fields/status/100').send();
expect(res.status).equals(404);
});
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 storedField = await knex('resource_fields').where('id', field.id).first();
expect(storedField.active).equals(0);
});
});
describe('DELETE: `/fields/:field_id`', () => {
it('Should response not found in case field id was not exist.', async () => {
const res = await request().delete('/api/fields/100').send();
expect(res.status).equals(404);
});
it('Should not delete predefined field.', async () => {
const field = await create('resource_field', { predefined: true });
const res = await request().delete(`/api/fields/${field.id}`).send();
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equals({
type: 'PREDEFINED_FIELD', code: 100,
});
});
it('Should delete the given field from the storage.', async () => {
const field = await create('resource_field');
const res = await request().delete(`/api/fields/${field.id}`).send();
expect(res.status).equals(200);
});
});
});

View File

@@ -1,7 +1,7 @@
import { request, expect, create } from '~/testInit';
import knex from '@/database/knex';
describe.only('routes: `/roles/`', () => {
describe('routes: `/roles/`', () => {
describe('POST: `/roles/`', () => {
it('Should `name` be required.', async () => {
const res = await request().post('/api/roles').send();
@@ -237,7 +237,7 @@ describe.only('routes: `/roles/`', () => {
expect(storedResources).to.have.lengthOf(1);
});
it.only('Should save the submit permissions in the storage in case was not exist.', async () => {
it('Should save the submit permissions in the storage in case was not exist.', async () => {
const role = await create('role');
await request().post(`/api/roles/${role.id}`).send({
name: 'Role Name',

View File

@@ -0,0 +1,310 @@
import { request, expect, create } from '~/testInit';
import View from '@/models/View';
import ViewRole from '@/models/ViewRole';
import '@/models/ResourceField';
describe('routes: `/views`', () => {
describe('POST: `/views/:resource_id`', () => {
it('Should `label` be required.', async () => {
const resource = await create('resource');
const res = await request().post(`/api/views/resource/${resource.id}`);
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
const paramsErrors = res.body.errors.map((error) => error.param);
expect(paramsErrors).to.include('label');
});
it('Should columns be minimum limited', async () => {
const resource = await create('resource');
const res = await request().post(`/api/views/resource/${resource.id}`, {
label: 'View Label',
columns: [],
});
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
const paramsErrors = res.body.errors.map((error) => error.param);
expect(paramsErrors).to.include('columns');
});
it('Should columns be array.', async () => {
const resource = await create('resource');
const res = await request().post(`/api/views/resource/${resource.id}`, {
label: 'View Label',
columns: 'not_array',
});
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
const paramsErrors = res.body.errors.map((error) => error.param);
expect(paramsErrors).to.include('columns');
});
it('Should `roles.*.field` be required.', async () => {
const resource = await create('resource');
const res = await request().post(`/api/views/resource/${resource.id}`).send({
label: 'View Label',
roles: [{}],
});
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
const paramsErrors = res.body.errors.map((error) => error.param);
expect(paramsErrors).to.include('roles[0].field');
});
it('Should `roles.*.comparator` be valid.', async () => {
const resource = await create('resource');
const res = await request().post(`/api/views/resource/${resource.id}`).send({
label: 'View Label',
roles: [{}],
});
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
const paramsErrors = res.body.errors.map((error) => error.param);
expect(paramsErrors).to.include('roles[0].comparator');
});
it('Should `roles.*.index` be number as integer.', async () => {
const resource = await create('resource');
const res = await request().post(`/api/views/resource/${resource.id}`).send({
label: 'View Label',
roles: [{ index: 'not_numeric' }],
});
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
const paramsErrors = res.body.errors.map((error) => error.param);
expect(paramsErrors).to.include('roles[0].index');
});
it('Should response not found in case resource was not exist.', async () => {
const res = await request().post('/api/views/resource/100').send({
label: 'View Label',
columns: ['amount', 'thumbnail', 'status'],
roles: [{
index: 1,
field: 'amount',
comparator: 'equals',
value: '100',
}],
});
expect(res.status).equals(404);
});
it('Should response the roles fields not exist in case role field was not exist.', async () => {
const resource = await create('resource');
await create('resource_field', {
resource_id: resource.id,
label_name: 'Amount',
});
const res = await request().post(`/api/views/resource/${resource.id}`).send({
label: 'View Label',
columns: ['amount', 'thumbnail', 'status'],
roles: [{
index: 1,
field: 'price',
comparator: 'equals',
value: '100',
}],
});
expect(res.body.errors).include.something.that.deep.equals({
type: 'RESOURCE_FIELDS_NOT_EXIST',
code: 100,
fields: ['price'],
});
});
it('Should response the columns not exists in case column was not exist.', async () => {
const resource = await create('resource');
await create('resource_field', {
resource_id: resource.id,
label_name: 'Amount',
});
const res = await request().post(`/api/views/resource/${resource.id}`).send({
label: 'View Label',
columns: ['amount', 'thumbnail', 'status'],
roles: [{
index: 1,
field: 'price',
comparator: 'equals',
value: '100',
}],
});
expect(res.body.errors).include.something.that.deep.equals({
type: 'COLUMNS_NOT_EXIST',
code: 200,
fields: ['thumbnail', 'status'],
});
});
it('Should save the given details with associated roles and columns.', async () => {
});
});
describe.only('POST: `/views/:view_id`', () => {
it('Should `label` be required.', async () => {
const view = await create('view');
const res = await request().post(`/api/views/${view.id}`);
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
const paramsErrors = res.body.errors.map((error) => error.param);
expect(paramsErrors).to.include('label');
});
it('Should columns be minimum limited', async () => {
const view = await create('view');
const res = await request().post(`/api/views/${view.id}`, {
label: 'View Label',
columns: [],
});
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
const paramsErrors = res.body.errors.map((error) => error.param);
expect(paramsErrors).to.include('columns');
});
it('Should columns be array.', async () => {
const view = await create('view');
const res = await request().post(`/api/views/${view.id}`, {
label: 'View Label',
columns: 'not_array',
});
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
const paramsErrors = res.body.errors.map((error) => error.param);
expect(paramsErrors).to.include('columns');
});
it('Should `roles.*.field` be required.', async () => {
const view = await create('view');
const res = await request().post(`/api/views/${view.id}`).send({
label: 'View Label',
roles: [{}],
});
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
const paramsErrors = res.body.errors.map((error) => error.param);
expect(paramsErrors).to.include('roles[0].field');
});
it('Should `roles.*.comparator` be valid.', async () => {
const view = await create('view');
const res = await request().post(`/api/views/${view.id}`).send({
label: 'View Label',
roles: [{}],
});
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
const paramsErrors = res.body.errors.map((error) => error.param);
expect(paramsErrors).to.include('roles[0].comparator');
});
it('Should `roles.*.index` be number as integer.', async () => {
const view = await create('view');
const res = await request().post(`/api/views/${view.id}`).send({
label: 'View Label',
roles: [{ index: 'not_numeric' }],
});
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
const paramsErrors = res.body.errors.map((error) => error.param);
expect(paramsErrors).to.include('roles[0].index');
});
it('Should response not found in case resource was not exist.', async () => {
const res = await request().post('/api/views/100').send({
label: 'View Label',
columns: ['amount', 'thumbnail', 'status'],
roles: [{
index: 1,
field: 'amount',
comparator: 'equals',
value: '100',
}],
});
expect(res.status).equals(404);
});
it.only('Should response the roles fields not exist in case role field was not exist.', async () => {
const view = await create('view');
await create('resource_field', {
resource_id: view.resource_id,
label_name: 'Amount',
});
const res = await request().post(`/api/views/${view.id}`).send({
label: 'View Label',
columns: ['amount', 'thumbnail', 'status'],
roles: [{
index: 1,
field: 'price',
comparator: 'equals',
value: '100',
}],
});
expect(res.body.errors).include.something.that.deep.equals({
type: 'RESOURCE_FIELDS_NOT_EXIST',
code: 100,
fields: ['price'],
});
});
});
describe('DELETE: `/views/:resource_id`', () => {
it('Should not delete predefined view.', async () => {
const view = await create('view', { predefined: true });
const res = await request().delete(`/api/views/${view.id}`).send();
expect(res.status).equals(400);
});
it('Should response not found in case view was not exist.', async () => {
const res = await request().delete('/api/views/100').send();
expect(res.status).equals(404);
expect(res.body.errors).include.something.that.deep.equals({
type: 'VIEW_NOT_FOUND', code: 100,
});
});
it('Should delete the given view and associated view columns and roles.', async () => {
const view = await create('view', { predefined: false });
await create('view_role', { view_id: view.id });
await create('view_has_columns', { view_id: view.id });
await request().delete(`/api/views/${view.id}`).send();
const foundViews = await View.where('id', view.id).fetchAll();
const foundViewRoles = await ViewRole.where('view_id', view.id).fetchAll();
expect(foundViews).to.have.lengthOf(0);
expect(foundViewRoles).to.have.lengthOf(0);
});
});
});