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

156 lines
4.6 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',
2017-08-24 22:21:39 +03:00
db: { test: true, host: 'rethinkdb' },
2017-08-14 22:21:49 +03:00
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);
2017-08-24 22:21:39 +03:00
expect(res.result.data).to.exist();
2017-04-28 00:59:03 +03:00
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();
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);
2017-08-24 22:21:39 +03:00
expect(res.result.data).to.exist();
expect(res.result.data.portal.deploymentGroups[0].name).to.equal('test1');
2017-06-08 00:35:45 +03:00
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();
const payload = {
query: '{ portal { datacenter { region }, deploymentGroups { name, slug } } }'
};
server.inject({ method: 'POST', url: '/graphql', payload }, (res) => {
expect(res.statusCode).to.equal(200);
2017-08-24 22:21:39 +03:00
expect(res.result.data).to.exist();
expect(res.result.data.portal.deploymentGroups.length).to.equal(2);
2017-06-08 00:35:45 +03:00
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();
const payload = {
2017-08-24 22:21:39 +03:00
query: '{ portal { datacenter { region }, deploymentGroups(name: "test") { name, services(name: "service") { name } } } }'
2017-06-08 00:35:45 +03:00
};
server.inject({ method: 'POST', url: '/graphql', payload }, (res) => {
expect(res.statusCode).to.equal(200);
2017-08-24 22:21:39 +03:00
console.log(res.result.data)
const deploymentGroup = res.result.data.portal.deploymentGroups[0];
expect(deploymentGroup.name).to.equal('test');
2017-06-08 00:35:45 +03:00
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) => {
2017-08-18 18:52:42 +03:00
const data = new PortalData(internals.options.data);
2017-06-08 00:35:45 +03:00
data.connect(() => {
2017-08-18 18:52:42 +03:00
data._db.r.dbDrop('test').run(data._db._connection, done);
2017-06-08 00:35:45 +03:00
});
});
2017-04-28 00:59:03 +03:00
});