diff --git a/common/lib/Lexer.test.js b/common/lib/Lexer.test.js deleted file mode 100644 index 14ca6a97a..000000000 --- a/common/lib/Lexer.test.js +++ /dev/null @@ -1,12 +0,0 @@ -import { expect } from '~/testInit'; -import { Lexer } from '@/lib/LogicEvaluation/Lexer'; - -describe('Lexer', () => { - - it('Should retrieve tokens of the expression.', () => { - const lexer = new Lexer('(1 && 2) || (2 || 3)'); - - const tokens = lexer.getTokens(); - expect(tokens.length).equals(11); - }); -}); diff --git a/common/lib/Metable/MetableModel.test.js b/common/lib/Metable/MetableModel.test.js deleted file mode 100644 index 77a8b4235..000000000 --- a/common/lib/Metable/MetableModel.test.js +++ /dev/null @@ -1,5 +0,0 @@ - - -describe('MetableModel', () => { - -}); \ No newline at end of file diff --git a/common/lib/MetableCollection.test.js b/common/lib/MetableCollection.test.js deleted file mode 100644 index e1d5cdc01..000000000 --- a/common/lib/MetableCollection.test.js +++ /dev/null @@ -1,159 +0,0 @@ -import Option from '@/models/Option'; -import MetadataCollection from '@/lib/Metable/MetableCollection'; -import ResourceFieldMetadata from '@/models/ResourceFieldMetadata'; -import { create, expect } from '~/testInit'; - -describe('MetableCollection', () => { - describe('findMeta', () => { - it('Should retrieve the found meta object.', async () => { - const option = await create('option'); - const metadataCollection = await Option.query(); - - const foundMeta = metadataCollection.findMeta(option.key); - expect(foundMeta).to.be.an('object'); - }); - - it('Should retrieve the found meta with extra columns.', async () => { - const option = await create('option'); - const metadataCollection = await Option.query(); - - const foundMeta = metadataCollection.findMeta({ - key: option.key, - group: option.group, - }); - expect(foundMeta).to.be.an('object'); - }); - }); - - describe('allMetadata', () => { - it('Should retrieve all exists metadata entries.', async () => { - const option = await create('option'); - const metadataCollection = await Option.query(); - const foundMetadata = metadataCollection.allMetadata(); - - expect(foundMetadata.length).equals(1); - }); - }); - - describe('getMeta', () => { - it('Should retrieve the found meta value.', async () => { - const option = await create('option'); - const metadataCollection = await Option.query(); - - const foundMeta = metadataCollection.getMeta(option.key); - expect(foundMeta).equals(option.value); - }); - - it('Should retrieve the default meta value in case the meta key was not exist.', async () => { - const option = await create('option'); - const metadataCollection = await Option.query(); - - const foundMeta = metadataCollection.getMeta('not-found', true); - expect(foundMeta).equals(true); - }); - }); - - describe('setMeta', () => { - it('Should sets the meta value to the stack.', async () => { - const metadataCollection = new MetadataCollection(); - metadataCollection.setMeta('key', 'value'); - - expect(metadataCollection.metadata.length).equals(1); - }); - - it('Should sets the meta value with extra columns', async () => { - const metadataCollection = new MetadataCollection(); - metadataCollection.setMeta({ - key: 'key', - value: 'value', - group: 'group-1', - }); - - expect(metadataCollection.metadata.length).equals(1); - - expect(metadataCollection.metadata[0].key).equals('key'); - expect(metadataCollection.metadata[0].value).equals('value'); - expect(metadataCollection.metadata[0].group).equals('group-1'); - }); - }); - - describe('removeAllMeta()', () => { - it('Should remove all metadata from the stack.', async () => { - const metadataCollection = new MetadataCollection(); - - metadataCollection.setModel(Option); - metadataCollection.setMeta('key', 'value'); - metadataCollection.setMeta('key2', 'value2'); - - metadataCollection.removeAllMeta(); - - expect(metadataCollection.metadata.length).equals(2); - expect(metadataCollection.allMetadata().length).equals(0); - }); - }); - - describe('saveMeta', () => { - it('Should save inserted new metadata.', async () => { - const metadataCollection = new MetadataCollection(); - - metadataCollection.setModel(Option); - metadataCollection.setMeta('key', 'value'); - metadataCollection.setModel(Option); - - await metadataCollection.saveMeta(); - - const storedMetadata = await Option.query(); - expect(storedMetadata.metadata.length).equals(1); - }); - - it('Should save updated the exist metadata.', async () => { - const option = await create('option'); - const metadataCollection = await Option.query(); - - metadataCollection.setModel(Option); - metadataCollection.setMeta(option.key, 'value'); - metadataCollection.setModel(Option); - - await metadataCollection.saveMeta(); - - const storedMetadata = await Option.query().where('key', option.key); - - expect(storedMetadata.metadata[0].value).equals('value'); - expect(storedMetadata.metadata[0].key).equals(option.key); - expect(storedMetadata.metadata[0].group).equals(option.group); - }); - - it('Should delete the removed metadata from storage.', async () => { - const option = await create('option'); - - const metadataCollection = await Option.query(); - metadataCollection.removeMeta(option.key); - - expect(metadataCollection.metadata.length).equals(1); - await metadataCollection.saveMeta(); - - const storedMetadata = await Option.query(); - expect(storedMetadata.metadata.length).equals(0); - }); - - it('Should save instered new metadata with extra columns.', async () => { - const resource = await create('resource'); - - const metadataCollection = new MetadataCollection(); - metadataCollection.extraColumns = ['resource_id']; - - metadataCollection.setModel(ResourceFieldMetadata); - metadataCollection.setMeta({ - key: 'key', - value: 'value', - resource_id: resource.id, - }); - await metadataCollection.saveMeta(); - - const storedMetadata = await ResourceFieldMetadata.query().first(); - - expect(storedMetadata.metadata.length).equals(1); - expect(storedMetadata.metadata[0].resourceId).equals(resource.id); - }); - }); -}); diff --git a/common/lib/Parser.test.js b/common/lib/Parser.test.js deleted file mode 100644 index fc54bf296..000000000 --- a/common/lib/Parser.test.js +++ /dev/null @@ -1,50 +0,0 @@ -import { expect } from '~/testInit'; -import { Lexer } from '@/lib/LogicEvaluation/Lexer'; -import Parser from '@/lib/LogicEvaluation/Parser'; -import QueryParser from '@/lib/LogicEvaluation/QueryParser'; -import Expense from '@/models/Expense'; -import knex from '@/database/knex'; - -describe('LoginEvaluation: Parser', () => { - - it('Should parse the logic', async () => { - const lexer = new Lexer('(1 OR 2) AND (1 AND 3)'); - const tokens = lexer.getTokens(); - - const parser = new Parser(tokens); - const parsedTree = parser.parse(); - - const queries = { - 1: (query) => { - query.where('expense_account_id', 1); - }, - 2: (query) => { - query.where('payment_account_id', 2); - }, - 3: (query) => { - query.where('amount', '<', 100); - }, - }; - - const queryParser = new QueryParser(parsedTree, queries); - const parsedQuery = queryParser.parse(); - - const parsedQueryWrapper = (builder) => { - parsedQuery(builder); - console.log(builder.toString()); - }; - const query = await knex.select('*').from('expenses') - .modify(parsedQueryWrapper); - - // console.log(query); - - - // }); - - // console.log(a); - - - }); - - -}); \ No newline at end of file diff --git a/common/lib/ResourceCustomFieldRepository.test.js b/common/lib/ResourceCustomFieldRepository.test.js deleted file mode 100644 index 42072249a..000000000 --- a/common/lib/ResourceCustomFieldRepository.test.js +++ /dev/null @@ -1,125 +0,0 @@ -import { - expect, - create, - login, -} from '~/testInit'; -import Expense from '@/models/Expense'; -import ResourceCustomFieldRepository from '@/services/CustomFields/ResourceCustomFieldRepository'; -import ResourceFieldMetadata from '@/models/ResourceFieldMetadata'; - -let loginRes; - -describe('ResourceCustomFieldRepository', () => { - beforeEach(async () => { - loginRes = await login(); - }); - afterEach(() => { - loginRes = null; - }); - - describe('constructor()', () => { - it('Should take the resource name from model class name', () => { - const customFieldsRepo = new ResourceCustomFieldRepository(Expense); - - expect(customFieldsRepo.resourceName).equals('Expense'); - }); - }); - - describe('loadResource()', () => { - it('Should fetches the resource name.', async () => { - const resource = await create('resource', { name: 'Expense' }); - const customFieldsRepo = new ResourceCustomFieldRepository(Expense); - await customFieldsRepo.loadResource(); - - expect(customFieldsRepo.resource.name).equals('Expense'); - }); - }); - - describe('loadResourceCustomFields()', () => { - it('Should fetches all custom fields that associated with the resource.', async () => { - const resource = await create('resource', { name: 'Expense' }); - const resourceField = await create('resource_field', { resource_id: resource.id }); - const resourceField2 = await create('resource_field', { resource_id: resource.id }); - - const customFieldsRepo = new ResourceCustomFieldRepository(Expense); - await customFieldsRepo.loadResource(); - await customFieldsRepo.loadResourceCustomFields(); - - expect(customFieldsRepo.customFields.length).equals(2); - }); - }); - - describe('fetchCustomFieldsMetadata', () => { - it('Should fetches all custom fields metadata that associated to the resource and resource item.', async () => { - const resource = await create('resource', { name: 'Expense' }); - const resourceField = await create('resource_field', { resource_id: resource.id }); - const resourceField2 = await create('resource_field', { resource_id: resource.id }); - - const expense = await create('expense'); - - const fieldMetadata = await create('resource_custom_field_metadata', { - resource_id: resource.id, resource_item_id: expense.id, - }); - const customFieldsRepo = new ResourceCustomFieldRepository(Expense); - - await customFieldsRepo.load(); - await customFieldsRepo.fetchCustomFieldsMetadata(expense.id); - - expect(customFieldsRepo.metadata[expense.id].metadata.length).equals(1); - expect(customFieldsRepo.metadata[expense.id].metadata[0].key).equals(fieldMetadata.key); - expect(customFieldsRepo.metadata[expense.id].metadata[0].value).equals(fieldMetadata.value); - }); - }); - - describe('fillCustomFields', () => { - it('Should fill custom fields metadata attributes to metadata object.', async () => { - const resource = await create('resource', { name: 'Expense' }); - const resourceField = await create('resource_field', { resource_id: resource.id }); - - const expense = await create('expense'); - - const customFieldsRepo = new ResourceCustomFieldRepository(Expense); - await customFieldsRepo.load(); - await customFieldsRepo.fetchCustomFieldsMetadata(expense.id); - - customFieldsRepo.fillCustomFields(expense.id, [ - { - key: resourceField.key, - value: 'Hello World', - }, - ]); - expect(customFieldsRepo.fieldsMetadata[expense.id].metadata.length).equals(1); - expect(customFieldsRepo.filledCustomFields[expense.id].length).equals(1); - }); - }); - - describe('saveCustomFields', () => { - it('Should save the given custom fields metadata to the resource item.', async () => { - const resource = await create('resource', { name: 'Expense' }); - const resourceField = await create('resource_field', { resource_id: resource.id }); - - const expense = await create('expense'); - const fieldMetadata = await create('resource_custom_field_metadata', { - key: resourceField.slug, - resource_id: resource.id, - resource_item_id: expense.id, - }); - const customFieldsRepo = new ResourceCustomFieldRepository(Expense); - await customFieldsRepo.load(); - await customFieldsRepo.fetchCustomFieldsMetadata(expense.id); - - customFieldsRepo.fillCustomFields(expense.id, [ - { key: resourceField.slug, value: 'Hello World' }, - ]); - - await customFieldsRepo.saveCustomFields(expense.id); - - const updateResourceFieldData = await ResourceFieldMetadata.query(); - expect(updateResourceFieldData.metadata[0].value).equals('Hello World'); - }); - }); - - describe('validateExistCustomFields', () => { - - }); -}); \ No newline at end of file diff --git a/common/lib/ViewRolesBuilder.test.js b/common/lib/ViewRolesBuilder.test.js deleted file mode 100644 index 7d63a6bf7..000000000 --- a/common/lib/ViewRolesBuilder.test.js +++ /dev/null @@ -1,89 +0,0 @@ -import { - create, - expect, - request, - login, -} from '~/testInit'; - -let loginRes; - -describe('ViewRolesBuilder', () => { - beforeEach(async () => { - loginRes = await login(); - }); - afterEach(() => { - loginRes = null; - }); - - it('Should not retrieve results when there is no match query from view roles.', async () => { - const expenseResource = await create('resource', { name: 'expenses' }); - const expenseField = await create('resource_field', { - label_name: 'Expense Account', - column_key: 'expense_account', - data_type: 'integer', - resource_id: expenseResource.id, - active: true, - predefined: true, - }); - const expenseView = await create('view', { - name: 'Expense View', - resource_id: expenseResource.id, - roles_logic_expression: '1', - }); - const expenseViewRole = await create('view_role', { - view_id: expenseView.id, - index: 1, - field_id: expenseField.id, - value: '12', - comparator: 'equals', - }); - - const expenseAccount = await create('account', { id: 10 }); - const expense = await create('expense', { expense_account_id: expenseAccount.id }); - - const res = await request() - .get('/api/expenses') - .set('x-access-token', loginRes.body.token) - .query({ custom_view_id: expenseView.id }) - .send(); - - expect(res.status).equals(200); - expect(res.body.expenses.results.length).equals(0); - }); - - it('Should retrieve results that match custom view conditionals roles.', async () => { - const expenseResource = await create('resource', { name: 'expenses' }); - const expenseField = await create('resource_field', { - label_name: 'Expense Account', - column_key: 'expense_account', - data_type: 'integer', - resource_id: expenseResource.id, - active: true, - predefined: true, - }); - const expenseView = await create('view', { - name: 'Expense View', - resource_id: expenseResource.id, - roles_logic_expression: '1', - }); - const expenseViewRole = await create('view_role', { - view_id: expenseView.id, - index: 1, - field_id: expenseField.id, - value: '10', - comparator: 'equals', - }); - - const expenseAccount = await create('account', { id: 10 }); - const expense = await create('expense', { expense_account_id: expenseAccount.id }); - - const res = await request() - .get('/api/expenses') - .set('x-access-token', loginRes.body.token) - .query({ custom_view_id: expenseView.id }) - .send(); - - expect(res.status).equals(200); - expect(res.body.expenses.results.length).equals(1); - }); -}); \ No newline at end of file diff --git a/common/routes/accountOpeningBalance.js b/common/routes/accountOpeningBalance.js deleted file mode 100644 index 29dd3c547..000000000 --- a/common/routes/accountOpeningBalance.js +++ /dev/null @@ -1,152 +0,0 @@ -import { request, expect, create, login } from '~/testInit'; -import ManualJournal from '../../src/models/ManualJournal'; -import AccountTransaction from '@/models/AccountTransaction'; -let loginRes; - -describe('routes: `/accountOpeningBalance`', () => { - beforeEach(async () => { - loginRes = await login(); - }); - afterEach(() => { - loginRes = null; - }); - describe('POST `/accountOpeningBalance`', () => { - it('Should `accounts` be array type.', async () => { - const res = await request() - .post('/api/accounts_opening_balances') - .send({ - accounts: 1000, - }); - - expect(res.status).equals(422); - expect(res.body.code).equals('validation_error'); - }); - - it('Should `accounts.*.id` be integer', async () => { - const res = await request() - .post('/api/accounts_opening_balances') - .send({ - accounts: [ - { id: 'id' }, - ] - }); - expect(res.status).equals(422); - expect(res.body.code).equals('validation_error'); - expect(res.body.errors).include.something.that.deep.equals({ - value: 'id', - msg: 'Invalid value', - param: 'accounts[0].id', - location: 'body', - }); - }); - - it('Should `accounts.*.debit` be numeric.', async () => { - const res = await request() - .post('/api/accounts_opening_balances') - .send({ - balance_adjustment_account: 10, - accounts: [{ id: 100, debit: 'id' }], - }); - expect(res.status).equals(422); - }); - - it('Should `accounts.*.id` be exist in the storage.', async () => { - const res = await request() - .post('/api/accounts_opening_balances') - .send({ - balance_adjustment_account: 10, - accounts: [ - { id: 100, credit: 100 }, - ], - }); - expect(res.status).equals(422); - expect(res.body.errors).include.something.that.deep.equals({ - type: 'NOT_FOUND_ACCOUNT', code: 100, ids: [100], - }); - }); - - it('Should response bad request in case balance adjustment account was not exist.', async () => { - const debitAccount = await create('account'); - const creditAccount = await create('account'); - - const res = await request() - .post('/api/accounts_opening_balances') - .send({ - balance_adjustment_account: 10, - accounts: [ - { id: debitAccount.id, credit: 100, debit: 2 }, - { id: creditAccount.id, credit: 0, debit: 100 }, - ] - }); - - expect(res.body.errors).include.something.that.deep.equals({ - type: 'BALANCE.ADJUSTMENT.ACCOUNT.NOT.EXIST', code: 300, - }); - }); - - it('Should store the manual transaction to the storage.', async () => { - const debitAccount = await create('account'); - const creditAccount = await create('account'); - const balance = await create('account'); - - const res = await request() - .post('/api/accounts_opening_balances') - .set('x-access-token', loginRes.body.token) - .send({ - balance_adjustment_account: balance.id, - accounts: [ - { id: debitAccount.id, credit: 100, debit: 2 }, - { id: creditAccount.id, credit: 0, debit: 100 }, - ] - }); - - const manualJournal = await ManualJournal.query().findById(res.body.id); - expect(manualJournal.amount).equals(100); - expect(manualJournal.transaction_type).equals('OpeningBalance'); - }); - - it('Should store the jouranl entries of account balance transaction.', async () => { - const debitAccount = await create('account'); - const creditAccount = await create('account'); - const balance = await create('account'); - - const res = await request() - .post('/api/accounts_opening_balances') - .set('x-access-token', loginRes.body.token) - .send({ - balance_adjustment_account: balance.id, - accounts: [ - { id: debitAccount.id, credit: 100, debit: 2 }, - { id: creditAccount.id, credit: 0, debit: 100 }, - ] - }); - - const transactions = await AccountTransaction.query() - .where('reference_type', 'OpeningBalance') - .where('reference_id', res.body.id); - - expect(transactions.length).equals(2); - }); - - it('Should adjustment with balance adjustment account with bigger than zero.', async () => { - const debitAccount = await create('account'); - const balance = await create('account'); - - const res = await request() - .post('/api/accounts_opening_balances') - .set('x-access-token', loginRes.body.token) - .send({ - balance_adjustment_account: balance.id, - accounts: [ - { id: debitAccount.id, credit: 0, debit: 100 }, - ] - }); - - const transactions = await AccountTransaction.query() - .where('reference_type', 'OpeningBalance') - .where('reference_id', res.body.id); - - expect(transactions.length).equals(2); - }); - }); -}); diff --git a/common/routes/authorization.js b/common/routes/authorization.js deleted file mode 100644 index 3f1eee0ed..000000000 --- a/common/routes/authorization.js +++ /dev/null @@ -1,10 +0,0 @@ - -describe('Authorization', () => { - it('Should response unauthorized in case use has no role has permissions to the given resource.', () => { - - }); - - it('Should response authorized in case user has role has all permissions.', () => { - - }); -}); diff --git a/common/routes/budget.js b/common/routes/budget.js deleted file mode 100644 index 19b3b5314..000000000 --- a/common/routes/budget.js +++ /dev/null @@ -1,262 +0,0 @@ -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('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); - }) - }) -}); \ No newline at end of file diff --git a/common/routes/budget_reports.js b/common/routes/budget_reports.js deleted file mode 100644 index aac4d0dd8..000000000 --- a/common/routes/budget_reports.js +++ /dev/null @@ -1,67 +0,0 @@ -import { - request, - expect, - create, - login, -} from '~/testInit'; -let loginRes; - -describe('routes: `/budget_reports`', () => { - beforeEach(async () => { - loginRes = await login(); - }); - afterEach(() => { - loginRes = null; - }); - - describe('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); - }); - - }); -}); \ No newline at end of file diff --git a/common/routes/expenses.js b/common/routes/expenses.js deleted file mode 100644 index f19a4be0a..000000000 --- a/common/routes/expenses.js +++ /dev/null @@ -1,491 +0,0 @@ -import { - request, - expect, - create, - login, -} from '~/testInit'; -import AccountTransaction from '@/models/AccountTransaction'; -import Expense from '@/models/Expense'; -import ResourceFieldMetadata from '@/models/ResourceFieldMetadata'; - -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.', 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 expenseTransaction = await Expense.query().where('id', res.body.id); - expect(expenseTransaction.amount).equals(100); - }); - - it('Should response bad request in case custom field slug was not exists in the storage.', 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, - custom_options: [ - { - key: 'random_key', - value: 'Value here', - }, - ], - }); - - expect(res.status).equals(400); - }); - - it('Should save expense custom fields to the storage.', async () => { - const resource = await create('resource', { name: 'Expense' }); - const resourceField = await create('resource_field', { - resource_id: resource.id, - slug: 'custom_field_1', - }); - - 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, - custom_fields: [ - { - key: 'custom_field_1', - value: 'Value here', - }, - ], - }); - - const storedResourceItemMetadata = await ResourceFieldMetadata.query() - .where('resource_id', resource.id) - .where('resource_item_id', res.body.id); - - expect(storedResourceItemMetadata.metadata.length).equals(1); - expect(storedResourceItemMetadata.metadata[0].resourceId).equals(resource.id); - expect(storedResourceItemMetadata.metadata[0].resourceItemId).equals(res.body.id); - - expect(storedResourceItemMetadata.metadata[0].key).equals('custom_field_1'); - expect(storedResourceItemMetadata.metadata[0].value).equals('Value here'); - }); - }); - - describe('POST: `/expenses/:id`', () => { - it('Should response unauthorized in case user was not authorized.', () => { - - }); - - it('Should response not found in case expense was not exist.', () => { - - }); - - it('Should update the expense transaction.', () => { - - }); - }); - - describe('DELETE: `/expenses/:id`', () => { - it('Should response not found in case expense not found.', async () => { - const res = await request() - .delete('/api/expenses/1000') - .set('x-access-token', loginRes.body.token) - .send(); - - expect(res.body.errors).include.something.that.deep.equals({ - type: 'EXPENSE.TRANSACTION.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/expenses/${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/expenses/${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.', () => { - - }); - - it('Should delete the custom fields that associated to resource and resource item.', () => { - - }); - }); - - 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('POST: `/expenses/:id/publish`', () => { - it('Should response not found in case the expense id was not exist.', async () => { - const expense = await create('expense', { published: false }); - const res = await request() - .post('/api/expenses/100/publish') - .set('x-access-token', loginRes.body.token) - .send(); - - expect(res.status).equals(400); - expect(res.body.errors).include.something.that.deep.equals({ - type: 'EXPENSE.NOT.FOUND', code: 100, - }); - }); - - it('Should response bad request in case expense is already published.', async () => { - const expense = await create('expense', { published: true }); - const res = await request() - .post(`/api/expenses/${expense.id}/publish`) - .set('x-access-token', loginRes.body.token) - .send(); - - expect(res.status).equals(400); - expect(res.body.errors).include.something.that.deep.equals({ - type: 'EXPENSE.ALREADY.PUBLISHED', code: 200, - }); - }); - - it('Should publish the expense transaction.', async () => { - const expense = await create('expense', { published: false }); - const res = await request() - .post(`/api/expenses/${expense.id}/publish`) - .set('x-access-token', loginRes.body.token) - .send(); - - const storedExpense = await Expense.query().findById(expense.id); - expect(storedExpense.published).equals(1); - }); - - it('Should publish the journal entries that associated to the given expense transaction.', async () => { - const expense = await create('expense', { published: false }); - const transaction = await create('account_transaction', { - reference_id: expense.id, - reference_type: 'Expense', - }); - const res = await request() - .post(`/api/expenses/${expense.id}/publish`) - .set('x-access-token', loginRes.body.token) - .send(); - - const entries = await AccountTransaction.query() - .where('reference_id', expense.id) - .where('reference_type', 'Expense'); - - entries.forEach((entry) => { - expect(entry.draft).equals(0); - }); - }); - - it('Should response success in case expense was exist and not published.', async () => { - const expense = await create('expense', { published: false }); - const res = await request() - .post(`/api/expenses/${expense.id}/publish`) - .set('x-access-token', loginRes.body.token) - .send(); - - expect(res.status).equals(200); - }); - }); - - describe('GET: `/expenses/:id`', () => { - it('Should response view not found in case the custom view id was not exist.', async () => { - const expense = await create('expense'); - - const res = await request() - .get(`/api/expenses/${expense.id}123`) - .set('x-access-token', loginRes.body.token) - .send(); - - expect(res.status).equals(404); - expect(res.body.errors).include.something.deep.equals({ - type: 'EXPENSE.TRANSACTION.NOT.FOUND', code: 100, - }); - }); - - it('Should retrieve custom fields metadata.', async () => { - const expense = await create('expense'); - const resource = await create('resource', { name: 'Expense' }); - const resourceField = await create('resource_field', { - resource_id: resource.id, - slug: 'custom_field_1', - }); - - const resourceFieldMetadata = await create('resource_custom_field_metadata', { - resource_id: resource.id, - resource_item_id: expense.id, - key: 'custom_field_1', - }); - - const res = await request() - .get(`/api/expenses/${expense.id}`) - .set('x-access-token', loginRes.body.token) - .send(); - - expect(res.status).equals(200); - - expect(res.body.custom_fields.length).equals(1); - expect(res.body.custom_fields[0].key).equals('custom_field_1'); - expect(res.body.custom_fields[0].value).equals(resourceFieldMetadata.value); - }); - }); -}); diff --git a/common/routes/fields.js b/common/routes/fields.js deleted file mode 100644 index 3528fcbfc..000000000 --- a/common/routes/fields.js +++ /dev/null @@ -1,338 +0,0 @@ -import { - create, - expect, - request, - login, -} from '~/testInit'; -import knex from '@/database/knex'; -import ResourceField from '@/models/ResourceField'; -import e from 'express'; -import Fields from '../../src/http/controllers/Fields'; - -let loginRes; - -describe('route: `/fields`', () => { - beforeEach(async () => { - loginRes = await login(); - }); - afterEach(() => { - loginRes = null; - }); - describe('POST: `/fields/:resource_id`', () => { - it('Should response unauthorized in case the user was not authorized.', async () => { - const res = await request() - .post('/api/fields/resource/items') - .send(); - - expect(res.status).equals(422); - expect(res.body.code).equals('validation_error'); - }); - - it('Should response bad request in case resource name was not exist.', async () => { - const res = await request() - .post('/api/fields/resource/not_found_resource') - .set('x-access-token', loginRes.body.token) - .send({ - label: 'Extra Field', - data_type: 'text', - }); - - expect(res.status).equals(404); - expect(res.body.errors).include.something.deep.equals({ - type: 'RESOURCE_NOT_FOUND', code: 100, - }); - }); - - it('Should `label` be required.', async () => { - const resource = await create('resource'); - const res = await request() - .post(`/api/fields/resource/${resource.resource_name}`) - .set('x-access-token', loginRes.body.token) - .send(); - - expect(res.status).equals(422); - expect(res.body.code).equals('validation_error'); - expect(res.body.errors).include.something.deep.equals({ - msg: 'Invalid value', param: 'label', location: 'body', - }); - }); - - it('Should `data_type` be required.', async () => { - const resource = await create('resource'); - const res = await request() - .post(`/api/fields/resource/${resource.resource_id}`) - .set('x-access-token', loginRes.body.token) - .send({ - label: 'Field label', - }); - - expect(res.status).equals(422); - expect(res.body.code).equals('validation_error'); - expect(res.body.errors).include.something.deep.equals({ - msg: 'Invalid value', param: 'data_type', location: 'body', - }); - }); - - it('Should `data_type` be one in the list.', async () => { - const resource = await create('resource'); - const res = await request() - .post(`/api/fields/resource/${resource.resource_id}`) - .set('x-access-token', loginRes.body.token) - .send({ - label: 'Field label', - data_type: 'invalid_type', - }); - - expect(res.status).equals(422); - expect(res.body.code).equals('validation_error'); - expect(res.body.errors).include.something.deep.equals({ - msg: 'Invalid value', param: 'data_type', location: 'body', value: 'invalid_type', - }); - }); - - it('Should `value` be boolean valid value in case `data_type` was `boolean`.', () => { - - }); - - 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 name was not exist.', async () => { - const res = await request() - .post('/api/fields/resource/resource_not_found') - .set('x-access-token', loginRes.body.token) - .send({ - label: 'Field label', - data_type: 'text', - default: 'default value', - help_text: 'help text', - }); - - expect(res.status).equals(404); - expect(res.body.errors).include.something.that.deep.equals({ - 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.name}`) - .set('x-access-token', loginRes.body.token) - .send({ - label: 'Field label', - data_type: 'text', - default: 'default value', - help_text: 'help text', - }); - - expect(res.status).equals(200); - }); - - it('Should store the given field details to the storage.', async () => { - const resource = await create('resource'); - const res = await request() - .post(`/api/fields/resource/${resource.name}`) - .set('x-access-token', loginRes.body.token) - .send({ - label: 'Field label', - data_type: 'text', - default: 'default value', - help_text: 'help text', - options: ['option 1', 'option 2'], - }); - - const foundField = await ResourceField.query().findById(res.body.id); - - expect(foundField.labelName).equals('Field label'); - expect(foundField.dataType).equals('text'); - expect(foundField.default).equals('default value'); - expect(foundField.helpText).equals('help text'); - expect(foundField.options.length).equals(2); - }); - }); - - describe('POST: `/fields/:field_id`', () => { - it('Should response unauthorized in case the user was not authorized.', async () => { - const field = await create('resource_field'); - const res = await request() - .post(`/api/fields/${field.id}`) - .send(); - - expect(res.status).equals(422); - expect(res.body.code).equals('validation_error'); - }); - - it('Should `label` be required.', async () => { - const field = await create('resource_field'); - const res = await request() - .post(`/api/fields/${field.id}`) - .set('x-access-token', loginRes.body.token) - .send(); - - expect(res.status).equals(422); - expect(res.body.code).equals('validation_error'); - expect(res.body.errors).include.something.deep.equals({ - msg: 'Invalid value', param: 'label', location: 'body', - }) - }); - - it('Should `data_type` be required.', async () => { - const field = await create('resource_field'); - const res = await request() - .post(`/api/fields/${field.id}`) - .set('x-access-token', loginRes.body.token) - .send(); - - expect(res.status).equals(422); - expect(res.body.code).equals('validation_error'); - expect(res.body.errors).include.something.deep.equals({ - msg: 'Invalid value', param: 'data_type', location: 'body', - }); - }); - - 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'); - expect(res.body.errors).include.something.deep.equals({ - value: 'invalid_type', - msg: 'Invalid value', - param: 'data_type', - location: 'body', - }); - }); - - it('Should response not found in case resource field id was not exist.', async () => { - const res = await request() - .post('/api/fields/100') - .set('x-access-token', loginRes.body.token) - .send({ - label: 'Field label', - data_type: 'text', - default: 'default value', - help_text: 'help text', - }); - - expect(res.status).equals(404); - expect(res.body.errors).include.something.that.deep.equals({ - type: 'FIELD_NOT_FOUND', code: 100, - }); - }); - - it('Should update details of the given resource field.', async () => { - const field = await create('resource_field'); - const res = await request() - .post(`/api/fields/${field.id}`) - .set('x-access-token', loginRes.body.token) - .send({ - label: 'Field label', - data_type: 'text', - default: 'default value', - help_text: 'help text', - }); - - const updateField = await ResourceField.query().findById(res.body.id); - - expect(updateField.labelName).equals('Field label'); - expect(updateField.dataType).equals('text'); - expect(updateField.default).equals('default value'); - expect(updateField.helpText).equals('help text'); - }); - - it('Should save the new options of the field with exist ones in the storage.', async () => { - const field = await create('resource_field', { - options: JSON.stringify([{ key: 1, value: 'Option 1' }]), - }); - const res = await request() - .post(`/api/fields/${field.id}`) - .set('x-access-token', loginRes.body.token) - .send({ - label: 'Field label', - data_type: 'text', - default: 'default value', - help_text: 'help text', - options: [ - { key: 1, value: 'Value Key 1' }, - { key: 2, value: 'Value Key 2' }, - ], - }); - - const updateField = await ResourceField.query().findById(res.body.id); - - expect(updateField.options.length).equals(2); - expect(updateField.options[0].key).equals(1); - expect(updateField.options[1].key).equals(2); - - expect(updateField.options[0].value).equals('Value Key 1'); - expect(updateField.options[1].value).equals('Value Key 2'); - }); - }); - - 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'); - const res = await request() - .post(`/api/fields/status/${field.id}`) - .set('x-access-token', loginRes.body.token) - .send({ - active: false, - }); - const storedField = await knex('resource_fields').where('id', field.id).first(); - expect(storedField.active).equals(0); - }); - }); - - 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); - }); - }); -}); diff --git a/common/routes/oauth.js b/common/routes/oauth.js deleted file mode 100644 index 55363e0f9..000000000 --- a/common/routes/oauth.js +++ /dev/null @@ -1,10 +0,0 @@ -import { request, expect } from '~/testInit'; - -describe('routes: /oauth2/', () => { - describe('POST `/api/oauth/token`', () => { - it('Should `crediential` be required.', async () => { - const res = await request().post('/api/oauth2/token').send({}); - expect(res.status).equals(200); - }); - }); -}); diff --git a/common/routes/resources.js b/common/routes/resources.js deleted file mode 100644 index e69de29bb..000000000 diff --git a/common/routes/roles.js b/common/routes/roles.js deleted file mode 100644 index d2ed6467e..000000000 --- a/common/routes/roles.js +++ /dev/null @@ -1,276 +0,0 @@ -import { request, expect, create } from '~/testInit'; -import knex from '@/database/knex'; - -describe('routes: `/roles/`', () => { - describe('POST: `/roles/`', () => { - it('Should `name` be required.', async () => { - const res = await request().post('/api/roles').send(); - - expect(res.status).equals(422); - expect(res.body.code).equals('validation_error'); - - const foundNameParam = res.body.errors.find((err) => err.param === 'name'); - expect(!!foundNameParam).equals(true); - }); - - it('Should `permissions` be array.', async () => { - const res = await request().post('/api/roles').send({ - permissions: 'not_array', - }); - - expect(res.status).equals(422); - expect(res.body.code).equals('validation_error'); - - const foundPermissionsPerm = res.body.errors.find((err) => err.param === 'permissions'); - expect(!!foundPermissionsPerm).equals(true); - }); - - it('Should `permissions.resource_slug` be slug.', async () => { - const res = await request().post('/api/roles').send({ - permissions: [{ slug: 'not slug' }], - }); - - expect(res.status).equals(422); - expect(res.body.code).equals('validation_error'); - - const foundPerm = res.body.errors.find((err) => err.param === 'permissions[0].resource_slug'); - expect(!!foundPerm).equals(true); - }); - - it('Should `permissions.permissions be array.`', async () => { - const res = await request().post('/api/roles').send({ - permissions: [{ permissions: 'not_array' }], - }); - - expect(res.status).equals(422); - expect(res.body.code).equals('validation_error'); - - const foundPerm = res.body.errors.find((err) => err.param === 'permissions[0].permissions'); - expect(!!foundPerm).equals(true); - }); - - it('Should response bad request in case the resource slug was invalid.', async () => { - const res = await request().post('/api/roles').send({ - name: 'name', - permissions: [{ resource_slug: 'invalid', permissions: ['item'] }], - }); - - expect(res.status).equals(400); - expect(res.body.errors).include.something.that.deep.equals({ - type: 'RESOURCE_SLUG_NOT_FOUND', - code: 100, - resources: ['invalid'], - }); - }); - - it('Should response bad request in case the permission type was invalid.', async () => { - const res = await request().post('/api/roles').send({ - name: 'name', - permissions: [{ resource_slug: 'items', permissions: ['item'] }], - }); - - expect(res.status).equals(400); - expect(res.body.errors).include.something.that.deep.equals({ - type: 'PERMISSIONS_SLUG_NOT_FOUND', - code: 200, - permissions: [{ resource_slug: 'items', permissions: ['item'] }], - }); - }); - - it('Should save the submit resources in the storage in case was not exist.', async () => { - await request().post('/api/roles').send({ - name: 'Role Name', - permissions: [{ resource_slug: 'items', permissions: ['create'] }], - }); - - const storedResources = await knex('resources'); - expect(storedResources).to.have.lengthOf(1); - }); - - it('Should save the submit permissions in the storage in case was not exist.', async () => { - await request().post('/api/roles').send({ - name: 'Role Name', - permissions: [{ resource_slug: 'items', permissions: ['create'] }], - }); - - const storedPermissions = await knex('permissions'); - expect(storedPermissions).to.have.lengthOf(1); - }); - - it('Should save the submit role in the storage with associated resource and permissions.', async () => { - await request().post('/api/roles').send({ - name: 'Role Name', - description: 'Role description', - permissions: [{ resource_slug: 'items', permissions: ['create', 'view'] }], - }); - - const storedRoles = await knex('roles'); - const storedResource = await knex('resources').where('name', 'items').first(); - const storedPermissions = await knex('permissions'); - const roleHasPermissions = await knex('role_has_permissions') - .where('role_id', storedRoles[0].id); - - expect(storedRoles).to.have.lengthOf(1); - expect(storedRoles[0].name).equals('Role Name'); - expect(storedRoles[0].description).equals('Role description'); - - expect(roleHasPermissions).to.have.lengthOf(2); - expect(roleHasPermissions[0].role_id).equals(storedRoles[0].id); - expect(roleHasPermissions[0].permission_id).equals(storedPermissions[0].id); - expect(roleHasPermissions[0].resource_id).equals(storedResource.id); - }); - - it('Should response success with correct data format.', async () => { - const res = await request().post('/api/roles').send(); - - expect(res.status).equals(422); - }); - - it('Should save the given role details in the storage.', async () => { - const res = await request().post('/api/roles').send(); - - expect(res.status).equals(422); - }); - }); - - describe('POST: `/roles/:id`', () => { - it('Should response not found in case role was not exist.', async () => { - const res = await request().post('/api/roles/10').send({ - name: 'Role Name', - description: 'Description', - permissions: [ - { resource_slug: 'items', permissions: ['create'] }, - ], - }); - - expect(res.status).equals(404); - }); - - it('Should `name` be required.', async () => { - const role = await create('role'); - const res = await request().post(`/api/roles/${role.id}`).send(); - - expect(res.status).equals(422); - expect(res.body.code).equals('validation_error'); - - const foundNameParam = res.body.errors.find((err) => err.param === 'name'); - expect(!!foundNameParam).equals(true); - }); - - it('Should `permissions` be array.', async () => { - const role = await create('role'); - const res = await request().post(`/api/roles/${role.id}`).send({ - permissions: 'not_array', - }); - - expect(res.status).equals(422); - expect(res.body.code).equals('validation_error'); - - const foundPermissionsPerm = res.body.errors.find((err) => err.param === 'permissions'); - expect(!!foundPermissionsPerm).equals(true); - }); - - it('Should `permissions.resource_slug` be slug.', async () => { - const role = await create('role'); - const res = await request().post(`/api/roles/${role.id}`).send({ - permissions: [{ slug: 'not slug' }], - }); - - expect(res.status).equals(422); - expect(res.body.code).equals('validation_error'); - - const foundPerm = res.body.errors.find((err) => err.param === 'permissions[0].resource_slug'); - expect(!!foundPerm).equals(true); - }); - - it('Should `permissions.permissions be array.`', async () => { - const role = await create('role'); - const res = await request().post(`/api/roles/${role.id}`).send({ - permissions: [{ permissions: 'not_array' }], - }); - - expect(res.status).equals(422); - expect(res.body.code).equals('validation_error'); - - const foundPerm = res.body.errors.find((err) => err.param === 'permissions[0].permissions'); - expect(!!foundPerm).equals(true); - }); - - it('Should response bad request in case the resource slug was invalid.', async () => { - const role = await create('role'); - const res = await request().post(`/api/roles/${role.id}`).send({ - name: 'name', - permissions: [{ resource_slug: 'invalid', permissions: ['item'] }], - }); - - expect(res.status).equals(400); - expect(res.body.errors).include.something.that.deep.equals({ - type: 'RESOURCE_SLUG_NOT_FOUND', - code: 100, - resources: ['invalid'], - }); - }); - - it('Should response bad request in case the permission type was invalid.', async () => { - const role = await create('role'); - const res = await request().post(`/api/roles/${role.id}`).send({ - name: 'name', - permissions: [{ resource_slug: 'items', permissions: ['item'] }], - }); - - expect(res.status).equals(400); - expect(res.body.errors).include.something.that.deep.equals({ - type: 'PERMISSIONS_SLUG_NOT_FOUND', - code: 200, - permissions: [{ resource_slug: 'items', permissions: ['item'] }], - }); - }); - - it('Should save the submit resources 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', - permissions: [{ resource_slug: 'items', permissions: ['create'] }], - }); - - const storedResources = await knex('resources'); - expect(storedResources).to.have.lengthOf(1); - }); - - 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', - permissions: [{ resource_slug: 'items', permissions: ['create'] }], - }); - - const storedPermissions = await knex('permissions'); - expect(storedPermissions).to.have.lengthOf(1); - }); - }); - - describe('DELETE: `/roles/:id`', () => { - it('Should response not found in case the role was not exist.', async () => { - const res = await request().delete('/api/roles/100').send(); - - expect(res.status).equals(404); - }); - - it('Should not delete the predefined role.', async () => { - const role = await create('role', { predefined: true }); - const res = await request().delete(`/api/roles/${role.id}`).send(); - - expect(res.status).equals(400); - }); - - it('Should delete the given role and its relations with permissions and resources.', async () => { - const role = await create('role'); - await create('role_has_permission', { role_id: role.id }); - - await request().delete(`/api/roles/${role.id}`).send(); - - const storedRole = await knex('roles').where('id', role.id).first(); - expect(storedRole).to.equals(undefined); - }); - }); -}); diff --git a/common/routes/users.test.js b/common/routes/users.test.js deleted file mode 100644 index 438c08240..000000000 --- a/common/routes/users.test.js +++ /dev/null @@ -1,425 +0,0 @@ -import knex from '@/database/knex'; -import { - request, - expect, - create, - make, - login, - createTenantFactory, - createTenant, -} from '~/testInit'; - -let tenantDb; -let tenantFactory; - -describe('routes: `/routes`', () => { - beforeEach(async () => { - tenantDb = await createTenant(); - tenantFactory = createTenantFactory(tenantDb); - - 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') - .set('x-access-token', loginRes.body.token) - .send(); - - expect(res.status).equals(422); - - const foundFirstNameParam = res.body.errors.find((error) => error.param === 'first_name'); - expect(!!foundFirstNameParam).equals(true); - }); - - it('Should `last_name` be required.', async () => { - const res = await request() - .post('/api/users') - .set('x-access-token', loginRes.body.token) - .send(); - - expect(res.status).equals(422); - - const foundFirstNameParam = res.body.errors.find((error) => error.param === 'last_name'); - expect(!!foundFirstNameParam).equals(true); - }); - - it('Should `email` be required.', async () => { - const res = await request() - .post('/api/users') - .set('x-access-token', loginRes.body.token) - .send(); - - expect(res.status).equals(422); - - const foundEmailParam = res.body.errors.find((error) => error.param === 'email'); - expect(!!foundEmailParam).equals(true); - }); - - it('Should be `email` be valid format.', async () => { - const user = make('user'); - 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); - - const foundEmailParam = res.body.errors.find((error) => error.param === 'email'); - expect(!!foundEmailParam).equals(true); - }); - - it('Should `phone_number` be valid format.', async () => { - const user = make('user'); - 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); - - const phoneNumberParam = res.body.errors.find((error) => error.param === 'phone_number'); - expect(!!phoneNumberParam).equals(true); - }); - - it('Should `password` be required.', async () => { - const res = await request() - .post('/api/users') - .set('x-access-token', loginRes.body.token) - .send(); - - expect(res.status).equals(422); - - const passwordParam = res.body.errors.find((error) => error.param === 'password'); - expect(!!passwordParam).equals(true); - }); - - it('Should password be equals confirm_password.', async () => { - const res = await request() - .post('/api/users') - .set('x-access-token', loginRes.body.token) - .send({ - password: '123123', - }); - - expect(res.status).equals(422); - - const passwordParam = res.body.errors.find((error) => error.param === 'password'); - expect(!!passwordParam).equals(true); - }); - - it('Should `status` be boolean', async () => { - const res = await request() - .post('/api/users') - .set('x-access-token', loginRes.body.token) - .send({ - status: 'not_boolean', - }); - - expect(res.status).equals(422); - - const statusParam = res.body.errors.find((error) => error.param === 'status'); - expect(!!statusParam).equals(true); - }); - - it('Should response bad request in case email was already exist.', async () => { - const user = await create('user'); - 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({ - type: 'EMAIL_ALREADY_EXIST', code: 100, - }); - }); - - it('Should response bad request in case phone number was already exist.', async () => { - 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({ - type: 'PHONE_NUMBER_ALREADY_EXIST', code: 120, - }); - }); - - it('Should response success with correct data type.', async () => { - 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.', 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}`) - .set('x-access-token', loginRes.body.token) - .send(); - - expect(res.status).equals(422); - - const foundFirstNameParam = res.body.errors.find((error) => error.param === 'first_name'); - expect(!!foundFirstNameParam).equals(true); - }); - - it('Should `last_name` be required.', async () => { - const user = await create('user'); - const res = await request() - .post(`/api/users/${user.id}`) - .set('x-access-token', loginRes.body.token) - .send(); - - expect(res.status).equals(422); - - const foundFirstNameParam = res.body.errors.find((error) => error.param === 'last_name'); - expect(!!foundFirstNameParam).equals(true); - }); - - it('Should `email` be required.', async () => { - const user = await create('user'); - const res = await request() - .post(`/api/users/${user.id}`) - .set('x-access-token', loginRes.body.token) - .send(); - - expect(res.status).equals(422); - - const foundEmailParam = res.body.errors.find((error) => error.param === 'email'); - expect(!!foundEmailParam).equals(true); - }); - - it('Should be `email` be valid format.', async () => { - const user = await create('user'); - 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); - - const foundEmailParam = res.body.errors.find((error) => error.param === 'email'); - expect(!!foundEmailParam).equals(true); - }); - - it('Should `phone_number` be valid format.', async () => { - const user = create('user'); - 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); - - const phoneNumberParam = res.body.errors.find((error) => error.param === 'phone_number'); - expect(!!phoneNumberParam).equals(true); - }); - - it('Should `password` be required.', async () => { - const user = create('user'); - const res = await request() - .post(`/api/users/${user.id}`) - .set('x-access-token', loginRes.body.token) - .send(); - - expect(res.status).equals(422); - - const passwordParam = res.body.errors.find((error) => error.param === 'password'); - expect(!!passwordParam).equals(true); - }); - - it('Should password be equals confirm_password.', async () => { - const user = create('user'); - const res = await request() - .post(`/api/users/${user.id}`) - .set('x-access-token', loginRes.body.token) - .send({ - password: '123123', - }); - - expect(res.status).equals(422); - - const passwordParam = res.body.errors.find((error) => error.param === 'password'); - expect(!!passwordParam).equals(true); - }); - - it('Should `status` be boolean', async () => { - const user = create('user'); - 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); - - const statusParam = res.body.errors.find((error) => error.param === 'status'); - expect(!!statusParam).equals(true); - }); - }); - - describe('GET: `/users/:id`', () => { - it('Should not success if the user was not authorized.', () => { - - }); - - it('Should response not found if the user was not exist.', async () => { - 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}`) - .set('x-access-token', loginRes.body.token) - .send(); - - expect(res.status).equals(200); - }); - }); - - describe('DELETE: `/users/:id`', () => { - it('Should not success if the user was not authorized.', () => { - - }); - - it('Should response not found if the user was not exist.', async () => { - 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}`) - .set('x-access-token', loginRes.body.token) - .send(); - - 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}`) - .set('x-access-token', loginRes.body.token) - .send(); - - const storedUsers = await knex('users').where('id', user.id); - expect(storedUsers).to.have.lengthOf(0); - }); - }); -});