|
| 1 | +const walk = require('walk-sync') |
| 2 | +const path = require('path') |
| 3 | +const { get, isPlainObject } = require('lodash') |
| 4 | +const allVersions = require('../../lib/all-versions') |
| 5 | +const nonEnterpriseDefaultVersion = require('../../lib/non-enterprise-default-version') |
| 6 | +const { operations } = require('../../lib/rest') |
| 7 | +const dedent = require('dedent') |
| 8 | +const schemasPath = path.join(__dirname, '../../lib/rest/static/decorated') |
| 9 | +const nonEnterpriseDefaultVersionSchema = operations[nonEnterpriseDefaultVersion] |
| 10 | + |
| 11 | +describe('OpenAPI schema validation', () => { |
| 12 | + test('exports an object', () => { |
| 13 | + expect(isPlainObject(operations)).toBe(true) |
| 14 | + }) |
| 15 | + |
| 16 | + // ensure every version defined in allVersions has a correlating static |
| 17 | + // decorated file, while allowing decorated files to exist when a version |
| 18 | + // is not yet defined in allVersions (e.g., a GHEC static file can exist |
| 19 | + // even though the version is not yet supported in the docs) |
| 20 | + test('every OpenAPI version must have a schema file in the docs', () => { |
| 21 | + const decoratedFilenames = walk(schemasPath) |
| 22 | + .map(filename => path.basename(filename, '.json')) |
| 23 | + |
| 24 | + Object.values(allVersions) |
| 25 | + .map(version => version.openApiVersionName) |
| 26 | + .forEach(openApiBaseName => { |
| 27 | + expect(decoratedFilenames.includes(openApiBaseName)).toBe(true) |
| 28 | + }) |
| 29 | + }) |
| 30 | + |
| 31 | + test('every value is an array of operations', () => { |
| 32 | + let checks = 0 |
| 33 | + for (const [, operation] of Object.entries(operations)) { |
| 34 | + checks++ |
| 35 | + expect(Array.isArray(operation)).toBe(true) |
| 36 | + } |
| 37 | + // there are at least 5 versions available (3 ghes [when a version |
| 38 | + // has been deprecated], api.github.com, and github.ae) |
| 39 | + expect(checks).toBeGreaterThanOrEqual(5) |
| 40 | + }) |
| 41 | +}) |
| 42 | + |
| 43 | +function findOperation (method, path) { |
| 44 | + return nonEnterpriseDefaultVersionSchema.find(operation => { |
| 45 | + return operation.requestPath === path && operation.verb.toLowerCase() === method.toLowerCase() |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +describe('x-codeSamples for curl', () => { |
| 50 | + test('GET', () => { |
| 51 | + const operation = findOperation('GET', '/repos/{owner}/{repo}') |
| 52 | + expect(isPlainObject(operation)).toBe(true) |
| 53 | + const { source } = operation['x-codeSamples'].find(sample => sample.lang === 'Shell') |
| 54 | + const expected = 'curl \\\n -H "Accept: application/vnd.github.v3+json" \\\n https://api.github.com/repos/octocat/hello-world' |
| 55 | + expect(source).toEqual(expected) |
| 56 | + }) |
| 57 | + |
| 58 | + test('operations with required preview headers', () => { |
| 59 | + const operationsWithRequiredPreviewHeaders = nonEnterpriseDefaultVersionSchema.filter(operation => { |
| 60 | + const previews = get(operation, 'x-github.previews', []) |
| 61 | + return previews.some(preview => preview.required) |
| 62 | + }) |
| 63 | + expect(operationsWithRequiredPreviewHeaders.length).toBeGreaterThan(0) |
| 64 | + const operationsWithHeadersInCodeSample = operationsWithRequiredPreviewHeaders.filter(operation => { |
| 65 | + const { source: codeSample } = operation['x-codeSamples'].find(sample => sample.lang === 'Shell') |
| 66 | + return codeSample.includes('-H "Accept: application/vnd.github') && !codeSample.includes('application/vnd.github.v3+json') |
| 67 | + }) |
| 68 | + expect(operationsWithRequiredPreviewHeaders.length).toEqual(operationsWithHeadersInCodeSample.length) |
| 69 | + }) |
| 70 | +}) |
| 71 | + |
| 72 | +describe('x-codeSamples for @octokit/core.js', () => { |
| 73 | + test('GET', () => { |
| 74 | + const operation = findOperation('GET', '/repos/{owner}/{repo}') |
| 75 | + expect(isPlainObject(operation)).toBe(true) |
| 76 | + const { source } = operation['x-codeSamples'].find(sample => sample.lang === 'JavaScript') |
| 77 | + const expected = dedent`await octokit.request('GET /repos/{owner}/{repo}', { |
| 78 | + owner: 'octocat', |
| 79 | + repo: 'hello-world' |
| 80 | + })` |
| 81 | + expect(source).toEqual(expected) |
| 82 | + }) |
| 83 | + |
| 84 | + test('POST', () => { |
| 85 | + const operation = findOperation('POST', '/repos/{owner}/{repo}/git/trees') |
| 86 | + expect(isPlainObject(operation)).toBe(true) |
| 87 | + const { source } = operation['x-codeSamples'].find(sample => sample.lang === 'JavaScript') |
| 88 | + const expected = dedent`await octokit.request('POST /repos/{owner}/{repo}/git/trees', { |
| 89 | + owner: 'octocat', |
| 90 | + repo: 'hello-world', |
| 91 | + tree: [ |
| 92 | + { |
| 93 | + path: 'path', |
| 94 | + mode: 'mode', |
| 95 | + type: 'type', |
| 96 | + sha: 'sha', |
| 97 | + content: 'content' |
| 98 | + } |
| 99 | + ] |
| 100 | + })` |
| 101 | + expect(source).toEqual(expected) |
| 102 | + }) |
| 103 | + |
| 104 | + test('PUT', () => { |
| 105 | + const operation = findOperation('PUT', '/authorizations/clients/{client_id}/{fingerprint}') |
| 106 | + expect(isPlainObject(operation)).toBe(true) |
| 107 | + const { source } = operation['x-codeSamples'].find(sample => sample.lang === 'JavaScript') |
| 108 | + const expected = dedent`await octokit.request('PUT /authorizations/clients/{client_id}/{fingerprint}', { |
| 109 | + client_id: 'client_id', |
| 110 | + fingerprint: 'fingerprint', |
| 111 | + client_secret: 'client_secret' |
| 112 | + })` |
| 113 | + expect(source).toEqual(expected) |
| 114 | + }) |
| 115 | + |
| 116 | + test('operations with required preview headers', () => { |
| 117 | + const operationsWithRequiredPreviewHeaders = nonEnterpriseDefaultVersionSchema.filter(operation => { |
| 118 | + const previews = get(operation, 'x-github.previews', []) |
| 119 | + return previews.some(preview => preview.required) |
| 120 | + }) |
| 121 | + expect(operationsWithRequiredPreviewHeaders.length).toBeGreaterThan(0) |
| 122 | + |
| 123 | + // Find something that looks like the following in each code sample: |
| 124 | + /* |
| 125 | + mediaType: { |
| 126 | + previews: [ |
| 127 | + 'machine-man' |
| 128 | + ] |
| 129 | + } |
| 130 | + */ |
| 131 | + const operationsWithHeadersInCodeSample = operationsWithRequiredPreviewHeaders.filter(operation => { |
| 132 | + const { source: codeSample } = operation['x-codeSamples'].find(sample => sample.lang === 'JavaScript') |
| 133 | + return codeSample.match(/mediaType: \{\s+previews: /g) |
| 134 | + }) |
| 135 | + expect(operationsWithRequiredPreviewHeaders.length).toEqual(operationsWithHeadersInCodeSample.length) |
| 136 | + }) |
| 137 | + |
| 138 | + // skipped because the definition is current missing the `content-type` parameter |
| 139 | + // See GitHub issue #155943 |
| 140 | + test.skip('operation with content-type parameter', () => { |
| 141 | + const operation = findOperation('POST', '/markdown/raw') |
| 142 | + expect(isPlainObject(operation)).toBe(true) |
| 143 | + const { source } = operation['x-codeSamples'].find(sample => sample.lang === 'JavaScript') |
| 144 | + const expected = dedent`await octokit.request('POST /markdown/raw', { |
| 145 | + data: 'data', |
| 146 | + headers: { |
| 147 | + 'content-type': 'text/plain; charset=utf-8' |
| 148 | + } |
| 149 | + })` |
| 150 | + expect(source).toEqual(expected) |
| 151 | + }) |
| 152 | +}) |
0 commit comments