Skip to content

Commit

Permalink
Add deploy command unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickreimer committed Nov 25, 2019
1 parent bb9fae7 commit 3e5f860
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/cmds/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const indent = (text, depth = 2) => {
};

const printErrors = errors => {
console.log('');
console.error('');
errors.forEach(printError);
console.log('');
console.error('');
};

const printError = (error, idx) => {
Expand Down Expand Up @@ -98,6 +98,8 @@ const errorMessage = error => {
break;
}

break;

default:
title = `${chalk.cyan(error.field)} ${error.message}`;
body = '';
Expand Down
102 changes: 102 additions & 0 deletions test/cmds/__snapshots__/deploy.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`displays general validation errors 1`] = `
Array [
Array [
"✕ Deployment failed (xxxx-xxxx-xxxx)",
],
Array [
"",
],
Array [
" 1) name is required",
],
Array [
"",
],
]
`;

exports[`displays instructions for a missing mailchimp api keys 1`] = `
Array [
Array [
"✕ Deployment failed (xxxx-xxxx-xxxx)",
],
Array [
"",
],
Array [
" 1) The secret @my-mailchimp-key is not defined yet (actions[0].apiKey)",
],
Array [
"",
],
Array [
" Here's where to find your API key:
• Log in to your Mailchimp account
• Open the menu with your avatar in the upper right
• Navigate to Profile › Extras › API keys
• Under \\"Your API keys\\", click Create A Key
Copy the generated key and run the following command:
$ statickit secrets add my-mailchimp-key <paste-api-key-here>
Then, try deploying again.",
],
Array [
"",
],
Array [
"",
],
]
`;
exports[`displays instructions when a secret reference is required 1`] = `
Array [
Array [
"✕ Deployment failed (xxxx-xxxx-xxxx)",
],
Array [
"",
],
Array [
" 1) actions[0].apiKey must reference a secret (e.g. @mailchimp-api-key)",
],
Array [
"",
],
Array [
" This field must reference a secret instead of a raw value,
because it's too sensitive to commit to version control.
-- Instructions ----------------------------------------------------------
First, choose a name for this value and run the following command:
$ statickit secrets add mailchimp-api-key \\"myinlinekey\\"
Then, reference your secret like this (notice the @-symbol prefix):
{
\\"apiKey\\": \\"@mailchimp-api-key\\"
}",
],
Array [
"",
],
Array [
"",
],
]
`;
exports[`sends a deploy request with the right params 1`] = `
Array [
Array [
"✔ Deployment succeeded (xxxx-xxxx-xxxx)",
],
]
`;
126 changes: 126 additions & 0 deletions test/cmds/deploy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
const deploy = require('@statickit/deploy');
const version = require('../../package.json').version;

jest.mock('process', () => ({ env: {} }));
jest.mock('@statickit/deploy');

const cmd = require('../../src/cmds/deploy');

beforeEach(() => {
jest.spyOn(console, 'log').mockImplementation();
jest.spyOn(console, 'error').mockImplementation();
});

afterEach(() => {
console.log.mockRestore();
console.error.mockRestore();
});

it('sends a deploy request with the right params', async () => {
const config = { forms: { newsletter: { name: 'Newsletter' } } };
const args = { config: JSON.stringify(config), key: 'xxx' };

deploy.getRawConfig.mockImplementation(args => args.config);
deploy.getDeployKey.mockImplementation(args => args.key);

deploy.request.mockImplementation(params => {
expect(params.config).toStrictEqual(config);
expect(params.key).toBe('xxx');
expect(params.userAgent).toBe(`@statickit/cli@${version}`);

return Promise.resolve({ status: 200, data: { id: 'xxxx-xxxx-xxxx' } });
});

await cmd.handler(args);
expect(console.log.mock.calls).toMatchSnapshot();
});

it('displays instructions for a missing mailchimp api keys', async () => {
const config = {};
const args = { config: JSON.stringify(config), key: 'xxx' };

deploy.getRawConfig.mockImplementation(args => args.config);
deploy.getDeployKey.mockImplementation(args => args.key);

deploy.request.mockImplementation(_params => {
return Promise.resolve({
status: 422,
data: {
id: 'xxxx-xxxx-xxxx',
errors: [
{
code: 'SECRET_REQUIRED',
field: 'actions[0].apiKey',
message: 'is required',
properties: {
secret_type: 'mailchimp_api_key',
secret_key: 'my-mailchimp-key'
}
}
]
}
});
});

await cmd.handler(args);
expect(console.error.mock.calls).toMatchSnapshot();
});

it('displays instructions when a secret reference is required', async () => {
const config = {};
const args = { config: JSON.stringify(config), key: 'xxx' };

deploy.getRawConfig.mockImplementation(args => args.config);
deploy.getDeployKey.mockImplementation(args => args.key);

deploy.request.mockImplementation(_params => {
return Promise.resolve({
status: 422,
data: {
id: 'xxxx-xxxx-xxxx',
errors: [
{
code: 'SECRET_REFERENCE_REQUIRED',
field: 'actions[0].apiKey',
message: 'must reference a secret (e.g. @mailchimp-api-key)',
properties: {
example_value: 'mailchimp-api-key',
given_value: 'myinlinekey'
}
}
]
}
});
});

await cmd.handler(args);
expect(console.error.mock.calls).toMatchSnapshot();
});

it('displays general validation errors', async () => {
const config = {};
const args = { config: JSON.stringify(config), key: 'xxx' };

deploy.getRawConfig.mockImplementation(args => args.config);
deploy.getDeployKey.mockImplementation(args => args.key);

deploy.request.mockImplementation(_params => {
return Promise.resolve({
status: 422,
data: {
id: 'xxxx-xxxx-xxxx',
errors: [
{
code: 'REQUIRED',
field: 'name',
message: 'is required',
properties: {}
}
]
}
});
});

await cmd.handler(args);
expect(console.error.mock.calls).toMatchSnapshot();
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const axios = require('axios');
const version = require('../../package.json').version;
const version = require('../../../package.json').version;

jest.mock('process', () => ({ env: {} }));
jest.mock('axios');

const cmd = require('../../src/cmds/secrets_cmds/add');
const cmd = require('../../../src/cmds/secrets_cmds/add');

let consoleSpy;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const axios = require('axios');
const version = require('../../package.json').version;
const version = require('../../../package.json').version;

jest.mock('process', () => ({ env: {} }));
jest.mock('axios');

const cmd = require('../../src/cmds/secrets_cmds/delete');
const cmd = require('../../../src/cmds/secrets_cmds/delete');

let consoleSpy;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const axios = require('axios');
const version = require('../../package.json').version;
const version = require('../../../package.json').version;

jest.mock('process', () => ({ env: {} }));
jest.mock('axios');

const cmd = require('../../src/cmds/secrets_cmds/update');
const cmd = require('../../../src/cmds/secrets_cmds/update');

let consoleSpy;

Expand Down

0 comments on commit 3e5f860

Please sign in to comment.