|
| 1 | +const path = require('path'); |
| 2 | +const commandLineValidator = require('../../../src/cli-validator/runValidator'); |
| 3 | +const config = require('../../../src/cli-validator/utils/processConfiguration'); |
| 4 | +const { getCapturedText } = require('../../test-utils'); |
| 5 | + |
| 6 | +describe('Spectral - test custom configuration', function() { |
| 7 | + it('test Spectral info and hint rules', async function() { |
| 8 | + // Set config to mock .spectral.yml file before running |
| 9 | + const mockPath = path.join( |
| 10 | + __dirname, |
| 11 | + '../mockFiles/mockConfig/info-and-hint.yaml' |
| 12 | + ); |
| 13 | + const mockConfig = jest |
| 14 | + .spyOn(config, 'getSpectralRuleset') |
| 15 | + .mockReturnValue(mockPath); |
| 16 | + |
| 17 | + const consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); |
| 18 | + // set up mock user input |
| 19 | + const program = {}; |
| 20 | + program.args = ['./test/spectral/mockFiles/oas3/enabled-rules.yml']; |
| 21 | + program.default_mode = true; |
| 22 | + program.json = true; |
| 23 | + |
| 24 | + // Note: validator does not set exitcode for jsonOutput |
| 25 | + await commandLineValidator(program); |
| 26 | + |
| 27 | + // Ensure mockConfig was called and revert it to its original state |
| 28 | + expect(mockConfig).toHaveBeenCalled(); |
| 29 | + mockConfig.mockRestore(); |
| 30 | + |
| 31 | + const capturedText = getCapturedText(consoleSpy.mock.calls); |
| 32 | + const jsonOutput = JSON.parse(capturedText); |
| 33 | + |
| 34 | + consoleSpy.mockRestore(); |
| 35 | + |
| 36 | + // Verify errors |
| 37 | + expect(jsonOutput['errors']['spectral'].length).toBe(2); |
| 38 | + |
| 39 | + // Verify warnings |
| 40 | + expect(jsonOutput['warnings']['spectral'].length).toBe(10); |
| 41 | + |
| 42 | + // Verify infos |
| 43 | + expect(jsonOutput['infos']['spectral'].length).toBe(5); |
| 44 | + expect(jsonOutput['infos']['spectral'][0]['message']).toEqual( |
| 45 | + 'Markdown descriptions should not contain `<script>` tags.' |
| 46 | + ); |
| 47 | + expect(jsonOutput['infos']['spectral'][4]['message']).toEqual( |
| 48 | + 'Operation tags should be defined in global tags.' |
| 49 | + ); |
| 50 | + |
| 51 | + // Verify hints |
| 52 | + expect(jsonOutput['hints']['spectral'].length).toBe(2); |
| 53 | + expect(jsonOutput['hints']['spectral'][0]['message']).toEqual( |
| 54 | + 'OpenAPI object should have non-empty `tags` array.' |
| 55 | + ); |
| 56 | + expect(jsonOutput['hints']['spectral'][1]['message']).toEqual( |
| 57 | + 'Operation should have non-empty `tags` array.' |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + it('test Spectral custom config that extends ibm:oas', async function() { |
| 62 | + // Set config to mock .spectral.yml file before running |
| 63 | + const mockPath = path.join( |
| 64 | + __dirname, |
| 65 | + '../mockFiles/mockConfig/extends-default.yaml' |
| 66 | + ); |
| 67 | + const mockConfig = jest |
| 68 | + .spyOn(config, 'getSpectralRuleset') |
| 69 | + .mockReturnValue(mockPath); |
| 70 | + |
| 71 | + const consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); |
| 72 | + // set up mock user input |
| 73 | + const program = {}; |
| 74 | + program.args = ['./test/spectral/mockFiles/oas3/enabled-rules.yml']; |
| 75 | + program.default_mode = true; |
| 76 | + program.json = true; |
| 77 | + |
| 78 | + // Note: validator does not set exitcode for jsonOutput |
| 79 | + await commandLineValidator(program); |
| 80 | + |
| 81 | + // Ensure mockConfig was called and revert it to its original state |
| 82 | + expect(mockConfig).toHaveBeenCalled(); |
| 83 | + mockConfig.mockRestore(); |
| 84 | + |
| 85 | + const capturedText = getCapturedText(consoleSpy.mock.calls); |
| 86 | + const jsonOutput = JSON.parse(capturedText); |
| 87 | + |
| 88 | + consoleSpy.mockRestore(); |
| 89 | + |
| 90 | + // Verify errors |
| 91 | + expect(jsonOutput['errors']['spectral'].length).toBe(1); |
| 92 | + expect(jsonOutput['errors']['spectral'][0]['message']).toEqual( |
| 93 | + 'Markdown descriptions should not contain `eval(`.' |
| 94 | + ); |
| 95 | + |
| 96 | + // Verify warnings |
| 97 | + expect(jsonOutput['warnings']['spectral'].length).toBe(17); |
| 98 | + const warnings = jsonOutput['warnings']['spectral'].map(w => w['message']); |
| 99 | + // This warning should be turned off |
| 100 | + expect(warnings).not.toContain( |
| 101 | + 'OpenAPI object should have non-empty `tags` array.' |
| 102 | + ); |
| 103 | + // This was redefined from error to warning |
| 104 | + expect(warnings).toContain( |
| 105 | + '`number_of_coins` property type should be integer' |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + it('test Spectral custom config that extends ibm:oas with custom rules', async function() { |
| 110 | + // Set config to mock .spectral.yml file before running |
| 111 | + const mockPath = path.join( |
| 112 | + __dirname, |
| 113 | + '../mockFiles/mockConfig/custom-rules.yaml' |
| 114 | + ); |
| 115 | + const mockConfig = jest |
| 116 | + .spyOn(config, 'getSpectralRuleset') |
| 117 | + .mockReturnValue(mockPath); |
| 118 | + |
| 119 | + const consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); |
| 120 | + // set up mock user input |
| 121 | + const program = {}; |
| 122 | + program.args = ['./test/spectral/mockFiles/oas3/enabled-rules.yml']; |
| 123 | + program.default_mode = true; |
| 124 | + program.json = true; |
| 125 | + |
| 126 | + // Note: validator does not set exitcode for jsonOutput |
| 127 | + await commandLineValidator(program); |
| 128 | + |
| 129 | + // Ensure mockConfig was called and revert it to its original state |
| 130 | + expect(mockConfig).toHaveBeenCalled(); |
| 131 | + mockConfig.mockRestore(); |
| 132 | + |
| 133 | + const capturedText = getCapturedText(consoleSpy.mock.calls); |
| 134 | + const jsonOutput = JSON.parse(capturedText); |
| 135 | + |
| 136 | + consoleSpy.mockRestore(); |
| 137 | + |
| 138 | + // Verify errors |
| 139 | + expect(jsonOutput['errors']['spectral'].length).toBe(2); |
| 140 | + |
| 141 | + // Verify warnings |
| 142 | + expect(jsonOutput['warnings']['spectral'].length).toBe(21); |
| 143 | + const warnings = jsonOutput['warnings']['spectral'].map(w => w['message']); |
| 144 | + // This is the new warning -- there should be four occurrences |
| 145 | + const warning = 'All request bodies should have an example.'; |
| 146 | + const occurrences = warnings.reduce( |
| 147 | + (a, v) => (v === warning ? a + 1 : a), |
| 148 | + 0 |
| 149 | + ); |
| 150 | + expect(occurrences).toBe(4); |
| 151 | + }); |
| 152 | +}); |
0 commit comments