WIP Items module.

This commit is contained in:
Ahmed Bouhuolia
2019-09-03 02:07:28 +02:00
parent cb8c294d74
commit 70809cb05c
142 changed files with 12674 additions and 64 deletions

View File

@@ -0,0 +1,11 @@
on: '2'
services:
mysql:
image: mysql/mysql-server:5.7
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=moosher_test
- MYSQL_USER=moosher
- MYSQL_PASSWORD=moosher

View File

@@ -0,0 +1,28 @@
import { create, expect } from '~/testInit';
import Item from '@/models/Item';
// eslint-disable-next-line no-unused-vars
import itemCategory from '@/models/ItemCategory';
import '@/models/ItemMetadata';
describe('Model: Item', () => {
it('Should item model belongs to the associated category model.', async () => {
const category = await create('item_category');
const item = await create('item', { category_id: category.id });
const itemModel = await Item.where('id', item.id).fetch();
const itemCategoryModel = await itemModel.category().fetch();
expect(itemCategoryModel.attributes.id).equals(category.id);
});
it('Should item model has many metadata that assciated to the item model.', async () => {
const item = await create('item');
await create('item_metadata', { item_id: item.id });
await create('item_metadata', { item_id: item.id });
const itemModel = await Item.where('id', item.id).fetch();
const itemMetadataCollection = await itemModel.metadata().fetch();
expect(itemMetadataCollection.length).equals(2);
});
});

View File

@@ -0,0 +1,16 @@
import { create, expect } from '~/testInit';
import '@/models/Item';
import ItemCategory from '@/models/ItemCategory';
describe('Model: ItemCategories', () => {
it('Shoud item category model has many associated items.', async () => {
const category = await create('item_category');
await create('item', { category_id: category.id });
await create('item', { category_id: category.id });
const categoryModel = await ItemCategory.where('id', category.id).fetch();
const categoryItems = await categoryModel.items().fetch();
expect(categoryItems.length).equals(2);
});
});

View File

@@ -0,0 +1,170 @@
import { request, expect, create } from '~/testInit';
import { hashPassword } from '@/utils';
describe('routes: /auth/', () => {
describe('POST `/api/auth/login`', () => {
it('Should `crediential` be required.', async () => {
const res = await request().post('/api/auth/login').send({});
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('crediential');
});
it('Should `password` be required.', async () => {
const res = await request().post('/api/auth/login').send();
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 the min length of the `password` be 5 ch.', async () => {
const res = await request().post('/api/auth/login').send({
crediential: 'admin@admin.com',
password: 'test',
});
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 be a valid email format in crediential attribute.', async () => {
const res = await request().post('/api/auth/login').send({
crediential: 'admin',
password: 'test',
});
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 not authenticate with wrong user email and password.', async () => {
const res = await request().post('/api/auth/login').send({
crediential: 'admin@admin.com',
password: 'admin',
});
expect(res.body.errors).include.something.that.deep.equals({
type: 'INVALID_DETAILS', code: 100,
});
});
it('Should not authenticate in case user was not active.', async () => {
const user = await create('user', {
active: false,
});
const res = await request().post('/api/auth/login').send({
crediential: user.email,
password: 'admin',
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equals({
type: 'INCORRECT_PASSWORD', code: 120,
});
});
it('Should authenticate with correct email and password and active user.', async () => {
const user = await create('user', {
password: hashPassword('admin'),
});
const res = await request().post('/api/auth/login').send({
crediential: user.email,
});
expect(res.status).equals(200);
});
it('Should autheticate success with correct phone number and password.', async () => {
const password = hashPassword('admin');
const user = await create('user', {
phone_number: '0920000000',
password,
});
const res = await request().post('/api/auth/login').send({
crediential: user.phone_number,
password,
});
expect(res.status).equals(200);
});
it('Should last login date be saved after success login.', async () => {
const user = await create('user', {
password: hashPassword('admin'),
});
const res = await request().post('/api/auth/login').send({
crediential: user.email,
password: 'admin',
});
expect(res.status).equals(200);
});
});
// describe('POST: `auth/send_reset_password`', () => {
// it('Should `email` be required.', () => {
// });
// it('Should response unproccessable if the email address was invalid.', () => {
// });
// it('Should response unproccessable if the email address was not exist.', () => {
// });
// it('Should delete all already tokens that associate to the given email.', () => {
// });
// it('Should store new token associate with the given email.', () => {
// });
// it('Should response success if the email was exist.', () => {
// });
// it('Should token be stored to the table after success request.', () => {
// });
// });
// describe('POST: `/auth/reset/:token`', () => {
// it('Should response forbidden if the token was invalid.', () => {
// });
// it('Should response forbidden if the token was expired.', () => {
// });
// it('Should password be required.', () => {
// });
// it('Should password and confirm_password be equal.', () => {
// });
// it('Should token be deleted after success response.', () => {
// });
// it('Should password be updated after success response.', () => {
// })
// });
});

View File

@@ -0,0 +1,176 @@
import { request, expect, create } from '~/testInit';
import knex from '@/database/knex';
describe('routes: `/items`', () => {
describe('POST: `/items`', () => {
it('Should not create a new item if the user was not authorized.', async () => {
});
it('Should `name` be required.', async () => {
const res = await request().post('/api/items').send();
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
const foundNameParam = res.body.errors.find((error) => error.param === 'name');
expect(!!foundNameParam).equals(true);
});
it('Should `type_id` be required.', async () => {
const res = await request().post('/api/items').send();
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
const foundTypeParam = res.body.errors.find((error) => error.param === 'type_id');
expect(!!foundTypeParam).equals(true);
});
it('Should `buy_price` be numeric.', async () => {
const res = await request().post('/api/items').send({
buy_price: 'not_numeric',
});
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
const foundBuyPrice = res.body.errors.find((error) => error.param === 'buy_price');
expect(!!foundBuyPrice).equals(true);
});
it('Should `cost_price` be numeric.', async () => {
const res = await request().post('/api/items').send({
cost_price: 'not_numeric',
});
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
const foundCostParam = res.body.errors.find((error) => error.param === 'cost_price');
expect(!!foundCostParam).equals(true);
});
it('Should `buy_account_id` be integer.', async () => {
const res = await request().post('/api/items').send({
buy_account_id: 'not_numeric',
});
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
const foundAccount = res.body.errors.find((error) => error.param === 'buy_account_id');
expect(!!foundAccount).equals(true);
});
it('Should `cost_account_id` be integer.', async () => {
const res = await request().post('/api/items').send({
cost_account_id: 'not_numeric',
});
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
const foundAccount = res.body.errors.find((error) => error.param === 'cost_account_id');
expect(!!foundAccount).equals(true);
});
it('Should `cost_account_id` be required if `cost_price` was presented.', async () => {
});
it('Should `buy_account_id` be required if `buy_price` was presented.', async () => {
});
it('Should response bad request in case cost account was not exist.', async () => {
const res = await request().post('/api/items').send({
name: 'Item Name',
type_id: 1,
buy_price: 10.2,
cost_price: 20.2,
sell_account_id: 10,
cost_account_id: 20,
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equals({
type: 'COST_ACCOUNT_NOT_FOUND', code: 100,
});
});
it('Should response bad request in case sell account was not exist.', async () => {
const res = await request().post('/api/items').send({
name: 'Item Name',
type_id: 1,
buy_price: 10.2,
cost_price: 20.2,
sell_account_id: 10,
cost_account_id: 20,
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equals({
type: 'SELL_ACCOUNT_NOT_FOUND', code: 120,
});
});
it('Should response not category found in case item category was not exist.', async () => {
const res = await request().post('/api/items').send({
name: 'Item Name',
type_id: 1,
buy_price: 10.2,
cost_price: 20.2,
sell_account_id: 10,
cost_account_id: 20,
category_id: 20,
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equals({
type: 'ITEM_CATEGORY_NOT_FOUND', code: 140,
});
});
it('Should response success with correct data format.', async () => {
const account = await create('account');
const anotherAccount = await create('account');
const itemCategory = await create('item_category');
const res = await request().post('/api/items').send({
name: 'Item Name',
type_id: 1,
buy_price: 10.2,
cost_price: 20.2,
sell_account_id: account.id,
cost_account_id: anotherAccount.id,
category_id: itemCategory.id,
});
expect(res.status).equals(200);
});
});
describe('DELETE: `items/:id`', () => {
it('Should response not found in case the item was not exist.', async () => {
const res = await request().delete('/api/items/10').send();
expect(res.status).equals(404);
});
it('Should response success in case was exist.', async () => {
const item = await create('item');
const res = await request().delete(`/api/items/${item.id}`);
expect(res.status).equals(200);
});
it('Should delete the given item from the storage.', async () => {
const item = await create('item');
await request().delete(`/api/items/${item.id}`);
const storedItem = await knex('items').where('id', item.id);
expect(storedItem).to.have.lengthOf(0);
});
});
});

View File

@@ -0,0 +1,160 @@
import { request, expect, create } from '~/testInit';
import knex from '@/database/knex';
describe('routes: /item_categories/', () => {
describe('POST `/items_categories``', async () => {
it('Should not create a item category if the user was not authorized.', () => {
});
it('Should `name` be required.', async () => {
const res = await request().post('/api/item_categories').send();
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
});
it('Should `parent_category_id` be exist in the storage.', async () => {
const res = await request().posjt('/api/item_categories').send({
name: 'Clothes',
parent_category_id: 10,
});
expect(res.status).equals(404);
expect(res.body.errors).include.something.that.deep.equals({
type: 'PARENT_CATEGORY_NOT_FOUND', code: 100,
});
});
it('Should response success with correct form data.', async () => {
const res = await request().post('/api/item_categories').send({
name: 'Clothes',
description: 'Here is description',
});
// eslint-disable-next-line no-unused-expressions
expect(res.body.id).to.exist;
expect(res.status).equals(200);
});
it('Should item category data be saved to the storage.', async () => {
const category = await create('item_category');
const res = await request().post('/api/item_categories').send({
name: 'Clothes',
description: 'Here is description',
parent_category_id: category.id,
});
// eslint-disable-next-line no-unused-expressions
expect(res.body.id).to.exist;
const storedCategory = await knex('items_categories').where('id', res.body.id).first();
expect(storedCategory.label).equals('Clothes');
expect(storedCategory.description).equals('Here is description');
expect(storedCategory.parent_category_id).equals(category.id);
});
});
describe('POST `/items_category/{id}`', () => {
it('Should not update a item category if the user was not authorized.', () => {
});
it('Should `name` be required.', async () => {
const category = await create('item_category');
const res = await request().post(`/api/item_categories/${category.id}`).send({
name: '',
});
expect(res.status).equals(422);
expect(res.body.code).equals('validation_error');
});
it('Should `parent_category_id` be exist in the storage.', async () => {
const category = await create('item_category');
const res = await request().post(`/api/item_categories/${category.id}`).send({
name: 'Name',
parent_category_id: 10,
});
expect(res.status).equals(404);
expect(res.body.errors).include.something.that.deep.equals({
type: 'PARENT_CATEGORY_NOT_FOUND', code: 100,
});
});
it('Should response success with correct data format.', async () => {
const category = await create('item_category');
const anotherCategory = await create('item_category');
const res = await request().post(`/api/item_categories/${category.id}`).send({
name: 'Name',
parent_category_id: anotherCategory.id,
description: 'updated description',
});
expect(res.status).equals(200);
});
it('Should item category data be update in the storage.', async () => {
const category = await create('item_category');
const anotherCategory = await create('item_category');
const res = await request().post(`/api/item_categories/${category.id}`).send({
name: 'Name',
parent_category_id: anotherCategory.id,
description: 'updated description',
});
const storedCategory = await knex('items_categories').where('id', res.body.id).first();
expect(storedCategory.label).equals('Name');
expect(storedCategory.description).equals('updated description');
expect(storedCategory.parent_category_id).equals(anotherCategory.id);
});
});
describe('DELETE: `/items_categories`', async () => {
it('Should not delete the give item category if the user was not authorized.', () => {
});
it('Should not delete if the item category was not found.', async () => {
const res = await request().delete('/api/item_categories/10');
expect(res.status).equals(404);
});
it('Should response success after delete the given item category.', async () => {
const category = await create('item_category');
const res = await request().delete(`/api/item_categories/${category.id}`);
expect(res.status).equals(200);
});
it('Should delete the give item category from the storage.', async () => {
const category = await create('item_category');
await request().delete(`/api/item_categories/${category.id}`);
const categories = await knex('items_categories').where('id', category.id);
expect(categories).to.have.lengthOf(0);
});
});
describe('GET `/items_category/{id}', () => {
it('Should response not found with incorrect item category ID.', () => {
});
it('Should response success with exist item category.', () => {
});
it('Should response data of item category.', () => {
});
});
});

View File

@@ -0,0 +1,11 @@
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({});
console.log(res.body);
expect(res.status).equals(200);
});
});
});

View File

View File

@@ -0,0 +1,42 @@
describe('routes: `/routes`', () => {
describe('POST: `/routes`', () => {
it('Should create a new user if the user was not authorized.', () => {
});
it('Should `email` be required.', () => {
});
it('Should `password` be required.', () => {
});
it('Should `status` be boolean', () => {
});
});
describe('DELETE: `/users/:id`', () => {
it('Should not success if the user was not authorized.', () => {
});
it('Should not success if the user was pre-defined user.', () => {
});
it('Should response not found if the user was not exist.', () => {
});
it('Should response success if the user was exist.', () => {
});
it('Should delete the give user after success response.', () => {
});
});
});

46
server/tests/testInit.js Normal file
View File

@@ -0,0 +1,46 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import chaiThings from 'chai-things';
import app from '@/app';
import knex from '@/database/knex';
import factory from '@/database/factories';
import { hashPassword } from '@/utils';
const request = () => chai.request(app);
const { expect } = chai;
beforeEach(async () => {
await knex.migrate.rollback();
await knex.migrate.latest();
});
afterEach(async () => {
await knex.migrate.rollback();
});
chai.use(chaiHttp);
chai.use(chaiThings);
const login = async (givenUser) => {
const user = givenUser === null ? await factory.create('user') : givenUser;
const response = await request()
.post('/api/auth/login')
.send({
crediential: user.email,
password: hashPassword('secret'),
});
return response;
};
const create = async (name, data) => factory.create(name, data);
const make = async (name, data) => factory.build(name, data);
export {
login,
create,
make,
expect,
request,
};