UNPKG

paradigm-taxonomies

Version:
247 lines (181 loc) 6.2 kB
const migrationItems = require('../../src/migrations') const Migrations = require('structure-migrations') const MockHTTPServer = require('../helpers/mock-http-server') const pluginsList = require('../helpers/plugins') const TaxonomyController = require('../../src/controller') const TaxonomyModel = require('../../src/model') const createOrgAndApp = async function(){ // getting an organization and application Ids var res0 = await new MockHTTPServer() .post(`/api/${process.env.API_VERSION}/organizations`) .send({ title: 'work it' }) const org = res0.body.pkg const orgId = org.id var app = await new MockHTTPServer() .post(`/api/${process.env.API_VERSION}/applications`) .set('organizationid', orgId) .send({ desc: '', title: 'App 45' }) const appId = app.body.pkg.id return {orgId, appId} } describe('Routes', function() { beforeEach(function() { this.migration = new Migrations({ db: 'test', plugins: pluginsList }) return this.migration.process() }) afterEach(function() { return this.migration.purge() }) it('should create an taxonomy', async function() { const {orgId, appId} = await createOrgAndApp() var taxonomy = new TaxonomyController() var pkg = { desc: '', title: 'lol' } var res = await new MockHTTPServer() .post(`/api/${process.env.API_VERSION}/taxonomies`) .set('organizationid',orgId) .set('applicationid',appId) .send(pkg) expect(res.body.pkg.title).to.equal('lol') expect(res.body.status).to.equal(201) }) it('should get an taxonomy by Id', async function() { const {orgId, appId} = await createOrgAndApp() var taxonomy = new TaxonomyController() var pkg = { desc: '', title: 'rofl' } var taxonomy = await new MockHTTPServer() .post(`/api/${process.env.API_VERSION}/taxonomies`) .set('organizationid',orgId) .set('applicationid',appId) .send(pkg) var taxonomyId = taxonomy.body.pkg.id var res = await new MockHTTPServer() .get(`/api/${process.env.API_VERSION}/taxonomies/${taxonomyId}`) .set('organizationid',orgId) .set('applicationid',appId) expect(res.body.pkg.title).to.equal('rofl') expect(res.body.status).to.equal(200) }) it('should get an taxonomy by slug', async function() { const {orgId, appId} = await createOrgAndApp() var res0 = await new MockHTTPServer() .post(`/api/${process.env.API_VERSION}/taxonomies`) .set('organizationid', orgId) .set('applicationid', appId) .send({ desc: '', title: 'rofl' }) var tag = res0.body.pkg var res = await new MockHTTPServer() .get(`/api/${process.env.API_VERSION}/taxonomies/slug/${tag.slug}`) .set('organizationid', orgId) .set('applicationid', appId) expect(res.body.pkg.title).to.equal('rofl') expect(res.body.status).to.equal(200) }) it('should match a taxonomy by slug', async function() { const {orgId, appId} = await createOrgAndApp() var res0 = await new MockHTTPServer() .post(`/api/${process.env.API_VERSION}/taxonomies`) .set('organizationid',orgId) .set('applicationid',appId) .send({ desc: '', title: 'rofl' }) var tag = res0.body.pkg var res = await new MockHTTPServer() .get(`/api/${process.env.API_VERSION}/taxonomies/match/${tag.slug}`) .set('organizationid',orgId) .set('applicationid',appId) const taxonomies = res.body.pkg expect(taxonomies[0].title).to.equal('rofl') expect(res.body.status).to.equal(200) }) it('should get all taxonomies', async function() { const {orgId, appId} = await createOrgAndApp() var taxonomy = new TaxonomyController() var pkg = { desc: '', title: 'lulz' } var app = await new MockHTTPServer() .post(`/api/${process.env.API_VERSION}/taxonomies`) .set('organizationid',orgId) .set('applicationid',appId) .send(pkg) var res = await new MockHTTPServer() .get(`/api/${process.env.API_VERSION}/taxonomies`) .set('organizationid',orgId) .set('applicationid',appId) expect(res.body.pkg.taxonomies.length).to.be.above(0) expect(res.body.status).to.equal(200) }) it('should create taxonomy once', async function() { const {orgId, appId} = await createOrgAndApp() var res0 = await new MockHTTPServer() .post(`/api/${process.env.API_VERSION}/taxonomies`) .set('organizationid',orgId) .set('applicationid',appId) .send({ desc: '', title: 'lulz' }) var res1 = await new MockHTTPServer() .post(`/api/${process.env.API_VERSION}/taxonomies`) .set('organizationid',orgId) .set('applicationid',appId) .send({ desc: '', title: 'lulz' }) var res = await new MockHTTPServer() .get(`/api/${process.env.API_VERSION}/taxonomies`) .set('organizationid',orgId) .set('applicationid',appId) expect(res.body.pkg.taxonomies.length).to.equal(1) expect(res.body.status).to.equal(200) }) it('should update an taxonomy by Id', async function() { const {orgId, appId} = await createOrgAndApp() var taxonomy = new TaxonomyController() var pkg = { desc: '', title: 'peeka' } var taxonomy = await new MockHTTPServer() .post(`/api/${process.env.API_VERSION}/taxonomies`) .set('organizationid',orgId) .set('applicationid',appId) .send(pkg) var taxonomyId = taxonomy.body.pkg.id var res = await new MockHTTPServer() .patch(`/api/${process.env.API_VERSION}/taxonomies/${taxonomyId}`) .set('organizationid',orgId) .set('applicationid',appId) .send({ desc: '', title: 'peekaboo' }) var res2 = await new MockHTTPServer() .get(`/api/${process.env.API_VERSION}/taxonomies/${taxonomyId}`) .set('organizationid',orgId) .set('applicationid',appId) expect(res2.body.pkg.title).to.equal('peekaboo') expect(res.body.status).to.equal(200) }) })