|
| 1 | +import type { OpenAPISchemaType } from './OpenAPISchemaType.js'; |
1 | 2 | import openAPI from '@openapi-qraft/test-fixtures/openapi.json' with { type: 'json' }; |
2 | 3 | import { describe, expect, it } from 'vitest'; |
3 | 4 | import { filterDocumentPaths } from '../filterDocumentPaths.js'; |
@@ -31,4 +32,126 @@ describe('getServices', () => { |
31 | 32 | it('matches snapshot with "serviceNameBase: tags"', () => { |
32 | 33 | expect(getServices(openAPI, { serviceNameBase: 'tags' })).toMatchSnapshot(); |
33 | 34 | }); |
| 35 | + |
| 36 | + it('inherits root-level security when `rootSecurity` is enabled and operation-level security is omitted', () => { |
| 37 | + const document: OpenAPISchemaType = { |
| 38 | + openapi: '3.1.0', |
| 39 | + info: { |
| 40 | + title: 'Fixture API', |
| 41 | + version: '1.0.0', |
| 42 | + }, |
| 43 | + security: [{ jwtUserToken: [] }], |
| 44 | + paths: { |
| 45 | + '/accounts': { |
| 46 | + get: { |
| 47 | + operationId: 'getAccounts', |
| 48 | + responses: { |
| 49 | + 200: { |
| 50 | + description: 'Successful response', |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + components: { |
| 57 | + parameters: undefined, |
| 58 | + }, |
| 59 | + }; |
| 60 | + |
| 61 | + expect( |
| 62 | + getServices(document, { rootSecurity: true })[0]?.operations[0]?.security |
| 63 | + ).toEqual([{ jwtUserToken: [] }]); |
| 64 | + }); |
| 65 | + |
| 66 | + it('does not inherit root-level security when `rootSecurity` is disabled', () => { |
| 67 | + const document: OpenAPISchemaType = { |
| 68 | + openapi: '3.1.0', |
| 69 | + info: { |
| 70 | + title: 'Fixture API', |
| 71 | + version: '1.0.0', |
| 72 | + }, |
| 73 | + security: [{ jwtUserToken: [] }], |
| 74 | + paths: { |
| 75 | + '/accounts': { |
| 76 | + get: { |
| 77 | + operationId: 'getAccounts', |
| 78 | + responses: { |
| 79 | + 200: { |
| 80 | + description: 'Successful response', |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + components: { |
| 87 | + parameters: undefined, |
| 88 | + }, |
| 89 | + }; |
| 90 | + |
| 91 | + expect( |
| 92 | + getServices(document, { rootSecurity: false })[0]?.operations[0]?.security |
| 93 | + ).toBeUndefined(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('prefers operation-level security over root-level security when `rootSecurity` is enabled', () => { |
| 97 | + const document: OpenAPISchemaType = { |
| 98 | + openapi: '3.1.0', |
| 99 | + info: { |
| 100 | + title: 'Fixture API', |
| 101 | + version: '1.0.0', |
| 102 | + }, |
| 103 | + security: [{ jwtUserToken: [] }], |
| 104 | + paths: { |
| 105 | + '/accounts': { |
| 106 | + get: { |
| 107 | + operationId: 'getAccounts', |
| 108 | + security: [{ apiKey: [] }], |
| 109 | + responses: { |
| 110 | + 200: { |
| 111 | + description: 'Successful response', |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + components: { |
| 118 | + parameters: undefined, |
| 119 | + }, |
| 120 | + }; |
| 121 | + |
| 122 | + expect( |
| 123 | + getServices(document, { rootSecurity: true })[0]?.operations[0]?.security |
| 124 | + ).toEqual([{ apiKey: [] }]); |
| 125 | + }); |
| 126 | + |
| 127 | + it('treats empty operation-level security as an explicit override when `rootSecurity` is enabled', () => { |
| 128 | + const document: OpenAPISchemaType = { |
| 129 | + openapi: '3.1.0', |
| 130 | + info: { |
| 131 | + title: 'Fixture API', |
| 132 | + version: '1.0.0', |
| 133 | + }, |
| 134 | + security: [{ jwtUserToken: [] }], |
| 135 | + paths: { |
| 136 | + '/health': { |
| 137 | + get: { |
| 138 | + operationId: 'getHealth', |
| 139 | + security: [], |
| 140 | + responses: { |
| 141 | + 200: { |
| 142 | + description: 'Successful response', |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, |
| 148 | + components: { |
| 149 | + parameters: undefined, |
| 150 | + }, |
| 151 | + }; |
| 152 | + |
| 153 | + expect( |
| 154 | + getServices(document, { rootSecurity: true })[0]?.operations[0]?.security |
| 155 | + ).toEqual([]); |
| 156 | + }); |
34 | 157 | }); |
0 commit comments