UNPKG

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