|
| 1 | +import { outdent } from 'outdent'; |
| 2 | +import { lintDocument } from '../../../lint'; |
| 3 | +import { parseYamlToDocument, replaceSourceWithRef, makeConfig } from '../../../../__tests__/utils'; |
| 4 | +import { BaseResolver } from '../../../resolve'; |
| 5 | + |
| 6 | +describe('license-strict', () => { |
| 7 | + it('should report on info.license with no url or identifier for OpenAPI 3.1', async () => { |
| 8 | + const document = parseYamlToDocument( |
| 9 | + outdent` |
| 10 | + openapi: 3.1.0 |
| 11 | + info: |
| 12 | + license: |
| 13 | + name: MIT |
| 14 | + `, |
| 15 | + 'foobar.yaml' |
| 16 | + ); |
| 17 | + |
| 18 | + const results = await lintDocument({ |
| 19 | + externalRefResolver: new BaseResolver(), |
| 20 | + document, |
| 21 | + config: await makeConfig({ rules: { 'info-license-strict': 'error' } }), |
| 22 | + }); |
| 23 | + |
| 24 | + expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(` |
| 25 | + [ |
| 26 | + { |
| 27 | + "location": [ |
| 28 | + { |
| 29 | + "pointer": "#/info/license", |
| 30 | + "reportOnKey": true, |
| 31 | + "source": "foobar.yaml", |
| 32 | + }, |
| 33 | + ], |
| 34 | + "message": "License object should contain one of the fields: \`url\`, \`identifier\`.", |
| 35 | + "ruleId": "info-license-strict", |
| 36 | + "severity": "error", |
| 37 | + "suggest": [], |
| 38 | + }, |
| 39 | + ] |
| 40 | + `); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should not report on info.license with url for OpenAPI 3.1', async () => { |
| 44 | + const document = parseYamlToDocument( |
| 45 | + outdent` |
| 46 | + openapi: 3.1.0 |
| 47 | + info: |
| 48 | + license: |
| 49 | + name: MIT |
| 50 | + url: google.com |
| 51 | + `, |
| 52 | + 'foobar.yaml' |
| 53 | + ); |
| 54 | + |
| 55 | + const results = await lintDocument({ |
| 56 | + externalRefResolver: new BaseResolver(), |
| 57 | + document, |
| 58 | + config: await makeConfig({ rules: { 'info-license-strict': 'error' } }), |
| 59 | + }); |
| 60 | + |
| 61 | + expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`[]`); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should not report on info.license with identifier', async () => { |
| 65 | + const document = parseYamlToDocument( |
| 66 | + outdent` |
| 67 | + openapi: 3.1.0 |
| 68 | + info: |
| 69 | + license: |
| 70 | + name: MIT |
| 71 | + identifier: MIT |
| 72 | + `, |
| 73 | + 'foobar.yaml' |
| 74 | + ); |
| 75 | + |
| 76 | + const results = await lintDocument({ |
| 77 | + externalRefResolver: new BaseResolver(), |
| 78 | + document, |
| 79 | + config: await makeConfig({ rules: { 'info-license-strict': 'error' } }), |
| 80 | + }); |
| 81 | + |
| 82 | + expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`[]`); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should report on info.license with no url for AsyncAPI 3.0', async () => { |
| 86 | + const document = parseYamlToDocument( |
| 87 | + outdent` |
| 88 | + asyncapi: 3.0.0 |
| 89 | + info: |
| 90 | + license: |
| 91 | + name: MIT |
| 92 | + `, |
| 93 | + 'foobar.yaml' |
| 94 | + ); |
| 95 | + |
| 96 | + const results = await lintDocument({ |
| 97 | + externalRefResolver: new BaseResolver(), |
| 98 | + document, |
| 99 | + config: await makeConfig({ rules: { 'info-license-strict': 'error' } }), |
| 100 | + }); |
| 101 | + |
| 102 | + expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(` |
| 103 | + [ |
| 104 | + { |
| 105 | + "location": [ |
| 106 | + { |
| 107 | + "pointer": "#/info/license/url", |
| 108 | + "reportOnKey": true, |
| 109 | + "source": "foobar.yaml", |
| 110 | + }, |
| 111 | + ], |
| 112 | + "message": "License object should contain \`url\` field.", |
| 113 | + "ruleId": "info-license-strict", |
| 114 | + "severity": "error", |
| 115 | + "suggest": [], |
| 116 | + }, |
| 117 | + ] |
| 118 | + `); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should not report on info.license with url for AsyncAPI 3.0', async () => { |
| 122 | + const document = parseYamlToDocument( |
| 123 | + outdent` |
| 124 | + asyncapi: 3.0.0 |
| 125 | + info: |
| 126 | + license: |
| 127 | + name: MIT |
| 128 | + url: google.com |
| 129 | + `, |
| 130 | + 'foobar.yaml' |
| 131 | + ); |
| 132 | + |
| 133 | + const results = await lintDocument({ |
| 134 | + externalRefResolver: new BaseResolver(), |
| 135 | + document, |
| 136 | + config: await makeConfig({ rules: { 'info-license-strict': 'error' } }), |
| 137 | + }); |
| 138 | + |
| 139 | + expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`[]`); |
| 140 | + }); |
| 141 | +}); |
0 commit comments