mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
WIP pass the failed tests.
This commit is contained in:
@@ -1,33 +1,28 @@
|
||||
import {
|
||||
request,
|
||||
expect,
|
||||
create,
|
||||
login,
|
||||
} from '~/testInit';
|
||||
import knex from '@/database/knex';
|
||||
|
||||
let loginRes;
|
||||
import ItemCategory from '@/models/ItemCategory';
|
||||
import {
|
||||
tenantWebsite,
|
||||
tenantFactory,
|
||||
loginRes
|
||||
} from '~/dbInit';
|
||||
|
||||
describe('routes: /item_categories/', () => {
|
||||
beforeEach(async () => {
|
||||
loginRes = await login();
|
||||
});
|
||||
afterEach(() => {
|
||||
loginRes = null;
|
||||
});
|
||||
|
||||
describe('POST `/items_categories``', async () => {
|
||||
it('Should not create a item category if the user was not authorized.', async () => {
|
||||
const res = await request().post('/api/item_categories').send();
|
||||
|
||||
expect(res.status).equals(401);
|
||||
expect(res.body.message).equals('unauthorized');
|
||||
expect(res.body.message).equals('Unauthorized');
|
||||
});
|
||||
|
||||
it('Should `name` be required.', async () => {
|
||||
const res = await request()
|
||||
.post('/api/item_categories')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.set('organization-id', tenantWebsite.organizationId)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(422);
|
||||
@@ -38,6 +33,7 @@ describe('routes: /item_categories/', () => {
|
||||
const res = await request()
|
||||
.post('/api/item_categories')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.set('organization-id', tenantWebsite.organizationId)
|
||||
.send({
|
||||
name: 'Clothes',
|
||||
parent_category_id: 10,
|
||||
@@ -53,6 +49,7 @@ describe('routes: /item_categories/', () => {
|
||||
const res = await request()
|
||||
.post('/api/item_categories')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.set('organization-id', tenantWebsite.organizationId)
|
||||
.send({
|
||||
name: 'Clothes',
|
||||
description: 'Here is description',
|
||||
@@ -66,10 +63,11 @@ describe('routes: /item_categories/', () => {
|
||||
});
|
||||
|
||||
it('Should item category data be saved to the storage.', async () => {
|
||||
const category = await create('item_category');
|
||||
const category = await tenantFactory.create('item_category');
|
||||
const res = await request()
|
||||
.post('/api/item_categories')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.set('organization-id', tenantWebsite.organizationId)
|
||||
.send({
|
||||
name: 'Clothes',
|
||||
description: 'Here is description',
|
||||
@@ -78,8 +76,9 @@ describe('routes: /item_categories/', () => {
|
||||
|
||||
expect(res.status).equals(200);
|
||||
|
||||
const storedCategory = await knex('items_categories')
|
||||
.where('id', res.body.category.id).first();
|
||||
const storedCategory = await ItemCategory.tenant().query()
|
||||
.where('id', res.body.category.id)
|
||||
.first();
|
||||
|
||||
expect(storedCategory.name).equals('Clothes');
|
||||
expect(storedCategory.description).equals('Here is description');
|
||||
@@ -90,32 +89,33 @@ describe('routes: /item_categories/', () => {
|
||||
|
||||
describe('POST `/items_category/{id}`', () => {
|
||||
it('Should not update a item category if the user was not authorized.', async () => {
|
||||
const category = await create('item_category');
|
||||
const category = await tenantFactory.create('item_category');
|
||||
const res = await request()
|
||||
.post(`/api/item_categories/${category.id}`)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(401);
|
||||
expect(res.body.message).equals('unauthorized');
|
||||
expect(res.body.message).equals('Unauthorized');
|
||||
});
|
||||
|
||||
it('Should `name` be required.', async () => {
|
||||
const category = await create('item_category');
|
||||
const category = await tenantFactory.create('item_category');
|
||||
const res = await request()
|
||||
.post(`/api/item_categories/${category.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.send({
|
||||
name: '',
|
||||
});
|
||||
.set('organization-id', tenantWebsite.organizationId)
|
||||
.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 category = await create('item_category');
|
||||
const category = await tenantFactory.create('item_category');
|
||||
const res = await request()
|
||||
.post(`/api/item_categories/${category.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.set('organization-id', tenantWebsite.organizationId)
|
||||
.send({
|
||||
name: 'Name',
|
||||
parent_category_id: 10,
|
||||
@@ -128,12 +128,13 @@ describe('routes: /item_categories/', () => {
|
||||
});
|
||||
|
||||
it('Should response success with correct data format.', async () => {
|
||||
const category = await create('item_category');
|
||||
const anotherCategory = await create('item_category');
|
||||
const category = await tenantFactory.create('item_category');
|
||||
const anotherCategory = await tenantFactory.create('item_category');
|
||||
|
||||
const res = await request()
|
||||
.post(`/api/item_categories/${category.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.set('organization-id', tenantWebsite.organizationId)
|
||||
.send({
|
||||
name: 'Name',
|
||||
parent_category_id: anotherCategory.id,
|
||||
@@ -144,20 +145,22 @@ describe('routes: /item_categories/', () => {
|
||||
});
|
||||
|
||||
it('Should item category data be update in the storage.', async () => {
|
||||
const category = await create('item_category');
|
||||
const anotherCategory = await create('item_category');
|
||||
const category = await tenantFactory.create('item_category');
|
||||
const anotherCategory = await tenantFactory.create('item_category');
|
||||
|
||||
const res = await request()
|
||||
.post(`/api/item_categories/${category.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.set('organization-id', tenantWebsite.organizationId)
|
||||
.send({
|
||||
name: 'Name',
|
||||
parent_category_id: anotherCategory.id,
|
||||
description: 'updated description',
|
||||
});
|
||||
|
||||
const storedCategory = await knex('items_categories')
|
||||
.where('id', res.body.id).first();
|
||||
const storedCategory = await ItemCategory.tenant().query()
|
||||
.where('id', res.body.id)
|
||||
.first();
|
||||
|
||||
expect(storedCategory.name).equals('Name');
|
||||
expect(storedCategory.description).equals('updated description');
|
||||
@@ -167,43 +170,48 @@ describe('routes: /item_categories/', () => {
|
||||
|
||||
describe('DELETE: `/items_categories`', async () => {
|
||||
it('Should not delete the give item category if the user was not authorized.', async () => {
|
||||
const category = await create('item_category');
|
||||
const category = await tenantFactory.create('item_category');
|
||||
|
||||
const res = await request()
|
||||
.delete(`/api/item_categories/${category.id}`)
|
||||
.set('organization-id', tenantWebsite.organizationId)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(401);
|
||||
expect(res.body.message).equals('unauthorized');
|
||||
expect(res.body.message).equals('Unauthorized');
|
||||
});
|
||||
|
||||
it('Should not delete if the item category was not found.', async () => {
|
||||
const res = await request()
|
||||
.delete('/api/item_categories/10')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.set('organization-id', tenantWebsite.organizationId)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(404);
|
||||
});
|
||||
|
||||
it('Should response success after delete the given item category.', async () => {
|
||||
const category = await create('item_category');
|
||||
const category = await tenantFactory.create('item_category');
|
||||
const res = await request()
|
||||
.delete(`/api/item_categories/${category.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.set('organization-id', tenantWebsite.organizationId)
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(200);
|
||||
});
|
||||
|
||||
it('Should delete the give item category from the storage.', async () => {
|
||||
const category = await create('item_category');
|
||||
const category = await tenantFactory.create('item_category');
|
||||
const res = await request()
|
||||
.delete(`/api/item_categories/${category.id}`)
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.set('organization-id', tenantWebsite.organizationId)
|
||||
.send();
|
||||
|
||||
const categories = await knex('items_categories').where('id', category.id);
|
||||
const categories = await ItemCategory.tenant().query()
|
||||
.where('id', category.id);
|
||||
|
||||
expect(categories).to.have.lengthOf(0);
|
||||
});
|
||||
@@ -212,12 +220,13 @@ describe('routes: /item_categories/', () => {
|
||||
describe('GET: `/item_categories`', () => {
|
||||
|
||||
it('Should retrieve list of item categories.', async () => {
|
||||
const category1 = await create('item_category');
|
||||
const category2 = await create('item_category', { parent_category_id: category1.id });
|
||||
const category1 = await tenantFactory.create('item_category');
|
||||
const category2 = await tenantFactory.create('item_category', { parent_category_id: category1.id });
|
||||
|
||||
const res = await request()
|
||||
.get('/api/item_categories')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.set('organization-id', tenantWebsite.organizationId)
|
||||
.send();
|
||||
|
||||
expect(res.body.categories).to.be.a('array');
|
||||
@@ -233,14 +242,15 @@ describe('routes: /item_categories/', () => {
|
||||
|
||||
|
||||
it('Should retrieve of related items.', async () => {
|
||||
const category1 = await create('item_category');
|
||||
const category2 = await create('item_category', { parent_category_id: category1.id });
|
||||
const category1 = await tenantFactory.create('item_category');
|
||||
const category2 = await tenantFactory.create('item_category', { parent_category_id: category1.id });
|
||||
|
||||
await create('item', { category_id: category1.id });
|
||||
await tenantFactory.create('item', { category_id: category1.id });
|
||||
|
||||
const res = await request()
|
||||
.get('/api/item_categories')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.set('organization-id', tenantWebsite.organizationId)
|
||||
.send();
|
||||
|
||||
expect(res.body.categories[0].count).to.be.a('number');
|
||||
@@ -261,4 +271,41 @@ describe('routes: /item_categories/', () => {
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
describe('DELETE: `/items_cateogires`', () => {
|
||||
it('Should response bad request in case one of item categories id not exists in the storage.', async () => {
|
||||
const res = await request()
|
||||
.delete('/api/item_categories/bulk')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.set('organization-id', tenantWebsite.organizationId)
|
||||
.query({
|
||||
ids: [1020, 2020],
|
||||
})
|
||||
.send();
|
||||
|
||||
expect(res.status).equals(400);
|
||||
expect(res.body.errors).include.something.deep.equals({
|
||||
type: 'ITEM.CATEGORIES.IDS.NOT.FOUND', code: 200
|
||||
});
|
||||
});
|
||||
|
||||
it('Should delete the given item categories.', async () => {
|
||||
const itemCategory = await tenantFactory.create('item_category');
|
||||
const itemCategory2 = await tenantFactory.create('item_category');
|
||||
|
||||
const res = await request()
|
||||
.delete('/api/item_categories/bulk')
|
||||
.set('x-access-token', loginRes.body.token)
|
||||
.set('organization-id', tenantWebsite.organizationId)
|
||||
.query({
|
||||
ids: [itemCategory.id, itemCategory2.id],
|
||||
})
|
||||
.send();
|
||||
|
||||
const deleteItemCategories = await ItemCategory.tenant().query()
|
||||
.whereIn('id', [itemCategory.id, itemCategory2.id]);
|
||||
|
||||
expect(deleteItemCategories.length).equals(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user