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

362 lines
9.8 KiB
JavaScript
Raw Normal View History

2017-04-28 00:59:03 +03:00
'use strict';
const Code = require('code');
const Hapi = require('hapi');
const Lab = require('lab');
const PortalApi = require('../');
// Test shortcuts
const lab = exports.lab = Lab.script();
const describe = lab.describe;
const it = lab.it;
const expect = Code.expect;
2017-05-02 01:28:03 +03:00
const internals = {
options: { data: { test: true, name: 'test' } }
};
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();
});
});
});
describe('deployments', () => {
it('can be created', (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();
const payload = {
name: 'User Services',
datacenter: 'us-sw-1'
};
server.inject({ method: 'POST', url: '/deployment', payload }, (res) => {
expect(res.statusCode).to.equal(201);
expect(res.headers.location).to.exist();
done();
});
});
});
it('can be updated', (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();
const payload = {
name: 'User Services',
datacenter: 'us-sw-1'
};
2017-05-02 01:28:03 +03:00
server.inject({ method: 'POST', url: '/deployment', payload }, (res) => {
expect(res.statusCode).to.equal(201);
expect(res.result.name).to.equal('User Services');
2017-05-02 23:31:05 +03:00
payload.name = 'Customer Services';
server.inject({ method: 'PUT', url: `/deployment/${res.result.id}`, payload }, (res) => {
2017-05-02 01:28:03 +03:00
expect(res.statusCode).to.equal(200);
expect(res.result.name).to.equal('Customer Services');
done();
});
2017-04-28 00:59:03 +03:00
});
});
});
it('can be retrieved', (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-02 23:31:05 +03:00
const payload = {
name: 'User Services',
datacenter: 'us-sw-1'
};
server.inject({ method: 'POST', url: '/deployment', payload }, (res) => {
expect(res.statusCode).to.equal(201);
2017-04-28 00:59:03 +03:00
expect(res.result.name).to.equal('User Services');
2017-05-02 23:31:05 +03:00
server.inject({ method: 'GET', url: `/deployment/${res.result.id}` }, (res) => {
expect(res.statusCode).to.equal(200);
expect(res.result.name).to.equal('User Services');
done();
});
2017-04-28 00:59:03 +03:00
});
});
});
it('can be deleted', (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-02 23:31:05 +03:00
const payload = {
name: 'User Services',
datacenter: 'us-sw-1'
};
server.inject({ method: 'POST', url: '/deployment', payload }, (res) => {
expect(res.statusCode).to.equal(201);
expect(res.result.name).to.equal('User Services');
server.inject({ method: 'DELETE', url: `/deployment/${res.result.id}` }, (res) => {
expect(res.statusCode).to.equal(200);
done();
});
2017-04-28 00:59:03 +03:00
});
});
});
it('can all be retrieved', (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-02 23:31:05 +03:00
const deployment1 = {
name: 'User Services',
datacenter: 'us-sw-1'
};
const deployment2 = {
name: 'Customer Services',
datacenter: 'us-sw-1'
};
server.inject({ method: 'POST', url: '/deployment', payload: deployment1 }, (res) => {
expect(res.statusCode).to.equal(201);
expect(res.result.name).to.equal(deployment1.name);
server.inject({ method: 'POST', url: '/deployment', payload: deployment2 }, (res) => {
expect(res.statusCode).to.equal(201);
expect(res.result.name).to.equal(deployment2.name);
server.inject({ method: 'GET', url: '/deployments' }, (res) => {
expect(res.statusCode).to.equal(200);
expect(res.result.length >= 2).to.be.true();
done();
});
});
2017-04-28 00:59:03 +03:00
});
});
});
});
describe('datacenters', () => {
it('can be retrieved', (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();
server.inject({ method: 'GET', url: '/datacenters' }, (res) => {
expect(res.statusCode).to.equal(200);
done();
});
});
});
});
describe('manifests', () => {
it('can be created', (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();
const payload = {
2017-05-02 23:31:05 +03:00
raw: 'blah',
obj: {}
2017-04-28 00:59:03 +03:00
};
2017-05-02 23:31:05 +03:00
const deployment = {
name: 'User Services',
datacenter: 'us-sw-1'
};
server.inject({ method: 'POST', url: '/deployment', payload: deployment }, (res) => {
2017-04-28 00:59:03 +03:00
expect(res.statusCode).to.equal(201);
2017-05-02 23:31:05 +03:00
server.inject({ method: 'POST', url: `/deployment/${res.result.id}/manifest`, payload }, (res) => {
expect(res.statusCode).to.equal(201);
expect(res.headers.location).to.exist();
done();
});
2017-04-28 00:59:03 +03:00
});
});
});
it('can be retrieved', (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-02 23:31:05 +03:00
const payload = {
raw: 'blah',
obj: {}
};
2017-04-28 00:59:03 +03:00
2017-05-02 23:31:05 +03:00
const deployment = {
name: 'User Services',
datacenter: 'us-sw-1'
};
2017-04-28 00:59:03 +03:00
2017-05-02 23:31:05 +03:00
server.inject({ method: 'POST', url: '/deployment', payload: deployment }, (res) => {
expect(res.statusCode).to.equal(201);
2017-04-28 00:59:03 +03:00
2017-05-02 23:31:05 +03:00
server.inject({ method: 'POST', url: `/deployment/${res.result.id}/manifest`, payload }, (res) => {
expect(res.statusCode).to.equal(201);
2017-04-28 00:59:03 +03:00
2017-05-02 23:31:05 +03:00
server.inject(res.headers.location, (res) => {
expect(res.statusCode).to.equal(200);
expect(res.result.raw).to.equal(payload.raw);
done();
});
});
2017-04-28 00:59:03 +03:00
});
});
});
});
2017-05-02 23:31:05 +03:00
describe('activities', () => {
2017-04-28 00:59:03 +03:00
it('can be retrieved', (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-02 23:31:05 +03:00
const deployment = {
name: 'User Services',
datacenter: 'us-sw-1'
};
server.inject({ method: 'POST', url: '/deployment', payload: deployment }, (res) => {
expect(res.statusCode).to.equal(201);
server.inject({ method: 'GET', url: `/deployment/${res.result.id}/activities` }, (res) => {
expect(res.statusCode).to.equal(200);
done();
});
2017-04-28 00:59:03 +03:00
});
});
});
});
2017-05-02 23:31:05 +03:00
describe('metrics', () => {
2017-05-23 03:13:15 +03:00
it.skip('can be retrieved', (done) => {
2017-04-28 00:59:03 +03:00
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-02 23:31:05 +03:00
const deployment = {
name: 'User Services',
datacenter: 'us-sw-1'
2017-04-28 00:59:03 +03:00
};
2017-05-02 23:31:05 +03:00
server.inject({ method: 'POST', url: '/deployment', payload: deployment }, (res) => {
expect(res.statusCode).to.equal(201);
server.inject({ method: 'GET', url: `/deployment/${res.result.id}/metrics` }, (res) => {
expect(res.statusCode).to.equal(200);
done();
});
2017-04-28 00:59:03 +03:00
});
});
});
});
describe('services', () => {
it('can be retrieved', (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-02 23:31:05 +03:00
const deployment = {
name: 'User Services',
datacenter: 'us-sw-1'
};
server.inject({ method: 'POST', url: '/deployment', payload: deployment }, (res) => {
expect(res.statusCode).to.equal(201);
server.inject({ method: 'GET', url: `/deployment/${res.result.id}/services` }, (res) => {
expect(res.statusCode).to.equal(200);
done();
});
2017-04-28 00:59:03 +03:00
});
});
});
it('can be updated', (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-02 23:31:05 +03:00
const deployment = {
name: 'User Services',
datacenter: 'us-sw-1'
2017-04-28 00:59:03 +03:00
};
2017-05-02 23:31:05 +03:00
server.inject({ method: 'POST', url: '/deployment', payload: deployment }, (res) => {
expect(res.statusCode).to.equal(201);
const deploymentId = res.result.id;
server.inject({ method: 'GET', url: `/deployment/${deploymentId}/services` }, (res) => {
expect(res.statusCode).to.equal(200);
const service = {
count: 2
};
server.inject({ method: 'PUT', url: `/deployment/${deploymentId}/service/consul`, payload: service }, (res) => {
expect(res.statusCode).to.equal(200);
done();
});
});
2017-04-28 00:59:03 +03:00
});
});
});
});
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();
2017-05-23 00:25:21 +03:00
expect(result.data.deploymentGroups).to.be.null();
2017-04-28 00:59:03 +03:00
done();
});
});
});
});