WIP server side.

This commit is contained in:
Ahmed Bouhuolia
2020-01-22 02:09:45 +02:00
parent de905d7e7c
commit 488709088b
123 changed files with 14885 additions and 771 deletions

View File

@@ -0,0 +1,159 @@
import { request, expect, create, login } from '~/testInit';
let loginRes;
describe('routes: /accounting', () => {
beforeEach(async () => {
loginRes = await login();
});
afterEach(() => {
loginRes = null;
});
describe('route: `/accounting/make-journal-entries`', async () => {
it('Should sumation of credit or debit does not equal zero.', async () => {
const account = await create('account');
const res = await request()
.post('/api/accounting/make-journal-entries')
.set('x-access-token', loginRes.body.token)
.send({
date: new Date().toISOString(),
reference: 'ASC',
entries: [
{
credit: 0,
debit: 0,
account_id: account.id,
},
{
credit: 0,
debit: 0,
account_id: account.id,
},
],
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equal({
type: 'CREDIT.DEBIT.SUMATION.SHOULD.NOT.EQUAL.ZERO',
code: 400,
});
});
it('Should all credit entries equal debit.', async () => {
const account = await create('account');
const res = await request()
.post('/api/accounting/make-journal-entries')
.set('x-access-token', loginRes.body.token)
.send({
date: new Date().toISOString(),
reference: 'ASC',
entries: [
{
credit: 1000,
debit: 0,
account_id: account.id,
},
{
credit: 0,
debit: 500,
account_id: account.id,
},
],
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equal({
type: 'CREDIT.DEBIT.NOT.EQUALS',
code: 100,
});
});
it('Should journal reference be not exists.', async () => {
const manualJournal = await create('manual_journal');
const account = await create('account');
const res = await request()
.post('/api/accounting/make-journal-entries')
.set('x-access-token', loginRes.body.token)
.send({
date: new Date().toISOString(),
reference: manualJournal.reference,
entries: [
{
credit: 1000,
debit: 0,
account_id: account.id,
},
{
credit: 0,
debit: 1000,
account_id: account.id,
},
],
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equal({
type: 'REFERENCE.ALREADY.EXISTS',
code: 300,
});
});
it('Should response error in case account id not exists.', async () => {
const res = await request()
.post('/api/accounting/make-journal-entries')
.set('x-access-token', loginRes.body.token)
.send({
date: new Date().toISOString(),
reference: '1000',
entries: [
{
credit: 1000,
debit: 0,
account_id: 12,
},
{
credit: 0,
debit: 1000,
account_id: 12,
},
],
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equal({
type: 'ACCOUNTS.IDS.NOT.FOUND',
code: 200,
});
});
it('Should store all journal entries to the storage.', async () => {
});
});
describe('route: `/accounting/quick-journal-entries`', async () => {
it('Shoud `credit_account_id` be required', () => {
});
it('Should `debit_account_id` be required.', () => {
});
it('Should `amount` be required.', () => {
});
it('Should credit account id be exists.', () => {
});
it('Should debit account id be exists.', () => {
});
it('Should store the quick journal entry to the storage.', () => {
});
});
});

View File

@@ -1,101 +1,76 @@
import { request, expect, create } from '~/testInit';
import { request, expect, create, login } from '~/testInit';
import knex from '@/database/knex';
import Account from '@/models/Account';
let loginRes;
describe('routes: /accounts/', () => {
beforeEach(async () => {
loginRes = await login();
});
afterEach(() => {
loginRes = null;
});
describe('POST `/accounts`', () => {
it('Should `name` be required.', async () => {
const res = await request().post('/api/accounts').send();
const res = await request()
.post('/api/accounts')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
});
it('Should `account_type_id` be required.', async () => {
const res = await request().post('/api/accounts').send();
const res = await request()
.post('/api/accounts')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
});
it('Should max length of `code` be limited.', async () => {
const res = await request().post('/api/accounts').send();
const res = await request()
.post('/api/accounts')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
});
it('Should response type not found in case `account_type_id` was not exist.', async () => {
const res = await request().post('/api/accounts').send();
expect(res.status).equals(422);
});
it('Should account code be unique in the storage.', async () => {
const account = await create('account');
const res = await request().post('/api/accounts').send({
...account,
});
const res = await request()
.post('/api/accounts')
.set('x-access-token', loginRes.body.token)
.send({
name: 'Account Name',
description: account.description,
account_type_id: 22, // not found.
code: 123,
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equals({
type: 'PARENT_CATEGORY_NOT_FOUND', code: 100,
type: 'NOT_EXIST_ACCOUNT_TYPE', code: 200,
});
});
it('Should response success with correct data form.', async () => {
const account = await create('account');
const res = await request().post('/api/accounts').send({
name: 'Name',
description: 'description here',
account_type_id: account.account_type_id,
parent_account_id: account.id,
});
expect(res.status).equals(200);
});
it('Should store account data in the storage.', async () => {
});
});
describe('POST `/accounts/:id`', () => {
it('Should `name` be required.', async () => {
const account = await create('account');
const res = await request().post(`/api/accounts/${account.id}`).send();
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
});
it('Should `account_type_id` be required.', async () => {
const account = await create('account');
const res = await request().post(`/api/accounts/${account.id}`).send();
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
});
it('Should max length of `code` be limited.', async () => {
const account = await create('account');
const res = await request().post(`/api/accounts/${account.id}`).send();
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
});
it('Should response type not found in case `account_type_id` was not exist.', async () => {
const res = await request().post('/api/accounts').send();
expect(res.status).equals(422);
});
it('Should account code be unique in the storage.', async () => {
const account = await create('account', { code: 'ABCD' });
const res = await request().post(`/api/accounts/${account.id}`).send({
// code: ',
...account,
});
const account = await create('account');
const res = await request()
.post('/api/accounts')
.set('x-access-token', loginRes.body.token)
.send({
name: account.name,
description: account.description,
account_type_id: account.accountTypeId,
code: account.code,
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equals({
@@ -105,34 +80,166 @@ describe('routes: /accounts/', () => {
it('Should response success with correct data form.', async () => {
const account = await create('account');
const res = await request().post('/api/accounts').send({
name: 'Name',
description: 'description here',
account_type_id: account.account_type_id,
parent_account_id: account.id,
const res = await request()
.post('/api/accounts')
.set('x-access-token', loginRes.body.token)
.send({
name: 'Name',
description: 'description here',
code: 100,
account_type_id: account.accountTypeId,
parent_account_id: account.id,
});
console.log(res.body);
expect(res.status).equals(200);
});
it('Should store account data in the storage.', async () => {
const account = await create('account');
await request().post('/api/accounts')
.set('x-access-token', loginRes.body.token)
.send({
name: 'Account Name',
description: 'desc here',
account_type: account.account_type_id,
parent_account_id: account.id,
});
const accountModel = await Account.query().where('name', 'Account Name');
expect(accountModel.description).equals('desc here');
expect(accountModel.account_type_id).equals(account.account_type_id);
expect(accountModel.parent_account_id).equals(account.parent_account_id);
});
});
describe('POST `/accounts/:id`', () => {
it('Should `name` be required.', async () => {
const account = await create('account');
const res = await request()
.post(`/api/accounts/${account.id}`)
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
});
it('Should `account_type_id` be required.', async () => {
const account = await create('account');
const res = await request()
.post(`/api/accounts/${account.id}`)
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
});
it('Should max length of `code` be limited.', async () => {
const account = await create('account');
const res = await request()
.post(`/api/accounts/${account.id}`)
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
});
it('Should response type not found in case `account_type_id` was not exist.', async () => {
const res = await request()
.post('/api/accounts')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(422);
});
it('Should account code be unique in the storage.', async () => {
await create('account', { code: 'ABCD' });
const account = await create('account');
const res = await request()
.post(`/api/accounts/${account.id}`)
.set('x-access-token', loginRes.body.token)
.send({
name: 'name',
code: 'ABCD',
account_type_id: account.accountTypeId,
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equals({
type: 'NOT_UNIQUE_CODE', code: 100,
});
});
it('Should response success with correct data form.', async () => {
const account = await create('account');
const res = await request()
.post('/api/accounts')
.set('x-access-token', loginRes.body.token)
.send({
name: 'Name',
description: 'description here',
account_type_id: account.accountTypeId,
parent_account_id: account.id,
code: '123',
});
expect(res.status).equals(200);
});
});
describe('GET: `/accounts`', () => {
it('Should retrieve chart of accounts', async () => {
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()
console.log(res.body);
});
});
describe('DELETE: `/accounts`', () => {
it('Should response not found in case account was not exist.', async () => {
const res = await request().delete('/api/accounts/10').send();
const res = await request()
.delete('/api/accounts/10')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(404);
});
it('Should delete the give account from the storage.', async () => {
const account = await create('account');
await request().delete(`/api/accounts/${account.id}`);
await request()
.delete(`/api/accounts/${account.id}`)
.set('x-access-token', loginRes.body.token)
.send();
const foundAccounts = await knex('accounts').where('id', account.id);
expect(foundAccounts).to.have.lengthOf(1);
const foundAccounts = await Account.query().where('id', account.id);
expect(foundAccounts).to.have.lengthOf(0);
});
it('Should not delete the given account in case account has associated transactions.', async () => {
const accountTransaction = await create('account_transaction');
const res = await request()
.delete(`/api/accounts/${accountTransaction.accountId}`)
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equals({
type: 'ACCOUNT.HAS.ASSOCIATED.TRANSACTIONS', code: 100,
});
});
});
});

View File

@@ -59,17 +59,15 @@ describe('routes: /auth/', () => {
});
it('Should not authenticate in case user was not active.', async () => {
const user = await create('user', {
active: false,
});
const user = await create('user', { active: false });
const res = await request().post('/api/auth/login').send({
crediential: user.email,
password: 'admin',
password: 'incorrect_password',
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equals({
type: 'INCORRECT_PASSWORD', code: 120,
type: 'INCORRECT_PASSWORD', code: 110,
});
});
@@ -79,6 +77,7 @@ describe('routes: /auth/', () => {
});
const res = await request().post('/api/auth/login').send({
crediential: user.email,
password: 'admin',
});
expect(res.status).equals(200);

View File

@@ -0,0 +1,262 @@
import {
request,
expect,
create,
login,
} from '~/testInit';
import Budget from '@/models/Budget';
import BudgetEntry from '@/models/BudgetEntry';
let loginRes;
describe('routes: `/budget`', () => {
beforeEach(async () => {
loginRes = await login();
});
afterEach(() => {
loginRes = null;
});
describe('POST: `/budget', () => {
it('Should `name` be required.', async () => {
const res = await request()
.post('/api/budget')
.set('x-access-token', loginRes.body.token).send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.that.deep.equal({
msg: 'Invalid value', param: 'name', location: 'body',
});
});
it('Should `period` be required.', async () => {
const res = await request()
.post('/api/budget')
.set('x-access-token', loginRes.body.token).send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.that.deep.equal({
msg: 'Invalid value', param: 'period', location: 'body',
});
});
it('Should `fiscal_year` be required.', async () => {
const res = await request()
.post('/api/budget')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(422);
expect(res.body.errors).include.something.that.deep.equal({
msg: 'Invalid value', param: 'fiscal_year', location: 'body',
});
});
it('Should `entries` alteast one item', () => {
});
it('Should account id be exist in the storage.', async () => {
const res = await request()
.post('/api/budget')
.set('x-access-token', loginRes.body.token)
.send({
name: 'Budget Name',
fiscal_year: '2020',
period: 'year',
accounts_type: 'profit_loss',
accounts: [
{
account_id: 100,
entries: [
{
amount: 1000,
order: 1,
},
],
},
],
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equals({
type: 'ACCOUNT.NOT.FOUND', code: 200, accounts: [100],
});
});
it('Should response success with budget id after post valid data.', async () => {
const account = await create('account');
const res = await request()
.post('/api/budget')
.set('x-access-token', loginRes.body.token)
.send({
name: 'Budget Name',
fiscal_year: '2020',
period: 'year',
accounts_type: 'profit_loss',
accounts: [
{
account_id: account.id,
entries: [
{
amount: 1000,
order: 1,
},
],
},
],
});
expect(res.status).equals(200);
});
it('Should save budget to the storage.', async () => {
const account = await create('account');
const res = await request()
.post('/api/budget')
.set('x-access-token', loginRes.body.token)
.send({
name: 'Budget Name',
fiscal_year: '2020',
period: 'year',
accounts_type: 'profit_loss',
accounts: [
{
account_id: account.id,
entries: [
{
amount: 1000,
order: 1,
}
],
},
],
});
// const storedBudget = await Budget.query().findById(res.body.id);
// expect(storedBudget.name).equals('Budget Name');
const storedBudgetEntries = await BudgetEntry.query()
.where('budget_id', storedBudget.id)
.where('account_id', account.id);
expect(storedBudgetEntries.length).equals(1);
});
it('Should save budget entries to the storage.', () => {
});
it('Should response success with correct data format.', () => {
});
});
describe('GET: `/budget/:id`', () => {
it('Should response not found in case budget id was not found.', async () => {
const budget = await create('budget');
const res = await request()
.get('/api/budget/1000')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(404);
});
it('Should retrieve columns of budget year date range with year period.', async () => {
const budget = await create('budget', { period: 'year' });
const res = await request()
.get(`/api/budget/${budget.id}`)
.set('x-access-token', loginRes.body.token)
.send();
expect(res.body.columns.length).equals(1);
});
it('Should retrieve columns of budget year range with month period.', async () => {
const budget = await create('budget', {
period: 'month',
});
const res = await request()
.get(`/api/budget/${budget.id}`)
.set('x-access-token', loginRes.body.token)
.send();
expect(res.body.columns.length).equals(12);
});
it('Should retrieve columns of budget year range with quarter period.', async () => {
const budget = await create('budget', {
period: 'quarter',
});
const res = await request()
.get(`/api/budget/${budget.id}`)
.set('x-access-token', loginRes.body.token)
.send();
expect(res.body.columns.length).equals(4);
});
it('Should retrieve columns of budget year range with half year period.', async () => {
const budget = await create('budget', {
period: 'half-year',
});
const res = await request()
.get(`/api/budget/${budget.id}`)
.set('x-access-token', loginRes.body.token)
.send();
expect(res.body.columns.length).equals(2);
});
it('Should retrieve budget accounts with associated entries.', async () => {
const budget = await create('budget', { period: 'year' });
const budgetEntry = await create('budget_entry', {
budget_id: budget.id,
});
const res = await request()
.get(`/api/budget/${budget.id}`)
.set('x-access-token', loginRes.body.token)
.send();
expect(res.body);
});
});
describe('DELETE: `/budget/:id`', () => {
it('Should response not found in case budget id was not found.', () => {
});
it('Should delete budget from the storage', () => {
});
it('Should delete budget entries from the storage.', () => {
});
it('Should response success in case budget was exists before the delete.', () => {
});
});
describe.only('GET: `/budget`', () => {
it('Should retrieve all budgets with pagination metadata.', async () => {
const res = await request()
.get('/api/budget')
.set('x-access-token', loginRes.body.token)
.send();
console.log(res.body);
expect(res.status).equals(200);
})
})
});

View File

@@ -0,0 +1,67 @@
import {
request,
expect,
create,
login,
} from '~/testInit';
let loginRes;
describe('routes: `/budget_reports`', () => {
beforeEach(async () => {
loginRes = await login();
});
afterEach(() => {
loginRes = null;
});
describe.only('GET: `/budget_verses_actual/:reportId`', () => {
it('Should retrieve columns of budget year range with quarter period.', async () => {
const budget = await create('budget', { period: 'quarter' });
const budgetEntry = await create('budget_entry', { budget_id: budget.id });
const res = await request()
.get(`/api/budget_reports/budget_verses_actual/${budget.id}`)
.set('x-access-token', loginRes.body.token)
.send();
expect(res.body.columns.length).equals(4);
});
it('Should retrieve columns of budget year range with month period.', async () => {
const budget = await create('budget', { period: 'month' });
const budgetEntry = await create('budget_entry', { budget_id: budget.id });
const res = await request()
.get(`/api/budget_reports/budget_verses_actual/${budget.id}`)
.set('x-access-token', loginRes.body.token)
.send();
expect(res.body.columns.length).equals(12);
});
it('Should retrieve columns of budget year range with year period.', async () => {
const budget = await create('budget', { period: 'year' });
const budgetEntry = await create('budget_entry', { budget_id: budget.id });
const res = await request()
.get(`/api/budget_reports/budget_verses_actual/${budget.id}`)
.set('x-access-token', loginRes.body.token)
.send();
expect(res.body.columns.length).equals(1);
});
it('Should retrieve columns of budget year range with half-year period.', async () => {
const budget = await create('budget', { period: 'half-year' });
const budgetEntry = await create('budget_entry', { budget_id: budget.id });
const res = await request()
.get(`/api/budget_reports/budget_verses_actual/${budget.id}`)
.set('x-access-token', loginRes.body.token)
.send();
expect(res.body.columns.length).equals(2);
});
});
});

View File

@@ -0,0 +1,309 @@
import { request, expect, create, login } from '~/testInit';
import AccountTransaction from '@/models/AccountTransaction';
import Expense from '@/models/Expense';
let loginRes;
let expenseType;
let cashType;
let expenseAccount;
let cashAccount;
describe('routes: /expenses/', () => {
beforeEach(async () => {
loginRes = await login();
expenseType = await create('account_type', { normal: 'debit' });
cashType = await create('account_type', { normal: 'debit' });
expenseAccount = await create('account', { account_type_id: expenseType.id });
cashAccount = await create('account', { account_type_id: cashType.id });
});
afterEach(() => {
loginRes = null;
});
describe('POST: `/expenses`', () => {
it('Should response unauthorized in case user was not authorized.', async () => {
const res = await request().post('/api/expenses').send();
expect(res.status).equals(401);
expect(res.body.message).equals('unauthorized');
});
it('Should `payment_account_id` be required.', async () => {
const res = await request().post('/api/expenses')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.body.errors).include.something.that.deep.equals({
msg: 'Invalid value',
param: 'payment_account_id',
location: 'body',
});
});
it('Should `expense_account_id` be required.', async () => {
const res = await request().post('/api/expenses')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.body.errors).include.something.that.deep.equals({
msg: 'Invalid value',
param: 'expense_account_id',
location: 'body',
});
});
it('Should `amount` be required.', async () => {
const res = await request().post('/api/expenses')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.body.errors).include.something.that.deep.equals({
msg: 'Invalid value',
param: 'amount',
location: 'body',
});
});
it('Should `exchange_rate` be required in case `currency_code` not equal default one.', () => {
});
it('Should response bad request in case expense account was not found.', async () => {
const res = await request().post('/api/expenses')
.set('x-access-token', loginRes.body.token)
.send({
expense_account_id: 100,
payment_account_id: 100,
amount: 100,
});
expect(res.body.errors).include.something.that.deep.equals({
type: 'EXPENSE.ACCOUNT.NOT.FOUND', code: 200,
});
});
it('Should response bad request in case payment account was not found.', async () => {
const res = await request().post('/api/expenses')
.set('x-access-token', loginRes.body.token)
.send({
expense_account_id: 100,
payment_account_id: 100,
amount: 100,
});
expect(res.body.errors).include.something.that.deep.equals({
type: 'PAYMENT.ACCOUNT.NOT.FOUND', code: 100,
});
});
it('Should response success with valid required data.', async () => {
const res = await request().post('/api/expenses')
.set('x-access-token', loginRes.body.token)
.send({
expense_account_id: expenseAccount.id,
payment_account_id: cashAccount.id,
amount: 100,
});
expect(res.status).equals(200);
});
it('Should record journal entries of expense transaction.', async () => {
const res = await request().post('/api/expenses')
.set('x-access-token', loginRes.body.token)
.send({
expense_account_id: expenseAccount.id,
payment_account_id: cashAccount.id,
amount: 100,
});
const expensesEntries = await AccountTransaction.query()
.where('reference_type', 'Expense')
.where('reference_id', res.body.id);
expect(expensesEntries.length).equals(2);
});
it('Should save expense transaction to the storage.', () => {
});
});
describe('POST: `/expenses/:id`', () => {
it('Should response unauthorized in case user was not authorized.', () => {
});
});
describe('DELETE: `/expenses/:id`', () => {
it('Should response not found in case expense not found.', async () => {
const res = await request()
.delete('/api/expense/1000')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.body.reasons).include.something.that.deep.equals({
type: 'EXPENSE.NOT.FOUND', code: 100,
});
});
it('Should response success in case expense transaction was exist.', async () => {
const expense = await create('expense');
const res = await request()
.delete(`/api/expense/${expense.id}`)
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(200);
});
it('Should delete the expense transaction from the storage.', async () => {
const expense = await create('expense');
await request()
.delete(`/api/expense/${expense.id}`)
.set('x-access-token', loginRes.body.token)
.send();
const storedExpense = await Expense.query().findById(expense.id);
expect(storedExpense).equals(undefined);
});
it('Should delete the journal entries that associated to expense transaction from the storage.', async () => {
const expense = await create('expense');
await request()
.delete(`/api/expense/${expense.id}`)
.set('x-access-token', loginRes.body.token)
.send();
const expenseEntries = await AccountTransaction.query()
.where('reference_type', 'Expense')
.where('reference_id', expense.id);
expect(expenseEntries.length).equals(0);
});
it('Should reverse accounts balance that associated to expense transaction.', () => {
});
});
describe('POST: `/expenses/bulk`', () => {
it('Should response unauthorized in case user was not authorized.', async () => {
const res = await request().post('/api/expenses/bluk').send();
expect(res.status).equals(401);
expect(res.body.message).equals('unauthorized');
});
it('Should response bad request in case expenses was not array.', async () => {
const res = await request()
.post('/api/expenses/bulk')
.set('x-access-token', loginRes.body.token)
.send({
expenses: 'Not array :(',
});
expect(res.body.errors).include.something.that.deep.equals({
value: 'Not array :(',
msg: 'Invalid value',
param: 'expenses',
location: 'body',
});
});
it('Should response bad request in case one expense account was not found.', async () => {
const res = await request()
.post('/api/expenses/bulk')
.set('x-access-token', loginRes.body.token)
.send({
expenses: [
{
payment_account_id: 100,
expense_account_id: 100,
amount: 1000,
},
],
});
expect(res.body.reasons).include.something.that.deep.equals({
type: 'EXPENSE.ACCOUNTS.NOT.FOUND', code: 200, accounts: [100],
});
});
it('Should response bad request in case one of payment account was not found.', async () => {
const res = await request()
.post('/api/expenses/bulk')
.set('x-access-token', loginRes.body.token)
.send({
expenses: [
{
payment_account_id: 100,
expense_account_id: 100,
amount: 1000,
},
],
});
expect(res.body.reasons).include.something.that.deep.equals({
type: 'PAYMENY.ACCOUNTS.NOT.FOUND', code: 100, accounts: [100],
});
});
it('Should store expenses transactions to the storage.', async () => {
const res = await request()
.post('/api/expenses/bulk')
.set('x-access-token', loginRes.body.token)
.send({
expenses: [
{
payment_account_id: cashAccount.id,
expense_account_id: expenseAccount.id,
amount: 1000,
},
{
payment_account_id: cashAccount.id,
expense_account_id: expenseAccount.id,
amount: 1000,
},
],
});
const expenseTransactions = await Expense.query();
expect(expenseTransactions.length).equals(2);
});
it('Should store journal entries of expenses transactions to the storage.', async () => {
const res = await request()
.post('/api/expenses/bulk')
.set('x-access-token', loginRes.body.token)
.send({
expenses: [
{
payment_account_id: cashAccount.id,
expense_account_id: expenseAccount.id,
amount: 1000,
},
{
payment_account_id: cashAccount.id,
expense_account_id: expenseAccount.id,
amount: 1000,
},
],
});
const expenseJournalEntries = await AccountTransaction.query();
expect(expenseJournalEntries.length).equals(4);
});
});
describe('GET: `/expenses/:id`', () => {
it('Should response view not found in case the custom view id was not exist.', async () => {
const res = await request()
.get('/api/expenses')
.set('x-access-token', loginRes.body.token)
.send();
console.log(res.status);
});
});
});

View File

@@ -187,7 +187,6 @@ describe('route: `/fields`', () => {
});
});
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();

View File

@@ -0,0 +1,648 @@
import {
expect,
request,
login,
create,
} from '~/testInit';
let loginRes;
let creditAccount;
let debitAccount;
describe('routes: `/financial_statements`', () => {
beforeEach(async () => {
loginRes = await login();
// Balance sheet types.
const creditAccType = await create('account_type', { normal: 'credit', balance_sheet: true });
const debitAccType = await create('account_type', { normal: 'debit', balance_sheet: true });
// Income statement types.
const incomeType = await create('account_type', { normal: 'credit', income_sheet: true });
const expenseType = await create('account_type', { normal: 'debit', income_sheet: true });
// Assets & liabilites accounts.
creditAccount = await create('account', { account_type_id: creditAccType.id });
debitAccount = await create('account', { account_type_id: debitAccType.id });
// Income && expenses accounts.
const incomeAccount = await create('account', { account_type_id: incomeType.id });
const expenseAccount = await create('account', { account_type_id: expenseType.id });
const income2Account = await create('account', { account_type_id: incomeType.id });
const accountTransactionMixied = { date: '2020-1-10' };
await create('account_transaction', {
credit: 1000, debit: 0, account_id: creditAccount.id, referenceType: 'Expense', ...accountTransactionMixied,
});
await create('account_transaction', {
credit: 1000, debit: 0, account_id: creditAccount.id, ...accountTransactionMixied,
});
await create('account_transaction', {
debit: 2000, credit: 0, account_id: debitAccount.id, ...accountTransactionMixied,
});
await create('account_transaction', {
debit: 2000, credit: 0, account_id: debitAccount.id, ...accountTransactionMixied,
});
await create('account_transaction', { credit: 2000, account_id: incomeAccount.id, ...accountTransactionMixied });
await create('account_transaction', { debit: 6000, account_id: expenseAccount.id, ...accountTransactionMixied });
});
afterEach(() => {
loginRes = null;
});
describe('routes: `/financial_statements/ledger`', () => {
it('Should response unauthorized in case the user was not authorized.', async () => {
const res = await request()
.get('/api/financial_statements/ledger')
.send();
expect(res.status).equals(400);
});
it('Should retrieve ledger transactions grouped by accounts.', async () => {
const res = await request()
.get('/api/financial_statements/ledger')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.status).equals(200);
expect(res.body.items.length).to.be.at.least(1);
expect(res.body.items[0]).to.have.property('id');
expect(res.body.items[0]).to.have.property('referenceType');
expect(res.body.items[0]).to.have.property('referenceId');
expect(res.body.items[0]).to.have.property('date');
expect(res.body.items[0]).to.have.property('account');
expect(res.body.items[0]).to.have.property('note');
});
it('Should retrieve transactions between date range.', async () => {
const res = await request()
.get('/api/financial_statements/ledger')
.set('x-access-token', loginRes.body.token)
.query({
from_date: '2018-01-01',
to_date: '2019-01-01',
})
.send();
expect(res.body.items.length).equals(0);
});
it('Should retrieve transactions that associated to the queried accounts.', async () => {
const res = await request()
.get('/api/financial_statements/ledger')
.set('x-access-token', loginRes.body.token)
.query({
account_ids: [creditAccount.id, debitAccount.id],
})
.send();
expect(res.body.items.length).equals(4);
});
it('Should retrieve tranasactions with the given types.', async () => {
const res = await request()
.get('/api/financial_statements/ledger')
.set('x-access-token', loginRes.body.token)
.query({
transaction_types: ['Expense'],
});
expect(res.body.items.length).equals(1);
});
it('Should retrieve transactions with range amount.', async () => {
const res = await request()
.get('/api/financial_statements/ledger')
.set('x-access-token', loginRes.body.token)
.query({
from_range: 1000,
to_range: 2000,
});
});
it('Should format credit and debit to no cents of retrieved transactions.', async () => {
});
it('Should divide credit/debit amount on 1000', async () => {
const res = await request()
.get('/api/financial_statements/ledger')
.set('x-access-token', loginRes.body.token)
.query({
number_format: {
divide_1000: true,
},
})
.send();
res.body.items.forEach((item) => {
expect(item.credit).to.be.at.most(100);
expect(item.debit).to.be.at.most(100);
});
});
});
describe('routes: `/financial_statements/general_ledger`', () => {
it('Should response unauthorized in case the user was not authorized.', async () => {
const res = await request()
.get('/api/financial_statements/general_ledger')
.send();
expect(res.status).equals(400);
});
it('Should retrieve the genereal ledger transactions.', async () => {
const res = await request()
.get('/api/financial_statements/general_ledger')
.set('x-access-token', loginRes.body.token)
.send();
expect(res.body.items).to.be.a('array');
expect(res.body.items.length).equals(4);
});
it('Should retrieve opeing and closing balance in each account.', async () => {
const res = await request()
.get('/api/financial_statements/general_ledger')
.set('x-access-token', loginRes.body.token)
.send();
const foundCreditAccount = res.body.items.find((a) => a.id === creditAccount.id);
expect(foundCreditAccount.closing.balance).equals(2000);
expect(foundCreditAccount.opening.balance).equals(0);
});
it('Should retrieve the general ledger transactions between date range.', async () => {
const res = await request()
.get('/api/financial_statements/general_ledger')
.set('x-access-token', loginRes.body.token)
.query({
from_date: '2020-04-04',
to_date: '2020-05-05',
})
.send();
const foundCreditAccount = res.body.items.find((a) => a.id === creditAccount.id);
expect(foundCreditAccount.transactions.length).equals(0);
});
it('Should retrieve the general ledger transactions with no cents numbers.', () => {
});
it('Should retrieve the transacvtions divided on 1000.', () => {
});
});
describe('routes: `financial_statements/balance_sheet`', () => {
it('Should response unauthorzied in case the user was not authorized.', async () => {
const res = await request()
.get('/api/financial_statements/balance_sheet')
.send();
expect(res.status).equals(401);
});
it('Should retrieve the asset accounts balance.', async () => {
const res = await request()
.get('/api/financial_statements/balance_sheet')
.set('x-access-token', loginRes.body.token)
.query({
display_columns_by: 'year',
})
.send();
expect(res.body.balance_sheet.assets).to.be.a('array');
expect(res.body.balance_sheet.liabilities_equity).to.be.a('array');
});
it('Should retrieve asset/liabilities balance sheet between the given date range.', async () => {
const res = await request()
.get('/api/financial_statements/balance_sheet')
.set('x-access-token', loginRes.body.token)
.query({
display_columns_by: 'year',
from_date: '2012-01-01',
to_date: '2018-02-02',
})
.send();
const { balance_sheet: balanceSheet } = res.body;
const foundCreditAccount = balanceSheet.assets.find((account) => {
return account.id === debitAccount.id;
});
expect(foundCreditAccount.transactions.length).equals(6);
foundCreditAccount.transactions.forEach((transaction) => {
expect(transaction.balance).equals(0);
});
const foundDebitAccount = balanceSheet.liabilities_equity.find((account) => {
return account.id === creditAccount.id;
});
expect(foundDebitAccount.transactions.length).equals(6);
foundDebitAccount.transactions.forEach((transaction) => {
expect(transaction.balance).equals(0);
});
});
it('Should retrieve balance sheet with display columns day.', async () => {
const res = await request()
.get('/api/financial_statements/balance_sheet')
.set('x-access-token', loginRes.body.token)
.query({
display_columns_by: 'day',
from_date: '2020-03-01',
to_date: '2020-04-01',
})
.send();
const { balance_sheet: balanceSheet } = res.body;
const foundDebitAccount = balanceSheet.assets.find((account) => {
return account.id === debitAccount.id;
});
const foundCreditAccount = balanceSheet.liabilities_equity.find((account) => {
return account.id === creditAccount.id;
});
expect(foundDebitAccount.transactions.length).equals(31);
expect(foundCreditAccount.transactions.length).equals(31);
foundDebitAccount.transactions.forEach((transaction) => {
expect(transaction.balance).equals(4000);
});
foundCreditAccount.transactions.forEach((transaction) => {
expect(transaction.balance).equals(2000);
});
});
it('Should retrieve the balance sheet with display columns month.', async () => {
const res = await request()
.get('/api/financial_statements/balance_sheet')
.set('x-access-token', loginRes.body.token)
.query({
display_columns_by: 'month',
from_date: '2020',
to_date: '2021',
})
.send();
const { balance_sheet: balanceSheet } = res.body;
const foundDebitAccount = balanceSheet.assets.find((account) => {
return account.id === debitAccount.id;
});
const foundCreditAccount = balanceSheet.liabilities_equity.find((account) => {
return account.id === creditAccount.id;
});
expect(foundDebitAccount.transactions.length).equals(12);
expect(foundCreditAccount.transactions.length).equals(12);
foundDebitAccount.transactions.forEach((transaction) => {
expect(transaction.balance).equals(4000);
});
foundCreditAccount.transactions.forEach((transaction) => {
expect(transaction.balance).equals(2000);
});
});
it('Should retrieve the balance sheet with display columns quarter.', async () => {
const res = await request()
.get('/api/financial_statements/balance_sheet')
.set('x-access-token', loginRes.body.token)
.query({
display_columns_by: 'quarter',
from_date: '2020',
to_date: '2021',
})
.send();
const { balance_sheet: balanceSheet } = res.body;
const foundDebitAccount = balanceSheet.assets.find((account) => {
return account.id === debitAccount.id;
});
const foundCreditAccount = balanceSheet.liabilities_equity.find((account) => {
return account.id === creditAccount.id;
});
expect(foundDebitAccount.transactions.length).equals(4);
expect(foundCreditAccount.transactions.length).equals(4);
foundDebitAccount.transactions.forEach((transaction) => {
expect(transaction.balance).equals(4000);
});
foundCreditAccount.transactions.forEach((transaction) => {
expect(transaction.balance).equals(2000);
});
});
it('Should retrieve the balance sheet amounts without cents.', () => {
});
it('Should retrieve the balance sheet amounts divided on 1000.', async () => {
const res = await request()
.get('/api/financial_statements/balance_sheet')
.set('x-access-token', loginRes.body.token)
.query({
display_columns_by: 'quarter',
from_date: '2020',
to_date: '2021',
number_format: {
divide_1000: true,
},
})
.send();
const { balance_sheet: balanceSheet } = res.body;
const foundDebitAccount = balanceSheet.assets.find((account) => {
return account.id === debitAccount.id;
});
const foundCreditAccount = balanceSheet.liabilities_equity.find((account) => {
return account.id === creditAccount.id;
});
expect(foundDebitAccount.transactions.length).equals(4);
expect(foundCreditAccount.transactions.length).equals(4);
foundDebitAccount.transactions.forEach((transaction) => {
expect(transaction.balance).equals(4);
});
foundCreditAccount.transactions.forEach((transaction) => {
expect(transaction.balance).equals(2);
});
});
it('Should not retrieve accounts has no transactions between the given date range in case query none_zero is true.', async () => {
const res = await request()
.get('/api/financial_statements/balance_sheet')
.set('x-access-token', loginRes.body.token)
.query({
display_columns_by: 'quarter',
from_date: '2002',
to_date: '2003',
number_format: {
divide_1000: true,
},
none_zero: true,
})
.send();
const { balance_sheet: balanceSheet } = res.body;
const foundDebitAccount = balanceSheet.assets.find((account) => {
return account.id === debitAccount.id;
});
const foundCreditAccount = balanceSheet.liabilities_equity.find((account) => {
return account.id === creditAccount.id;
});
expect(foundDebitAccount).equals(undefined);
expect(foundCreditAccount).equals(undefined);
});
});
describe('routes: `/financial_statements/trial_balance`', () => {
it('Should response unauthorized in case the user was not authorized.', async () => {
const res = await request()
.get('/api/financial_statements/trial_balance_sheet')
.send();
expect(res.status).equals(401);
});
it('Should retrieve the trial balance of accounts.', async () => {
const res = await request()
.get('/api/financial_statements/trial_balance_sheet')
.set('x-access-token', loginRes.body.token)
.send();
const foundCreditAccount = res.body.items.find((item) => {
return item.account_id === creditAccount.id;
});
expect(foundCreditAccount.credit).equals(2000);
expect(foundCreditAccount.debit).equals(0);
expect(foundCreditAccount.balance).equals(2000);
});
it('Should not retrieve accounts has no transactions between the given date range.', async () => {
const res = await request()
.get('/api/financial_statements/trial_balance_sheet')
.set('x-access-token', loginRes.body.token)
.query({
// There is no transactions between these dates.
from_date: '2002-01-01',
to_date: '2003-01-01',
})
.send();
res.body.items.forEach((item) => {
expect(item.credit).equals(0);
expect(item.debit).equals(0);
expect(item.balance).equals(0);
});
});
it('Should retrieve trial balance of accounts between the given date range.', async () => {
const res = await request()
.get('/api/financial_statements/trial_balance_sheet')
.set('x-access-token', loginRes.body.token)
.query({
// There is no transactions between these dates.
from_date: '2020-01-05',
to_date: '2020-01-10',
})
.send();
const foundCreditAccount = res.body.items.find((item) => {
return item.account_id === creditAccount.id;
});
expect(foundCreditAccount.credit).equals(2000);
expect(foundCreditAccount.debit).equals(0);
expect(foundCreditAccount.balance).equals(2000);
});
it('Should credit, debit and balance amount be divided on 1000.', async () => {
const res = await request()
.get('/api/financial_statements/trial_balance_sheet')
.set('x-access-token', loginRes.body.token)
.query({
// There is no transactions between these dates.
from_date: '2020-01-05',
to_date: '2020-01-10',
number_format: {
divide_1000: true,
},
})
.send();
const foundCreditAccount = res.body.items.find((item) => {
return item.account_id === creditAccount.id;
});
expect(foundCreditAccount.credit).equals(2);
expect(foundCreditAccount.debit).equals(0);
expect(foundCreditAccount.balance).equals(2);
});
it('Should credit, debit and balance amount rounded without cents.', async () => {
const res = await request()
.get('/api/financial_statements/trial_balance_sheet')
.set('x-access-token', loginRes.body.token)
.query({
// There is no transactions between these dates.
from_date: '2020-01-05',
to_date: '2020-01-10',
number_format: {
no_cents: true,
},
})
.send();
});
});
describe('routes: `/api/financial_statements/profit_loss_sheet`', () => {
it('Should response unauthorized in case the user was not authorized.', async () => {
const res = await request()
.get('/api/financial_statements/profit_loos_sheet')
.send();
expect(res.status).equals(401);
expect(res.body.message).equals('unauthorzied');
});
it('Should retrieve credit sumation of income accounts.', async () => {
const res = await request()
.get('/api/financial_statements/profit_loss_sheet')
.set('x-access-token', loginRes.body.token)
.query({
from_date: '2020-01-01',
to_date: '2021-01-01',
number_format: {
divide_1000: true,
},
})
.send();
res.body.income.accounts[0].dates.forEach((item) => {
expect(item.rawAmount).equals(2000);
});
});
it('Should retrieve debit sumation of expenses accounts.', async () => {
const res = await request()
.get('/api/financial_statements/profit_loss_sheet')
.set('x-access-token', loginRes.body.token)
.query({
from_date: '2020-01-01',
to_date: '2021-01-01',
number_format: {
divide_1000: true,
},
})
.send();
res.body.expenses.accounts[0].dates.forEach((item) => {
expect(item.rawAmount).equals(4000);
});
});
it('Should retrieve credit sumation of income accounts between the given date range.', async () => {
const res = await request()
.get('/api/financial_statements/profit_loss_sheet')
.set('x-access-token', loginRes.body.token)
.query({
from_date: '2020-01-01',
to_date: '2021-01-01',
display_columns_by: 'month',
number_format: {
divide_1000: true,
},
})
.send();
expect(res.body.income.accounts[0].dates.length).equals(12);
});
it('Should retrieve debit sumation of expenses accounts between the given date range.', async () => {
const res = await request()
.get('/api/financial_statements/profit_loss_sheet')
.set('x-access-token', loginRes.body.token)
.query({
from_date: '2020-01-01',
to_date: '2021-01-01',
display_columns_by: 'month',
number_format: {
divide_1000: true,
},
})
.send();
expect(res.body.expenses.accounts[0].dates.length).equals(12);
});
it('Should retrieve total income of income accounts between the given date range.', async () => {
const res = await request()
.get('/api/financial_statements/profit_loss_sheet')
.set('x-access-token', loginRes.body.token)
.query({
from_date: '2020-01-01',
to_date: '2021-01-01',
display_columns_by: 'month',
})
.send();
expect(res.body.total_income[0].rawAmount).equals(2000);
});
it('Should retrieve total expenses of expenses accounts between the given date range.', async () => {
const res = await request()
.get('/api/financial_statements/profit_loss_sheet')
.set('x-access-token', loginRes.body.token)
.query({
from_date: '2020-01-01',
to_date: '2021-01-01',
display_columns_by: 'month',
})
.send();
expect(res.body.total_expenses[0].rawAmount).equals(6000);
});
it('Should retrieve total net income between the given date range.', async () => {
const res = await request()
.get('/api/financial_statements/profit_loss_sheet')
.set('x-access-token', loginRes.body.token)
.query({
from_date: '2020-01-01',
to_date: '2021-01-01',
display_columns_by: 'month',
})
.send();
expect(res.body.total_net_income[0].rawAmount).equals(-4000);
});
it('Should not retrieve income or expenses accounts that has no transactions between the given date range in case none_zero equals true.', async () => {
const res = await request()
.get('/api/financial_statements/profit_loss_sheet')
.set('x-access-token', loginRes.body.token)
.query({
from_date: '2020-01-01',
to_date: '2021-01-01',
display_columns_by: 'month',
none_zero: true
})
.send();
expect(res.body.income.accounts.length).equals(1);
});
});
});

View File

@@ -6,8 +6,8 @@ import {
} from '~/testInit';
import knex from '@/database/knex';
describe.only('routes: `/items`', () => {
describe.only('POST: `/items`', () => {
describe('routes: `/items`', () => {
describe('POST: `/items`', () => {
it('Should not create a new item if the user was not authorized.', async () => {
const res = await request().post('/api/items').send();

View File

@@ -4,7 +4,6 @@ describe('routes: /oauth2/', () => {
describe('POST `/api/oauth/token`', () => {
it('Should `crediential` be required.', async () => {
const res = await request().post('/api/oauth2/token').send({});
console.log(res.body);
expect(res.status).equals(200);
});
});

View File

View File

@@ -4,10 +4,10 @@ import ViewRole from '@/models/ViewRole';
import '@/models/ResourceField';
describe('routes: `/views`', () => {
describe('POST: `/views/:resource_id`', () => {
describe('POST: `/views`', () => {
it('Should `label` be required.', async () => {
const resource = await create('resource');
const res = await request().post(`/api/views/resource/${resource.id}`);
await create('resource');
const res = await request().post('/api/views');
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
@@ -16,9 +16,17 @@ describe('routes: `/views`', () => {
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}`, {
it('Should `resource_name` be required.', async () => {
await create('resource');
const res = await request().post('/api/views');
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
});
it('Should `columns` be minimum limited', async () => {
await create('resource');
const res = await request().post('/api/views', {
label: 'View Label',
columns: [],
});
@@ -30,9 +38,9 @@ describe('routes: `/views`', () => {
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}`, {
it('Should `columns` be array.', async () => {
await create('resource');
const res = await request().post('/api/views', {
label: 'View Label',
columns: 'not_array',
});
@@ -46,7 +54,8 @@ describe('routes: `/views`', () => {
it('Should `roles.*.field` be required.', async () => {
const resource = await create('resource');
const res = await request().post(`/api/views/resource/${resource.id}`).send({
const res = await request().post('/api/views').send({
resource_name: resource.name,
label: 'View Label',
roles: [{}],
});
@@ -60,7 +69,8 @@ describe('routes: `/views`', () => {
it('Should `roles.*.comparator` be valid.', async () => {
const resource = await create('resource');
const res = await request().post(`/api/views/resource/${resource.id}`).send({
const res = await request().post('/api/views').send({
resource_name: resource.name,
label: 'View Label',
roles: [{}],
});
@@ -74,7 +84,8 @@ describe('routes: `/views`', () => {
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({
const res = await request().post('/api/views').send({
resource_name: resource.name,
label: 'View Label',
roles: [{ index: 'not_numeric' }],
});
@@ -87,7 +98,8 @@ describe('routes: `/views`', () => {
});
it('Should response not found in case resource was not exist.', async () => {
const res = await request().post('/api/views/resource/100').send({
const res = await request().post('/api/views').send({
resource_name: 'not_found',
label: 'View Label',
columns: ['amount', 'thumbnail', 'status'],
roles: [{
@@ -99,15 +111,17 @@ describe('routes: `/views`', () => {
});
expect(res.status).equals(404);
expect(res.body.errors).include.something.that.deep.equals({
type: 'RESOURCE_NOT_FOUND', code: 100,
});
});
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({
await create('resource_field', { resource_id: resource.id, label_name: 'Amount' });
const res = await request().post('/api/views').send({
resource_name: resource.name,
label: 'View Label',
columns: ['amount', 'thumbnail', 'status'],
roles: [{
@@ -119,19 +133,17 @@ describe('routes: `/views`', () => {
});
expect(res.body.errors).include.something.that.deep.equals({
type: 'RESOURCE_FIELDS_NOT_EXIST',
code: 100,
fields: ['price'],
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',
resource_id: resource.id, label_name: 'Amount', slug: 'amount',
});
const res = await request().post(`/api/views/resource/${resource.id}`).send({
const res = await request().post('/api/views').send({
resource_name: resource.name,
label: 'View Label',
columns: ['amount', 'thumbnail', 'status'],
roles: [{
@@ -145,11 +157,39 @@ describe('routes: `/views`', () => {
expect(res.body.errors).include.something.that.deep.equals({
type: 'COLUMNS_NOT_EXIST',
code: 200,
fields: ['thumbnail', 'status'],
columns: ['thumbnail', 'status'],
});
});
it('Should save the given details with associated roles and columns.', async () => {
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',
});
const res = await request().post('/api/views').send({
resource_name: resource.name,
label: 'View Label',
columns: ['amount'],
roles: [{
index: 1,
field: 'amount',
comparator: 'equals',
value: '100',
}],
});
const storedView = await View.query().where('name', 'View Label').first();
expect(storedView.name).equals('View Label');
expect(storedView.predefined).equals(0);
expect(storedView.resourceId).equals(resource.id);
});
it('Should save the given details of view fields that associated to the given view id.', () => {
});
it('Should save the given details of view roles that associated to the given view.', async () => {
});
});
@@ -269,9 +309,7 @@ describe('routes: `/views`', () => {
});
expect(res.body.errors).include.something.that.deep.equals({
type: 'RESOURCE_FIELDS_NOT_EXIST',
code: 100,
fields: ['price'],
type: 'RESOURCE_FIELDS_NOT_EXIST', code: 100, fields: ['price'],
});
});
});
@@ -282,6 +320,9 @@ describe('routes: `/views`', () => {
const res = await request().delete(`/api/views/${view.id}`).send();
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equals({
type: 'PREDEFINED_VIEW', code: 200,
});
});
it('Should response not found in case view was not exist.', async () => {
@@ -300,8 +341,8 @@ describe('routes: `/views`', () => {
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();
const foundViews = await View.query().where('id', view.id).first();
const foundViewRoles = await ViewRole.query().where('view_id', view.id).first();
expect(foundViews).to.have.lengthOf(0);
expect(foundViewRoles).to.have.lengthOf(0);