mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 14:20:31 +00:00
WIP
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { request, expect, create, login } from '~/testInit';
|
||||
import knex from '@/database/knex';
|
||||
import Account from '@/models/Account';
|
||||
|
||||
let loginRes;
|
||||
@@ -194,16 +193,112 @@ describe('routes: /accounts/', () => {
|
||||
});
|
||||
|
||||
describe('GET: `/accounts`', () => {
|
||||
it('Should retrieve accounts resource not found.', async () => {
|
||||
const res = await request()
|
||||
.get('/api/accounts')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(400);
|
||||
expect(res.body.errors).include.something.that.deep.equals({
|
||||
type: 'ACCOUNTS_RESOURCE_NOT_FOUND', code: 200,
|
||||
});
|
||||
});
|
||||
|
||||
it('Should retrieve chart of accounts', async () => {
|
||||
await create('resource', { name: 'accounts' });
|
||||
const account = await create('account');
|
||||
const account2 = await create('account', { parent_account_id: account.id });
|
||||
|
||||
const res = await request()
|
||||
.get('/api/accounts')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send()
|
||||
.send();
|
||||
|
||||
console.log(res.body);
|
||||
expect(res.status).equals(200);
|
||||
expect(res.body.accounts.length).equals(1);
|
||||
});
|
||||
|
||||
it('Should retrieve accounts based on view roles conditionals of the custom view.', async () => {
|
||||
const resource = await create('resource', { name: 'accounts' });
|
||||
|
||||
const accountTypeField = await create('resource_field', {
|
||||
label_name: 'Account type',
|
||||
column_key: 'account_type',
|
||||
resource_id: resource.id,
|
||||
active: true,
|
||||
predefined: true,
|
||||
});
|
||||
const accountsView = await create('view', {
|
||||
name: 'Accounts View',
|
||||
resource_id: resource.id,
|
||||
roles_logic_expression: '1',
|
||||
});
|
||||
const accountsViewRole = await create('view_role', {
|
||||
view_id: accountsView.id,
|
||||
index: 1,
|
||||
field_id: accountTypeField.id,
|
||||
value: '2',
|
||||
comparator: 'equals',
|
||||
});
|
||||
|
||||
await create('account');
|
||||
await create('account');
|
||||
await create('account');
|
||||
|
||||
const res = await request()
|
||||
.get('/api/accounts')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.query({ custom_view_id: accountsView.id })
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(200);
|
||||
res.body.accounts.forEach((account) => {
|
||||
expect(account).to.deep.include({ accountTypeId: 2 });
|
||||
});
|
||||
});
|
||||
|
||||
it('Should retrieve accounts and child accounts in nested set graph.', async () => {
|
||||
const resource = await create('resource', { name: 'accounts' });
|
||||
|
||||
const account1 = await create('account');
|
||||
const account2 = await create('account', { parent_account_id: account1.id });
|
||||
const account3 = await create('account', { parent_account_id: account2.id });
|
||||
|
||||
const res = await request()
|
||||
.get('/api/accounts')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.query({ display_type: 'tree' })
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(200);
|
||||
|
||||
expect(res.body.accounts[0].id).equals(account1.id);
|
||||
expect(res.body.accounts[0].children[0].id).equals(account2.id);
|
||||
expect(res.body.accounts[0].children[0].children[0].id).equals(account3.id);
|
||||
});
|
||||
|
||||
it('Should retrieve accounts and child accounts in flat display with dashed accounts name.', async () => {
|
||||
const resource = await create('resource', { name: 'accounts' });
|
||||
|
||||
const account1 = await create('account');
|
||||
const account2 = await create('account', { parent_account_id: account1.id });
|
||||
const account3 = await create('account', { parent_account_id: account2.id });
|
||||
|
||||
const res = await request()
|
||||
.get('/api/accounts')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.query({ display_type: 'flat' })
|
||||
.send();
|
||||
|
||||
expect(res.body.accounts[0].id).equals(account1.id);
|
||||
expect(res.body.accounts[0].name).equals(account1.name);
|
||||
|
||||
expect(res.body.accounts[1].id).equals(account2.id);
|
||||
expect(res.body.accounts[1].name).equals(`${account1.name} ― ${account2.name}`);
|
||||
|
||||
expect(res.body.accounts[2].id).equals(account3.id);
|
||||
expect(res.body.accounts[2].name).equals(`${account1.name} ― ${account2.name} ― ${account3.name}`);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -205,6 +205,7 @@ describe('routes: /expenses/', () => {
|
||||
it('Should response unauthorized in case user was not authorized.', () => {
|
||||
|
||||
});
|
||||
|
||||
it('Should response not found in case expense was not exist.', () => {
|
||||
|
||||
});
|
||||
|
||||
0
server/tests/routes/resources.test.js
Normal file
0
server/tests/routes/resources.test.js
Normal file
@@ -4,16 +4,53 @@ import {
|
||||
expect,
|
||||
create,
|
||||
make,
|
||||
login,
|
||||
} from '~/testInit';
|
||||
|
||||
describe('routes: `/routes`', () => {
|
||||
describe('POST: `/routes`', () => {
|
||||
it('Should create a new user if the user was not authorized.', () => {
|
||||
let loginRes;
|
||||
|
||||
describe('routes: `/routes`', () => {
|
||||
beforeEach(async () => {
|
||||
loginRes = await login();
|
||||
});
|
||||
afterEach(() => {
|
||||
loginRes = null;
|
||||
});
|
||||
|
||||
describe('GET: `/users`', () => {
|
||||
it('Should response unauthorized if the user was not authorized.', async () => {
|
||||
const res = await request().get('/api/users');
|
||||
|
||||
expect(res.status).equals(401);
|
||||
expect(res.body.message).equals('unauthorized');
|
||||
});
|
||||
|
||||
it('Should retrieve the stored users with pagination meta.', async () => {
|
||||
await create('user');
|
||||
|
||||
const res = await request()
|
||||
.get('/api/users')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
expect(res.body.users.results.length).equals(2);
|
||||
expect(res.body.users.total).equals(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST: `/users`', () => {
|
||||
it('Should create a new user if the user was not authorized.', async () => {
|
||||
const res = await request().post('/api/users');
|
||||
|
||||
expect(res.status).equals(401);
|
||||
expect(res.body.message).equals('unauthorized');
|
||||
});
|
||||
|
||||
it('Should `first_name` be required.', async () => {
|
||||
const res = await request().post('/api/users');
|
||||
const res = await request()
|
||||
.post('/api/users')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(422);
|
||||
|
||||
@@ -22,7 +59,10 @@ describe('routes: `/routes`', () => {
|
||||
});
|
||||
|
||||
it('Should `last_name` be required.', async () => {
|
||||
const res = await request().post('/api/users');
|
||||
const res = await request()
|
||||
.post('/api/users')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(422);
|
||||
|
||||
@@ -31,7 +71,10 @@ describe('routes: `/routes`', () => {
|
||||
});
|
||||
|
||||
it('Should `email` be required.', async () => {
|
||||
const res = await request().post('/api/users');
|
||||
const res = await request()
|
||||
.post('/api/users')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(422);
|
||||
|
||||
@@ -41,13 +84,16 @@ describe('routes: `/routes`', () => {
|
||||
|
||||
it('Should be `email` be valid format.', async () => {
|
||||
const user = make('user');
|
||||
const res = await request().post('/api/users').send({
|
||||
first_name: user.first_name,
|
||||
last_name: user.last_name,
|
||||
email: 'email',
|
||||
phone_number: user.phone_number,
|
||||
status: 1,
|
||||
});
|
||||
const res = await request()
|
||||
.post('/api/users')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
first_name: user.first_name,
|
||||
last_name: user.last_name,
|
||||
email: 'email',
|
||||
phone_number: user.phone_number,
|
||||
status: 1,
|
||||
});
|
||||
|
||||
expect(res.status).equals(422);
|
||||
|
||||
@@ -57,13 +103,16 @@ describe('routes: `/routes`', () => {
|
||||
|
||||
it('Should `phone_number` be valid format.', async () => {
|
||||
const user = make('user');
|
||||
const res = await request().post('/api/users').send({
|
||||
first_name: user.first_name,
|
||||
last_name: user.last_name,
|
||||
email: user.email,
|
||||
phone_number: 'phone_number',
|
||||
status: 1,
|
||||
});
|
||||
const res = await request()
|
||||
.post('/api/users')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
first_name: user.first_name,
|
||||
last_name: user.last_name,
|
||||
email: user.email,
|
||||
phone_number: 'phone_number',
|
||||
status: 1,
|
||||
});
|
||||
|
||||
expect(res.status).equals(422);
|
||||
|
||||
@@ -72,7 +121,10 @@ describe('routes: `/routes`', () => {
|
||||
});
|
||||
|
||||
it('Should `password` be required.', async () => {
|
||||
const res = await request().post('/api/users').send();
|
||||
const res = await request()
|
||||
.post('/api/users')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(422);
|
||||
|
||||
@@ -81,9 +133,12 @@ describe('routes: `/routes`', () => {
|
||||
});
|
||||
|
||||
it('Should password be equals confirm_password.', async () => {
|
||||
const res = await request().post('/api/users').send({
|
||||
password: '123123',
|
||||
});
|
||||
const res = await request()
|
||||
.post('/api/users')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
password: '123123',
|
||||
});
|
||||
|
||||
expect(res.status).equals(422);
|
||||
|
||||
@@ -92,9 +147,12 @@ describe('routes: `/routes`', () => {
|
||||
});
|
||||
|
||||
it('Should `status` be boolean', async () => {
|
||||
const res = await request().post('/api/users').send({
|
||||
status: 'not_boolean',
|
||||
});
|
||||
const res = await request()
|
||||
.post('/api/users')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
status: 'not_boolean',
|
||||
});
|
||||
|
||||
expect(res.status).equals(422);
|
||||
|
||||
@@ -104,15 +162,18 @@ describe('routes: `/routes`', () => {
|
||||
|
||||
it('Should response bad request in case email was already exist.', async () => {
|
||||
const user = await create('user');
|
||||
const res = await request().post('/api/users').send({
|
||||
first_name: user.first_name,
|
||||
last_name: user.last_name,
|
||||
email: user.email,
|
||||
phone_number: user.phone_number,
|
||||
password: '123123123',
|
||||
confirm_password: '123123123',
|
||||
status: 1,
|
||||
});
|
||||
const res = await request()
|
||||
.post('/api/users')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
first_name: user.firstName,
|
||||
last_name: user.lastName,
|
||||
email: user.email,
|
||||
phone_number: user.phoneNumber,
|
||||
password: '123123123',
|
||||
confirm_password: '123123123',
|
||||
status: 1,
|
||||
});
|
||||
|
||||
expect(res.status).equals(400);
|
||||
expect(res.body.errors).include.something.that.deep.equals({
|
||||
@@ -121,16 +182,20 @@ describe('routes: `/routes`', () => {
|
||||
});
|
||||
|
||||
it('Should response bad request in case phone number was already exist.', async () => {
|
||||
const user = await create('user');
|
||||
const res = await request().post('/api/users').send({
|
||||
first_name: user.first_name,
|
||||
last_name: user.last_name,
|
||||
email: user.email,
|
||||
phone_number: user.phone_number,
|
||||
password: user.password,
|
||||
confirm_password: user.password,
|
||||
status: 1,
|
||||
});
|
||||
const user = await create('user', { phone_number: '0927918381' });
|
||||
|
||||
const res = await request()
|
||||
.post('/api/users')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
first_name: user.firstName,
|
||||
last_name: user.lastName,
|
||||
email: user.email,
|
||||
phone_number: '0927918381',
|
||||
password: user.password,
|
||||
confirm_password: user.password,
|
||||
status: 1,
|
||||
});
|
||||
|
||||
expect(res.status).equals(400);
|
||||
expect(res.body.errors).include.something.that.deep.equals({
|
||||
@@ -139,29 +204,40 @@ describe('routes: `/routes`', () => {
|
||||
});
|
||||
|
||||
it('Should response success with correct data type.', async () => {
|
||||
const user = await make('user');
|
||||
const res = await request().post('/api/users').send({
|
||||
first_name: user.first_name,
|
||||
last_name: user.last_name,
|
||||
email: user.email,
|
||||
phone_number: user.phone_number,
|
||||
password: user.password,
|
||||
confirm_password: user.password,
|
||||
status: 1,
|
||||
});
|
||||
const user = await make('user', { phone_number: '0920000000' });
|
||||
const res = await request()
|
||||
.post('/api/users')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
first_name: user.firstName,
|
||||
last_name: user.lastName,
|
||||
email: user.email,
|
||||
phone_number: '0920000000',
|
||||
password: user.password,
|
||||
confirm_password: user.password,
|
||||
status: 1,
|
||||
});
|
||||
|
||||
expect(res.status).equals(200);
|
||||
expect(res.body.user.id).equals(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST: `/users/:id`', () => {
|
||||
it('Should create a new user if the user was not authorized.', () => {
|
||||
it('Should create a new user if the user was not authorized.', async () => {
|
||||
const user = await create('user');
|
||||
const res = await request().post(`/api/users/${user.id}`);
|
||||
|
||||
expect(res.status).equals(401);
|
||||
expect(res.body.message).equals('unauthorized');
|
||||
});
|
||||
|
||||
it('Should `first_name` be required.', async () => {
|
||||
const user = await create('user');
|
||||
const res = await request().post(`/api/users/${user.id}`);
|
||||
const res = await request()
|
||||
.post(`/api/users/${user.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(422);
|
||||
|
||||
@@ -171,7 +247,10 @@ describe('routes: `/routes`', () => {
|
||||
|
||||
it('Should `last_name` be required.', async () => {
|
||||
const user = await create('user');
|
||||
const res = await request().post(`/api/users/${user.id}`);
|
||||
const res = await request()
|
||||
.post(`/api/users/${user.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(422);
|
||||
|
||||
@@ -181,7 +260,10 @@ describe('routes: `/routes`', () => {
|
||||
|
||||
it('Should `email` be required.', async () => {
|
||||
const user = await create('user');
|
||||
const res = await request().post(`/api/users/${user.id}`);
|
||||
const res = await request()
|
||||
.post(`/api/users/${user.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(422);
|
||||
|
||||
@@ -191,13 +273,16 @@ describe('routes: `/routes`', () => {
|
||||
|
||||
it('Should be `email` be valid format.', async () => {
|
||||
const user = await create('user');
|
||||
const res = await request().post(`/api/users/${user.id}`).send({
|
||||
first_name: user.first_name,
|
||||
last_name: user.last_name,
|
||||
email: 'email',
|
||||
phone_number: user.phone_number,
|
||||
status: 1,
|
||||
});
|
||||
const res = await request()
|
||||
.post(`/api/users/${user.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
first_name: user.first_name,
|
||||
last_name: user.last_name,
|
||||
email: 'email',
|
||||
phone_number: user.phone_number,
|
||||
status: 1,
|
||||
});
|
||||
|
||||
expect(res.status).equals(422);
|
||||
|
||||
@@ -207,13 +292,16 @@ describe('routes: `/routes`', () => {
|
||||
|
||||
it('Should `phone_number` be valid format.', async () => {
|
||||
const user = create('user');
|
||||
const res = await request().post(`/api/users/${user.id}`).send({
|
||||
first_name: user.first_name,
|
||||
last_name: user.last_name,
|
||||
email: user.email,
|
||||
phone_number: 'phone_number',
|
||||
status: 1,
|
||||
});
|
||||
const res = await request()
|
||||
.post(`/api/users/${user.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
first_name: user.first_name,
|
||||
last_name: user.last_name,
|
||||
email: user.email,
|
||||
phone_number: 'phone_number',
|
||||
status: 1,
|
||||
});
|
||||
|
||||
expect(res.status).equals(422);
|
||||
|
||||
@@ -223,7 +311,10 @@ describe('routes: `/routes`', () => {
|
||||
|
||||
it('Should `password` be required.', async () => {
|
||||
const user = create('user');
|
||||
const res = await request().post(`/api/users/${user.id}`).send();
|
||||
const res = await request()
|
||||
.post(`/api/users/${user.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(422);
|
||||
|
||||
@@ -233,9 +324,12 @@ describe('routes: `/routes`', () => {
|
||||
|
||||
it('Should password be equals confirm_password.', async () => {
|
||||
const user = create('user');
|
||||
const res = await request().post(`/api/users/${user.id}`).send({
|
||||
password: '123123',
|
||||
});
|
||||
const res = await request()
|
||||
.post(`/api/users/${user.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
password: '123123',
|
||||
});
|
||||
|
||||
expect(res.status).equals(422);
|
||||
|
||||
@@ -245,9 +339,12 @@ describe('routes: `/routes`', () => {
|
||||
|
||||
it('Should `status` be boolean', async () => {
|
||||
const user = create('user');
|
||||
const res = await request().post(`/api/users/${user.id}`).send({
|
||||
status: 'not_boolean',
|
||||
});
|
||||
const res = await request()
|
||||
.post(`/api/users/${user.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
status: 'not_boolean',
|
||||
});
|
||||
|
||||
expect(res.status).equals(422);
|
||||
|
||||
@@ -262,14 +359,20 @@ describe('routes: `/routes`', () => {
|
||||
});
|
||||
|
||||
it('Should response not found if the user was not exist.', async () => {
|
||||
const res = await request().get('/api/users/10').send();
|
||||
const res = await request()
|
||||
.get('/api/users/10')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(404);
|
||||
});
|
||||
|
||||
it('Should response success if the user was exist.', async () => {
|
||||
const user = await create('user');
|
||||
const res = await request().get(`/api/users/${user.id}`).send();
|
||||
const res = await request()
|
||||
.get(`/api/users/${user.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(200);
|
||||
});
|
||||
@@ -281,24 +384,35 @@ describe('routes: `/routes`', () => {
|
||||
});
|
||||
|
||||
it('Should response not found if the user was not exist.', async () => {
|
||||
const res = await request().delete('/api/users/10').send();
|
||||
const res = await request()
|
||||
.delete('/api/users/10')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(404);
|
||||
expect(res.body.errors).include.something.that.deep.equals({
|
||||
type: 'USER_NOT_FOUND', code: 100,
|
||||
});
|
||||
});
|
||||
|
||||
it('Should response success if the user was exist.', async () => {
|
||||
const user = await create('user');
|
||||
const res = await request().delete(`/api/users/${user.id}`).send();
|
||||
const res = await request()
|
||||
.delete(`/api/users/${user.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(200);
|
||||
expect(res.status).equals(200);
|
||||
});
|
||||
|
||||
it('Should delete the give user from the storage.', async () => {
|
||||
const user = await create('user');
|
||||
await request().delete(`/api/users/${user.id}`).send();
|
||||
await request()
|
||||
.delete(`/api/users/${user.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
const storedUsers = await knex('users').where('id', user.id);
|
||||
|
||||
expect(storedUsers).to.have.lengthOf(0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -11,18 +11,124 @@ import ViewColumn from '../../src/models/ViewColumn';
|
||||
|
||||
let loginRes;
|
||||
|
||||
describe('routes: `/views`', () => {
|
||||
describe.only('routes: `/views`', () => {
|
||||
beforeEach(async () => {
|
||||
loginRes = await login();
|
||||
});
|
||||
afterEach(() => {
|
||||
loginRes = null;
|
||||
});
|
||||
|
||||
describe('GET: `/views`', () => {
|
||||
it('Should response unauthorized in case the user was not authorized.', async () => {
|
||||
const res = await request().get('/api/views');
|
||||
|
||||
expect(res.status).equals(401);
|
||||
expect(res.body.message).equals('unauthorized');
|
||||
});
|
||||
|
||||
it('Should retrieve all views of the given resource name.', async () => {
|
||||
const resource = await create('resource', { name: 'resource_name' });
|
||||
const resourceFields = await create('view', {
|
||||
name: 'Resource View',
|
||||
resource_id: resource.id,
|
||||
roles_logic_expression: '',
|
||||
});
|
||||
|
||||
const res = await request()
|
||||
.get('/api/views')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.query({ resource_name: 'resource_name' })
|
||||
.send();
|
||||
|
||||
// console.log(res.body);
|
||||
|
||||
expect(res.status).equals(200);
|
||||
expect(res.body.views.length).equals(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET `/views/:id`', () => {
|
||||
it('Should response unauthorized in case the user was not authorized.', async () => {
|
||||
const resource = await create('resource', { name: 'resource_name' });
|
||||
const resourceView = await create('view', {
|
||||
name: 'Resource View',
|
||||
resource_id: resource.id,
|
||||
roles_logic_expression: '',
|
||||
});
|
||||
|
||||
const res = await request()
|
||||
.get(`/api/views/${resourceView.id}`)
|
||||
.query({ resource_name: 'resource_name' })
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(401);
|
||||
expect(res.body.message).equals('unauthorized');
|
||||
});
|
||||
|
||||
it('Should response not found in case the given view was not found.', async () => {
|
||||
const resource = await create('resource', { name: 'resource_name' });
|
||||
const resourceView = await create('view', {
|
||||
name: 'Resource View',
|
||||
resource_id: resource.id,
|
||||
roles_logic_expression: '',
|
||||
});
|
||||
|
||||
const res = await request()
|
||||
.get('/api/views/123')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(404);
|
||||
expect(res.body.errors[0]).deep.equals({
|
||||
type: 'VIEW_NOT_FOUND', code: 100,
|
||||
});
|
||||
});
|
||||
|
||||
it('Should retrieve details of the given view with associated graphs.', async () => {
|
||||
const resource = await create('resource', { name: 'resource_name' });
|
||||
const resourceView = await create('view', {
|
||||
name: 'Resource View',
|
||||
resource_id: resource.id,
|
||||
roles_logic_expression: '1 AND 2',
|
||||
});
|
||||
const resourceField = await create('resource_field', {
|
||||
label_name: 'Expense Account',
|
||||
key: 'expense_account',
|
||||
data_type: 'integer',
|
||||
resource_id: resource.id,
|
||||
active: true,
|
||||
predefined: true,
|
||||
builtin: true,
|
||||
});
|
||||
const viewRole = await create('view_role', {
|
||||
view_id: resourceView.id,
|
||||
index: 1,
|
||||
field_id: resourceField.id,
|
||||
value: '12',
|
||||
comparator: 'equals',
|
||||
});
|
||||
|
||||
const res = await request()
|
||||
.get(`/api/views/${resourceView.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(200);
|
||||
expect(res.body.view.name).equals(resourceView.name);
|
||||
expect(res.body.view.resourceId).equals(resourceView.resourceId);
|
||||
expect(res.body.view.rolesLogicExpression).equals(resourceView.rolesLogicExpression);
|
||||
|
||||
expect(res.body.view.viewRoles.length).equals(1);
|
||||
expect(res.body.view.viewRoles[0].viewId).equals(viewRole.viewId);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST: `/views`', () => {
|
||||
it('Should response unauthorzied in case the user was not authorized.', async () => {
|
||||
const res = await request().post('/api/views');
|
||||
|
||||
expect(res.status).equals(422);
|
||||
expect(res.status).equals(401);
|
||||
expect(res.body.message).equals('unauthorized');
|
||||
});
|
||||
|
||||
@@ -84,31 +190,34 @@ describe('routes: `/views`', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('Should `roles.*.field` be required.', async () => {
|
||||
it('Should `roles.*.field_key` be required.', async () => {
|
||||
const resource = await create('resource');
|
||||
const res = await request()
|
||||
.post('/api/views')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
resource_name: resource.name,
|
||||
label: 'View Label',
|
||||
roles: [{}],
|
||||
})
|
||||
.set('x-access-token', loginRes.body.token);
|
||||
});
|
||||
|
||||
expect(res.status).equals(422);
|
||||
expect(res.body.code).equals('validation_error');
|
||||
expect(res.body.errors).include.something.deep.equals({
|
||||
msg: 'Invalid value', param: 'roles[0].field', location: 'body',
|
||||
msg: 'Invalid value', param: 'roles[0].field_key', location: 'body',
|
||||
});
|
||||
});
|
||||
|
||||
it('Should `roles.*.comparator` be valid.', async () => {
|
||||
const resource = await create('resource');
|
||||
const res = await request().post('/api/views').send({
|
||||
resource_name: resource.name,
|
||||
label: 'View Label',
|
||||
roles: [{}],
|
||||
});
|
||||
const res = await request()
|
||||
.post('/api/views')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
resource_name: resource.name,
|
||||
label: 'View Label',
|
||||
roles: [{}],
|
||||
});
|
||||
|
||||
expect(res.status).equals(422);
|
||||
expect(res.body.code).equals('validation_error');
|
||||
@@ -143,18 +252,22 @@ describe('routes: `/views`', () => {
|
||||
it('Should response not found in case resource was not exist.', async () => {
|
||||
const res = await request()
|
||||
.post('/api/views')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
resource_name: 'not_found',
|
||||
label: 'View Label',
|
||||
columns: ['amount', 'thumbnail', 'status'],
|
||||
columns: [
|
||||
{ key: 'amount', index: 1 },
|
||||
{ key: 'thumbnail', index: 1 },
|
||||
{ key: 'status', index: 1 },
|
||||
],
|
||||
roles: [{
|
||||
index: 1,
|
||||
field: 'amount',
|
||||
field_key: 'amount',
|
||||
comparator: 'equals',
|
||||
value: '100',
|
||||
}],
|
||||
})
|
||||
.set('x-access-token', loginRes.body.token);
|
||||
});
|
||||
|
||||
expect(res.status).equals(404);
|
||||
expect(res.body.errors).include.something.that.deep.equals({
|
||||
@@ -162,76 +275,118 @@ describe('routes: `/views`', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('Should response invalid logic expression.', async () =>{
|
||||
const resource = await create('resource');
|
||||
await create('resource_field', {
|
||||
resource_id: resource.id,
|
||||
label_name: 'Amount',
|
||||
key: 'amount',
|
||||
});
|
||||
const res = await request()
|
||||
.post('/api/views')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
resource_name: resource.name,
|
||||
logic_expression: '100 && 100',
|
||||
label: 'View Label',
|
||||
columns: [
|
||||
{ key: 'amount', index: 1 },
|
||||
],
|
||||
roles: [{
|
||||
index: 1,
|
||||
field_key: 'amount',
|
||||
comparator: 'equals',
|
||||
value: '100',
|
||||
}],
|
||||
});
|
||||
|
||||
expect(res.body.errors).include.something.that.deep.equals({
|
||||
type: 'VIEW.ROLES.LOGIC.EXPRESSION.INVALID', code: 400,
|
||||
});
|
||||
})
|
||||
|
||||
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')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
resource_name: resource.name,
|
||||
label: 'View Label',
|
||||
columns: ['amount', 'thumbnail', 'status'],
|
||||
columns: [
|
||||
{ key: 'amount', index: 1 },
|
||||
{ key: 'thumbnail', index: 1 },
|
||||
{ key: 'status', index: 1 },
|
||||
],
|
||||
roles: [{
|
||||
index: 1,
|
||||
field: 'price',
|
||||
field_key: 'price',
|
||||
comparator: 'equals',
|
||||
value: '100',
|
||||
}],
|
||||
})
|
||||
.set('x-access-token', loginRes.body.token);
|
||||
});
|
||||
|
||||
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 () => {
|
||||
it('Should response the columns that not exists in case column was not exist.', async () => {
|
||||
const resource = await create('resource');
|
||||
const resourceField = await create('resource_field', {
|
||||
resource_id: resource.id, label_name: 'Amount', slug: 'amount',
|
||||
resource_id: resource.id,
|
||||
label_name: 'Amount',
|
||||
key: 'amount',
|
||||
});
|
||||
const res = await request()
|
||||
.post('/api/views')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
resource_name: resource.name,
|
||||
label: 'View Label',
|
||||
columns: ['amount', 'thumbnail', 'status'],
|
||||
columns: [
|
||||
{ key: 'amount', index: 1 },
|
||||
{ key: 'thumbnail', index: 2 },
|
||||
{ key: 'status', index: 3 },
|
||||
],
|
||||
roles: [{
|
||||
index: 1,
|
||||
field: 'price',
|
||||
field_key: 'price',
|
||||
comparator: 'equals',
|
||||
value: '100',
|
||||
}],
|
||||
})
|
||||
.set('x-access-token', loginRes.body.token);
|
||||
});
|
||||
|
||||
expect(res.body.errors).include.something.that.deep.equals({
|
||||
type: 'COLUMNS_NOT_EXIST',
|
||||
code: 200,
|
||||
columns: ['thumbnail', 'status'],
|
||||
type: 'COLUMNS_NOT_EXIST', code: 200, columns: ['thumbnail', 'status'],
|
||||
});
|
||||
});
|
||||
|
||||
it('Should save the given details of the view.', async () => {
|
||||
const resource = await create('resource');
|
||||
await create('resource_field', {
|
||||
resource_id: resource.id, label_name: 'Amount', slug: 'amount',
|
||||
resource_id: resource.id,
|
||||
label_name: 'Amount',
|
||||
key: 'amount',
|
||||
});
|
||||
const res = await request()
|
||||
.post('/api/views')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
resource_name: resource.name,
|
||||
label: 'View Label',
|
||||
columns: ['amount'],
|
||||
columns: [
|
||||
{ key: 'amount', index: 1 },
|
||||
],
|
||||
roles: [{
|
||||
index: 1,
|
||||
field: 'amount',
|
||||
field_key: 'amount',
|
||||
comparator: 'equals',
|
||||
value: '100',
|
||||
}],
|
||||
})
|
||||
.set('x-access-token', loginRes.body.token);
|
||||
});
|
||||
|
||||
const storedView = await View.query().where('name', 'View Label').first();
|
||||
|
||||
@@ -243,7 +398,9 @@ describe('routes: `/views`', () => {
|
||||
it('Should save the given details of view fields that associated to the given view id.', async () => {
|
||||
const resource = await create('resource');
|
||||
const resourceField = await create('resource_field', {
|
||||
resource_id: resource.id, label_name: 'Amount', slug: 'amount',
|
||||
resource_id: resource.id,
|
||||
label_name: 'Amount',
|
||||
key: 'amount',
|
||||
});
|
||||
|
||||
const res = await request()
|
||||
@@ -251,10 +408,10 @@ describe('routes: `/views`', () => {
|
||||
.send({
|
||||
resource_name: resource.name,
|
||||
label: 'View Label',
|
||||
columns: ['amount'],
|
||||
columns: [{ key: 'amount', index: 1 }],
|
||||
roles: [{
|
||||
index: 1,
|
||||
field: 'amount',
|
||||
field_key: 'amount',
|
||||
comparator: 'equals',
|
||||
value: '100',
|
||||
}],
|
||||
@@ -270,26 +427,30 @@ describe('routes: `/views`', () => {
|
||||
expect(viewRoles[0].comparator).equals('equals');
|
||||
});
|
||||
|
||||
it.only('Should save columns that associated to the given view.', async () => {
|
||||
it('Should save columns that associated to the given view.', async () => {
|
||||
const resource = await create('resource');
|
||||
const resourceField = await create('resource_field', {
|
||||
resource_id: resource.id, label_name: 'Amount', slug: 'amount',
|
||||
resource_id: resource.id,
|
||||
label_name: 'Amount',
|
||||
key: 'amount',
|
||||
});
|
||||
|
||||
const res = await request()
|
||||
.post('/api/views')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
resource_name: resource.name,
|
||||
label: 'View Label',
|
||||
columns: ['amount'],
|
||||
columns: [
|
||||
{ key: 'amount', index: 1 },
|
||||
],
|
||||
roles: [{
|
||||
index: 1,
|
||||
field: 'amount',
|
||||
field_key: 'amount',
|
||||
comparator: 'equals',
|
||||
value: '100',
|
||||
}],
|
||||
})
|
||||
.set('x-access-token', loginRes.body.token);
|
||||
});
|
||||
|
||||
const viewColumns = await ViewColumn.query().where('view_id', res.body.id);
|
||||
expect(viewColumns.length).equals(1);
|
||||
@@ -419,7 +580,10 @@ describe('routes: `/views`', () => {
|
||||
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();
|
||||
const res = await request()
|
||||
.delete(`/api/views/${view.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(400);
|
||||
expect(res.body.errors).include.something.that.deep.equals({
|
||||
@@ -428,7 +592,10 @@ describe('routes: `/views`', () => {
|
||||
});
|
||||
|
||||
it('Should response not found in case view was not exist.', async () => {
|
||||
const res = await request().delete('/api/views/100').send();
|
||||
const res = await request()
|
||||
.delete('/api/views/100')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(404);
|
||||
expect(res.body.errors).include.something.that.deep.equals({
|
||||
@@ -439,12 +606,17 @@ describe('routes: `/views`', () => {
|
||||
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 create('view_column', { view_id: view.id });
|
||||
|
||||
await request().delete(`/api/views/${view.id}`).send();
|
||||
const res = await request()
|
||||
.delete(`/api/views/${view.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send();
|
||||
|
||||
const foundViews = await View.query().where('id', view.id).first();
|
||||
const foundViewRoles = await ViewRole.query().where('view_id', view.id).first();
|
||||
expect(res.body.id).equals(view.id);
|
||||
|
||||
const foundViews = await View.query().where('id', view.id);
|
||||
const foundViewRoles = await ViewRole.query().where('view_id', view.id);
|
||||
|
||||
expect(foundViews).to.have.lengthOf(0);
|
||||
expect(foundViewRoles).to.have.lengthOf(0);
|
||||
|
||||
Reference in New Issue
Block a user