mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 14:20:31 +00:00
fix: New item API test passing.
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { check, validationResult } from 'express-validator';
|
import { check, oneOf, validationResult } from 'express-validator';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import { difference } from 'lodash';
|
import { difference } from 'lodash';
|
||||||
import asyncMiddleware from '@/http/middleware/asyncMiddleware';
|
import asyncMiddleware from '@/http/middleware/asyncMiddleware';
|
||||||
@@ -11,6 +11,8 @@ import Resource from '@/models/Resource';
|
|||||||
import ResourceField from '@/models/ResourceField';
|
import ResourceField from '@/models/ResourceField';
|
||||||
import Authorization from '@/http/middleware/authorization';
|
import Authorization from '@/http/middleware/authorization';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
|
||||||
router() {
|
router() {
|
||||||
@@ -52,11 +54,13 @@ export default {
|
|||||||
check('type').exists().trim().escape()
|
check('type').exists().trim().escape()
|
||||||
.isIn(['service', 'non-inventory', 'inventory']),
|
.isIn(['service', 'non-inventory', 'inventory']),
|
||||||
check('sku').optional().trim().escape(),
|
check('sku').optional().trim().escape(),
|
||||||
check('cost_price').exists().isNumeric(),
|
check('cost_price').exists().isNumeric().toFloat(),
|
||||||
check('sell_price').exists().isNumeric(),
|
check('sell_price').exists().isNumeric().toFloat(),
|
||||||
check('cost_account_id').exists().isInt().toInt(),
|
check('cost_account_id').exists().isInt().toInt(),
|
||||||
check('sell_account_id').exists().isInt().toInt(),
|
check('sell_account_id').exists().isInt().toInt(),
|
||||||
check('inventory_account_id').exists().isInt().toInt(),
|
check('inventory_account_id')
|
||||||
|
.if(check('type').equals('inventory'))
|
||||||
|
.exists().isInt().toInt(),
|
||||||
check('category_id').optional().isInt().toInt(),
|
check('category_id').optional().isInt().toInt(),
|
||||||
|
|
||||||
check('custom_fields').optional().isArray({ min: 1 }),
|
check('custom_fields').optional().isArray({ min: 1 }),
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import {
|
import {
|
||||||
request,
|
request,
|
||||||
expect,
|
|
||||||
create,
|
create,
|
||||||
|
expect,
|
||||||
login,
|
login,
|
||||||
} from '~/testInit';
|
} from '~/testInit';
|
||||||
import knex from '@/database/knex';
|
import knex from '@/database/knex';
|
||||||
@@ -20,7 +20,6 @@ describe('routes: `/items`', () => {
|
|||||||
it('Should not create a new item if the user was not authorized.', async () => {
|
it('Should not create a new item if the user was not authorized.', async () => {
|
||||||
const res = await request()
|
const res = await request()
|
||||||
.post('/api/items')
|
.post('/api/items')
|
||||||
.set('x-access-token', loginRes.body.token)
|
|
||||||
.send();
|
.send();
|
||||||
|
|
||||||
expect(res.status).equals(401);
|
expect(res.status).equals(401);
|
||||||
@@ -48,7 +47,7 @@ describe('routes: `/items`', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Should `type_id` be required.', async () => {
|
it('Should `type` be required.', async () => {
|
||||||
const res = await request()
|
const res = await request()
|
||||||
.post('/api/items')
|
.post('/api/items')
|
||||||
.set('x-access-token', loginRes.body.token)
|
.set('x-access-token', loginRes.body.token)
|
||||||
@@ -57,29 +56,27 @@ describe('routes: `/items`', () => {
|
|||||||
expect(res.status).equals(422);
|
expect(res.status).equals(422);
|
||||||
expect(res.body.code).equals('validation_error');
|
expect(res.body.code).equals('validation_error');
|
||||||
expect(res.body.errors).include.something.deep.equals({
|
expect(res.body.errors).include.something.deep.equals({
|
||||||
msg: 'Invalid value', param: 'type_id', location: 'body',
|
msg: 'Invalid value', param: 'type', location: 'body',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Should `buy_price` be numeric.', async () => {
|
it('Should `type` be one of defined words.', async () => {
|
||||||
const res = await request()
|
const res = await request()
|
||||||
.post('/api/items')
|
.post('/api/items')
|
||||||
.set('x-access-token', loginRes.body.token)
|
.set('x-access-token', loginRes.body.token)
|
||||||
.send({
|
.send({
|
||||||
buy_price: 'not_numeric',
|
type: 'not-defined',
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(res.status).equals(422);
|
|
||||||
expect(res.body.code).equals('validation_error');
|
|
||||||
expect(res.body.errors).include.something.deep.equals({
|
expect(res.body.errors).include.something.deep.equals({
|
||||||
value: 'not_numeric',
|
value: 'not-defined',
|
||||||
msg: 'Invalid value',
|
msg: 'Invalid value',
|
||||||
param: 'buy_price',
|
param: 'type',
|
||||||
location: 'body',
|
location: 'body',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Should `cost_price` be numeric.', async () => {
|
it('Should `buy_price` be numeric.', async () => {
|
||||||
const res = await request()
|
const res = await request()
|
||||||
.post('/api/items')
|
.post('/api/items')
|
||||||
.set('x-access-token', loginRes.body.token)
|
.set('x-access-token', loginRes.body.token)
|
||||||
@@ -89,7 +86,7 @@ describe('routes: `/items`', () => {
|
|||||||
|
|
||||||
expect(res.status).equals(422);
|
expect(res.status).equals(422);
|
||||||
expect(res.body.code).equals('validation_error');
|
expect(res.body.code).equals('validation_error');
|
||||||
expect(res.body.errors).include.something.deep.equals({
|
expect(res.body.errors).include.something.deep.equals({
|
||||||
value: 'not_numeric',
|
value: 'not_numeric',
|
||||||
msg: 'Invalid value',
|
msg: 'Invalid value',
|
||||||
param: 'cost_price',
|
param: 'cost_price',
|
||||||
@@ -97,12 +94,12 @@ describe('routes: `/items`', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Should `sell_account_id` be integer.', async () => {
|
it('Should `sell_price` be numeric.', async () => {
|
||||||
const res = await request()
|
const res = await request()
|
||||||
.post('/api/items')
|
.post('/api/items')
|
||||||
.set('x-access-token', loginRes.body.token)
|
.set('x-access-token', loginRes.body.token)
|
||||||
.send({
|
.send({
|
||||||
sell_account_id: 'not_numeric',
|
sell_price: 'not_numeric',
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(res.status).equals(422);
|
expect(res.status).equals(422);
|
||||||
@@ -110,12 +107,12 @@ describe('routes: `/items`', () => {
|
|||||||
expect(res.body.errors).include.something.deep.equals({
|
expect(res.body.errors).include.something.deep.equals({
|
||||||
value: 'not_numeric',
|
value: 'not_numeric',
|
||||||
msg: 'Invalid value',
|
msg: 'Invalid value',
|
||||||
param: 'sell_account_id',
|
param: 'sell_price',
|
||||||
location: 'body',
|
location: 'body',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Should `cost_account_id` be integer.', async () => {
|
it('Should `sell_account_id` be integer.', async () => {
|
||||||
const res = await request()
|
const res = await request()
|
||||||
.post('/api/items')
|
.post('/api/items')
|
||||||
.set('x-access-token', loginRes.body.token)
|
.set('x-access-token', loginRes.body.token)
|
||||||
@@ -133,6 +130,24 @@ describe('routes: `/items`', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Should `cost_account_id` be integer.', async () => {
|
||||||
|
const res = await request()
|
||||||
|
.post('/api/items')
|
||||||
|
.set('x-access-token', loginRes.body.token)
|
||||||
|
.send({
|
||||||
|
sell_account_id: 'not_numeric',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.status).equals(422);
|
||||||
|
expect(res.body.code).equals('validation_error');
|
||||||
|
expect(res.body.errors).include.something.deep.equals({
|
||||||
|
value: 'not_numeric',
|
||||||
|
msg: 'Invalid value',
|
||||||
|
param: 'sell_account_id',
|
||||||
|
location: 'body',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('Should `cost_account_id` be required if `cost_price` was presented.', async () => {
|
it('Should `cost_account_id` be required if `cost_price` was presented.', async () => {
|
||||||
|
|
||||||
});
|
});
|
||||||
@@ -141,14 +156,54 @@ describe('routes: `/items`', () => {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Should response bad request in case cost account was not exist.', async () => {
|
it('Should `inventory_account_id` be required if type was `inventory` item.', async () => {
|
||||||
const res = await request()
|
const res = await request()
|
||||||
.post('/api/items')
|
.post('/api/items')
|
||||||
.set('x-access-token', loginRes.body.token)
|
.set('x-access-token', loginRes.body.token)
|
||||||
.send({
|
.send({
|
||||||
name: 'Item Name',
|
name: 'Item Name',
|
||||||
type_id: 1,
|
type: 'inventory',
|
||||||
buy_price: 10.2,
|
sell_price: 10.2,
|
||||||
|
cost_price: 20.2,
|
||||||
|
sell_account_id: 10,
|
||||||
|
cost_account_id: 20,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.body.errors).include.something.deep.equals({
|
||||||
|
msg: 'Invalid value',
|
||||||
|
param: 'inventory_account_id',
|
||||||
|
location: 'body',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should `inventory_account_id` be not required if type was not `inventory`.', async () => {
|
||||||
|
const res = await request()
|
||||||
|
.post('/api/items')
|
||||||
|
.set('x-access-token', loginRes.body.token)
|
||||||
|
.send({
|
||||||
|
name: 'Item Name',
|
||||||
|
type: 'service',
|
||||||
|
sell_price: 10.2,
|
||||||
|
cost_price: 20.2,
|
||||||
|
sell_account_id: 10,
|
||||||
|
cost_account_id: 20,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.body.errors).include.something.deep.not.equals({
|
||||||
|
msg: 'Invalid value',
|
||||||
|
param: 'inventory_account_id',
|
||||||
|
location: 'body',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should response bad request in case `cost account` was not exist.', async () => {
|
||||||
|
const res = await request()
|
||||||
|
.post('/api/items')
|
||||||
|
.set('x-access-token', loginRes.body.token)
|
||||||
|
.send({
|
||||||
|
name: 'Item Name',
|
||||||
|
type: 'service',
|
||||||
|
sell_price: 10.2,
|
||||||
cost_price: 20.2,
|
cost_price: 20.2,
|
||||||
sell_account_id: 10,
|
sell_account_id: 10,
|
||||||
cost_account_id: 20,
|
cost_account_id: 20,
|
||||||
@@ -166,7 +221,7 @@ describe('routes: `/items`', () => {
|
|||||||
.set('x-access-token', loginRes.body.token)
|
.set('x-access-token', loginRes.body.token)
|
||||||
.send({
|
.send({
|
||||||
name: 'Item Name',
|
name: 'Item Name',
|
||||||
type_id: 1,
|
type: 'service',
|
||||||
sell_price: 10.2,
|
sell_price: 10.2,
|
||||||
cost_price: 20.2,
|
cost_price: 20.2,
|
||||||
sell_account_id: 10,
|
sell_account_id: 10,
|
||||||
@@ -185,7 +240,7 @@ describe('routes: `/items`', () => {
|
|||||||
.set('x-access-token', loginRes.body.token)
|
.set('x-access-token', loginRes.body.token)
|
||||||
.send({
|
.send({
|
||||||
name: 'Item Name',
|
name: 'Item Name',
|
||||||
type_id: 1,
|
type: 'service',
|
||||||
sell_price: 10.2,
|
sell_price: 10.2,
|
||||||
cost_price: 20.2,
|
cost_price: 20.2,
|
||||||
sell_account_id: 10,
|
sell_account_id: 10,
|
||||||
@@ -209,7 +264,7 @@ describe('routes: `/items`', () => {
|
|||||||
.set('x-access-token', loginRes.body.token)
|
.set('x-access-token', loginRes.body.token)
|
||||||
.send({
|
.send({
|
||||||
name: 'Item Name',
|
name: 'Item Name',
|
||||||
type_id: 1,
|
type: 'service',
|
||||||
sell_price: 10.2,
|
sell_price: 10.2,
|
||||||
cost_price: 20.2,
|
cost_price: 20.2,
|
||||||
sell_account_id: account.id,
|
sell_account_id: account.id,
|
||||||
|
|||||||
Reference in New Issue
Block a user