UNPKG

4.51 kBJavaScriptView Raw
1'use strict';
2
3const Path = require('path');
4const { expect } = require('code');
5const Hapi = require('hapi');
6const Lab = require('lab');
7const StandIn = require('stand-in');
8const CloudApiGql = require('../lib/');
9const CloudApi = require('../lib/cloudapi');
10
11
12const lab = exports.lab = Lab.script();
13const { describe, it, afterEach } = lab;
14
15
16describe('datacenters', () => {
17 afterEach(() => {
18 StandIn.restoreAll();
19 });
20
21 const register = {
22 plugin: CloudApiGql,
23 options: {
24 keyPath: Path.join(__dirname, 'test.key'),
25 keyId: 'test',
26 apiBaseUrl: 'http://us-west-1.joyent.com'
27 }
28 };
29
30 it('can get the current datacenter', async () => {
31 const server = new Hapi.Server();
32 StandIn.replaceOnce(CloudApi.prototype, 'fetch', (stand, path) => {
33 expect(path).to.contain('us-west-1');
34 return {
35 res: {
36 headers: {
37 Location: 'http://us-west-1.joyent.com'
38 }
39 },
40 payload: {
41 code: 'ResourceMoved',
42 message: 'us-west-1 http://us-west-1.joyent.com'
43 }
44 };
45 });
46
47 await server.register(register);
48 await server.initialize();
49 const res = await server.inject({
50 url: '/graphql',
51 method: 'post',
52 payload: { query: 'query { datacenter { name, place, url } }' }
53 });
54 expect(res.statusCode).to.equal(200);
55 expect(res.result.data.datacenter.place).to.equal('Americas');
56 expect(res.result.data.datacenter.name).to.equal('us-west-1');
57 expect(res.result.data.datacenter.url).to.equal('http://us-west-1.joyent.com');
58 });
59
60 it('can get the current datacenter configured by dcName', async () => {
61 const server = new Hapi.Server();
62 StandIn.replaceOnce(CloudApi.prototype, 'fetch', (stand, path) => {
63 expect(path).to.contain('us-west-1');
64 return {
65 res: {
66 headers: {
67 Location: 'http://us-west-1.joyent.com'
68 }
69 },
70 payload: {
71 code: 'ResourceMoved',
72 message: 'us-west-1 http://us-west-1.joyent.com'
73 }
74 };
75 });
76
77 await server.register({
78 plugin: CloudApiGql,
79 options: {
80 keyPath: Path.join(__dirname, 'test.key'),
81 keyId: 'test',
82 apiBaseUrl: 'http://localhost',
83 dcName: 'us-west-1'
84 }
85 });
86 await server.initialize();
87 const res = await server.inject({
88 url: '/graphql',
89 method: 'post',
90 payload: { query: 'query { datacenter { name, place, url } }' }
91 });
92 expect(res.statusCode).to.equal(200);
93 expect(res.result.data.datacenter.place).to.equal('Americas');
94 expect(res.result.data.datacenter.name).to.equal('us-west-1');
95 expect(res.result.data.datacenter.url).to.equal('http://us-west-1.joyent.com');
96 });
97
98 it('can get a single datacenter by name', async () => {
99 const server = new Hapi.Server();
100 StandIn.replaceOnce(CloudApi.prototype, 'fetch', () => {
101 return {
102 res: {
103 headers: {
104 Location: 'http://us-west-1.joyent.com'
105 }
106 },
107 payload: {
108 code: 'ResourceMoved',
109 message: 'us-west-1 http://us-west-1.joyent.com'
110 }
111 };
112 });
113
114 await server.register(register);
115 await server.initialize();
116 const res = await server.inject({
117 url: '/graphql',
118 method: 'post',
119 payload: { query: 'query { datacenter(name: "us-west-1") { name, place, url } }' }
120 });
121 expect(res.statusCode).to.equal(200);
122 expect(res.result.data.datacenter.place).to.equal('Americas');
123 expect(res.result.data.datacenter.name).to.equal('us-west-1');
124 expect(res.result.data.datacenter.url).to.equal('http://us-west-1.joyent.com');
125 });
126
127 it('can get all datacenters', async () => {
128 const datacenters = {
129 'us-west-1': 'http://test.com'
130 };
131
132 const server = new Hapi.Server();
133 StandIn.replaceOnce(CloudApi.prototype, 'fetch', () => {
134 return datacenters;
135 });
136
137 await server.register(register);
138 await server.initialize();
139 const res = await server.inject({
140 url: '/graphql',
141 method: 'post',
142 payload: { query: 'query { datacenters { name, place, url } }' }
143 });
144 expect(res.statusCode).to.equal(200);
145 expect(res.result.data.datacenters).to.exist();
146 expect(res.result.data.datacenters[0].place).to.equal('Americas');
147 expect(res.result.data.datacenters[0].name).to.equal('us-west-1');
148 expect(res.result.data.datacenters[0].url).to.equal('http://test.com');
149 });
150});