From 932a6d459e0ae76fe5eb00339835f0dcb3503ad7 Mon Sep 17 00:00:00 2001 From: Kanad Gupta <8854718+kanadgupta@users.noreply.github.com> Date: Tue, 19 Nov 2024 12:00:03 -0600 Subject: [PATCH] chore: rename test mocks, smol comment/type fixes (#1076) --- __tests__/commands/categories/create.test.ts | 41 +++-- __tests__/commands/categories/index.test.ts | 16 +- __tests__/commands/changelogs/index.test.ts | 26 +-- __tests__/commands/changelogs/single.test.ts | 24 +-- __tests__/commands/custompages/index.test.ts | 30 ++-- __tests__/commands/custompages/single.test.ts | 31 ++-- __tests__/commands/docs/index.test.ts | 90 ++++++----- __tests__/commands/docs/multiple.test.ts | 34 ++-- __tests__/commands/docs/prune.test.ts | 40 +++-- __tests__/commands/docs/single.test.ts | 64 ++++---- __tests__/commands/login.test.ts | 16 +- __tests__/commands/open.test.ts | 4 +- __tests__/commands/openapi/index.test.ts | 148 +++++++++--------- __tests__/commands/versions/create.test.ts | 10 +- __tests__/commands/versions/delete.test.ts | 6 +- __tests__/commands/versions/index.test.ts | 6 +- __tests__/commands/versions/update.test.ts | 20 +-- __tests__/helpers/get-api-mock.ts | 11 +- __tests__/lib/fetch.test.ts | 32 ++-- src/commands/openapi/index.ts | 2 - src/lib/readmeAPIFetch.ts | 2 +- src/lib/streamSpecToRegistry.ts | 10 +- 22 files changed, 365 insertions(+), 298 deletions(-) diff --git a/__tests__/commands/categories/create.test.ts b/__tests__/commands/categories/create.test.ts index eed89cb03..6d756802b 100644 --- a/__tests__/commands/categories/create.test.ts +++ b/__tests__/commands/categories/create.test.ts @@ -2,7 +2,7 @@ import nock from 'nock'; import { describe, beforeAll, afterEach, it, expect } from 'vitest'; import Command from '../../../src/commands/categories/create.js'; -import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock.js'; +import { getAPIV1Mock, getAPIV1MockWithVersionHeader } from '../../helpers/get-api-mock.js'; import { runCommand } from '../../helpers/setup-oclif-config.js'; const key = 'API_KEY'; @@ -33,7 +33,7 @@ describe('rdme categories:create', () => { }); it('should create a new category if the title and type do not match and preventDuplicates=true', async () => { - const getMock = getAPIMockWithVersionHeader(version) + const getMock = getAPIV1MockWithVersionHeader(version) .persist() .get('/api/v1/categories?perPage=20&page=1') .basicAuth({ user: key }) @@ -41,12 +41,15 @@ describe('rdme categories:create', () => { 'x-total-count': '1', }); - const postMock = getAPIMockWithVersionHeader(version) + const postMock = getAPIV1MockWithVersionHeader(version) .post('/api/v1/categories') .basicAuth({ user: key }) .reply(201, { title: 'New Category', slug: 'new-category', type: 'guide', id: '123' }); - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); + const versionMock = getAPIV1Mock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); await expect( run(['New Category', '--categoryType', 'guide', '--key', key, '--version', '1.0.0', '--preventDuplicates']), @@ -58,7 +61,7 @@ describe('rdme categories:create', () => { }); it('should create a new category if the title matches but the type does not match and preventDuplicates=true', async () => { - const getMock = getAPIMockWithVersionHeader(version) + const getMock = getAPIV1MockWithVersionHeader(version) .persist() .get('/api/v1/categories?perPage=20&page=1') .basicAuth({ user: key }) @@ -66,12 +69,15 @@ describe('rdme categories:create', () => { 'x-total-count': '1', }); - const postMock = getAPIMockWithVersionHeader(version) + const postMock = getAPIV1MockWithVersionHeader(version) .post('/api/v1/categories') .basicAuth({ user: key }) .reply(201, { title: 'Category', slug: 'category', type: 'reference', id: '123' }); - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); + const versionMock = getAPIV1Mock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); await expect( run(['--categoryType', 'reference', '--key', key, '--version', '1.0.0', '--preventDuplicates', 'Category']), @@ -83,12 +89,15 @@ describe('rdme categories:create', () => { }); it('should create a new category if the title and type match and preventDuplicates=false', async () => { - const postMock = getAPIMockWithVersionHeader(version) + const postMock = getAPIV1MockWithVersionHeader(version) .post('/api/v1/categories') .basicAuth({ user: key }) .reply(201, { title: 'Category', slug: 'category', type: 'reference', id: '123' }); - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); + const versionMock = getAPIV1Mock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); await expect(run(['Category', '--categoryType', 'guide', '--key', key, '--version', '1.0.0'])).resolves.toBe( "🌱 successfully created 'Category' with a type of 'reference' and an id of '123'", @@ -99,7 +108,7 @@ describe('rdme categories:create', () => { }); it('should not create a new category if the title and type match and preventDuplicates=true', async () => { - const getMock = getAPIMockWithVersionHeader(version) + const getMock = getAPIV1MockWithVersionHeader(version) .persist() .get('/api/v1/categories?perPage=20&page=1') .basicAuth({ user: key }) @@ -107,7 +116,10 @@ describe('rdme categories:create', () => { 'x-total-count': '1', }); - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); + const versionMock = getAPIV1Mock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); await expect( run(['Category', '--categoryType', 'guide', '--key', key, '--version', '1.0.0', '--preventDuplicates']), @@ -122,7 +134,7 @@ describe('rdme categories:create', () => { }); it('should not create a new category if the non case sensitive title and type match and preventDuplicates=true', async () => { - const getMock = getAPIMockWithVersionHeader(version) + const getMock = getAPIV1MockWithVersionHeader(version) .persist() .get('/api/v1/categories?perPage=20&page=1') .basicAuth({ user: key }) @@ -130,7 +142,10 @@ describe('rdme categories:create', () => { 'x-total-count': '1', }); - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); + const versionMock = getAPIV1Mock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); await expect( run(['Category', '--categoryType', 'guide', '--key', key, '--version', '1.0.0', '--preventDuplicates']), diff --git a/__tests__/commands/categories/index.test.ts b/__tests__/commands/categories/index.test.ts index 7572f71a6..51d9a5311 100644 --- a/__tests__/commands/categories/index.test.ts +++ b/__tests__/commands/categories/index.test.ts @@ -2,7 +2,7 @@ import nock from 'nock'; import { describe, beforeAll, afterEach, it, expect } from 'vitest'; import Command from '../../../src/commands/categories/index.js'; -import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock.js'; +import { getAPIV1Mock, getAPIV1MockWithVersionHeader } from '../../helpers/get-api-mock.js'; import { runCommand } from '../../helpers/setup-oclif-config.js'; const key = 'API_KEY'; @@ -19,7 +19,7 @@ describe('rdme categories', () => { afterEach(() => nock.cleanAll()); it('should return all categories for a single page', async () => { - const getMock = getAPIMockWithVersionHeader(version) + const getMock = getAPIV1MockWithVersionHeader(version) .persist() .get('/api/v1/categories?perPage=20&page=1') .basicAuth({ user: key }) @@ -27,7 +27,10 @@ describe('rdme categories', () => { 'x-total-count': '1', }); - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); + const versionMock = getAPIV1Mock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); await expect(run(['--key', key, '--version', '1.0.0'])).resolves.toBe( JSON.stringify([{ title: 'One Category', slug: 'one-category', type: 'guide' }], null, 2), @@ -38,7 +41,7 @@ describe('rdme categories', () => { }); it('should return all categories for multiple pages', async () => { - const getMock = getAPIMockWithVersionHeader(version) + const getMock = getAPIV1MockWithVersionHeader(version) .persist() .get('/api/v1/categories?perPage=20&page=1') .basicAuth({ user: key }) @@ -51,7 +54,10 @@ describe('rdme categories', () => { 'x-total-count': '21', }); - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); + const versionMock = getAPIV1Mock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); await expect(run(['--key', key, '--version', '1.0.0'])).resolves.toBe( JSON.stringify( diff --git a/__tests__/commands/changelogs/index.test.ts b/__tests__/commands/changelogs/index.test.ts index a849b18c7..1db515865 100644 --- a/__tests__/commands/changelogs/index.test.ts +++ b/__tests__/commands/changelogs/index.test.ts @@ -8,7 +8,7 @@ import { describe, beforeAll, afterAll, beforeEach, it, expect } from 'vitest'; import Command from '../../../src/commands/changelogs.js'; import APIError from '../../../src/lib/apiError.js'; -import getAPIMock from '../../helpers/get-api-mock.js'; +import { getAPIV1Mock } from '../../helpers/get-api-mock.js'; import hashFileContents from '../../helpers/hash-file-contents.js'; import { runCommand } from '../../helpers/setup-oclif-config.js'; @@ -67,7 +67,7 @@ describe('rdme changelogs', () => { it('should fetch changelog and merge with what is returned', () => { expect.assertions(1); - const getMocks = getAPIMock() + const getMocks = getAPIV1Mock() .get('/api/v1/changelogs/simple-doc') .basicAuth({ user: key }) .reply(200, { slug: simpleDoc.slug, lastUpdatedHash: 'anOldHash' }) @@ -75,7 +75,7 @@ describe('rdme changelogs', () => { .basicAuth({ user: key }) .reply(200, { slug: anotherDoc.slug, lastUpdatedHash: 'anOldHash' }); - const updateMocks = getAPIMock() + const updateMocks = getAPIV1Mock() .put('/api/v1/changelogs/simple-doc', { body: simpleDoc.doc.content, lastUpdatedHash: simpleDoc.hash, @@ -112,7 +112,7 @@ describe('rdme changelogs', () => { it('should return changelog update info for dry run', () => { expect.assertions(1); - const getMocks = getAPIMock() + const getMocks = getAPIV1Mock() .get('/api/v1/changelogs/simple-doc') .basicAuth({ user: key }) .reply(200, { slug: simpleDoc.slug, lastUpdatedHash: 'anOldHash' }) @@ -141,7 +141,7 @@ describe('rdme changelogs', () => { it('should not send requests for changelogs that have not changed', () => { expect.assertions(1); - const getMocks = getAPIMock() + const getMocks = getAPIV1Mock() .get('/api/v1/changelogs/simple-doc') .basicAuth({ user: key }) .reply(200, { slug: simpleDoc.slug, lastUpdatedHash: simpleDoc.hash }) @@ -164,7 +164,7 @@ describe('rdme changelogs', () => { it('should adjust "no changes" message if in dry run', () => { expect.assertions(1); - const getMocks = getAPIMock() + const getMocks = getAPIV1Mock() .get('/api/v1/changelogs/simple-doc') .basicAuth({ user: key }) .reply(200, { slug: simpleDoc.slug, lastUpdatedHash: simpleDoc.hash }) @@ -192,7 +192,7 @@ describe('rdme changelogs', () => { const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get(`/api/v1/changelogs/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -202,7 +202,7 @@ describe('rdme changelogs', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMock = getAPIMock() + const postMock = getAPIV1Mock() .post('/api/v1/changelogs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) .reply(201, { slug, _id: id, body: doc.content, ...doc.data, lastUpdatedHash: hash }); @@ -219,7 +219,7 @@ describe('rdme changelogs', () => { const slug = 'new-doc'; const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get(`/api/v1/changelogs/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -254,7 +254,7 @@ describe('rdme changelogs', () => { const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/${folder}/${slug}.md`))); - const getMocks = getAPIMock() + const getMocks = getAPIV1Mock() .get(`/api/v1/changelogs/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -264,7 +264,7 @@ describe('rdme changelogs', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMocks = getAPIMock() + const postMocks = getAPIV1Mock() .post('/api/v1/changelogs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) .reply(400, errorObject); @@ -290,7 +290,7 @@ describe('rdme changelogs', () => { const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/slug-docs/${slug}.md`))); const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/slug-docs/${slug}.md`))); - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get(`/api/v1/changelogs/${doc.data.slug}`) .basicAuth({ user: key }) .reply(404, { @@ -300,7 +300,7 @@ describe('rdme changelogs', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMock = getAPIMock() + const postMock = getAPIV1Mock() .post('/api/v1/changelogs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) .reply(201, { slug: doc.data.slug, _id: id, body: doc.content, ...doc.data, lastUpdatedHash: hash }); diff --git a/__tests__/commands/changelogs/single.test.ts b/__tests__/commands/changelogs/single.test.ts index a89a88b46..0048083d9 100644 --- a/__tests__/commands/changelogs/single.test.ts +++ b/__tests__/commands/changelogs/single.test.ts @@ -8,7 +8,7 @@ import { describe, beforeAll, afterAll, beforeEach, it, expect } from 'vitest'; import Command from '../../../src/commands/changelogs.js'; import APIError from '../../../src/lib/apiError.js'; -import getAPIMock from '../../helpers/get-api-mock.js'; +import { getAPIV1Mock } from '../../helpers/get-api-mock.js'; import hashFileContents from '../../helpers/hash-file-contents.js'; import { runCommand } from '../../helpers/setup-oclif-config.js'; @@ -49,7 +49,7 @@ describe('rdme changelogs (single)', () => { const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get(`/api/v1/changelogs/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -59,7 +59,7 @@ describe('rdme changelogs (single)', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMock = getAPIMock() + const postMock = getAPIV1Mock() .post('/api/v1/changelogs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) .reply(201, { slug, _id: id, body: doc.content, ...doc.data }); @@ -76,7 +76,7 @@ describe('rdme changelogs (single)', () => { const slug = 'new-doc'; const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get(`/api/v1/changelogs/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -113,7 +113,7 @@ describe('rdme changelogs (single)', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }; - const getMock = getAPIMock().get(`/api/v1/changelogs/${slug}`).basicAuth({ user: key }).reply(500, errorObject); + const getMock = getAPIV1Mock().get(`/api/v1/changelogs/${slug}`).basicAuth({ user: key }).reply(500, errorObject); const filePath = `./__tests__/${fixturesBaseDir}/failure-docs/${slug}.md`; @@ -135,7 +135,7 @@ describe('rdme changelogs (single)', () => { const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/slug-docs/${slug}.md`))); const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/slug-docs/${slug}.md`))); - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get(`/api/v1/changelogs/${doc.data.slug}`) .basicAuth({ user: key }) .reply(404, { @@ -145,7 +145,7 @@ describe('rdme changelogs (single)', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMock = getAPIMock() + const postMock = getAPIV1Mock() .post('/api/v1/changelogs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) .reply(201, { slug: doc.data.slug, _id: id, body: doc.content, ...doc.data, lastUpdatedHash: hash }); @@ -172,12 +172,12 @@ describe('rdme changelogs (single)', () => { }); it('should fetch changelog and merge with what is returned', () => { - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get('/api/v1/changelogs/simple-doc') .basicAuth({ user: key }) .reply(200, { slug: simpleDoc.slug, lastUpdatedHash: 'anOldHash' }); - const updateMock = getAPIMock() + const updateMock = getAPIV1Mock() .put('/api/v1/changelogs/simple-doc', { body: simpleDoc.doc.content, lastUpdatedHash: simpleDoc.hash, @@ -202,7 +202,7 @@ describe('rdme changelogs (single)', () => { it('should return changelog update info for dry run', () => { expect.assertions(1); - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get('/api/v1/changelogs/simple-doc') .basicAuth({ user: key }) .reply(200, { slug: simpleDoc.slug, lastUpdatedHash: 'anOldHash' }); @@ -227,7 +227,7 @@ describe('rdme changelogs (single)', () => { it('should not send requests for changelogs that have not changed', () => { expect.assertions(1); - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get('/api/v1/changelogs/simple-doc') .basicAuth({ user: key }) .reply(200, { slug: simpleDoc.slug, lastUpdatedHash: simpleDoc.hash }); @@ -240,7 +240,7 @@ describe('rdme changelogs (single)', () => { }); it('should adjust "no changes" message if in dry run', () => { - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get('/api/v1/changelogs/simple-doc') .basicAuth({ user: key }) .reply(200, { slug: simpleDoc.slug, lastUpdatedHash: simpleDoc.hash }); diff --git a/__tests__/commands/custompages/index.test.ts b/__tests__/commands/custompages/index.test.ts index 10c106978..a96353085 100644 --- a/__tests__/commands/custompages/index.test.ts +++ b/__tests__/commands/custompages/index.test.ts @@ -8,7 +8,7 @@ import { describe, beforeAll, afterAll, beforeEach, it, expect } from 'vitest'; import Command from '../../../src/commands/custompages.js'; import APIError from '../../../src/lib/apiError.js'; -import getAPIMock from '../../helpers/get-api-mock.js'; +import { getAPIV1Mock } from '../../helpers/get-api-mock.js'; import hashFileContents from '../../helpers/hash-file-contents.js'; import { runCommand } from '../../helpers/setup-oclif-config.js'; @@ -67,7 +67,7 @@ describe('rdme custompages', () => { it('should fetch custom page and merge with what is returned', () => { expect.assertions(1); - const getMocks = getAPIMock() + const getMocks = getAPIV1Mock() .get('/api/v1/custompages/simple-doc') .basicAuth({ user: key }) .reply(200, { slug: simpleDoc.slug, htmlmode: false, lastUpdatedHash: 'anOldHash' }) @@ -75,7 +75,7 @@ describe('rdme custompages', () => { .basicAuth({ user: key }) .reply(200, { slug: anotherDoc.slug, htmlmode: false, lastUpdatedHash: 'anOldHash' }); - const updateMocks = getAPIMock() + const updateMocks = getAPIV1Mock() .put('/api/v1/custompages/simple-doc', { body: simpleDoc.doc.content, htmlmode: false, @@ -115,7 +115,7 @@ describe('rdme custompages', () => { it('should return custom page update info for dry run', () => { expect.assertions(1); - const getMocks = getAPIMock() + const getMocks = getAPIV1Mock() .get('/api/v1/custompages/simple-doc') .basicAuth({ user: key }) .reply(200, { slug: simpleDoc.slug, lastUpdatedHash: 'anOldHash' }) @@ -144,7 +144,7 @@ describe('rdme custompages', () => { it('should not send requests for custompages that have not changed', () => { expect.assertions(1); - const getMocks = getAPIMock() + const getMocks = getAPIV1Mock() .get('/api/v1/custompages/simple-doc') .basicAuth({ user: key }) .reply(200, { slug: simpleDoc.slug, lastUpdatedHash: simpleDoc.hash }) @@ -167,7 +167,7 @@ describe('rdme custompages', () => { it('should adjust "no changes" message if in dry run', () => { expect.assertions(1); - const getMocks = getAPIMock() + const getMocks = getAPIV1Mock() .get('/api/v1/custompages/simple-doc') .basicAuth({ user: key }) .reply(200, { slug: simpleDoc.slug, lastUpdatedHash: simpleDoc.hash }) @@ -195,7 +195,7 @@ describe('rdme custompages', () => { const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get(`/api/v1/custompages/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -205,7 +205,7 @@ describe('rdme custompages', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMock = getAPIMock() + const postMock = getAPIV1Mock() .post('/api/v1/custompages', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) .reply(201, { slug, _id: id, body: doc.content, ...doc.data, lastUpdatedHash: hash }); @@ -224,7 +224,7 @@ describe('rdme custompages', () => { const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs-html/${slug}.html`))); const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/new-docs-html/${slug}.html`))); - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get(`/api/v1/custompages/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -234,7 +234,7 @@ describe('rdme custompages', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMock = getAPIMock() + const postMock = getAPIV1Mock() .post('/api/v1/custompages', { slug, html: doc.content, htmlmode: true, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) .reply(201, { slug, _id: id, html: doc.content, htmlmode: true, ...doc.data, lastUpdatedHash: hash }); @@ -251,7 +251,7 @@ describe('rdme custompages', () => { const slug = 'new-doc'; const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get(`/api/v1/custompages/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -286,7 +286,7 @@ describe('rdme custompages', () => { const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/${folder}/${slug}.md`))); - const getMocks = getAPIMock() + const getMocks = getAPIV1Mock() .get(`/api/v1/custompages/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -296,7 +296,7 @@ describe('rdme custompages', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMocks = getAPIMock() + const postMocks = getAPIV1Mock() .post('/api/v1/custompages', { slug, body: doc.content, htmlmode: false, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) .reply(400, errorObject); @@ -322,7 +322,7 @@ describe('rdme custompages', () => { const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/slug-docs/${slug}.md`))); const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/slug-docs/${slug}.md`))); - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get(`/api/v1/custompages/${doc.data.slug}`) .basicAuth({ user: key }) .reply(404, { @@ -332,7 +332,7 @@ describe('rdme custompages', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMock = getAPIMock() + const postMock = getAPIV1Mock() .post('/api/v1/custompages', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) .reply(201, { slug: doc.data.slug, _id: id, body: doc.content, ...doc.data, lastUpdatedHash: hash }); diff --git a/__tests__/commands/custompages/single.test.ts b/__tests__/commands/custompages/single.test.ts index 9bc691fc5..2e452963b 100644 --- a/__tests__/commands/custompages/single.test.ts +++ b/__tests__/commands/custompages/single.test.ts @@ -8,7 +8,7 @@ import { describe, beforeAll, afterAll, beforeEach, it, expect } from 'vitest'; import Command from '../../../src/commands/custompages.js'; import APIError from '../../../src/lib/apiError.js'; -import getAPIMock from '../../helpers/get-api-mock.js'; +import { getAPIV1Mock } from '../../helpers/get-api-mock.js'; import hashFileContents from '../../helpers/hash-file-contents.js'; import { runCommand } from '../../helpers/setup-oclif-config.js'; @@ -49,7 +49,7 @@ describe('rdme custompages (single)', () => { const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get(`/api/v1/custompages/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -59,7 +59,7 @@ describe('rdme custompages (single)', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMock = getAPIMock() + const postMock = getAPIV1Mock() .post('/api/v1/custompages', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) .reply(201, { slug, _id: id, body: doc.content, ...doc.data }); @@ -78,7 +78,7 @@ describe('rdme custompages (single)', () => { const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs-html/${slug}.html`))); const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/new-docs-html/${slug}.html`))); - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get(`/api/v1/custompages/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -88,7 +88,7 @@ describe('rdme custompages (single)', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMock = getAPIMock() + const postMock = getAPIV1Mock() .post('/api/v1/custompages', { slug, html: doc.content, htmlmode: true, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) .reply(201, { slug, _id: id, html: doc.content, htmlmode: true, ...doc.data }); @@ -105,7 +105,7 @@ describe('rdme custompages (single)', () => { const slug = 'new-doc'; const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get(`/api/v1/custompages/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -142,7 +142,10 @@ describe('rdme custompages (single)', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }; - const getMock = getAPIMock().get(`/api/v1/custompages/${slug}`).basicAuth({ user: key }).reply(500, errorObject); + const getMock = getAPIV1Mock() + .get(`/api/v1/custompages/${slug}`) + .basicAuth({ user: key }) + .reply(500, errorObject); const filePath = `./__tests__/${fixturesBaseDir}/failure-docs/${slug}.md`; @@ -164,7 +167,7 @@ describe('rdme custompages (single)', () => { const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/slug-docs/${slug}.md`))); const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/slug-docs/${slug}.md`))); - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get(`/api/v1/custompages/${doc.data.slug}`) .basicAuth({ user: key }) .reply(404, { @@ -174,7 +177,7 @@ describe('rdme custompages (single)', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMock = getAPIMock() + const postMock = getAPIV1Mock() .post('/api/v1/custompages', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) .reply(201, { slug: doc.data.slug, _id: id, body: doc.content, ...doc.data, lastUpdatedHash: hash }); @@ -201,12 +204,12 @@ describe('rdme custompages (single)', () => { }); it('should fetch custom page and merge with what is returned', () => { - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get('/api/v1/custompages/simple-doc') .basicAuth({ user: key }) .reply(200, { slug: simpleDoc.slug, lastUpdatedHash: 'anOldHash' }); - const updateMock = getAPIMock() + const updateMock = getAPIV1Mock() .put('/api/v1/custompages/simple-doc', { body: simpleDoc.doc.content, htmlmode: false, @@ -233,7 +236,7 @@ describe('rdme custompages (single)', () => { it('should return custom page update info for dry run', () => { expect.assertions(1); - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get('/api/v1/custompages/simple-doc') .basicAuth({ user: key }) .reply(200, { slug: simpleDoc.slug, lastUpdatedHash: 'anOldHash' }); @@ -258,7 +261,7 @@ describe('rdme custompages (single)', () => { it('should not send requests for custompages that have not changed', () => { expect.assertions(1); - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get('/api/v1/custompages/simple-doc') .basicAuth({ user: key }) .reply(200, { slug: simpleDoc.slug, lastUpdatedHash: simpleDoc.hash }); @@ -271,7 +274,7 @@ describe('rdme custompages (single)', () => { }); it('should adjust "no changes" message if in dry run', () => { - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get('/api/v1/custompages/simple-doc') .basicAuth({ user: key }) .reply(200, { slug: simpleDoc.slug, lastUpdatedHash: simpleDoc.hash }); diff --git a/__tests__/commands/docs/index.test.ts b/__tests__/commands/docs/index.test.ts index 81a870e0a..d5a753d3a 100644 --- a/__tests__/commands/docs/index.test.ts +++ b/__tests__/commands/docs/index.test.ts @@ -12,7 +12,7 @@ import { describe, beforeAll, afterAll, beforeEach, afterEach, it, expect, vi, t import Command from '../../../src/commands/docs/index.js'; import APIError from '../../../src/lib/apiError.js'; -import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock.js'; +import { getAPIV1Mock, getAPIV1MockWithVersionHeader } from '../../helpers/get-api-mock.js'; import { after, before } from '../../helpers/get-gha-setup.js'; import hashFileContents from '../../helpers/hash-file-contents.js'; import { after as afterGHAEnv, before as beforeGHAEnv } from '../../helpers/setup-gha-env.js'; @@ -40,7 +40,10 @@ describe('rdme docs', () => { }); it('should error if the argument is not a folder', async () => { - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); + const versionMock = getAPIV1Mock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); await expect(run(['--key', key, '--version', '1.0.0', 'not-a-folder'])).rejects.toStrictEqual( new Error("Oops! We couldn't locate a file or directory at the path you provided."), @@ -50,7 +53,10 @@ describe('rdme docs', () => { }); it('should error if the folder contains no markdown files', async () => { - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); + const versionMock = getAPIV1Mock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); await expect(run(['--key', key, '--version', '1.0.0', '.github/workflows'])).rejects.toStrictEqual( new Error( @@ -84,7 +90,7 @@ describe('rdme docs', () => { it('should fetch doc and merge with what is returned', () => { expect.assertions(1); - const getMocks = getAPIMockWithVersionHeader(version) + const getMocks = getAPIV1MockWithVersionHeader(version) .get('/api/v1/docs/simple-doc') .basicAuth({ user: key }) .reply(200, { category, slug: simpleDoc.slug, lastUpdatedHash: 'anOldHash' }) @@ -92,7 +98,7 @@ describe('rdme docs', () => { .basicAuth({ user: key }) .reply(200, { category, slug: anotherDoc.slug, lastUpdatedHash: 'anOldHash' }); - const updateMocks = getAPIMockWithVersionHeader(version) + const updateMocks = getAPIV1MockWithVersionHeader(version) .put('/api/v1/docs/simple-doc', { body: simpleDoc.doc.content, lastUpdatedHash: simpleDoc.hash, @@ -112,7 +118,7 @@ describe('rdme docs', () => { .basicAuth({ user: key }) .reply(200, { category, slug: anotherDoc.slug, body: anotherDoc.doc.content }); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -138,7 +144,7 @@ describe('rdme docs', () => { it('should return doc update info for dry run', () => { expect.assertions(1); - const getMocks = getAPIMockWithVersionHeader(version) + const getMocks = getAPIV1MockWithVersionHeader(version) .get('/api/v1/docs/simple-doc') .basicAuth({ user: key }) .reply(200, { category, slug: simpleDoc.slug, lastUpdatedHash: 'anOldHash' }) @@ -146,7 +152,7 @@ describe('rdme docs', () => { .basicAuth({ user: key }) .reply(200, { category, slug: anotherDoc.slug, lastUpdatedHash: 'anOldHash' }); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -175,7 +181,7 @@ describe('rdme docs', () => { it('should not send requests for docs that have not changed', () => { expect.assertions(1); - const getMocks = getAPIMockWithVersionHeader(version) + const getMocks = getAPIV1MockWithVersionHeader(version) .get('/api/v1/docs/simple-doc') .basicAuth({ user: key }) .reply(200, { category, slug: simpleDoc.slug, lastUpdatedHash: simpleDoc.hash }) @@ -183,7 +189,7 @@ describe('rdme docs', () => { .basicAuth({ user: key }) .reply(200, { category, slug: anotherDoc.slug, lastUpdatedHash: anotherDoc.hash }); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -206,7 +212,7 @@ describe('rdme docs', () => { it('should adjust "no changes" message if in dry run', () => { expect.assertions(1); - const getMocks = getAPIMockWithVersionHeader(version) + const getMocks = getAPIV1MockWithVersionHeader(version) .get('/api/v1/docs/simple-doc') .basicAuth({ user: key }) .reply(200, { category, slug: simpleDoc.slug, lastUpdatedHash: simpleDoc.hash }) @@ -214,7 +220,7 @@ describe('rdme docs', () => { .basicAuth({ user: key }) .reply(200, { category, slug: anotherDoc.slug, lastUpdatedHash: anotherDoc.hash }); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -242,7 +248,7 @@ describe('rdme docs', () => { const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); - const getMock = getAPIMockWithVersionHeader(version) + const getMock = getAPIV1MockWithVersionHeader(version) .get(`/api/v1/docs/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -252,12 +258,12 @@ describe('rdme docs', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMock = getAPIMockWithVersionHeader(version) + const postMock = getAPIV1MockWithVersionHeader(version) .post('/api/v1/docs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) .reply(201, { slug, _id: id, body: doc.content, ...doc.data, lastUpdatedHash: hash }); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -275,7 +281,7 @@ describe('rdme docs', () => { const slug = 'new-doc'; const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); - const getMock = getAPIMockWithVersionHeader(version) + const getMock = getAPIV1MockWithVersionHeader(version) .get(`/api/v1/docs/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -285,7 +291,7 @@ describe('rdme docs', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -315,7 +321,7 @@ describe('rdme docs', () => { const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/${folder}/${slug}.md`))); - const getMocks = getAPIMockWithVersionHeader(version) + const getMocks = getAPIV1MockWithVersionHeader(version) .get(`/api/v1/docs/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -325,12 +331,12 @@ describe('rdme docs', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMocks = getAPIMockWithVersionHeader(version) + const postMocks = getAPIV1MockWithVersionHeader(version) .post('/api/v1/docs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) .reply(400, errorObject); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -359,7 +365,7 @@ describe('rdme docs', () => { const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/slug-docs/${slug}.md`))); const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/slug-docs/${slug}.md`))); - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get(`/api/v1/docs/${doc.data.slug}`) .basicAuth({ user: key }) .reply(404, { @@ -369,12 +375,12 @@ describe('rdme docs', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMock = getAPIMock() + const postMock = getAPIV1Mock() .post('/api/v1/docs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) .reply(201, { slug: doc.data.slug, _id: id, body: doc.content, ...doc.data, lastUpdatedHash: hash }); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -420,12 +426,12 @@ describe('rdme docs', () => { const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); - const versionsMock = getAPIMock() + const versionsMock = getAPIV1Mock() .get('/api/v1/version') .basicAuth({ user: key }) .reply(200, [{ version }, { version: altVersion }]); - const getMock = getAPIMockWithVersionHeader(altVersion) + const getMock = getAPIV1MockWithVersionHeader(altVersion) .get(`/api/v1/docs/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -435,7 +441,7 @@ describe('rdme docs', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMock = getAPIMockWithVersionHeader(altVersion) + const postMock = getAPIV1MockWithVersionHeader(altVersion) .post('/api/v1/docs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) .reply(201, { _id: id, slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }); @@ -464,7 +470,7 @@ describe('rdme docs', () => { const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); - const getMock = getAPIMockWithVersionHeader(version) + const getMock = getAPIV1MockWithVersionHeader(version) .get(`/api/v1/docs/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -474,12 +480,12 @@ describe('rdme docs', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMock = getAPIMockWithVersionHeader(version) + const postMock = getAPIV1MockWithVersionHeader(version) .post('/api/v1/docs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) .reply(201, { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -506,7 +512,7 @@ describe('rdme docs', () => { const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); - const getMock = getAPIMockWithVersionHeader(version) + const getMock = getAPIV1MockWithVersionHeader(version) .get(`/api/v1/docs/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -516,12 +522,12 @@ describe('rdme docs', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMock = getAPIMockWithVersionHeader(version) + const postMock = getAPIV1MockWithVersionHeader(version) .post('/api/v1/docs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) .reply(201, { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -546,7 +552,7 @@ describe('rdme docs', () => { const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); - const getMock = getAPIMockWithVersionHeader(version) + const getMock = getAPIV1MockWithVersionHeader(version) .get(`/api/v1/docs/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -556,12 +562,12 @@ describe('rdme docs', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMock = getAPIMockWithVersionHeader(version) + const postMock = getAPIV1MockWithVersionHeader(version) .post('/api/v1/docs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) .reply(201, { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -595,7 +601,7 @@ describe('rdme docs', () => { const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); - const getMock = getAPIMockWithVersionHeader(version) + const getMock = getAPIV1MockWithVersionHeader(version) .get(`/api/v1/docs/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -605,7 +611,7 @@ describe('rdme docs', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMock = getAPIMock({ + const postMock = getAPIV1Mock({ 'x-rdme-ci': 'GitHub Actions (test)', 'x-readme-source': 'cli-gh', 'x-readme-source-url': @@ -616,7 +622,7 @@ describe('rdme docs', () => { .basicAuth({ user: key }) .reply(201, { slug, _id: id, body: doc.content, ...doc.data, lastUpdatedHash: hash }); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -647,7 +653,7 @@ describe('rdme docs', () => { expect.assertions(1); - const getMocks = getAPIMockWithVersionHeader(version) + const getMocks = getAPIV1MockWithVersionHeader(version) .get('/api/v1/docs/simple-doc') .basicAuth({ user: key }) .reply(200, { category, slug: simpleDoc.slug, lastUpdatedHash: 'anOldHash' }) @@ -655,7 +661,7 @@ describe('rdme docs', () => { .basicAuth({ user: key }) .reply(200, { category, slug: anotherDoc.slug, lastUpdatedHash: 'anOldHash' }); - const firstUpdateMock = getAPIMock({ + const firstUpdateMock = getAPIV1Mock({ 'x-rdme-ci': 'GitHub Actions (test)', 'x-readme-source': 'cli-gh', 'x-readme-source-url': @@ -674,7 +680,7 @@ describe('rdme docs', () => { body: simpleDoc.doc.content, }); - const secondUpdateMock = getAPIMock({ + const secondUpdateMock = getAPIV1Mock({ 'x-rdme-ci': 'GitHub Actions (test)', 'x-readme-source': 'cli-gh', 'x-readme-source-url': @@ -689,7 +695,7 @@ describe('rdme docs', () => { .basicAuth({ user: key }) .reply(200, { category, slug: anotherDoc.slug, body: anotherDoc.doc.content }); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); diff --git a/__tests__/commands/docs/multiple.test.ts b/__tests__/commands/docs/multiple.test.ts index 439d04bb6..65beca3d8 100644 --- a/__tests__/commands/docs/multiple.test.ts +++ b/__tests__/commands/docs/multiple.test.ts @@ -6,7 +6,7 @@ import nock from 'nock'; import { describe, beforeAll, afterAll, it, expect } from 'vitest'; import Command from '../../../src/commands/docs/index.js'; -import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock.js'; +import { getAPIV1Mock, getAPIV1MockWithVersionHeader } from '../../helpers/get-api-mock.js'; import hashFileContents from '../../helpers/hash-file-contents.js'; import { runCommand } from '../../helpers/setup-oclif-config.js'; @@ -36,7 +36,7 @@ describe('rdme docs (multiple)', () => { const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/${dir}/${slug}.md`))); return [ - getAPIMockWithVersionHeader(version) + getAPIV1MockWithVersionHeader(version) .get(`/api/v1/docs/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -45,7 +45,7 @@ describe('rdme docs (multiple)', () => { suggestion: '...a suggestion to resolve the issue...', help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }), - getAPIMockWithVersionHeader(version) + getAPIV1MockWithVersionHeader(version) .post('/api/v1/docs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) // eslint-disable-next-line no-plusplus @@ -53,7 +53,10 @@ describe('rdme docs (multiple)', () => { ]; }); - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); + const versionMock = getAPIV1Mock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); const promise = run([`./__tests__/${fixturesBaseDir}/${dir}`, '--key', key, '--version', version]); @@ -80,7 +83,7 @@ describe('rdme docs (multiple)', () => { const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/${dir}/${slug}.md`))); return [ - getAPIMockWithVersionHeader(version) + getAPIV1MockWithVersionHeader(version) .get(`/api/v1/docs/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -89,7 +92,7 @@ describe('rdme docs (multiple)', () => { suggestion: '...a suggestion to resolve the issue...', help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }), - getAPIMockWithVersionHeader(version) + getAPIV1MockWithVersionHeader(version) .post('/api/v1/docs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) // eslint-disable-next-line no-plusplus @@ -97,7 +100,10 @@ describe('rdme docs (multiple)', () => { ]; }); - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); + const versionMock = getAPIV1Mock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); const promise = run([`./__tests__/${fixturesBaseDir}/${dir}`, '--key', key, '--version', version]); @@ -124,7 +130,7 @@ describe('rdme docs (multiple)', () => { const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/${dir}/${slug}.md`))); return [ - getAPIMockWithVersionHeader(version) + getAPIV1MockWithVersionHeader(version) .get(`/api/v1/docs/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -133,7 +139,7 @@ describe('rdme docs (multiple)', () => { suggestion: '...a suggestion to resolve the issue...', help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }), - getAPIMockWithVersionHeader(version) + getAPIV1MockWithVersionHeader(version) .post('/api/v1/docs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) // eslint-disable-next-line no-plusplus @@ -141,7 +147,10 @@ describe('rdme docs (multiple)', () => { ]; }); - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); + const versionMock = getAPIV1Mock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); const promise = run([`./__tests__/${fixturesBaseDir}/${dir}`, '--key', key, '--version', version]); @@ -158,7 +167,10 @@ describe('rdme docs (multiple)', () => { it('should return an error message when it encounters a cycle', async () => { const dir = 'multiple-docs-cycle'; - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); + const versionMock = getAPIV1Mock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); const promise = run([`./__tests__/${fixturesBaseDir}/${dir}`, '--key', key, '--version', version]); diff --git a/__tests__/commands/docs/prune.test.ts b/__tests__/commands/docs/prune.test.ts index 72225efee..71ec66b13 100644 --- a/__tests__/commands/docs/prune.test.ts +++ b/__tests__/commands/docs/prune.test.ts @@ -4,7 +4,7 @@ import prompts from 'prompts'; import { describe, beforeAll, afterAll, it, expect } from 'vitest'; import Command from '../../../src/commands/docs/prune.js'; -import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock.js'; +import { getAPIV1Mock, getAPIV1MockWithVersionHeader } from '../../helpers/get-api-mock.js'; import { runCommand } from '../../helpers/setup-oclif-config.js'; const fixturesBaseDir = '__fixtures__/docs'; @@ -28,7 +28,10 @@ describe('rdme docs:prune', () => { }); it('should error if the argument is not a folder', async () => { - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); + const versionMock = getAPIV1Mock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); await expect(run(['--key', key, '--version', version, 'not-a-folder'])).rejects.toStrictEqual( new Error("ENOENT: no such file or directory, scandir 'not-a-folder'"), @@ -40,7 +43,10 @@ describe('rdme docs:prune', () => { it('should do nothing if the user aborted', async () => { prompts.inject([false]); - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); + const versionMock = getAPIV1Mock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); await expect(run([folder, '--key', key, '--version', version])).rejects.toStrictEqual( new Error('Aborting, no changes were made.'), @@ -50,9 +56,12 @@ describe('rdme docs:prune', () => { }); it('should not ask for user confirmation if `confirm` is set to true', async () => { - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); + const versionMock = getAPIV1Mock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); - const apiMocks = getAPIMockWithVersionHeader(version) + const apiMocks = getAPIV1MockWithVersionHeader(version) .get('/api/v1/categories?perPage=20&page=1') .basicAuth({ user: key }) .reply(200, [{ slug: 'category1', type: 'guide' }], { 'x-total-count': '1' }) @@ -74,9 +83,12 @@ describe('rdme docs:prune', () => { it('should delete doc if file is missing', async () => { prompts.inject([true]); - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); + const versionMock = getAPIV1Mock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); - const apiMocks = getAPIMockWithVersionHeader(version) + const apiMocks = getAPIV1MockWithVersionHeader(version) .get('/api/v1/categories?perPage=20&page=1') .basicAuth({ user: key }) .reply(200, [{ slug: 'category1', type: 'guide' }], { 'x-total-count': '1' }) @@ -98,9 +110,12 @@ describe('rdme docs:prune', () => { it('should delete doc and its child if they are missing', async () => { prompts.inject([true]); - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); + const versionMock = getAPIV1Mock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); - const apiMocks = getAPIMockWithVersionHeader(version) + const apiMocks = getAPIV1MockWithVersionHeader(version) .get('/api/v1/categories?perPage=20&page=1') .basicAuth({ user: key }) .reply(200, [{ slug: 'category1', type: 'guide' }], { 'x-total-count': '1' }) @@ -128,8 +143,11 @@ describe('rdme docs:prune', () => { it('should return doc delete info for dry run', async () => { prompts.inject([true]); - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); - const apiMocks = getAPIMockWithVersionHeader(version) + const versionMock = getAPIV1Mock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); + const apiMocks = getAPIV1MockWithVersionHeader(version) .get('/api/v1/categories?perPage=20&page=1') .basicAuth({ user: key }) .reply(200, [{ slug: 'category1', type: 'guide' }], { 'x-total-count': '1' }) diff --git a/__tests__/commands/docs/single.test.ts b/__tests__/commands/docs/single.test.ts index 0e8158c7a..9aeea4cd8 100644 --- a/__tests__/commands/docs/single.test.ts +++ b/__tests__/commands/docs/single.test.ts @@ -8,7 +8,7 @@ import { describe, beforeAll, afterAll, beforeEach, afterEach, it, expect } from import Command from '../../../src/commands/docs/index.js'; import APIError from '../../../src/lib/apiError.js'; -import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock.js'; +import { getAPIV1Mock, getAPIV1MockWithVersionHeader } from '../../helpers/get-api-mock.js'; import hashFileContents from '../../helpers/hash-file-contents.js'; import { after as afterGHAEnv, before as beforeGHAEnv } from '../../helpers/setup-gha-env.js'; import { runCommand } from '../../helpers/setup-oclif-config.js'; @@ -35,7 +35,10 @@ describe('rdme docs (single)', () => { }); it('should error if the argument is not a Markdown file', async () => { - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); + const versionMock = getAPIV1Mock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); await expect(run(['--key', key, '--version', version, 'not-a-markdown-file'])).rejects.toStrictEqual( new Error("Oops! We couldn't locate a file or directory at the path you provided."), @@ -45,7 +48,10 @@ describe('rdme docs (single)', () => { }); it('should support .markdown files but error if file path cannot be found', async () => { - const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version }); + const versionMock = getAPIV1Mock() + .get(`/api/v1/version/${version}`) + .basicAuth({ user: key }) + .reply(200, { version }); await expect(run(['--key', key, '--version', version, 'non-existent-file.markdown'])).rejects.toStrictEqual( new Error("Oops! We couldn't locate a file or directory at the path you provided."), ); @@ -59,7 +65,7 @@ describe('rdme docs (single)', () => { const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); - const getMock = getAPIMockWithVersionHeader(version) + const getMock = getAPIV1MockWithVersionHeader(version) .get(`/api/v1/docs/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -69,12 +75,12 @@ describe('rdme docs (single)', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMock = getAPIMockWithVersionHeader(version) + const postMock = getAPIV1MockWithVersionHeader(version) .post('/api/v1/docs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) .reply(201, { slug, _id: id, body: doc.content, ...doc.data, lastUpdatedHash: hash }); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -94,7 +100,7 @@ describe('rdme docs (single)', () => { const slug = 'new-doc'; const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); - const getMock = getAPIMockWithVersionHeader(version) + const getMock = getAPIV1MockWithVersionHeader(version) .get(`/api/v1/docs/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -104,7 +110,7 @@ describe('rdme docs (single)', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -122,7 +128,7 @@ describe('rdme docs (single)', () => { }); it('should skip doc if it does not contain any front matter attributes', async () => { - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -146,12 +152,12 @@ describe('rdme docs (single)', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }; - const getMock = getAPIMockWithVersionHeader(version) + const getMock = getAPIV1MockWithVersionHeader(version) .get(`/api/v1/docs/${slug}`) .basicAuth({ user: key }) .reply(500, errorObject); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -179,7 +185,7 @@ describe('rdme docs (single)', () => { const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/slug-docs/${slug}.md`))); const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/slug-docs/${slug}.md`))); - const getMock = getAPIMock() + const getMock = getAPIV1Mock() .get(`/api/v1/docs/${doc.data.slug}`) .basicAuth({ user: key }) .reply(404, { @@ -189,12 +195,12 @@ describe('rdme docs (single)', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMock = getAPIMock() + const postMock = getAPIV1Mock() .post('/api/v1/docs', { slug, body: doc.content, ...doc.data, lastUpdatedHash: hash }) .basicAuth({ user: key }) .reply(201, { slug: doc.data.slug, _id: id, body: doc.content, ...doc.data, lastUpdatedHash: hash }); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -224,12 +230,12 @@ describe('rdme docs (single)', () => { }); it('should fetch doc and merge with what is returned', async () => { - const getMock = getAPIMockWithVersionHeader(version) + const getMock = getAPIV1MockWithVersionHeader(version) .get('/api/v1/docs/simple-doc') .basicAuth({ user: key }) .reply(200, { category, slug: simpleDoc.slug, lastUpdatedHash: 'anOldHash' }); - const updateMock = getAPIMockWithVersionHeader(version) + const updateMock = getAPIV1MockWithVersionHeader(version) .put('/api/v1/docs/simple-doc', { body: simpleDoc.doc.content, lastUpdatedHash: simpleDoc.hash, @@ -242,7 +248,7 @@ describe('rdme docs (single)', () => { body: simpleDoc.doc.content, }); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -259,12 +265,12 @@ describe('rdme docs (single)', () => { }); it('should return doc update info for dry run', async () => { - const getMock = getAPIMockWithVersionHeader(version) + const getMock = getAPIV1MockWithVersionHeader(version) .get('/api/v1/docs/simple-doc') .basicAuth({ user: key }) .reply(200, { category, slug: simpleDoc.slug, lastUpdatedHash: 'anOldHash' }); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -291,12 +297,12 @@ describe('rdme docs (single)', () => { }); it('should not send requests for docs that have not changed', async () => { - const getMock = getAPIMockWithVersionHeader(version) + const getMock = getAPIV1MockWithVersionHeader(version) .get('/api/v1/docs/simple-doc') .basicAuth({ user: key }) .reply(200, { category, slug: simpleDoc.slug, lastUpdatedHash: simpleDoc.hash }); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -310,12 +316,12 @@ describe('rdme docs (single)', () => { }); it('should adjust "no changes" message if in dry run', async () => { - const getMock = getAPIMockWithVersionHeader(version) + const getMock = getAPIV1MockWithVersionHeader(version) .get('/api/v1/docs/simple-doc') .basicAuth({ user: key }) .reply(200, { category, slug: simpleDoc.slug, lastUpdatedHash: simpleDoc.hash }); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -349,7 +355,7 @@ describe('rdme docs (single)', () => { const doc = frontMatter(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); const hash = hashFileContents(fs.readFileSync(path.join(fullFixturesDir, `/new-docs/${slug}.md`))); - const getMock = getAPIMockWithVersionHeader(version) + const getMock = getAPIV1MockWithVersionHeader(version) .get(`/api/v1/docs/${slug}`) .basicAuth({ user: key }) .reply(404, { @@ -359,7 +365,7 @@ describe('rdme docs (single)', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }); - const postMock = getAPIMock({ + const postMock = getAPIV1Mock({ 'x-rdme-ci': 'GitHub Actions (test)', 'x-readme-source': 'cli-gh', 'x-readme-source-url': @@ -370,7 +376,7 @@ describe('rdme docs (single)', () => { .basicAuth({ user: key }) .reply(201, { slug, _id: id, body: doc.content, ...doc.data, lastUpdatedHash: hash }); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); @@ -394,12 +400,12 @@ describe('rdme docs (single)', () => { hash: hashFileContents(fileContents), }; - const getMock = getAPIMockWithVersionHeader(version) + const getMock = getAPIV1MockWithVersionHeader(version) .get('/api/v1/docs/simple-doc') .basicAuth({ user: key }) .reply(200, { category, slug: simpleDoc.slug, lastUpdatedHash: 'anOldHash' }); - const updateMock = getAPIMock({ + const updateMock = getAPIV1Mock({ 'x-rdme-ci': 'GitHub Actions (test)', 'x-readme-source': 'cli-gh', 'x-readme-source-url': @@ -418,7 +424,7 @@ describe('rdme docs (single)', () => { body: simpleDoc.doc.content, }); - const versionMock = getAPIMock() + const versionMock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }); diff --git a/__tests__/commands/login.test.ts b/__tests__/commands/login.test.ts index 45d1fb1f8..501be9db1 100644 --- a/__tests__/commands/login.test.ts +++ b/__tests__/commands/login.test.ts @@ -5,7 +5,7 @@ import { describe, beforeAll, afterAll, afterEach, it, expect } from 'vitest'; import Command from '../../src/commands/login.js'; import APIError from '../../src/lib/apiError.js'; import configStore from '../../src/lib/configstore.js'; -import getAPIMock from '../helpers/get-api-mock.js'; +import { getAPIV1Mock } from '../helpers/get-api-mock.js'; import { runCommand } from '../helpers/setup-oclif-config.js'; const apiKey = 'abcdefg'; @@ -39,7 +39,7 @@ describe('rdme login', () => { it('should post to /login on the API', async () => { prompts.inject([email, password, project]); - const mock = getAPIMock().post('/api/v1/login', { email, password, project }).reply(200, { apiKey }); + const mock = getAPIV1Mock().post('/api/v1/login', { email, password, project }).reply(200, { apiKey }); await expect(run()).resolves.toBe('Successfully logged in as user@example.com to the subdomain project.'); @@ -53,7 +53,7 @@ describe('rdme login', () => { it('should post to /login on the API if passing in project via opt', async () => { prompts.inject([email, password]); - const mock = getAPIMock().post('/api/v1/login', { email, password, project }).reply(200, { apiKey }); + const mock = getAPIV1Mock().post('/api/v1/login', { email, password, project }).reply(200, { apiKey }); await expect(run(['--project', project])).resolves.toBe( 'Successfully logged in as user@example.com to the subdomain project.', @@ -67,7 +67,7 @@ describe('rdme login', () => { }); it('should bypass prompts and post to /login on the API if passing in every opt', async () => { - const mock = getAPIMock().post('/api/v1/login', { email, password, project, token }).reply(200, { apiKey }); + const mock = getAPIV1Mock().post('/api/v1/login', { email, password, project, token }).reply(200, { apiKey }); await expect(run(['--email', email, '--password', password, '--project', project, '--otp', token])).resolves.toBe( 'Successfully logged in as user@example.com to the subdomain project.', @@ -81,7 +81,7 @@ describe('rdme login', () => { }); it('should bypass prompts and post to /login on the API if passing in every opt (no 2FA)', async () => { - const mock = getAPIMock().post('/api/v1/login', { email, password, project }).reply(200, { apiKey }); + const mock = getAPIV1Mock().post('/api/v1/login', { email, password, project }).reply(200, { apiKey }); await expect(run(['--email', email, '--password', password, '--project', project])).resolves.toBe( 'Successfully logged in as user@example.com to the subdomain project.', @@ -103,7 +103,7 @@ describe('rdme login', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }; - const mock = getAPIMock().post('/api/v1/login', { email, password, project }).reply(401, errorResponse); + const mock = getAPIV1Mock().post('/api/v1/login', { email, password, project }).reply(401, errorResponse); await expect(run()).rejects.toStrictEqual(new APIError(errorResponse)); mock.done(); @@ -118,7 +118,7 @@ describe('rdme login', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }; - const mock = getAPIMock() + const mock = getAPIV1Mock() .post('/api/v1/login', { email, password, project }) .reply(401, errorResponse) .post('/api/v1/login', { email, password, project, token }) @@ -143,7 +143,7 @@ describe('rdme login', () => { help: 'If you need help, email support@readme.io', }; - const mock = getAPIMock() + const mock = getAPIV1Mock() .post('/api/v1/login', { email, password, project: projectThatIsNotYours }) .reply(404, errorResponse); diff --git a/__tests__/commands/open.test.ts b/__tests__/commands/open.test.ts index fbb4cf9c3..e26609b9d 100644 --- a/__tests__/commands/open.test.ts +++ b/__tests__/commands/open.test.ts @@ -6,7 +6,7 @@ import { describe, afterEach, beforeAll, it, expect } from 'vitest'; import pkg from '../../package.json'; import Command from '../../src/commands/open.js'; import configStore from '../../src/lib/configstore.js'; -import getAPIMock from '../helpers/get-api-mock.js'; +import { getAPIV1Mock } from '../helpers/get-api-mock.js'; import { runCommand } from '../helpers/setup-oclif-config.js'; const mockArg = ['--mock']; @@ -53,7 +53,7 @@ describe('rdme open', () => { version, }; - const mockRequest = getAPIMock() + const mockRequest = getAPIV1Mock() .get('/api/v1/version') .basicAuth({ user: key }) .reply(200, [versionPayload, { version: '1.0.1' }]); diff --git a/__tests__/commands/openapi/index.test.ts b/__tests__/commands/openapi/index.test.ts index 0173ff07e..96f8778cc 100644 --- a/__tests__/commands/openapi/index.test.ts +++ b/__tests__/commands/openapi/index.test.ts @@ -11,7 +11,7 @@ import Command from '../../../src/commands/openapi/index.js'; import APIError from '../../../src/lib/apiError.js'; import config from '../../../src/lib/config.js'; import petstoreWeird from '../../__fixtures__/petstore-simple-weird-version.json' with { type: 'json' }; -import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock.js'; +import { getAPIV1Mock, getAPIV1MockWithVersionHeader } from '../../helpers/get-api-mock.js'; import { after, before } from '../../helpers/get-gha-setup.js'; import { after as afterGHAEnv, before as beforeGHAEnv } from '../../helpers/setup-gha-env.js'; import { runCommand } from '../../helpers/setup-oclif-config.js'; @@ -88,7 +88,7 @@ describe('rdme openapi', () => { ])('should support uploading a %s definition (format: %s)', async (_, format, specVersion, type) => { const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: specVersion } }) .get('/api/v1/api-specification') @@ -98,7 +98,7 @@ describe('rdme openapi', () => { .basicAuth({ user: key }) .reply(200, { version: '1.0.0' }); - const postMock = getAPIMockWithVersionHeader(version) + const postMock = getAPIV1MockWithVersionHeader(version) .post('/api/v1/api-specification', { registryUUID }) .basicAuth({ user: key }) .reply(201, { _id: 1 }, { location: exampleRefLocation }); @@ -122,14 +122,14 @@ describe('rdme openapi', () => { prompts.inject(['create']); const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }) .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const mockWithHeader = getAPIMockWithVersionHeader(version) + const mockWithHeader = getAPIV1MockWithVersionHeader(version) .get('/api/v1/api-specification') .basicAuth({ user: key }) .reply(200, [{ _id: 'spec1', title: 'spec1_title' }]) @@ -148,14 +148,14 @@ describe('rdme openapi', () => { it('should create a new spec via `--create` flag', async () => { const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }) .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const postMock = getAPIMockWithVersionHeader(version) + const postMock = getAPIV1MockWithVersionHeader(version) .post('/api/v1/api-specification', { registryUUID }) .basicAuth({ user: key }) .reply(201, { _id: 1 }, { location: exampleRefLocation }); @@ -171,14 +171,14 @@ describe('rdme openapi', () => { it('should create a new spec via `--create` flag and ignore `--id`', async () => { const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get('/api/v1/version') .basicAuth({ user: key }) .reply(200, [{ version }]) .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const postMock = getAPIMockWithVersionHeader(version) + const postMock = getAPIV1MockWithVersionHeader(version) .post('/api/v1/api-specification', { registryUUID }) .basicAuth({ user: key }) .reply(201, { _id: 1 }, { location: exampleRefLocation }); @@ -201,7 +201,7 @@ describe('rdme openapi', () => { it('should bundle and upload the expected content', async () => { let requestBody; const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version: '1.0.0' }) @@ -216,7 +216,7 @@ describe('rdme openapi', () => { .basicAuth({ user: key }) .reply(200, []); - const postMock = getAPIMockWithVersionHeader(version) + const postMock = getAPIV1MockWithVersionHeader(version) .post('/api/v1/api-specification', { registryUUID }) .basicAuth({ user: key }) .reply(201, { _id: 1 }, { location: exampleRefLocation }); @@ -237,7 +237,7 @@ describe('rdme openapi', () => { let requestBody; const registryUUID = getRandomRegistryId(); const title = 'some alternative title'; - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version: '1.0.0' }) @@ -252,7 +252,7 @@ describe('rdme openapi', () => { .basicAuth({ user: key }) .reply(200, []); - const postMock = getAPIMockWithVersionHeader(version) + const postMock = getAPIV1MockWithVersionHeader(version) .post('/api/v1/api-specification', { registryUUID }) .basicAuth({ user: key }) .reply(201, { _id: 1 }, { location: exampleRefLocation }); @@ -274,7 +274,7 @@ describe('rdme openapi', () => { it('should upload the expected content and return raw output', async () => { let requestBody; const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version: '1.0.0' }) @@ -289,7 +289,7 @@ describe('rdme openapi', () => { .basicAuth({ user: key }) .reply(200, []); - const postMock = getAPIMockWithVersionHeader(version) + const postMock = getAPIV1MockWithVersionHeader(version) .post('/api/v1/api-specification', { registryUUID }) .basicAuth({ user: key }) .reply(201, { _id: 1 }, { location: exampleRefLocation }); @@ -314,11 +314,11 @@ describe('rdme openapi', () => { ])('should support updating a %s definition (format: %s)', async (_, format, specVersion, type) => { const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: specVersion } }); - const putMock = getAPIMockWithVersionHeader(version) + const putMock = getAPIV1MockWithVersionHeader(version) .put(`/api/v1/api-specification/${id}`, { registryUUID }) .basicAuth({ user: key }) .reply(201, { _id: 1 }, { location: exampleRefLocation }); @@ -337,11 +337,11 @@ describe('rdme openapi', () => { expect.assertions(4); const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const putMock = getAPIMockWithVersionHeader(version) + const putMock = getAPIV1MockWithVersionHeader(version) .put(`/api/v1/api-specification/${id}`, { registryUUID }) .basicAuth({ user: key }) .reply(201, { _id: 1 }, { location: exampleRefLocation }); @@ -365,14 +365,14 @@ describe('rdme openapi', () => { prompts.inject(['update', 'spec2']); const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }) .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const mockWithHeader = getAPIMockWithVersionHeader(version) + const mockWithHeader = getAPIV1MockWithVersionHeader(version) .get('/api/v1/api-specification') .basicAuth({ user: key }) .reply(200, [ @@ -394,7 +394,7 @@ describe('rdme openapi', () => { it('should discover and upload an API definition if none is provided', async () => { const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }) @@ -406,7 +406,7 @@ describe('rdme openapi', () => { .basicAuth({ user: key }) .reply(200, []); - const postMock = getAPIMockWithVersionHeader(version) + const postMock = getAPIV1MockWithVersionHeader(version) .post('/api/v1/api-specification', { registryUUID }) .basicAuth({ user: key }) .reply(201, { _id: 1 }, { location: exampleRefLocation }); @@ -430,7 +430,7 @@ describe('rdme openapi', () => { let requestBody; const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }) @@ -445,7 +445,7 @@ describe('rdme openapi', () => { .basicAuth({ user: key }) .reply(200, []); - const postMock = getAPIMockWithVersionHeader(version) + const postMock = getAPIV1MockWithVersionHeader(version) .post('/api/v1/api-specification', { registryUUID }) .basicAuth({ user: key }) .reply(201, { _id: 1 }, { location: exampleRefLocation }); @@ -476,14 +476,14 @@ describe('rdme openapi', () => { prompts.inject(['update', 'spec2']); const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }) .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const mockWithHeader = getAPIMockWithVersionHeader(version) + const mockWithHeader = getAPIV1MockWithVersionHeader(version) .get('/api/v1/api-specification') .basicAuth({ user: key }) .reply(200, [ @@ -504,7 +504,7 @@ describe('rdme openapi', () => { it('should return spec create info for dry run (with working directory)', async () => { const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }) @@ -542,14 +542,14 @@ describe('rdme openapi', () => { it("should update a spec file without prompts if providing `update` and it's the one spec available", async () => { const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }) .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const mockWithHeader = getAPIMockWithVersionHeader(version) + const mockWithHeader = getAPIV1MockWithVersionHeader(version) .get('/api/v1/api-specification') .basicAuth({ user: key }) .reply(200, [{ _id: 'spec1', title: 'spec1_title' }]) @@ -568,7 +568,7 @@ describe('rdme openapi', () => { it('should error if providing `update` and there are multiple specs available', async () => { const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }) @@ -595,7 +595,7 @@ describe('rdme openapi', () => { expect.assertions(5); const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }) .put('/api/v1/api-specification/spec1', { registryUUID }) @@ -626,7 +626,7 @@ describe('rdme openapi', () => { expect.assertions(2); let requestBody = ''; const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version: '1.0.0' }) @@ -638,7 +638,7 @@ describe('rdme openapi', () => { }) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const mockWithHeader = getAPIMockWithVersionHeader(version) + const mockWithHeader = getAPIV1MockWithVersionHeader(version) .get('/api/v1/api-specification') .basicAuth({ user: key }) .reply(200, []) @@ -662,7 +662,7 @@ describe('rdme openapi', () => { const specVersion = '1.2.3'; let requestBody = ''; const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${specVersion}`) .basicAuth({ user: key }) .reply(200, { version: specVersion }) @@ -674,7 +674,7 @@ describe('rdme openapi', () => { }) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const mockWithHeader = getAPIMockWithVersionHeader(specVersion) + const mockWithHeader = getAPIV1MockWithVersionHeader(specVersion) .get('/api/v1/api-specification') .basicAuth({ user: key }) .reply(200, []) @@ -708,7 +708,7 @@ describe('rdme openapi', () => { expect.assertions(2); let requestBody = ''; const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .post('/api/v1/api-registry', body => { requestBody = body.substring(body.indexOf('{'), body.lastIndexOf('}') + 1); requestBody = JSON.parse(requestBody); @@ -752,7 +752,7 @@ describe('rdme openapi', () => { ], }; - const mock = getAPIMock().get(`/api/v1/version/${invalidVersion}`).reply(404, errorObject); + const mock = getAPIV1Mock().get(`/api/v1/version/${invalidVersion}`).reply(404, errorObject); await expect( run([ @@ -773,14 +773,14 @@ describe('rdme openapi', () => { const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get('/api/v1/version') .basicAuth({ user: key }) .reply(200, [{ version: '1.0.0' }, { version: '1.0.1' }]) .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const mockWithHeader = getAPIMockWithVersionHeader(selectedVersion) + const mockWithHeader = getAPIV1MockWithVersionHeader(selectedVersion) .get('/api/v1/api-specification') .basicAuth({ user: key }) .reply(200, []) @@ -820,7 +820,7 @@ describe('rdme openapi', () => { ], }; - const mock = getAPIMock().get('/api/v1/version').reply(401, errorObject); + const mock = getAPIV1Mock().get('/api/v1/version').reply(401, errorObject); await expect( run([require.resolve('@readme/oas-examples/3.1/json/petstore.json'), '--key', 'key']), @@ -857,14 +857,14 @@ describe('rdme openapi', () => { const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version: '1.0.0' }) .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const mockWithHeader = getAPIMockWithVersionHeader(version) + const mockWithHeader = getAPIV1MockWithVersionHeader(version) .get('/api/v1/api-specification') .basicAuth({ user: key }) .reply(200, []) @@ -890,11 +890,11 @@ describe('rdme openapi', () => { const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const putMock = getAPIMockWithVersionHeader(version) + const putMock = getAPIV1MockWithVersionHeader(version) .put(`/api/v1/api-specification/${id}`, { registryUUID }) .basicAuth({ user: key }) .reply(400, errorObject); @@ -923,7 +923,7 @@ describe('rdme openapi', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }; - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version: '1.0.0' }) @@ -948,14 +948,14 @@ describe('rdme openapi', () => { const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version: '1.0.0' }) .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const mockWithHeader = getAPIMockWithVersionHeader(version) + const mockWithHeader = getAPIV1MockWithVersionHeader(version) .get('/api/v1/api-specification') .basicAuth({ user: key }) .reply(200, []) @@ -974,14 +974,14 @@ describe('rdme openapi', () => { it('should error if API errors (generic upload error)', async () => { const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version: '1.0.0' }) .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const mockWithHeader = getAPIMockWithVersionHeader(version) + const mockWithHeader = getAPIV1MockWithVersionHeader(version) .get('/api/v1/api-specification') .basicAuth({ user: key }) .reply(200, []) @@ -1004,14 +1004,14 @@ describe('rdme openapi', () => { it('should error if API errors (request timeout)', async () => { const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version: '1.0.0' }) .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const mockWithHeader = getAPIMockWithVersionHeader(version) + const mockWithHeader = getAPIV1MockWithVersionHeader(version) .get('/api/v1/api-specification') .basicAuth({ user: key }) .reply(200, []) @@ -1059,14 +1059,14 @@ describe('rdme openapi', () => { prompts.inject(['create', true, 'openapi-branch', yamlFileName]); const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }) .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const mockWithHeader = getAPIMockWithVersionHeader(version) + const mockWithHeader = getAPIV1MockWithVersionHeader(version) .get('/api/v1/api-specification') .basicAuth({ user: key }) .reply(200, [{ _id: 'spec1', title: 'spec1_title' }]) @@ -1095,14 +1095,14 @@ describe('rdme openapi', () => { prompts.inject(['create', 'openapi-branch-github-flag', yamlFileName]); const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }) .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const mockWithHeader = getAPIMockWithVersionHeader(version) + const mockWithHeader = getAPIV1MockWithVersionHeader(version) .get('/api/v1/api-specification') .basicAuth({ user: key }) .reply(200, [{ _id: 'spec1', title: 'spec1_title' }]) @@ -1131,14 +1131,14 @@ describe('rdme openapi', () => { prompts.inject(['update', 'spec2', true, 'openapi-branch-update-prompt', yamlFileName]); const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }) .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const mockWithHeader = getAPIMockWithVersionHeader(version) + const mockWithHeader = getAPIV1MockWithVersionHeader(version) .get('/api/v1/api-specification') .basicAuth({ user: key }) .reply(200, [ @@ -1167,14 +1167,14 @@ describe('rdme openapi', () => { prompts.inject([true, 'openapi-branch-create-flag', yamlFileName]); const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${altVersion}`) .basicAuth({ user: key }) .reply(200, { version: altVersion }) .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const mockWithHeader = getAPIMockWithVersionHeader(altVersion) + const mockWithHeader = getAPIV1MockWithVersionHeader(altVersion) .post('/api/v1/api-specification', { registryUUID }) .basicAuth({ user: key }) .reply(201, { _id: 1 }, { location: exampleRefLocation }); @@ -1196,14 +1196,14 @@ describe('rdme openapi', () => { prompts.inject([version, true, 'openapi-branch-create-flag-id-opt', yamlFileName]); const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get('/api/v1/version') .basicAuth({ user: key }) .reply(200, [{ version }, { version: '1.1.0' }]) .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const postMock = getAPIMockWithVersionHeader(version) + const postMock = getAPIV1MockWithVersionHeader(version) .post('/api/v1/api-specification', { registryUUID }) .basicAuth({ user: key }) .reply(201, { _id: 1 }, { location: exampleRefLocation }); @@ -1225,14 +1225,14 @@ describe('rdme openapi', () => { prompts.inject([true, 'openapi-branch-update-flag', yamlFileName]); const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }) .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const mockWithHeader = getAPIMockWithVersionHeader(version) + const mockWithHeader = getAPIV1MockWithVersionHeader(version) .get('/api/v1/api-specification') .basicAuth({ user: key }) .reply(200, [{ _id: 'spec1', title: 'spec1_title' }]) @@ -1256,7 +1256,7 @@ describe('rdme openapi', () => { prompts.inject([true, 'openapi-branch-workingdirectory', yamlFileName]); const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }) @@ -1268,7 +1268,7 @@ describe('rdme openapi', () => { .basicAuth({ user: key }) .reply(200, []); - const postMock = getAPIMockWithVersionHeader(version) + const postMock = getAPIV1MockWithVersionHeader(version) .post('/api/v1/api-specification', { registryUUID }) .basicAuth({ user: key }) .reply(201, { _id: 1 }, { location: exampleRefLocation }); @@ -1299,14 +1299,14 @@ describe('rdme openapi', () => { prompts.inject(['create', false]); const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }) .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID, spec: { openapi: '3.0.0' } }); - const mockWithHeader = getAPIMockWithVersionHeader(version) + const mockWithHeader = getAPIV1MockWithVersionHeader(version) .get('/api/v1/api-specification') .basicAuth({ user: key }) .reply(200, [{ _id: 'spec1', title: 'spec1_title' }]) @@ -1343,11 +1343,11 @@ describe('rdme openapi', () => { it('should send proper headers in GitHub Actions CI for local spec file', async () => { const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID }); - const putMock = getAPIMock({ + const putMock = getAPIV1Mock({ 'x-rdme-ci': 'GitHub Actions (test)', 'x-readme-source': 'cli-gh', 'x-readme-source-url': @@ -1370,13 +1370,13 @@ describe('rdme openapi', () => { const registryUUID = getRandomRegistryId(); const spec = 'https://example.com/openapi.json'; - const mock = getAPIMock() + const mock = getAPIV1Mock() .post('/api/v1/api-registry', body => body.match('form-data; name="spec"')) .reply(201, { registryUUID }); const exampleMock = nock('https://example.com').get('/openapi.json').reply(200, petstoreWeird); - const putMock = getAPIMock({ + const putMock = getAPIV1Mock({ 'x-rdme-ci': 'GitHub Actions (test)', 'x-readme-source': 'cli-gh', 'x-readme-source-url': spec, @@ -1395,7 +1395,7 @@ describe('rdme openapi', () => { it('should contain request header with correct URL with working directory', async () => { const registryUUID = getRandomRegistryId(); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }) @@ -1407,7 +1407,7 @@ describe('rdme openapi', () => { .basicAuth({ user: key }) .reply(200, []); - const postMock = getAPIMock({ + const postMock = getAPIV1Mock({ 'x-rdme-ci': 'GitHub Actions (test)', 'x-readme-source': 'cli-gh', 'x-readme-source-url': diff --git a/__tests__/commands/versions/create.test.ts b/__tests__/commands/versions/create.test.ts index 3c93b4a0e..e559f8d37 100644 --- a/__tests__/commands/versions/create.test.ts +++ b/__tests__/commands/versions/create.test.ts @@ -4,7 +4,7 @@ import { describe, beforeAll, afterEach, it, expect } from 'vitest'; import Command from '../../../src/commands/versions/create.js'; import APIError from '../../../src/lib/apiError.js'; -import getAPIMock from '../../helpers/get-api-mock.js'; +import { getAPIV1Mock } from '../../helpers/get-api-mock.js'; import { runCommand } from '../../helpers/setup-oclif-config.js'; const key = 'API_KEY'; @@ -34,7 +34,7 @@ describe('rdme versions:create', () => { prompts.inject([version, false, true, true, false]); const newVersion = '1.0.1'; - const mockRequest = getAPIMock() + const mockRequest = getAPIV1Mock() .get('/api/v1/version') .basicAuth({ user: key }) .reply(200, [{ version }, { version: '1.1.0' }]) @@ -56,7 +56,7 @@ describe('rdme versions:create', () => { it('should create a specific version with options', async () => { const newVersion = '1.0.1'; - const mockRequest = getAPIMock() + const mockRequest = getAPIV1Mock() .post('/api/v1/version', { version: newVersion, codename: 'test', @@ -95,7 +95,7 @@ describe('rdme versions:create', () => { it('should create successfully a main version', async () => { const newVersion = '1.0.1'; - const mockRequest = getAPIMock() + const mockRequest = getAPIV1Mock() .post('/api/v1/version', { version: newVersion, from: '1.0.0', @@ -134,7 +134,7 @@ describe('rdme versions:create', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }; - const mockRequest = getAPIMock().post('/api/v1/version').basicAuth({ user: key }).reply(400, errorResponse); + const mockRequest = getAPIV1Mock().post('/api/v1/version').basicAuth({ user: key }).reply(400, errorResponse); await expect(run(['--key', key, version, '--fork', '0.0.5'])).rejects.toStrictEqual(new APIError(errorResponse)); mockRequest.done(); diff --git a/__tests__/commands/versions/delete.test.ts b/__tests__/commands/versions/delete.test.ts index 6565a2aa3..8cb58f778 100644 --- a/__tests__/commands/versions/delete.test.ts +++ b/__tests__/commands/versions/delete.test.ts @@ -3,7 +3,7 @@ import { describe, beforeAll, afterEach, it, expect } from 'vitest'; import Command from '../../../src/commands/versions/delete.js'; import APIError from '../../../src/lib/apiError.js'; -import getAPIMock from '../../helpers/get-api-mock.js'; +import { getAPIV1Mock } from '../../helpers/get-api-mock.js'; import { runCommand } from '../../helpers/setup-oclif-config.js'; const key = 'API_KEY'; @@ -20,7 +20,7 @@ describe('rdme versions:delete', () => { afterEach(() => nock.cleanAll()); it('should delete a specific version', async () => { - const mockRequest = getAPIMock() + const mockRequest = getAPIV1Mock() .delete(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { removed: true }) @@ -41,7 +41,7 @@ describe('rdme versions:delete', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }; - const mockRequest = getAPIMock() + const mockRequest = getAPIV1Mock() .delete(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(404, errorResponse) diff --git a/__tests__/commands/versions/index.test.ts b/__tests__/commands/versions/index.test.ts index 3eb6b7e13..a98e363cc 100644 --- a/__tests__/commands/versions/index.test.ts +++ b/__tests__/commands/versions/index.test.ts @@ -4,7 +4,7 @@ import nock from 'nock'; import { describe, beforeAll, afterEach, it, expect } from 'vitest'; import Command from '../../../src/commands/versions/index.js'; -import getAPIMock from '../../helpers/get-api-mock.js'; +import { getAPIV1Mock } from '../../helpers/get-api-mock.js'; import { runCommand } from '../../helpers/setup-oclif-config.js'; const key = 'API_KEY'; @@ -42,7 +42,7 @@ describe('rdme versions', () => { afterEach(() => nock.cleanAll()); it('should make a request to get a list of existing versions', async () => { - const mockRequest = getAPIMock() + const mockRequest = getAPIV1Mock() .get('/api/v1/version') .basicAuth({ user: key }) .reply(200, [versionPayload, version2Payload]); @@ -53,7 +53,7 @@ describe('rdme versions', () => { }); it('should get a specific version object if version flag provided', async () => { - const mockRequest = getAPIMock() + const mockRequest = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, versionPayload); diff --git a/__tests__/commands/versions/update.test.ts b/__tests__/commands/versions/update.test.ts index 708eba49a..fca232ce2 100644 --- a/__tests__/commands/versions/update.test.ts +++ b/__tests__/commands/versions/update.test.ts @@ -4,7 +4,7 @@ import { describe, beforeAll, afterEach, it, expect } from 'vitest'; import Command from '../../../src/commands/versions/update.js'; import APIError from '../../../src/lib/apiError.js'; -import getAPIMock from '../../helpers/get-api-mock.js'; +import { getAPIV1Mock } from '../../helpers/get-api-mock.js'; import { runCommand } from '../../helpers/setup-oclif-config.js'; const key = 'API_KEY'; @@ -32,7 +32,7 @@ describe('rdme versions:update', () => { is_hidden: false, }; - const mockRequest = getAPIMock() + const mockRequest = getAPIV1Mock() .get('/api/v1/version') .basicAuth({ user: key }) .reply(200, [{ version }, { version: versionToChange }]) @@ -60,7 +60,7 @@ describe('rdme versions:update', () => { is_hidden: false, }; - const mockRequest = getAPIMock() + const mockRequest = getAPIV1Mock() .get('/api/v1/version') .basicAuth({ user: key }) .reply(200, [{ version }, { version: versionToChange }]) @@ -84,7 +84,7 @@ describe('rdme versions:update', () => { is_beta: true, }; - const mockRequest = getAPIMock() + const mockRequest = getAPIV1Mock() .get('/api/v1/version') .basicAuth({ user: key }) .reply(200, [{ version }, { version: versionToChange, is_stable: true }]) @@ -112,7 +112,7 @@ describe('rdme versions:update', () => { is_stable: false, }; - const mockRequest = getAPIMock() + const mockRequest = getAPIV1Mock() .get(`/api/v1/version/${versionToChange}`) .basicAuth({ user: key }) .reply(200, { version: versionToChange }) @@ -158,7 +158,7 @@ describe('rdme versions:update', () => { is_stable: false, }; - const mockRequest = getAPIMock() + const mockRequest = getAPIV1Mock() .get(`/api/v1/version/${versionToChange}`) .basicAuth({ user: key }) .reply(200, { version: versionToChange }) @@ -205,7 +205,7 @@ describe('rdme versions:update', () => { is_stable: false, }; - const mockRequest = getAPIMock() + const mockRequest = getAPIV1Mock() .get(`/api/v1/version/${versionToChange}`) .basicAuth({ user: key }) .reply(200, { version: versionToChange }) @@ -247,7 +247,7 @@ describe('rdme versions:update', () => { version: versionToChange, }; - const mockRequest = getAPIMock() + const mockRequest = getAPIV1Mock() .get(`/api/v1/version/${versionToChange}`) .basicAuth({ user: key }) .reply(200, { version: versionToChange }) @@ -286,7 +286,7 @@ describe('rdme versions:update', () => { is_stable: true, }; - const mockRequest = getAPIMock() + const mockRequest = getAPIV1Mock() .get(`/api/v1/version/${versionToChange}`) .basicAuth({ user: key }) .reply(200, { version: versionToChange }) @@ -337,7 +337,7 @@ describe('rdme versions:update', () => { help: 'If you need help, email support@readme.io and mention log "fake-metrics-uuid".', }; - const mockRequest = getAPIMock() + const mockRequest = getAPIV1Mock() .get(`/api/v1/version/${version}`) .basicAuth({ user: key }) .reply(200, { version }) diff --git a/__tests__/helpers/get-api-mock.ts b/__tests__/helpers/get-api-mock.ts index 9a2456cbe..8afd0c853 100644 --- a/__tests__/helpers/get-api-mock.ts +++ b/__tests__/helpers/get-api-mock.ts @@ -4,11 +4,10 @@ import config from '../../src/lib/config.js'; import { getUserAgent } from '../../src/lib/readmeAPIFetch.js'; /** - * Nock wrapper that adds required `user-agent` request header - * so it gets properly picked up by nock. - * @param proxy Optional proxy URL. Must contain trailing slash. + * Nock wrapper for ReadMe API v1 that adds required + * `user-agent` request header so it gets properly picked up by nock. */ -export default function getAPIMock(reqHeaders = {}) { +export function getAPIV1Mock(reqHeaders = {}) { return nock(config.host, { reqheaders: { 'User-Agent': getUserAgent(), @@ -17,8 +16,8 @@ export default function getAPIMock(reqHeaders = {}) { }); } -export function getAPIMockWithVersionHeader(v: string) { - return getAPIMock({ +export function getAPIV1MockWithVersionHeader(v: string) { + return getAPIV1Mock({ 'x-readme-version': v, }); } diff --git a/__tests__/lib/fetch.test.ts b/__tests__/lib/fetch.test.ts index 5cf2f7b49..d65370296 100644 --- a/__tests__/lib/fetch.test.ts +++ b/__tests__/lib/fetch.test.ts @@ -4,7 +4,7 @@ import { describe, beforeEach, afterEach, it, expect, vi, beforeAll, type MockIn import pkg from '../../package.json' with { type: 'json' }; import { cleanHeaders, handleRes, readmeAPIV1Fetch } from '../../src/lib/readmeAPIFetch.js'; -import getAPIMock from '../helpers/get-api-mock.js'; +import { getAPIV1Mock } from '../helpers/get-api-mock.js'; import { after, before } from '../helpers/setup-gha-env.js'; describe('#readmeAPIV1Fetch()', () => { @@ -22,7 +22,7 @@ describe('#readmeAPIV1Fetch()', () => { it('should have correct headers for requests in GitHub Action env', async () => { const key = 'API_KEY'; - const mock = getAPIMock() + const mock = getAPIV1Mock() .get('/api/v1') .basicAuth({ user: key }) .reply(200, function () { @@ -49,7 +49,7 @@ describe('#readmeAPIV1Fetch()', () => { it('should include source URL header with simple path', async () => { const key = 'API_KEY'; - const mock = getAPIMock() + const mock = getAPIV1Mock() .get('/api/v1') .basicAuth({ user: key }) .reply(200, function () { @@ -74,7 +74,7 @@ describe('#readmeAPIV1Fetch()', () => { it('should include source URL header with path that contains weird characters', async () => { const key = 'API_KEY'; - const mock = getAPIMock() + const mock = getAPIV1Mock() .get('/api/v1') .basicAuth({ user: key }) .reply(200, function () { @@ -100,7 +100,7 @@ describe('#readmeAPIV1Fetch()', () => { const key = 'API_KEY'; vi.stubEnv('GITHUB_SERVER_URL', undefined); - const mock = getAPIMock() + const mock = getAPIV1Mock() .get('/api/v1') .basicAuth({ user: key }) .reply(200, function () { @@ -123,7 +123,7 @@ describe('#readmeAPIV1Fetch()', () => { it('should include source URL header with relative path', async () => { const key = 'API_KEY'; - const mock = getAPIMock() + const mock = getAPIV1Mock() .get('/api/v1') .basicAuth({ user: key }) .reply(200, function () { @@ -149,7 +149,7 @@ describe('#readmeAPIV1Fetch()', () => { const key = 'API_KEY'; const filePath = 'https://example.com/openapi.json'; - const mock = getAPIMock() + const mock = getAPIV1Mock() .get('/api/v1') .basicAuth({ user: key }) .reply(200, function () { @@ -174,7 +174,7 @@ describe('#readmeAPIV1Fetch()', () => { it('should wrap all requests with standard user-agent and source headers', async () => { const key = 'API_KEY'; - const mock = getAPIMock() + const mock = getAPIV1Mock() .get('/api/v1') .basicAuth({ user: key }) .reply(200, function () { @@ -197,7 +197,7 @@ describe('#readmeAPIV1Fetch()', () => { }); it('should make fetch call if no other request options are provided', async () => { - const mock = getAPIMock() + const mock = getAPIV1Mock() .get('/api/v1/doesnt-need-auth') .reply(200, function () { return this.req.headers; @@ -231,7 +231,7 @@ describe('#readmeAPIV1Fetch()', () => { }); it('should not log anything if no warning header was passed', async () => { - const mock = getAPIMock().get('/api/v1/some-warning').reply(200, undefined, { + const mock = getAPIV1Mock().get('/api/v1/some-warning').reply(200, undefined, { Warning: '', }); @@ -244,7 +244,7 @@ describe('#readmeAPIV1Fetch()', () => { }); it('should surface a single warning header', async () => { - const mock = getAPIMock().get('/api/v1/some-warning').reply(200, undefined, { + const mock = getAPIV1Mock().get('/api/v1/some-warning').reply(200, undefined, { Warning: '199 - "some error"', }); @@ -257,7 +257,7 @@ describe('#readmeAPIV1Fetch()', () => { }); it('should surface multiple warning headers', async () => { - const mock = getAPIMock().get('/api/v1/some-warning').reply(200, undefined, { + const mock = getAPIV1Mock().get('/api/v1/some-warning').reply(200, undefined, { Warning: '199 - "some error" 199 - "another error"', }); @@ -272,7 +272,7 @@ describe('#readmeAPIV1Fetch()', () => { }); it('should surface header content even if parsing fails', async () => { - const mock = getAPIMock().get('/api/v1/some-warning').reply(200, undefined, { + const mock = getAPIV1Mock().get('/api/v1/some-warning').reply(200, undefined, { Warning: 'some garbage error', }); @@ -300,7 +300,7 @@ describe('#readmeAPIV1Fetch()', () => { vi.stubEnv('HTTPS_PROXY', proxy); - const mock = getAPIMock({}).get('/api/v1/proxy').reply(200); + const mock = getAPIV1Mock({}).get('/api/v1/proxy').reply(200); await readmeAPIV1Fetch('/api/v1/proxy'); @@ -312,7 +312,7 @@ describe('#readmeAPIV1Fetch()', () => { vi.stubEnv('https_proxy', proxy); - const mock = getAPIMock({}).get('/api/v1/proxy').reply(200); + const mock = getAPIV1Mock({}).get('/api/v1/proxy').reply(200); await readmeAPIV1Fetch('/api/v1/proxy'); @@ -324,7 +324,7 @@ describe('#readmeAPIV1Fetch()', () => { vi.stubEnv('https_proxy', proxy); - const mock = getAPIMock({}).get('/api/v1/proxy').reply(200); + const mock = getAPIV1Mock({}).get('/api/v1/proxy').reply(200); await readmeAPIV1Fetch('/api/v1/proxy'); diff --git a/src/commands/openapi/index.ts b/src/commands/openapi/index.ts index 148015fbb..97de0b13f 100644 --- a/src/commands/openapi/index.ts +++ b/src/commands/openapi/index.ts @@ -91,8 +91,6 @@ export default class OpenAPICommand extends BaseCommand { ); } - // Reason we're hardcoding in command here is because `swagger` command - // relies on this and we don't want to use `swagger` in this function const { preparedSpec, specFileType, specPath, specType, specVersion } = await prepareOas(spec, 'openapi', { title, }); diff --git a/src/lib/readmeAPIFetch.ts b/src/lib/readmeAPIFetch.ts index c2b9d1751..207f1a9a4 100644 --- a/src/lib/readmeAPIFetch.ts +++ b/src/lib/readmeAPIFetch.ts @@ -122,7 +122,7 @@ function sanitizeHeaders(headers: Headers) { } /** - * Wrapper for the `fetch` API so we can add rdme-specific headers to all API requests. + * Wrapper for the `fetch` API so we can add rdme-specific headers to all ReadMe API v1 requests. * * @param pathname the pathname to make the request to. Must have a leading slash. * @param fileOpts optional object containing information about the file being sent. diff --git a/src/lib/streamSpecToRegistry.ts b/src/lib/streamSpecToRegistry.ts index ba2d46ea5..422740b5c 100644 --- a/src/lib/streamSpecToRegistry.ts +++ b/src/lib/streamSpecToRegistry.ts @@ -9,10 +9,14 @@ import { handleRes, readmeAPIV1Fetch } from './readmeAPIFetch.js'; /** * Uploads a spec to the API registry for usage in ReadMe * - * @param {String} spec path to a bundled/validated spec file - * @returns {String} a UUID in the API registry + * @returns a UUID in the API registry */ -export default async function streamSpecToRegistry(spec: string) { +export default async function streamSpecToRegistry( + /** + * path to a bundled/validated spec file + */ + spec: string, +): Promise { const spinner = ora({ text: 'Staging your API definition for upload...', ...oraOptions() }).start(); // Create a temporary file to write the bundled spec to, // which we will then stream into the form data body