Skip to content

Commit

Permalink
test(cli): add api tests (zapier#58)
Browse files Browse the repository at this point in the history
* add api tests
  • Loading branch information
xavdid authored Aug 29, 2019
1 parent e8050a1 commit c04c81b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"markdown-toc": "1.1.0",
"mock-fs": "4.10.0",
"ngrok": "2.2.10",
"nock": "10.0.6",
"node-watch": "0.5.4",
"stdout-stderr": "0.1.9",
"yamljs": "0.3.0"
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/smoke-tests/smoke-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ describe('smoke tests - setup will take some time', () => {
this.skip();
}
const stdout = runCommand(context.cliBin, ['apps', '--format=json']);
console.log('stdout is', stdout);
const result = JSON.parse(stdout);
result.should.be.Array();
});
Expand Down
74 changes: 65 additions & 9 deletions packages/cli/src/tests/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@ const should = require('should');
const api = require('../../utils/api');
const { AUTH_KEY, CURRENT_APP_FILE } = require('../../constants');
const mock = require('mock-fs');
const nock = require('nock');

describe('api', () => {
const answer = 'verysecretkey';
describe('credentials', () => {
beforeEach(() => {
process.env.ZAPIER_DEPLOY_KEY = answer;
});

it('should read credentials from env', async () => {
const creds = await api.readCredentials();

it('should read credentials from env', done => {
process.env.ZAPIER_DEPLOY_KEY = answer;
api
.readCredentials()
.then(creds => {
should.exist(creds[AUTH_KEY]);
done();
})
.catch(done);
should.exist(creds[AUTH_KEY]);
});

afterEach(() => {
delete process.env.ZAPIER_DEPLOY_KEY;
});
});

describe('linkedAppConfigs', () => {
Expand Down Expand Up @@ -53,4 +58,55 @@ describe('api', () => {

afterEach(mock.restore);
});

describe('listEndpoint', () => {
beforeEach(() => {
nock.disableNetConnect();
process.env.ZAPIER_DEPLOY_KEY = answer;
mock({
[CURRENT_APP_FILE]: '{ "id": 2864, "key": "App2864", "extra": 5 }'
});
});

it('should read from and endpoint', async () => {
const reqheaders = {
'X-Deploy-Key': answer
};

nock('https://zapier.com', { reqheaders })
.get('/api/platform/cli/check')
.reply(200, {})
.get('/api/platform/cli/apps/2864')
.reply(200, {
latest_core_version: '8.0.1',
id: 2864,
slug: 'steam',
versions: ['2.0.2'],
all_versions: ['1.0.0', '2.0.0', '2.0.1', '2.0.2'],
latest_version: '2.0.2',
core_versions: ['8.0.1']
})
.get('/api/platform/cli/apps/2864/versions')
.reply(200, {
objects: [
{ id: 116873 },
{ id: 110884 },
{ id: 11028649 },
{ id: 40878 }
]
});

const res = await api.listEndpoint('versions');

should.exist(res.app);
should.exist(res.versions);
should(res.versions.length).eql(4);
});

afterEach(() => {
mock.restore();
delete process.env.ZAPIER_DEPLOY_KEY;
nock.enableNetConnect();
});
});
});
7 changes: 6 additions & 1 deletion packages/cli/src/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const { prettyJSONstringify, startSpinner, endSpinner } = require('./display');

const { localAppCommand } = require('./local');

// TODO split these out into better files

// Reads the JSON file at ~/.zapierrc (AUTH_LOCATION).
const readCredentials = (explodeIfMissing = true) => {
if (process.env.ZAPIER_DEPLOY_KEY) {
Expand Down Expand Up @@ -180,7 +182,10 @@ const getLinkedApp = appDir => {
}
return callAPI('/apps/' + app.id);
})
.catch(() => {
.catch(e => {
if (process.env.NODE_ENV === 'test') {
console.error(e);
}
throw new Error(
`Warning! ${constants.CURRENT_APP_FILE} seems to be incorrect. Try running \`zapier link\` or \`zapier register\`.`
);
Expand Down

0 comments on commit c04c81b

Please sign in to comment.