|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Salesforce, Inc. |
| 3 | + * SPDX-License-Identifier: Apache-2 |
| 4 | + * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import * as assert from 'assert'; |
| 8 | +import type {SchemaEntry} from '../api-browser/api-browser-tree-provider.js'; |
| 9 | +import {detectApiType, injectCustomApiOrgPathPrefix} from '../api-browser/swagger-webview.js'; |
| 10 | + |
| 11 | +function entry(apiFamily: string, apiName: string): SchemaEntry { |
| 12 | + return {apiFamily, apiName, apiVersion: 'v1'}; |
| 13 | +} |
| 14 | + |
| 15 | +function specWithGlobalSecurity(schemes: Record<string, string[]>[]): Record<string, unknown> { |
| 16 | + return {security: schemes, paths: {}}; |
| 17 | +} |
| 18 | + |
| 19 | +function specWithOpSecurity(perOp: Record<string, string[]>[]): Record<string, unknown> { |
| 20 | + return { |
| 21 | + paths: { |
| 22 | + '/foo': { |
| 23 | + get: {security: perOp, responses: {'200': {description: 'ok'}}}, |
| 24 | + }, |
| 25 | + }, |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +suite('detectApiType', () => { |
| 30 | + test('returns Shopper for ShopperToken-only spec', () => { |
| 31 | + const spec = specWithGlobalSecurity([{ShopperToken: ['c_loyalty']}]); |
| 32 | + assert.strictEqual(detectApiType(spec, entry('custom', 'loyalty')), 'Shopper'); |
| 33 | + }); |
| 34 | + |
| 35 | + test('returns Admin for AmOAuth2-only spec', () => { |
| 36 | + const spec = specWithGlobalSecurity([{AmOAuth2: ['c_agentforce']}]); |
| 37 | + assert.strictEqual(detectApiType(spec, entry('custom', 'agentforce')), 'Admin'); |
| 38 | + }); |
| 39 | + |
| 40 | + test('returns Admin for BearerToken (SLAS Admin API)', () => { |
| 41 | + const spec = specWithOpSecurity([{BearerToken: []}]); |
| 42 | + assert.strictEqual(detectApiType(spec, entry('shopper', 'auth-admin')), 'Admin'); |
| 43 | + }); |
| 44 | + |
| 45 | + test('Shopper for shopper-named spec mixing AmOAuth2 + ShopperToken', () => { |
| 46 | + const spec = specWithOpSecurity([{ShopperToken: []}, {AmOAuth2: []}]); |
| 47 | + assert.strictEqual(detectApiType(spec, entry('checkout', 'shopper-baskets')), 'Shopper'); |
| 48 | + }); |
| 49 | + |
| 50 | + test('Admin for non-shopper-named spec mixing AmOAuth2 + ShopperToken', () => { |
| 51 | + const spec = specWithOpSecurity([{AmOAuth2: []}, {ShopperToken: []}]); |
| 52 | + assert.strictEqual(detectApiType(spec, entry('checkout', 'orders')), 'Admin'); |
| 53 | + }); |
| 54 | + |
| 55 | + test('per-op security takes precedence over global', () => { |
| 56 | + const spec: Record<string, unknown> = { |
| 57 | + security: [{AmOAuth2: ['c_admin']}], |
| 58 | + paths: { |
| 59 | + '/foo': {get: {security: [{ShopperToken: ['c_x']}], responses: {'200': {description: 'ok'}}}}, |
| 60 | + }, |
| 61 | + }; |
| 62 | + assert.strictEqual(detectApiType(spec, entry('custom', 'thing')), 'Shopper'); |
| 63 | + }); |
| 64 | + |
| 65 | + test('falls back to apiName/family heuristic when no recognized scheme', () => { |
| 66 | + const spec = specWithGlobalSecurity([{UnknownScheme: []}]); |
| 67 | + assert.strictEqual(detectApiType(spec, entry('product', 'shopper-products')), 'Shopper'); |
| 68 | + assert.strictEqual(detectApiType(spec, entry('product', 'products')), 'Admin'); |
| 69 | + assert.strictEqual(detectApiType(spec, entry('shopper', 'auth')), 'Shopper'); |
| 70 | + }); |
| 71 | + |
| 72 | + test('respects info.x-api-type when present', () => { |
| 73 | + const spec: Record<string, unknown> = { |
| 74 | + info: {'x-api-type': 'Shopper'}, |
| 75 | + security: [{AmOAuth2: []}], |
| 76 | + paths: {}, |
| 77 | + }; |
| 78 | + assert.strictEqual(detectApiType(spec, entry('custom', 'override')), 'Shopper'); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +suite('injectCustomApiOrgPathPrefix', () => { |
| 83 | + test('rewrites path keys with /organizations/{organizationId} prefix', () => { |
| 84 | + const spec: Record<string, unknown> = { |
| 85 | + paths: { |
| 86 | + '/customers/{customerId}/loyalty': {get: {responses: {'200': {description: 'ok'}}}}, |
| 87 | + '/groups/{ids}': {get: {responses: {'200': {description: 'ok'}}}}, |
| 88 | + }, |
| 89 | + }; |
| 90 | + injectCustomApiOrgPathPrefix(spec); |
| 91 | + const paths = spec.paths as Record<string, unknown>; |
| 92 | + assert.deepStrictEqual(Object.keys(paths).sort(), [ |
| 93 | + '/organizations/{organizationId}/customers/{customerId}/loyalty', |
| 94 | + '/organizations/{organizationId}/groups/{ids}', |
| 95 | + ]); |
| 96 | + }); |
| 97 | + |
| 98 | + test('adds organizationId path parameter when missing', () => { |
| 99 | + const spec: Record<string, unknown> = { |
| 100 | + paths: {'/foo': {get: {responses: {'200': {description: 'ok'}}}}}, |
| 101 | + }; |
| 102 | + injectCustomApiOrgPathPrefix(spec); |
| 103 | + const item = (spec.paths as Record<string, Record<string, unknown>>)['/organizations/{organizationId}/foo']; |
| 104 | + const params = item.parameters as Array<Record<string, unknown>>; |
| 105 | + const orgParam = params.find((p) => p.name === 'organizationId'); |
| 106 | + assert.ok(orgParam, 'organizationId parameter should be added'); |
| 107 | + assert.strictEqual(orgParam.in, 'path'); |
| 108 | + assert.strictEqual(orgParam.required, true); |
| 109 | + }); |
| 110 | + |
| 111 | + test('does not duplicate organizationId parameter when already present', () => { |
| 112 | + const existing = {name: 'organizationId', in: 'path', required: true, schema: {type: 'string'}}; |
| 113 | + const spec: Record<string, unknown> = { |
| 114 | + paths: { |
| 115 | + '/foo': { |
| 116 | + parameters: [existing], |
| 117 | + get: {responses: {'200': {description: 'ok'}}}, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }; |
| 121 | + injectCustomApiOrgPathPrefix(spec); |
| 122 | + const item = (spec.paths as Record<string, Record<string, unknown>>)['/organizations/{organizationId}/foo']; |
| 123 | + const params = item.parameters as Array<Record<string, unknown>>; |
| 124 | + const orgParams = params.filter((p) => p.name === 'organizationId'); |
| 125 | + assert.strictEqual(orgParams.length, 1); |
| 126 | + }); |
| 127 | + |
| 128 | + test('is a no-op when spec has no paths', () => { |
| 129 | + const spec: Record<string, unknown> = {}; |
| 130 | + injectCustomApiOrgPathPrefix(spec); |
| 131 | + assert.strictEqual(spec.paths, undefined); |
| 132 | + }); |
| 133 | +}); |
0 commit comments