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,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);
});
});
});