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('users', () => {
|
17 | const user = {
|
18 | id: '4fc13ac6-1e7d-cd79-f3d2-96276af0d638',
|
19 | login: 'barbar',
|
20 | email: 'barbar@example.com',
|
21 | companyName: 'Example',
|
22 | firstName: 'BarBar',
|
23 | lastName: 'Jinks',
|
24 | phone: '(123)457-6890',
|
25 | updated: '2015-12-23T06:41:11.032Z',
|
26 | created: '2015-12-23T06:41:11.032Z'
|
27 | };
|
28 |
|
29 | afterEach(() => {
|
30 | StandIn.restoreAll();
|
31 | });
|
32 |
|
33 | it('can get your account', async () => {
|
34 | const key = {
|
35 | name: 'test',
|
36 | fingerprint: 'fingerprint',
|
37 | value: 'foo'
|
38 | };
|
39 |
|
40 | const server = new Hapi.Server();
|
41 | StandIn.replace(CloudApi.prototype, 'fetch', (stand, path, options) => {
|
42 | if (stand.invocations === 1) {
|
43 | return user;
|
44 | }
|
45 |
|
46 | return key;
|
47 | }, { stopAfter: 2 });
|
48 |
|
49 | await server.register({ plugin: CloudApiGql, options: { keyPath: Path.join(__dirname, 'test.key') } });
|
50 | await server.initialize();
|
51 | const res = await server.inject({
|
52 | url: '/graphql',
|
53 | method: 'post',
|
54 | payload: { query: 'query { account { login keys(name: "test") { name } } }' }
|
55 | });
|
56 | expect(res.statusCode).to.equal(200);
|
57 | expect(res.result.data.account).to.exist();
|
58 | expect(res.result.data.account.login).to.equal(user.login);
|
59 | expect(res.result.data.account.keys[0].name).to.equal(key.name);
|
60 | });
|
61 |
|
62 | it('can update your account', async () => {
|
63 | const server = new Hapi.Server();
|
64 | StandIn.replaceOnce(CloudApi.prototype, 'fetch', (stand, path, options) => {
|
65 | user.firstName = options.payload.firstName;
|
66 | return user;
|
67 | });
|
68 |
|
69 | await server.register({ plugin: CloudApiGql, options: { keyPath: Path.join(__dirname, 'test.key') } });
|
70 | await server.initialize();
|
71 | const res = await server.inject({
|
72 | url: '/graphql',
|
73 | method: 'post',
|
74 | payload: { query: 'mutation { updateAccount(firstName: "Boom") { login firstName } }' }
|
75 | });
|
76 |
|
77 | expect(res.statusCode).to.equal(200);
|
78 | expect(res.result.data.updateAccount).to.exist();
|
79 | expect(res.result.data.updateAccount.login).to.equal(user.login);
|
80 | expect(res.result.data.updateAccount.firstName).to.equal('Boom');
|
81 | });
|
82 |
|
83 | it('can create a subuser', async () => {
|
84 | const server = new Hapi.Server();
|
85 | StandIn.replaceOnce(CloudApi.prototype, 'fetch', (stand, path, options) => {
|
86 | return user;
|
87 | });
|
88 |
|
89 | await server.register({ plugin: CloudApiGql, options: { keyPath: Path.join(__dirname, 'test.key') } });
|
90 | await server.initialize();
|
91 | const res = await server.inject({
|
92 | url: '/graphql',
|
93 | method: 'post',
|
94 | payload: { query: 'mutation { createUser(email: "email", login: "test") { login } }' }
|
95 | });
|
96 |
|
97 | expect(res.statusCode).to.equal(200);
|
98 | expect(res.result.data.createUser).to.exist();
|
99 | expect(res.result.data.createUser.login).to.equal(user.login);
|
100 | });
|
101 |
|
102 | it('can query for a user', async () => {
|
103 | const keys = [{
|
104 | name: 'test',
|
105 | fingerprint: 'fingerprint',
|
106 | value: 'foo'
|
107 | }];
|
108 |
|
109 | const server = new Hapi.Server();
|
110 | StandIn.replace(CloudApi.prototype, 'fetch', (stand) => {
|
111 | if (stand.invocations === 1) {
|
112 | return user;
|
113 | }
|
114 |
|
115 | return keys;
|
116 | }, { stopAfter: 2 });
|
117 |
|
118 | await server.register({ plugin: CloudApiGql, options: { keyPath: Path.join(__dirname, 'test.key') } });
|
119 | await server.initialize();
|
120 | const res = await server.inject({
|
121 | url: '/graphql',
|
122 | method: 'post',
|
123 | payload: { query: 'query { user(id: "foo") { login keys { name } } }' }
|
124 | });
|
125 | expect(res.statusCode).to.equal(200);
|
126 | expect(res.result.data.user).to.exist();
|
127 | expect(res.result.data.user.login).to.equal(user.login);
|
128 | expect(res.result.data.user.keys[0].name).to.equal(keys[0].name);
|
129 | });
|
130 |
|
131 | it('can query for users', async () => {
|
132 | const server = new Hapi.Server();
|
133 | StandIn.replaceOnce(CloudApi.prototype, 'fetch', (stand) => {
|
134 | return [user];
|
135 | });
|
136 |
|
137 | await server.register({ plugin: CloudApiGql, options: { keyPath: Path.join(__dirname, 'test.key') } });
|
138 | await server.initialize();
|
139 | const res = await server.inject({
|
140 | url: '/graphql',
|
141 | method: 'post',
|
142 | payload: { query: 'query { users { login } }' }
|
143 | });
|
144 | expect(res.statusCode).to.equal(200);
|
145 | expect(res.result.data.users).to.exist();
|
146 | expect(res.result.data.users[0].login).to.equal(user.login);
|
147 | });
|
148 | });
|