Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: commands into command classes #424

Merged
merged 12 commits into from
Jan 12, 2022
2 changes: 2 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"extends": ["@readme/eslint-config"],
"root": true,
"rules": {
"class-methods-use-this": ["error", { "exceptMethods": ["run"] }],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wanna add a small comment explaining why we need this?


/**
* This is a small rule to prevent us from using console.log() statements in our commands.
*
Expand Down
33 changes: 18 additions & 15 deletions __tests__/cmds/docs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ const frontMatter = require('gray-matter');

const APIError = require('../../src/lib/apiError');

const docs = require('../../src/cmds/docs');
const docsEdit = require('../../src/cmds/docs/edit');
const DocsCommand = require('../../src/cmds/docs');
const DocsEditCommand = require('../../src/cmds/docs/edit');

const docs = new DocsCommand();
const docsEdit = new DocsEditCommand();

const fixturesDir = `${__dirname}./../__fixtures__`;
const key = 'API_KEY';
Expand All @@ -18,7 +21,7 @@ const category = 'CATEGORY_ID';
const apiSetting = 'API_SETTING_ID';

function getNockWithVersionHeader(v) {
return nock(config.host, {
return nock(config.get('host'), {
reqheaders: {
'x-readme-version': v,
},
Expand Down Expand Up @@ -105,7 +108,7 @@ describe('rdme docs', () => {
.basicAuth({ user: key })
.reply(200, { category, slug: anotherDoc.slug, body: anotherDoc.doc.content });

const versionMock = nock(config.host)
const versionMock = nock(config.get('host'))
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });
Expand Down Expand Up @@ -139,7 +142,7 @@ describe('rdme docs', () => {
.basicAuth({ user: key })
.reply(200, { category, slug: anotherDoc.slug, lastUpdatedHash: anotherDoc.hash });

const versionMock = nock(config.host)
const versionMock = nock(config.get('host'))
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });
Expand Down Expand Up @@ -177,7 +180,7 @@ describe('rdme docs', () => {
.basicAuth({ user: key })
.reply(201, { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash });

const versionMock = nock(config.host)
const versionMock = nock(config.get('host'))
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });
Expand Down Expand Up @@ -246,7 +249,7 @@ describe('rdme docs', () => {
.basicAuth({ user: key })
.reply(400, errorObject);

const versionMock = nock(config.host)
const versionMock = nock(config.get('host'))
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });
Expand Down Expand Up @@ -289,7 +292,7 @@ describe('rdme docs', () => {
.basicAuth({ user: key })
.reply(201, { slug: doc.data.slug, body: doc.content, ...doc.data, lastUpdatedHash: hash });

const versionMock = nock(config.host)
const versionMock = nock(config.get('host'))
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });
Expand Down Expand Up @@ -335,7 +338,7 @@ describe('rdme docs:edit', () => {
.basicAuth({ user: key })
.reply(200, { category, slug });

const versionMock = nock(config.host)
const versionMock = nock(config.get('host'))
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });
Expand Down Expand Up @@ -367,9 +370,9 @@ describe('rdme docs:edit', () => {
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".',
};

const getMock = nock(config.host).get(`/api/v1/docs/${slug}`).reply(404, errorObject);
const getMock = nock(config.get('host')).get(`/api/v1/docs/${slug}`).reply(404, errorObject);

const versionMock = nock(config.host)
const versionMock = nock(config.get('host'))
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });
Expand All @@ -392,11 +395,11 @@ describe('rdme docs:edit', () => {
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".',
};

const getMock = nock(config.host).get(`/api/v1/docs/${slug}`).reply(200, { body });
const getMock = nock(config.get('host')).get(`/api/v1/docs/${slug}`).reply(200, { body });

const putMock = nock(config.host).put(`/api/v1/docs/${slug}`).reply(400, errorObject);
const putMock = nock(config.get('host')).put(`/api/v1/docs/${slug}`).reply(400, errorObject);

const versionMock = nock(config.host)
const versionMock = nock(config.get('host'))
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });
Expand All @@ -420,7 +423,7 @@ describe('rdme docs:edit', () => {
const slug = 'getting-started';
const body = 'abcdef';

const getMock = nock(config.host)
const getMock = nock(config.get('host'))
.get(`/api/v1/docs/${slug}`)
.reply(200, { body })
.get(`/api/v1/version/${version}`)
Expand Down
38 changes: 19 additions & 19 deletions __tests__/cmds/login.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const nock = require('nock');
const config = require('config');
const configStore = require('../../src/lib/configstore');
const cmd = require('../../src/cmds/login');
const Command = require('../../src/cmds/login');

const cmd = new Command();

describe('rdme login', () => {
beforeAll(() => nock.disableNetConnect());
Expand All @@ -15,28 +17,27 @@ describe('rdme login', () => {
});

it('should error if email is invalid', () => {
expect.assertions(1);
return expect(cmd.run({ project: 'subdomain', email: 'this-is-not-an-email' })).rejects.toThrow(
'You must provide a valid email address.'
);
});

it('should post to /login on the API', () => {
it('should post to /login on the API', async () => {
expect.assertions(3);
const email = '[email protected]';
const password = '123456';
const project = 'subdomain';
const apiKey = 'abcdefg';

const mock = nock(config.host).post('/api/v1/login', { email, password, project }).reply(200, { apiKey });
const mock = nock(config.get('host')).post('/api/v1/login', { email, password, project }).reply(200, { apiKey });

return cmd.run({ email, password, project }).then(() => {
mock.done();
expect(configStore.get('apiKey')).toBe(apiKey);
expect(configStore.get('email')).toBe(email);
expect(configStore.get('project')).toBe(project);
configStore.clear();
});
await cmd.run({ email, password, project });
mock.done();

expect(configStore.get('apiKey')).toBe(apiKey);
expect(configStore.get('email')).toBe(email);
expect(configStore.get('project')).toBe(project);
configStore.clear();
});

it('should error if invalid credentials are given', () => {
Expand All @@ -45,7 +46,7 @@ describe('rdme login', () => {
const password = '123456';
const project = 'subdomain';

const mock = nock(config.host).post('/api/v1/login', { email, password, project }).reply(401, {
const mock = nock(config.get('host')).post('/api/v1/login', { email, password, project }).reply(401, {
error: 'LOGIN_INVALID',
message: 'Either your email address or password is incorrect',
suggestion: 'You can reset your password at https://dash.readme.com/forgot',
Expand All @@ -65,33 +66,32 @@ describe('rdme login', () => {
const password = '123456';
const project = 'subdomain';

const mock = nock(config.host).post('/api/v1/login', { email, password, project }).reply(401, {
const mock = nock(config.get('host')).post('/api/v1/login', { email, password, project }).reply(401, {
error: 'LOGIN_TWOFACTOR',
message: 'You must provide a two-factor code',
suggestion: 'You can do it via the API using `token`, or via the CLI using `rdme login --2fa`',
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".',
});

return cmd.run({ email, password, project }).catch(err => {
mock.done();
expect(err.code).toBe('LOGIN_TWOFACTOR');
expect(err.message).toContain('You must provide a two-factor code');
mock.done();
});
});

it('should send 2fa token if provided', () => {
it('should send 2fa token if provided', async () => {
const email = '[email protected]';
const password = '123456';
const project = 'subdomain';
const token = '123456';

const mock = nock(config.host)
const mock = nock(config.get('host'))
.post('/api/v1/login', { email, password, project, token })
.reply(200, { apiKey: '123' });

return cmd.run({ email, password, project, token }).then(() => {
mock.done();
});
await cmd.run({ email, password, project, token });
mock.done();
});

it.todo('should error if trying to access a project that is not yours');
Expand Down
9 changes: 4 additions & 5 deletions __tests__/cmds/logout.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
const config = require('config');
const configStore = require('../../src/lib/configstore');
const cmd = require('../../src/cmds/logout');
const loginCmd = require('../../src/cmds/login');
const Command = require('../../src/cmds/logout');

const cmd = new Command();

describe('rdme logout', () => {
it("should report the user as logged out if they aren't logged in", () => {
configStore.delete('email');
configStore.delete('project');

return cmd.run({}).then(msg => {
expect(msg).toBe(
`You have logged out of ReadMe. Please use \`${config.cli} ${loginCmd.command}\` to login again.`
);
expect(msg).toBe(`You have logged out of ReadMe. Please use \`${config.get('cli')} login\` to login again.`);
});
});

Expand Down
7 changes: 4 additions & 3 deletions __tests__/cmds/open.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const chalk = require('chalk');
const config = require('config');
const configStore = require('../../src/lib/configstore');
const cmd = require('../../src/cmds/open');
const loginCmd = require('../../src/cmds/login');
const Command = require('../../src/cmds/open');

const cmd = new Command();

describe('rdme open', () => {
it('should error if no project provided', () => {
configStore.delete('project');

return expect(cmd.run({})).rejects.toThrow(`Please login using \`${config.cli} ${loginCmd.command}\`.`);
return expect(cmd.run({})).rejects.toThrow(`Please login using \`${config.get('cli')} login\`.`);
});

it('should open the project', () => {
Expand Down
Loading