fix api global options.

This commit is contained in:
Ahmed Bouhuolia
2020-04-15 20:13:55 +02:00
parent d02517e66d
commit ff0a26a790
7 changed files with 281 additions and 25 deletions

View File

@@ -12,6 +12,17 @@ describe('MetableCollection', () => {
const foundMeta = metadataCollection.findMeta(option.key);
expect(foundMeta).to.be.an('object');
});
it('Should retrieve the found meta with extra columns.', async () => {
const option = await create('option');
const metadataCollection = await Option.query();
const foundMeta = metadataCollection.findMeta({
key: option.key,
group: option.group,
});
expect(foundMeta).to.be.an('object');
});
});
describe('allMetadata', () => {
@@ -49,6 +60,21 @@ describe('MetableCollection', () => {
expect(metadataCollection.metadata.length).equals(1);
});
it('Should sets the meta value with extra columns', async () => {
const metadataCollection = new MetadataCollection();
metadataCollection.setMeta({
key: 'key',
value: 'value',
group: 'group-1',
});
expect(metadataCollection.metadata.length).equals(1);
expect(metadataCollection.metadata[0].key).equals('key');
expect(metadataCollection.metadata[0].value).equals('value');
expect(metadataCollection.metadata[0].group).equals('group-1');
});
});
describe('removeAllMeta()', () => {
@@ -82,7 +108,7 @@ describe('MetableCollection', () => {
it('Should save updated the exist metadata.', async () => {
const option = await create('option');
const metadataCollection = new MetadataCollection();
const metadataCollection = await Option.query();
metadataCollection.setModel(Option);
metadataCollection.setMeta(option.key, 'value');
@@ -90,23 +116,24 @@ describe('MetableCollection', () => {
await metadataCollection.saveMeta();
const storedMetadata = await Option.query().where('key', option.key).first();
expect(storedMetadata.value).equals('value');
const storedMetadata = await Option.query().where('key', option.key);
expect(storedMetadata.metadata[0].value).equals('value');
expect(storedMetadata.metadata[0].key).equals(option.key);
expect(storedMetadata.metadata[0].group).equals(option.group);
});
it('Should delete the removed metadata from storage.', async () => {
const option = await create('option');
const options = await Option.query();
const metadataCollection = MetadataCollection.from(options);
metadataCollection.setModel(Option);
const metadataCollection = await Option.query();
metadataCollection.removeMeta(option.key);
expect(metadataCollection.metadata.length).equals(1);
await metadataCollection.saveMeta();
const storedMetadata = await Option.query();
expect(storedMetadata.length).equals(0);
expect(storedMetadata.metadata.length).equals(0);
});
it('Should save instered new metadata with extra columns.', async () => {
@@ -116,8 +143,11 @@ describe('MetableCollection', () => {
metadataCollection.extraColumns = ['resource_id'];
metadataCollection.setModel(ResourceFieldMetadata);
metadataCollection.setMeta('key', 'value', { resource_id: resource.id });
metadataCollection.setMeta({
key: 'key',
value: 'value',
resource_id: resource.id,
});
await metadataCollection.saveMeta();
const storedMetadata = await ResourceFieldMetadata.query().first();

View File

@@ -608,7 +608,7 @@ describe('routes: `/accounting`', () => {
});
});
describe.only('route: POST `accounting/manual-journals/:id/publish`', () => {
describe('route: POST `accounting/manual-journals/:id/publish`', () => {
it('Should response not found in case the manual journal id was not exists.', async () => {
const manualJournal = await create('manual_journal');

View File

@@ -0,0 +1,123 @@
import knex from '@/database/knex';
import {
request,
expect,
create,
make,
login,
} from '~/testInit';
import Option from '@/models/Option';
let loginRes;
describe('routes: `/options`', () => {
beforeEach(async () => {
loginRes = await login();
});
afterEach(() => {
loginRes = null;
});
describe('POST: `/options/`', () => {
it('Should response unauthorized if the user was not logged in.', async () => {
const res = await request()
.post('/api/options')
.send();
expect(res.status).equals(401);
expect(res.body.message).equals('unauthorized');
});
it('Should response the options key and group is not defined.', async () => {
const res = await request()
.post('/api/options')
.set('x-access-token', loginRes.body.token)
.send({
options: [
{
key: 'key',
value: 'hello world',
group: 'group',
},
],
});
expect(res.status).equals(400);
expect(res.body.errors).include.something.that.deep.equals({
type: 'OPTIONS.KEY.NOT.DEFINED',
code: 200,
keys: [
{ key: 'key', group: 'group' },
],
});
});
it('Should save options to the storage.', async () => {
const res = await request()
.post('/api/options')
.set('x-access-token', loginRes.body.token)
.send({
options: [{
key: 'name',
group: 'organization',
value: 'hello world',
}],
});
expect(res.status).equals(200);
const storedOptions = await Option.query()
.where('group', 'organization')
.where('key', 'name');
expect(storedOptions.metadata.length).equals(1);
});
});
describe('GET: `/options`', () => {
it('Should response unauthorized if the user was not unauthorized.', async () => {
const res = await request()
.get('/api/options')
.query({
group: 'organization',
})
.send();
expect(res.status).equals(401);
expect(res.body.message).equals('unauthorized');
});
it('Should retrieve options the associated to the given group.', async () => {
await create('option', { group: 'organization', key: 'name' });
await create('option', { group: 'organization', key: 'base_currency' });
const res = await request()
.get('/api/options')
.set('x-access-token', loginRes.body.token)
.query({
group: 'organization',
})
.send();
expect(res.status).equals(200);
expect(res.body.options).is.an('array');
expect(res.body.options.length).equals(2);
});
it('Should retrieve options that associated to the given key.', async () => {
await create('option', { group: 'organization', key: 'base_currency' });
await create('option', { group: 'organization', key: 'name' });
const res = await request()
.get('/api/options')
.set('x-access-token', loginRes.body.token)
.query({
key: 'name',
})
.send();
expect(res.status).equals(200);
expect(res.body.options).is.an('array');
expect(res.body.options.length).equals(1);
});
});
});