UNPKG

1.87 kBJavaScriptView Raw
1'use strict';
2
3const { AssertionError } = require('assert');
4const Fs = require('fs');
5const Path = require('path');
6const { expect } = require('code');
7const Lab = require('lab');
8const StandIn = require('stand-in');
9const CloudApi = require('webconsole-cloudapi-client');
10
11
12const lab = exports.lab = Lab.script();
13const { describe, it } = lab;
14
15describe('cloudapi', () => {
16 const keyId = 'ba:co:n1';
17 const key = Fs.readFileSync(Path.join(__dirname, 'test.key'));
18 const log = () => {};
19
20 it('throws when missing a token in production', () => {
21 const env = process.env.NODE_ENV;
22 process.env.NODE_ENV = 'production';
23 expect(() => { return new CloudApi({ keyId, key, log }); }).to.throw(AssertionError);
24 process.env.NODE_ENV = env;
25 });
26
27 it('won\'t throw when missing a token in development', () => {
28 const env = process.env.NODE_ENV;
29 process.env.NODE_ENV = 'development';
30 expect(() => { return new CloudApi({ keyId, key, log }); }).to.not.throw();
31 process.env.NODE_ENV = env;
32 });
33
34 it('won\'t throw when missing a token in test', () => {
35 const env = process.env.NODE_ENV;
36 process.env.NODE_ENV = 'test';
37 expect(() => { return new CloudApi({ keyId, key, log }); }).to.not.throw();
38 process.env.NODE_ENV = env;
39 });
40
41 it('won\'t throw when there is a token', () => {
42 expect(() => { return new CloudApi({ keyId, key, log, token: 'blah' }); }).to.not.throw();
43 });
44
45 describe('fetch', () => {
46 it('returns GET responses', async () => {
47 const cloudapi = new CloudApi({ keyId, key, log, token: 'blah' });
48 StandIn.replaceOnce(cloudapi._wreck, 'get', (stand) => {
49 return {
50 res: {},
51 payload: {
52 foo: 'bar'
53 }
54 };
55 });
56
57 const results = await cloudapi.fetch('/test');
58 expect(results).to.equal({foo: 'bar'});
59 });
60 });
61});