-
Notifications
You must be signed in to change notification settings - Fork 199
Add unit tests for publisher Resources operationUtils #1406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Gayuth-W
wants to merge
1
commit into
wso2:main
Choose a base branch
from
Gayuth-W:test/add-missing-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
221 changes: 221 additions & 0 deletions
221
portals/publisher/src/main/webapp/source/Tests/Unit/operationUtils.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| /* | ||
| * Copyright (c) 2026, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import { | ||
| mapAPIOperations, | ||
| getTaggedOperations, | ||
| getAPIProductTaggedOperations, | ||
| extractPathParameters, | ||
| getOperationScopes, | ||
| isSelectAll, | ||
| } from '../../src/app/components/Apis/Details/Resources/operationUtils'; | ||
|
|
||
| const OAS_3 = { openapi: '3.0.0' }; | ||
| const OAS_31 = { openapi: '3.1.0' }; | ||
| const SWAGGER_2 = { swagger: '2.0' }; | ||
|
|
||
| /** | ||
| * Path parameters are generated differently for OpenAPI 3.x and Swagger 2.0. | ||
| * These tests make sure each format stays correct. | ||
| */ | ||
| describe('extractPathParameters', () => { | ||
| test('should nest the type under `schema` for OpenAPI 3.0 definitions', () => { | ||
| expect(extractPathParameters('/shops/{shopId}/orders/{orderId}', OAS_3)).toEqual([ | ||
| { name: 'shopId', in: 'path', required: true, schema: { type: 'string', format: 'string' } }, | ||
| { name: 'orderId', in: 'path', required: true, schema: { type: 'string', format: 'string' } }, | ||
| ]); | ||
| }); | ||
|
|
||
| test('should nest the type under `schema` for OpenAPI 3.1 definitions', () => { | ||
| expect(extractPathParameters('/orders/{orderId}', OAS_31)).toEqual([ | ||
| { name: 'orderId', in: 'path', required: true, schema: { type: 'string', format: 'string' } }, | ||
| ]); | ||
| }); | ||
|
|
||
| test('should declare the type inline for Swagger 2.0 definitions', () => { | ||
| expect(extractPathParameters('/orders/{orderId}', SWAGGER_2)).toEqual([ | ||
| { name: 'orderId', in: 'path', required: true, type: 'string', format: 'string' }, | ||
| ]); | ||
| }); | ||
|
|
||
| test('should return an empty array when the URI template declares no parameters', () => { | ||
| expect(extractPathParameters('/orders', OAS_3)).toEqual([]); | ||
| }); | ||
|
|
||
| test('should return an empty array when the definition version is not recognised', () => { | ||
| expect(extractPathParameters('/orders/{orderId}', {})).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| /** | ||
| * Operation scopes can come from the `default` security scheme or the legacy `x-scope` field. Both should be supported. | ||
| */ | ||
| describe('getOperationScopes', () => { | ||
| test('should return the scopes of the `default` security scheme', () => { | ||
| const operation = { security: [{ default: ['order_read', 'order_write'] }] }; | ||
| expect(getOperationScopes(operation, OAS_3)).toEqual(['order_read', 'order_write']); | ||
| expect(getOperationScopes(operation, SWAGGER_2)).toEqual(['order_read', 'order_write']); | ||
| }); | ||
|
|
||
| test('should fall back to the `x-scope` extension wrapped in an array', () => { | ||
| expect(getOperationScopes({ 'x-scope': 'order_read' }, OAS_3)).toEqual(['order_read']); | ||
| }); | ||
|
|
||
| test('should return an empty array when no `default` scheme or `x-scope` is present', () => { | ||
| expect(getOperationScopes({ security: [{ oauth2: ['order_read'] }] }, OAS_3)).toEqual([]); | ||
| expect(getOperationScopes({}, OAS_3)).toEqual([]); | ||
| }); | ||
|
|
||
| test('should return an empty array when the definition version is not recognised', () => { | ||
| expect(getOperationScopes({ security: [{ default: ['order_read'] }] }, {})).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| /** | ||
| * Converts the API operation list into a Swagger/OpenAPI `paths` object. | ||
| */ | ||
| describe('mapAPIOperations', () => { | ||
| test('should key operations by target then verb, dropping both from the payload', () => { | ||
| const operations = [{ | ||
| target: '/orders', | ||
| verb: 'GET', | ||
| authType: 'Application & Application User', | ||
| throttlingPolicy: 'Unlimited', | ||
| }]; | ||
| expect(mapAPIOperations(operations)).toEqual({ | ||
| '/orders': { GET: { authType: 'Application & Application User', throttlingPolicy: 'Unlimited' } }, | ||
| }); | ||
| }); | ||
|
|
||
| test('should merge multiple verbs that share the same target', () => { | ||
| const operations = [ | ||
| { target: '/orders', verb: 'GET' }, | ||
| { target: '/orders', verb: 'POST' }, | ||
| { target: '/orders/{orderId}', verb: 'DELETE' }, | ||
| ]; | ||
| const mapped = mapAPIOperations(operations); | ||
| expect(Object.keys(mapped['/orders'])).toEqual(['GET', 'POST']); | ||
| expect(Object.keys(mapped['/orders/{orderId}'])).toEqual(['DELETE']); | ||
| }); | ||
| }); | ||
|
|
||
| /** | ||
| * Checks whether every operation is selected, not just every path. | ||
| */ | ||
| describe('isSelectAll', () => { | ||
| const operations = { | ||
| '/orders': { get: {}, post: {} }, | ||
| '/orders/{orderId}': { delete: {} }, | ||
| }; | ||
|
|
||
| test('should return true when every verb of every path is selected', () => { | ||
| expect(isSelectAll(operations, operations)).toBe(true); | ||
| }); | ||
|
|
||
| test('should return false when a path is only partially selected', () => { | ||
| const selected = { '/orders': { get: {} }, '/orders/{orderId}': { delete: {} } }; | ||
| expect(isSelectAll(selected, operations)).toBe(false); | ||
| }); | ||
|
|
||
| test('should return false when a path is not selected at all', () => { | ||
| expect(isSelectAll({ '/orders': { get: {}, post: {} } }, operations)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| /** | ||
| * Groups operations by their OpenAPI tags. | ||
| * Operations missing from the definition should be ignored. | ||
| */ | ||
| describe('getTaggedOperations', () => { | ||
| const openAPI = { | ||
| paths: { | ||
| '/orders': { get: { operationId: 'getOrders', tags: ['Orders'] } }, | ||
| '/menu': { get: { operationId: 'getMenu' } }, | ||
| }, | ||
| }; | ||
|
|
||
| test('should group operations by the tags declared in the definition', () => { | ||
| const api = { isAPIProduct: () => false, operations: [{ target: '/orders', verb: 'GET' }] }; | ||
| const tagged = getTaggedOperations(api, openAPI); | ||
| expect(tagged.Orders).toEqual([{ | ||
| spec: { operationId: 'getOrders', tags: ['Orders'] }, | ||
| target: '/orders', | ||
| verb: 'GET', | ||
| }]); | ||
| expect(tagged.Default).toEqual([]); | ||
| }); | ||
|
|
||
| test('should place untagged operations under the `Default` tag', () => { | ||
| const api = { isAPIProduct: () => false, operations: [{ target: '/menu', verb: 'GET' }] }; | ||
| expect(getTaggedOperations(api, openAPI).Default).toHaveLength(1); | ||
| }); | ||
|
|
||
| test('should skip operations that are absent from the definition instead of throwing', () => { | ||
| const warn = jest.spyOn(console, 'warn').mockImplementation(() => { }); | ||
| const api = { isAPIProduct: () => false, operations: [{ target: '/unknown', verb: 'GET' }] }; | ||
| expect(getTaggedOperations(api, openAPI)).toEqual({ Default: [] }); | ||
| expect(warn).toHaveBeenCalledTimes(1); | ||
| warn.mockRestore(); | ||
| }); | ||
|
|
||
| test('should delegate to the API product grouping when the artifact is an API product', () => { | ||
| const apiProduct = { | ||
| isAPIProduct: () => true, | ||
| apis: [{ name: 'OrderAPI', operations: [{ target: '/orders', verb: 'GET' }] }], | ||
| }; | ||
| expect(getTaggedOperations(apiProduct, openAPI).OrderAPI).toHaveLength(1); | ||
| }); | ||
| }); | ||
|
|
||
| /** | ||
| * API products group by dependent API name rather than by tag. | ||
| */ | ||
| describe('getAPIProductTaggedOperations', () => { | ||
| const openAPI = { | ||
| paths: { | ||
| '/orders': { get: { operationId: 'getOrders' } }, | ||
| '/menu': { get: { operationId: 'getMenu' } }, | ||
| }, | ||
| }; | ||
|
|
||
| test('should group operations under the name of each dependent API', () => { | ||
| const apiProduct = { | ||
| apis: [ | ||
| { name: 'PizzaShackAPI', operations: [{ target: '/menu', verb: 'GET' }] }, | ||
| { name: 'OrderAPI', operations: [{ target: '/orders', verb: 'GET' }] }, | ||
| ], | ||
| }; | ||
| const tagged = getAPIProductTaggedOperations(apiProduct, openAPI); | ||
| expect(tagged.PizzaShackAPI).toEqual([{ spec: { operationId: 'getMenu' }, target: '/menu', verb: 'GET' }]); | ||
| expect(tagged.OrderAPI).toHaveLength(1); | ||
| }); | ||
|
|
||
| test('should skip operations that are absent from the definition instead of throwing', () => { | ||
| const warn = jest.spyOn(console, 'warn').mockImplementation(() => { }); | ||
| const apiProduct = { | ||
| apis: [{ | ||
| name: 'PizzaShackAPI', | ||
| operations: [{ target: '/menu', verb: 'GET' }, { target: '/unknown', verb: 'GET' }], | ||
| }], | ||
| }; | ||
| const tagged = getAPIProductTaggedOperations(apiProduct, openAPI); | ||
| expect(tagged.PizzaShackAPI).toHaveLength(1); | ||
| expect(warn).toHaveBeenCalledTimes(1); | ||
| warn.mockRestore(); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.