UNPKG

2.18 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('webconsole-cloudapi-client');
10
11
12const lab = exports.lab = Lab.script();
13const { describe, it, afterEach } = lab;
14
15
16describe('policies', () => {
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://localhost'
27 }
28 };
29
30 it('can get all policies', async () => {
31 const policies = [{
32 id: 'test',
33 name: 'name',
34 rules: ['foo', 'barr'],
35 description: 'description'
36 }];
37
38 const server = new Hapi.Server();
39 StandIn.replaceOnce(CloudApi.prototype, 'fetch', () => {
40 return policies;
41 });
42
43 await server.register(register);
44 await server.initialize();
45 const res = await server.inject({
46 url: '/graphql',
47 method: 'post',
48 payload: { query: 'query { policies { id, name, rules, description } }' }
49 });
50 expect(res.statusCode).to.equal(200);
51 expect(res.result.data.policies).to.exist();
52 expect(res.result.data.policies[0].id).to.equal(policies[0].id);
53 expect(res.result.data.policies[0].name).to.equal(policies[0].name);
54 });
55
56
57 it('can get a single policy', async () => {
58 const policy = {
59 id: 'test',
60 name: 'name',
61 rules: ['foo', 'barr'],
62 description: 'description'
63 };
64
65 const server = new Hapi.Server();
66 StandIn.replaceOnce(CloudApi.prototype, 'fetch', () => {
67 return policy;
68 });
69
70 await server.register(register);
71 await server.initialize();
72 const res = await server.inject({
73 url: '/graphql',
74 method: 'post',
75 payload: { query: 'query { policy(id: "test") { id, name, rules, description } }' }
76 });
77 expect(res.statusCode).to.equal(200);
78 expect(res.result.data.policy).to.exist();
79 expect(res.result.data.policy.id).to.equal(policy.id);
80 expect(res.result.data.policy.name).to.equal(policy.name);
81 });
82});