Skip to content

Commit

Permalink
fix: login issue when user doesn't pass in API key (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
kanadgupta authored Dec 16, 2022
1 parent d197242 commit 9561f7c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
30 changes: 30 additions & 0 deletions __tests__/cmds/docs/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import prompts from 'prompts';
import DocsCommand from '../../../src/cmds/docs';
import GuidesCommand from '../../../src/cmds/guides';
import APIError from '../../../src/lib/apiError';
import configstore from '../../../src/lib/configstore';
import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock';
import { after, before } from '../../helpers/get-gha-setup';
import hashFileContents from '../../helpers/hash-file-contents';
Expand Down Expand Up @@ -39,6 +40,35 @@ describe('rdme docs', () => {
consoleInfoSpy.mockRestore();
});

it('should successfully log in user via prompts if API key is not provided', async () => {
const email = '[email protected]';
const password = 'pass123';
const project = 'proj1';

const consoleInfoSpy = jest.spyOn(console, 'info').mockImplementation();
const getCommandOutput = () => {
return [consoleInfoSpy.mock.calls.join('\n\n')].filter(Boolean).join('\n\n');
};

prompts.inject([email, password, project]);

const mock = getAPIMock()
.post('/api/v1/login', { email, password, project })
.reply(200, { apiKey: key })
.get('/api/v1/version')
.basicAuth({ user: key })
.reply(200, [{ version }]);

await expect(docs.run({})).rejects.toStrictEqual(
new Error('No path provided. Usage `rdme docs <path> [options]`.')
);
expect(getCommandOutput()).toContain("Looks like you're missing a ReadMe API key, let's fix that! 🦉");
expect(getCommandOutput()).toContain('Successfully logged in as [email protected] to the proj1 project.');
mock.done();
configstore.clear();
jest.resetAllMocks();
});

it('should error in CI if no API key provided', async () => {
process.env.TEST_RDME_CI = 'true';
await expect(docs.run({})).rejects.toStrictEqual(new Error('No project API key provided. Please use `--key`.'));
Expand Down
4 changes: 2 additions & 2 deletions src/lib/baseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default class Command {
Command.debug(`opts: ${JSON.stringify(opts)}`);

if (this.args.some(arg => arg.name === 'key')) {
const { apiKey, email, project } = getCurrentConfig();
const { email, project } = getCurrentConfig();

// We only want to log this if the API key is stored in the configstore, **not** in an env var.
if (opts.key && configstore.get('apiKey') === opts.key) {
Expand All @@ -109,7 +109,7 @@ export default class Command {
const result = await loginFlow();
info(result, { includeEmojiPrefix: false });
// eslint-disable-next-line no-param-reassign
opts.key = apiKey;
opts.key = configstore.get('apiKey');
}
}

Expand Down

0 comments on commit 9561f7c

Please sign in to comment.