joyent-portal/packages/portal-api/test/index.js

166 lines
4.9 KiB
JavaScript
Raw Normal View History

2017-04-28 00:59:03 +03:00
'use strict';
const Hapi = require('hapi');
const { describe, it, beforeEach, afterEach, expect } = exports.lab = require('lab').script();
const PortalData = require('../lib/data');
2017-04-28 00:59:03 +03:00
const PortalApi = require('../');
2017-05-02 01:28:03 +03:00
const internals = {
2017-08-14 22:21:49 +03:00
options: {
data: {
name: 'test',
db: { test: true },
server: {
log: function () { }
}
}
}
2017-05-02 01:28:03 +03:00
};
internals.register = { register: PortalApi, options: internals.options };
2017-04-28 00:59:03 +03:00
describe('portal-api plugin', () => {
it('can be registered with hapi', (done) => {
const server = new Hapi.Server();
server.connection();
2017-05-02 01:28:03 +03:00
server.register(internals.register, (err) => {
2017-04-28 00:59:03 +03:00
expect(err).to.not.exist();
done();
});
});
});
2017-05-23 00:25:21 +03:00
describe('graphql', () => {
2017-04-28 00:59:03 +03:00
it('route exists', (done) => {
const server = new Hapi.Server();
server.connection();
2017-05-02 01:28:03 +03:00
server.register(internals.register, (err) => {
2017-04-28 00:59:03 +03:00
expect(err).to.not.exist();
2017-05-23 00:25:21 +03:00
const url = '/graphql?query=%7B%0A%20%20deploymentGroups(name%3A%22hi%22%2C%20slug%3A%20%22hi%22)%20%7B%0A%20%20%20%20id%0A%20%20%7D%0A%7D';
2017-04-28 00:59:03 +03:00
server.inject({ method: 'GET', url }, (res) => {
expect(res.statusCode).to.equal(200);
const result = JSON.parse(res.result);
expect(result.data).to.exist();
done();
});
});
});
2017-06-08 00:35:45 +03:00
it('can query for a deployment group inside a portal', (done) => {
const server = new Hapi.Server();
server.connection();
server.register(internals.register, (err) => {
expect(err).to.not.exist();
// internals.bootstrap(server.plugins['portal-api'].data, (err) => {
const payload = {
query: '{ portal { datacenter { region }, deploymentGroups(name: "test1") { name, slug } } }'
};
server.inject({ method: 'POST', url: '/graphql', payload }, (res) => {
expect(res.statusCode).to.equal(200);
const result = JSON.parse(res.result);
expect(result.data).to.exist();
expect(result.data.portal.deploymentGroups[0].name).to.equal('test1');
done();
});
});
});
it('defaults to all deployment groups inside a portal', (done) => {
const server = new Hapi.Server();
server.connection();
server.register(internals.register, (err) => {
expect(err).to.not.exist();
// internals.bootstrap(server.plugins['portal-api'].data, (err) => {
const payload = {
query: '{ portal { datacenter { region }, deploymentGroups { name, slug } } }'
};
server.inject({ method: 'POST', url: '/graphql', payload }, (res) => {
expect(res.statusCode).to.equal(200);
const result = JSON.parse(res.result);
expect(result.data).to.exist();
expect(result.data.portal.deploymentGroups.length).to.equal(2);
done();
});
});
});
it('can sub-filter services for a deployment group', (done) => {
const server = new Hapi.Server();
server.connection();
server.register(internals.register, (err) => {
expect(err).to.not.exist();
// internals.bootstrap(server.plugins['portal-api'].data, (err) => {
const payload = {
query: '{ portal { datacenter { region }, deploymentGroups(name: "test2") { name, services(name: "service") { name } } } }'
};
server.inject({ method: 'POST', url: '/graphql', payload }, (res) => {
expect(res.statusCode).to.equal(200);
const result = JSON.parse(res.result);
const deploymentGroup = result.data.portal.deploymentGroups[0];
expect(deploymentGroup.name).to.equal('test2');
expect(deploymentGroup.services[0].name).to.equal('service');
done();
});
});
});
beforeEach((done) => {
2017-08-14 22:21:49 +03:00
const data = new PortalData(internals.options.data);
2017-06-08 00:35:45 +03:00
data.connect(() => {
data.createDatacenter({ region: 'us-sw', name: 'us-sw' }, (err, datacenter) => {
if (err) {
return done(err);
}
data.createUser({ firstName: 'Nikola', lastName: 'Tesla', email: 'nikola@tesla.com', login: 'nikola' }, (err, user) => {
if (err) {
return done(err);
}
data.createPortal({
user,
datacenter
}, (err, portal) => {
if (err) {
return done(err);
}
data.createDeploymentGroup({ name: 'test1' }, () => {
data.createDeploymentGroup({ name: 'test2' }, (err, deploymentGroup) => {
if (err) {
return done(err);
}
data.createService({
name: 'service',
slug: 'service_slug',
deploymentGroupId: deploymentGroup.id
}, () => { done(); });
});
});
});
});
});
});
});
afterEach((done) => {
const data = new PortalData({ name: 'test', db: { test: true } });
data.connect(() => {
data._db.r.dbDrop('test').run(data._db._connection, () => {
done();
});
});
});
2017-04-28 00:59:03 +03:00
});