-
Notifications
You must be signed in to change notification settings - Fork 49
fix: improving our error messages when API keys aren't supplied #1463
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,9 +5,10 @@ import fs from 'node:fs/promises'; | |
| import nock from 'nock'; | ||
| import prompts from 'prompts'; | ||
| import slugify from 'slugify'; | ||
| import { afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest'; | ||
| import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import Command from '../../../src/commands/openapi/upload.js'; | ||
| import configstore from '../../../src/lib/configstore.js'; | ||
| import petstore from '../../__fixtures__/petstore-simple-weird-version.json' with { type: 'json' }; | ||
| import { getAPIv2Mock, getAPIv2MockForGHA } from '../../helpers/get-api-mock.js'; | ||
| import { githubActionsEnv } from '../../helpers/git-mock.js'; | ||
|
|
@@ -42,6 +43,47 @@ describe('rdme openapi upload', () => { | |
|
|
||
| expect(result).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| describe('API key validation', () => { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests are well covered already in |
||
| it('should error when `--key` is empty', async () => { | ||
| const result = await run(['--branch', branch, filename, '--key', '']); | ||
|
|
||
| expect(result.error?.message).toContain('No project API key was specified.'); | ||
| }); | ||
|
|
||
| it('should error when `--key` is whitespace-only', async () => { | ||
| const result = await run(['--branch', branch, filename, '--key', ' ']); | ||
|
|
||
| expect(result.error?.message).toContain('No project API key was specified.'); | ||
| }); | ||
|
|
||
| describe('in CI without env or configstore key', () => { | ||
| const originalGet = configstore.get.bind(configstore); | ||
|
|
||
| beforeEach(() => { | ||
| vi.stubEnv('TEST_RDME_CI', 'true'); | ||
| vi.stubEnv('RDME_API_KEY', ''); | ||
| vi.stubEnv('README_API_KEY', ''); | ||
| vi.spyOn(configstore, 'get').mockImplementation((storeKey: string) => { | ||
| if (storeKey === 'apiKey') return; | ||
| return originalGet(storeKey); | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllEnvs(); | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('should error with guidance when no API key is available', async () => { | ||
| const result = await run(['--branch', branch, filename]); | ||
|
|
||
| expect(result.error?.message).toMatchInlineSnapshot( | ||
| `"No project API key was provided. Please provide one with \`--key\` or the \`RDME_API_KEY\` or \`README_API_KEY\` environment variables."`, | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('given that the API definition is a local file', () => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,6 @@ export const mockVersion = '7.0.0'; | |
| * @see {@link https://oclif.io/docs/testing} | ||
| */ | ||
| export function setupOclifConfig() { | ||
| // https://stackoverflow.com/a/61829368 | ||
| const root = path.join(new URL('.', import.meta.url).pathname, '.'); | ||
|
|
||
| return Config.load({ | ||
|
|
@@ -39,9 +38,16 @@ export function setupOclifConfig() { | |
| export function runCommand(Command: CommandClass) { | ||
| return async function runCommandAgainstArgs(args?: string[]) { | ||
| const oclifConfig = await setupOclifConfig(); | ||
| // @ts-expect-error currently we have mismatching return types in our commands. | ||
| // we can fix this later but it's not a priority right now. | ||
| return captureOutput<string>(() => Command.run(args, oclifConfig), { testNodeEnv }); | ||
| return captureOutput<string>( | ||
| async () => { | ||
| await oclifConfig.runHook('prerun', { argv: args ?? [], Command }); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because |
||
|
|
||
| // @ts-expect-error currently we have mismatching return types in our commands. | ||
| // we can fix this later but it's not a priority right now. | ||
| return Command.run(args ?? [], oclifConfig); | ||
| }, | ||
| { testNodeEnv }, | ||
| ); | ||
| }; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { normalizeAPIKey } from '../../src/lib/getCurrentConfig.js'; | ||
|
|
||
| describe('#normalizeAPIKey()', () => { | ||
| it('returns undefined for missing or whitespace-only values', () => { | ||
| expect(normalizeAPIKey('')).toBeUndefined(); | ||
| expect(normalizeAPIKey(' ')).toBeUndefined(); | ||
| expect(normalizeAPIKey('\t\n')).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('returns trimmed non-empty strings', () => { | ||
| expect(normalizeAPIKey(' rdme_abc ')).toBe('rdme_abc'); | ||
| expect(normalizeAPIKey('x')).toBe('x'); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated these docs because
--keyis a flag, not an option: