mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-20 06:40:31 +00:00
WIP Items module.
This commit is contained in:
170
server/tests/routes/auth.test.js
Normal file
170
server/tests/routes/auth.test.js
Normal 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.', () => {
|
||||
|
||||
// })
|
||||
// });
|
||||
});
|
||||
176
server/tests/routes/items.test.js
Normal file
176
server/tests/routes/items.test.js
Normal 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);
|
||||
});
|
||||
});
|
||||
});
|
||||
160
server/tests/routes/itemsCategories.test.js
Normal file
160
server/tests/routes/itemsCategories.test.js
Normal 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.', () => {
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
11
server/tests/routes/oauth.test.js
Normal file
11
server/tests/routes/oauth.test.js
Normal 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);
|
||||
});
|
||||
});
|
||||
});
|
||||
0
server/tests/routes/roles.test.js
Normal file
0
server/tests/routes/roles.test.js
Normal file
42
server/tests/routes/users.test.js
Normal file
42
server/tests/routes/users.test.js
Normal 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.', () => {
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user