1 | 'use strict';
|
2 |
|
3 | const Path = require('path');
|
4 | const { expect } = require('code');
|
5 | const Hapi = require('hapi');
|
6 | const Lab = require('lab');
|
7 | const StandIn = require('stand-in');
|
8 | const CloudApiGql = require('../lib/');
|
9 | const CloudApi = require('../lib/cloudapi');
|
10 |
|
11 |
|
12 | const lab = exports.lab = Lab.script();
|
13 | const { describe, it, afterEach } = lab;
|
14 |
|
15 |
|
16 | describe('policies', () => {
|
17 | afterEach(() => {
|
18 | StandIn.restoreAll();
|
19 | });
|
20 |
|
21 | it('can get all policies', async () => {
|
22 | const policies = [{
|
23 | id: 'test',
|
24 | name: 'name',
|
25 | rules: ['foo', 'barr'],
|
26 | description: 'description'
|
27 | }];
|
28 |
|
29 | const server = new Hapi.Server();
|
30 | StandIn.replaceOnce(CloudApi.prototype, 'fetch', () => {
|
31 | return policies;
|
32 | });
|
33 |
|
34 | await server.register({ plugin: CloudApiGql, options: { keyPath: Path.join(__dirname, 'test.key') } });
|
35 | await server.initialize();
|
36 | const res = await server.inject({
|
37 | url: '/graphql',
|
38 | method: 'post',
|
39 | payload: { query: 'query { policies { id, name, rules, description } }' }
|
40 | });
|
41 | expect(res.statusCode).to.equal(200);
|
42 | expect(res.result.data.policies).to.exist();
|
43 | expect(res.result.data.policies[0].id).to.equal(policies[0].id);
|
44 | expect(res.result.data.policies[0].name).to.equal(policies[0].name);
|
45 | });
|
46 |
|
47 |
|
48 | it('can get a single policy', async () => {
|
49 | const policy = {
|
50 | id: 'test',
|
51 | name: 'name',
|
52 | rules: ['foo', 'barr'],
|
53 | description: 'description'
|
54 | };
|
55 |
|
56 | const server = new Hapi.Server();
|
57 | StandIn.replaceOnce(CloudApi.prototype, 'fetch', () => {
|
58 | return policy;
|
59 | });
|
60 |
|
61 | await server.register({ plugin: CloudApiGql, options: { keyPath: Path.join(__dirname, 'test.key') } });
|
62 | await server.initialize();
|
63 | const res = await server.inject({
|
64 | url: '/graphql',
|
65 | method: 'post',
|
66 | payload: { query: 'query { policy(id: "test") { id, name, rules, description } }' }
|
67 | });
|
68 | expect(res.statusCode).to.equal(200);
|
69 | expect(res.result.data.policy).to.exist();
|
70 | expect(res.result.data.policy.id).to.equal(policy.id);
|
71 | expect(res.result.data.policy.name).to.equal(policy.name);
|
72 | });
|
73 | });
|