WIP Metadata class.

This commit is contained in:
Ahmed Bouhuolia
2019-09-08 02:41:46 +02:00
parent 70809cb05c
commit 9a8de9ca7d
29 changed files with 1707 additions and 98 deletions

View File

@@ -0,0 +1,16 @@
import { create, expect } from '~/testInit';
import Account from '@/models/Account';
// eslint-disable-next-line no-unused-vars
import AccountType from '@/models/AccountType';
describe('Model: Account', () => {
it('Should account model belongs to the associated account type model.', async () => {
const accountType = await create('account_type');
const account = await create('account', { account_type_id: accountType.id });
const accountModel = await Account.where('id', account.id).fetch();
const accountTypeModel = await accountModel.type().fetch();
expect(accountTypeModel.attributes.id).equals(account.id);
});
});

View File

@@ -0,0 +1,16 @@
import { create, expect } from '~/testInit';
import '@/models/Account';
import AccountType from '@/models/AccountType';
describe.only('Model: AccountType', () => {
it('Shoud account type model has many associated accounts.', async () => {
const accountType = await create('account_type');
await create('account', { account_type_id: accountType.id });
await create('account', { account_type_id: accountType.id });
const accountTypeModel = await AccountType.where('id', accountType.id).fetch();
const typeAccounts = await accountTypeModel.accounts().fetch();
expect(typeAccounts.length).equals(2);
});
});

View File

@@ -0,0 +1,197 @@
import sinon from 'sinon';
import { create, expect } from '~/testInit';
import Setting from '@/models/Setting';
import knex from '../../src/database/knex';
describe('Model: Setting', () => {
afterEach(() => {
Setting.purgeMetadata();
});
describe('Setting.AllMeta()', async () => {
it('Should fetch all metadata from storage in the first call.', async () => {
await create('setting');
const querySpy = sinon.spy(Setting, 'query');
const metadata = await Setting.allMeta();
expect(querySpy.calledOnce).equals(true);
expect(metadata).to.have.lengthOf(1);
querySpy.restore();
});
it('Should get all meta data from stored cache in the second call.', async () => {
await create('setting');
const querySpy = sinon.spy(Setting, 'query');
await Setting.allMeta();
await Setting.allMeta();
expect(querySpy.calledOnce).equals(true);
expect(Setting.metadata).to.have.lengthOf(1);
querySpy.restore();
});
});
describe('Setting.getMeta()', () => {
it('Should fetch metadata of the given key from storage.', async () => {
const setting = await create('setting');
const metadata = await Setting.getMeta(setting.key);
expect(metadata).equals(setting.value);
});
it('Should retrieve the default value if the metadata key was not found.', async () => {
const metadata = await Setting.getMeta('setting', 'default');
expect(metadata).equals('default');
});
it('Should get the same metadata key from cache in the second call.', async () => {
const setting = await create('setting');
await create('setting');
const querySpy = sinon.spy(Setting, 'query');
await Setting.getMeta(setting.key);
expect(querySpy.calledOnce).equals(true);
await Setting.getMeta(setting.key);
expect(querySpy.calledOnce).equals(true);
querySpy.restore();
});
it('Should get the different metadata key from storage.', async () => {
const setting = await create('setting');
const settingAnother = await create('setting');
const querySpy = sinon.spy(Setting, 'query');
await Setting.getMeta(setting.key);
expect(querySpy.calledOnce).equals(true);
await Setting.getMeta(settingAnother.key);
expect(querySpy.calledOnce).equals(true);
querySpy.restore();
});
it('Should hard fetching the metadata from the storage when passing `force` parameter.', async () => {
const setting = await create('setting');
await create('setting');
const querySpy = sinon.spy(Setting, 'query');
await Setting.allMeta();
expect(querySpy.calledOnce).equals(true);
expect(Setting.metadata).to.have.lengthOf(2);
await Setting.getMeta(setting.key, null, true);
expect(querySpy.calledTwice).equals(true);
expect(Setting.metadata).to.have.lengthOf(2);
querySpy.restore();
});
});
describe('Setting.setMeta()', () => {
it('Should mark the given metadata as updated in the stack.', async () => {
const setting = await create('setting');
await Setting.setMeta(setting.key, 'Ahmed');
const foundMeta = Setting.metadata.find((metadata) => (
metadata.key === setting.key && metadata.markAsUpdated === true
&& metadata.value === 'Ahmed'
));
expect(!!foundMeta).equals(true);
});
it('Should mark the set metadata as inserted metadata in the stack.', async () => {
await create('setting');
await Setting.setMeta('key', 'value');
const foundMeta = Setting.metadata.find((metadata) => (
metadata.key === 'key' && metadata.markAsInserted === true
&& metadata.value === 'value'
));
expect(!!foundMeta).equals(true);
});
it('Should fetch the metadata from the storage in case the metadata was exist.', async () => {
const setting = await create('setting');
const querySpy = sinon.spy(Setting, 'query');
await Setting.setMeta(setting.key, 'value');
expect(querySpy.calledOnce).equals(true);
await Setting.setMeta(setting.key, 'updated-value');
expect(querySpy.calledOnce).equals(true);
});
it('Should mark the updated bluk metadata as updated in the stock.', async () => {
});
it('Should mark the inserted bluk metadata as inserted in the stock.', async () => {
});
});
describe('Setting.removeMeta()', () => {
it('Should mark the given metadata as deleted', async () => {
const setting = await create('setting');
await Setting.removeMeta(setting.key);
const foundMeta = Setting.metadata.find((metadata) => (
metadata.key === setting.key && metadata.markAsDeleted === true
));
expect(!!foundMeta).equals(true);
});
it('Should not query the storage when found cached the metadata.', async () => {
const setting = await create('setting');
await Setting.allMeta();
const querySpy = sinon.spy(Setting, 'query');
await Setting.removeMeta(setting.key);
expect(querySpy.calledOnce).equals(false);
querySpy.restore();
});
});
describe('Setting.saveMeta()', () => {
it('Should insert the metadata that set to the stock.', async () => {
await Setting.setMeta('key', 'value');
await Setting.saveMeta();
const storedMetadata = await knex('settings');
expect(storedMetadata).to.have.lengthOf(1);
});
it('Should update the metadata that updated in the stock.', async () => {
const setting = await create('setting');
await Setting.setMeta(setting.key, 'value');
await Setting.saveMeta();
const storedMetadata = await knex('settings');
expect(storedMetadata).to.have.lengthOf(1);
expect(storedMetadata[0].value).equals('value');
});
it('Should delete the metadata that removed from the stock.', async () => {
const setting = await create('setting');
await Setting.removeMeta(setting.key);
await Setting.saveMeta();
const storedMetadata = await knex('settings');
expect(storedMetadata).to.have.lengthOf(0);
});
});
});

View File

@@ -0,0 +1,44 @@
import { request, expect } from '~/testInit';
describe.only('routes: `/accountOpeningBalance`', () => {
describe('POST `/accountOpeningBalance`', () => {
it('Should `accounts` be array type.', async () => {
const res = await request().post('/api/accountOpeningBalance').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/accountOpeningBalance').send({
accounts: 1000,
});
expect(res.status).equals(422);
expect(res.body.code).equals('VALIDATION_ERROR');
});
it('Should `accounts.*.debit` be numeric.', async () => {
const res = await request().post('/api/accountOpeningBalance').send({
accounts: [{ id: 'id' }],
});
expect(res.status).equals(422);
});
it.only('Should `accounts.*.id` be exist in the storage.', async () => {
const res = await request().post('/api/accountOpeningBalance').send({
accounts: [
{ id: 100, credit: 100, debit: 100 },
],
});
expect(res.status).equals(422);
expect(res.body.errors).include.something.that.deep.equals({
type: 'NOT_FOUND_ACCOUNT', code: 100, ids: [100],
});
});
});
});

View File

@@ -0,0 +1,138 @@
import { request, expect, create } from '~/testInit';
import knex from '@/database/knex';
describe('routes: /accounts/', () => {
describe('POST `/accounts`', () => {
it('Should `name` be required.', async () => {
const res = await request().post('/api/accounts').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();
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();
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,
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equals({
type: 'PARENT_CATEGORY_NOT_FOUND', code: 100,
});
});
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,
});
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').send({
name: 'Name',
description: 'description here',
account_type_id: account.account_type_id,
parent_account_id: account.id,
});
expect(res.status).equals(200);
});
});
describe('GET: `/accounts`', () => {
});
describe('DELETE: `/accounts`', () => {
it('Should response not found in case account was not exist.', async () => {
const res = await request().delete('/api/accounts/10').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}`);
const foundAccounts = await knex('accounts').where('id', account.id);
expect(foundAccounts).to.have.lengthOf(1);
});
});
});

View File

@@ -1,5 +1,6 @@
import { request, expect, create } from '~/testInit';
import { hashPassword } from '@/utils';
import knex from '@/database/knex';
describe('routes: /auth/', () => {
describe('POST `/api/auth/login`', () => {
@@ -110,61 +111,136 @@ describe('routes: /auth/', () => {
});
});
// describe('POST: `auth/send_reset_password`', () => {
describe('POST: `/auth/send_reset_password`', () => {
it('Should `email` be required.', async () => {
const res = await request().post('/api/auth/send_reset_password').send();
// it('Should `email` be required.', () => {
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
});
// });
it('Should response unproccessable if the email address was invalid.', async () => {
const res = await request().post('/api/auth/send_reset_password').send({
email: 'invalid_email',
});
// it('Should response unproccessable if the email address was invalid.', () => {
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
});
// });
it('Should response unproccessable if the email address was not exist.', async () => {
const res = await request().post('/api/auth/send_reset_password').send({
email: 'admin@admin.com',
});
// it('Should response unproccessable if the email address was not exist.', () => {
expect(res.status).equals(422);
expect(res.body.errors).include.something.that.deep.equals({
type: 'EMAIL_NOT_FOUND', code: 100,
});
});
// });
it('Should delete all already tokens that associate to the given email.', async () => {
const user = await create('user');
const token = '123123';
// it('Should delete all already tokens that associate to the given email.', () => {
await knex('password_resets').insert({ email: user.email, token });
await request().post('/api/auth/send_reset_password').send({
email: user.email,
});
// });
const oldPasswordToken = await knex('password_resets').where('token', token);
// it('Should store new token associate with the given email.', () => {
expect(oldPasswordToken).to.have.lengthOf(0);
});
// });
it('Should store new token associate with the given email.', async () => {
const user = await create('user');
await request().post('/api/auth/send_reset_password').send({
email: user.email,
});
// it('Should response success if the email was exist.', () => {
const token = await knex('password_resets').where('email', user.email);
// });
expect(token).to.have.lengthOf(1);
});
// it('Should token be stored to the table after success request.', () => {
it('Should response success if the email was exist.', async () => {
const user = await create('user');
const res = await request().post('/api/auth/send_reset_password').send({
email: user.email,
});
// });
// });
expect(res.status).equals(200);
});
});
// describe('POST: `/auth/reset/:token`', () => {
describe('POST: `/auth/reset/:token`', () => {
// it('Should response forbidden if the token was invalid.', () => {
// it('Should response forbidden if the token was invalid.', () => {
// });
// });
it('Should response forbidden if the token was expired.', () => {
// it('Should response forbidden if the token was expired.', () => {
});
// });
it('Should `password` be required.', async () => {
const passwordReset = await create('password_reset');
const res = await request().post(`/api/reset/${passwordReset.token}`).send();
// it('Should password be required.', () => {
expect(res.status).equals(422);
expect(res.body.code).equals('VALIDATION_ERROR');
// });
const paramsErrors = res.body.errors.map((error) => error.param);
expect(paramsErrors).to.include('password');
});
// it('Should password and confirm_password be equal.', () => {
it('Should password and confirm_password be equal.', async () => {
const passwordReset = await create('password_reset');
const res = await request().post(`/api/reset/${passwordReset.token}`).send({
password: '123123',
});
// });
expect(res.status).equals(422);
expect(res.body.code).equals('VALIDATION_ERROR');
// it('Should token be deleted after success response.', () => {
const paramsErrors = res.body.errors.map((error) => error.param);
expect(paramsErrors).to.include('password');
});
// });
it('Should response success with correct data form.', async () => {
const passwordReset = await create('password_reset');
const res = await request().post(`/api/reset/${passwordReset.token}`).send({
password: '123123',
confirm_password: '123123',
});
// it('Should password be updated after success response.', () => {
expect(res.status).equals(200);
});
// })
// });
it('Should token be deleted after success response.', async () => {
const passwordReset = await create('password_reset');
await request().post(`/api/reset/${passwordReset.token}`).send({
password: '123123',
confirm_password: '123123',
});
const foundTokens = await knex('password_resets').where('email', passwordReset.email);
expect(foundTokens).to.have.lengthOf(0);
});
it('Should password be updated after success response.', async () => {
const user = await create('user');
const passwordReset = await create('password_reset', { user_id: user.id });
await request().post(`/api/reset/${passwordReset.token}`).send({
password: '123123',
confirm_password: '123123',
});
const foundUser = await knex('users').where('id', user.id);
expect(foundUser.id).equals(user.id);
expect(foundUser.password).not.equals(user.password);
});
});
});

View File

@@ -0,0 +1,26 @@
describe('routes: `/roles/`', () => {
describe('POST: `/roles/`', () => {
it('Should name be required.', () => {
});
it('Should `permissions` be ', () => {
});
it('Should response success with correct data format.', async () => {
});
it('Should save the given role details in the storage.', () => {
});
});
describe('DELETE: `/roles/:id`', () => {
it('Should not delete the predefined role.', () => {
});
});
});

View File

@@ -1,3 +1,10 @@
import knex from '@/database/knex';
import {
request,
expect,
create,
make,
} from '~/testInit';
describe('routes: `/routes`', () => {
describe('POST: `/routes`', () => {
@@ -5,16 +12,266 @@ describe('routes: `/routes`', () => {
});
it('Should `email` be required.', () => {
it('Should `first_name` be required.', async () => {
const res = await request().post('/api/users');
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');
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');
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').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').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').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').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').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').send({
first_name: user.first_name,
last_name: user.last_name,
email: user.email,
phone_number: user.phone_number,
password: '123123123',
confirm_password: '123123123',
status: 1,
});
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');
const res = await request().post('/api/users').send({
first_name: user.first_name,
last_name: user.last_name,
email: user.email,
phone_number: user.phone_number,
password: user.password,
confirm_password: user.password,
status: 1,
});
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');
const res = await request().post('/api/users').send({
first_name: user.first_name,
last_name: user.last_name,
email: user.email,
phone_number: user.phone_number,
password: user.password,
confirm_password: user.password,
status: 1,
});
expect(res.status).equals(200);
});
});
describe('POST: `/users/:id`', () => {
it('Should create a new user if the user was not authorized.', () => {
});
it('Should `password` be required.', () => {
it('Should `first_name` be required.', async () => {
const user = await create('user');
const res = await request().post(`/api/users/${user.id}`);
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}`);
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}`);
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}`).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}`).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}`).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}`).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}`).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 `status` be boolean', () => {
it('Should response not found if the user was not exist.', async () => {
const res = await request().get('/api/users/10').send();
expect(res.status).equals(404);
});
it('Should response success if the user was exist.', async () => {
const user = await create('user');
const res = await request().get(`/api/users/${user.id}`).send();
expect(res.status).equals(200);
});
});
@@ -23,20 +280,26 @@ describe('routes: `/routes`', () => {
});
it('Should not success if the user was pre-defined user.', () => {
it('Should response not found if the user was not exist.', async () => {
const res = await request().delete('/api/users/10').send();
expect(res.status).equals(404);
});
it('Should response not found if the user was not exist.', () => {
it('Should response success if the user was exist.', async () => {
const user = await create('user');
const res = await request().delete(`/api/users/${user.id}`).send();
expect(res.status).equals(200);
});
it('Should response success if the user was exist.', () => {
it('Should delete the give user from the storage.', async () => {
const user = await create('user');
await request().delete(`/api/users/${user.id}`).send();
});
it('Should delete the give user after success response.', () => {
const storedUsers = await knex('users').where('id', user.id);
expect(storedUsers).to.have.lengthOf(0);
});
});
});