Skip to content

Commit 9561f7c

Browse files
authored
fix: login issue when user doesn't pass in API key (#714)
1 parent d197242 commit 9561f7c

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

__tests__/cmds/docs/index.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import prompts from 'prompts';
1010
import DocsCommand from '../../../src/cmds/docs';
1111
import GuidesCommand from '../../../src/cmds/guides';
1212
import APIError from '../../../src/lib/apiError';
13+
import configstore from '../../../src/lib/configstore';
1314
import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock';
1415
import { after, before } from '../../helpers/get-gha-setup';
1516
import hashFileContents from '../../helpers/hash-file-contents';
@@ -39,6 +40,35 @@ describe('rdme docs', () => {
3940
consoleInfoSpy.mockRestore();
4041
});
4142

43+
it('should successfully log in user via prompts if API key is not provided', async () => {
44+
const email = 'owlbert@readme.io';
45+
const password = 'pass123';
46+
const project = 'proj1';
47+
48+
const consoleInfoSpy = jest.spyOn(console, 'info').mockImplementation();
49+
const getCommandOutput = () => {
50+
return [consoleInfoSpy.mock.calls.join('\n\n')].filter(Boolean).join('\n\n');
51+
};
52+
53+
prompts.inject([email, password, project]);
54+
55+
const mock = getAPIMock()
56+
.post('/api/v1/login', { email, password, project })
57+
.reply(200, { apiKey: key })
58+
.get('/api/v1/version')
59+
.basicAuth({ user: key })
60+
.reply(200, [{ version }]);
61+
62+
await expect(docs.run({})).rejects.toStrictEqual(
63+
new Error('No path provided. Usage `rdme docs <path> [options]`.')
64+
);
65+
expect(getCommandOutput()).toContain("Looks like you're missing a ReadMe API key, let's fix that! 🦉");
66+
expect(getCommandOutput()).toContain('Successfully logged in as owlbert@readme.io to the proj1 project.');
67+
mock.done();
68+
configstore.clear();
69+
jest.resetAllMocks();
70+
});
71+
4272
it('should error in CI if no API key provided', async () => {
4373
process.env.TEST_RDME_CI = 'true';
4474
await expect(docs.run({})).rejects.toStrictEqual(new Error('No project API key provided. Please use `--key`.'));

src/lib/baseCommand.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export default class Command {
8989
Command.debug(`opts: ${JSON.stringify(opts)}`);
9090

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

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

0 commit comments

Comments
 (0)