@apistudio/apim-cli
Version:
CLI for API Management Products
186 lines (157 loc) • 5.18 kB
JavaScript
import { WMGWRuntimeInventory } from './dist/index.js';
import fs from 'fs';
// Initialize the inventory
const inventory = new WMGWRuntimeInventory();
// Test case with useIncoming as an empty object
const testWithEmptyUseIncoming = {
apiVersion: "api.ibm.com/v1",
kind: "SetAuthorization",
metadata: {
name: "test-setauthorization"
},
spec: {
useIncoming: {}
}
};
// Test case with static authorization
const testWithStaticAuth = {
apiVersion: "api.ibm.com/v1",
kind: "SetAuthorization",
metadata: {
name: "test-setauthorization"
},
spec: {
static: {
type: "basic",
username: "testuser",
password: "testpass"
}
}
};
// Test case with both (should fail)
const testWithBoth = {
apiVersion: "api.ibm.com/v1",
kind: "SetAuthorization",
metadata: {
name: "test-setauthorization"
},
spec: {
useIncoming: {},
static: {
type: "basic",
username: "testuser",
password: "testpass"
}
}
};
// Test case with neither (should fail)
const testWithNeither = {
apiVersion: "api.ibm.com/v1",
kind: "SetAuthorization",
metadata: {
name: "test-setauthorization"
},
spec: {}
};
// Test case with useIncoming as a boolean (should fail)
const testWithInvalidUseIncoming = {
apiVersion: "api.ibm.com/v1",
kind: "SetAuthorization",
metadata: {
name: "test-setauthorization"
},
spec: {
useIncoming: true
}
};
// Write test cases to files for manual validation
function writeTestCases() {
const testDir = './test-cases';
// Create test directory if it doesn't exist
if (!fs.existsSync(testDir)) {
fs.mkdirSync(testDir);
}
// Write each test case to a file
fs.writeFileSync(`${testDir}/test-empty-useincoming.json`, JSON.stringify(testWithEmptyUseIncoming, null, 2));
fs.writeFileSync(`${testDir}/test-static-auth.json`, JSON.stringify(testWithStaticAuth, null, 2));
fs.writeFileSync(`${testDir}/test-both.json`, JSON.stringify(testWithBoth, null, 2));
fs.writeFileSync(`${testDir}/test-neither.json`, JSON.stringify(testWithNeither, null, 2));
fs.writeFileSync(`${testDir}/test-invalid-useincoming.json`, JSON.stringify(testWithInvalidUseIncoming, null, 2));
console.log(`Test cases written to ${testDir} directory`);
}
// Validate a resource against its schema
function validateResource(resource) {
try {
const kind = resource.kind;
const schema = inventory.getSchema(kind);
if (!schema) {
return [`Schema not found for kind: ${kind}`];
}
// Basic validation checks
const errors = [];
// Check required top-level fields
['apiVersion', 'kind', 'metadata', 'spec'].forEach(field => {
if (!resource[field]) {
errors.push(`Missing required field: ${field}`);
}
});
// Check spec structure based on the specific test case
const spec = resource.spec;
// For SetAuthorization, check oneOf constraint (either useIncoming or static, not both, not neither)
if (kind === 'SetAuthorization') {
const hasUseIncoming = spec.hasOwnProperty('useIncoming');
const hasStatic = spec.hasOwnProperty('static');
if (hasUseIncoming && hasStatic) {
errors.push('Cannot have both useIncoming and static properties');
} else if (!hasUseIncoming && !hasStatic) {
errors.push('Must have either useIncoming or static property');
}
// Check useIncoming is an object if present
if (hasUseIncoming && typeof spec.useIncoming !== 'object') {
errors.push('useIncoming must be an object');
}
// Check static structure if present
if (hasStatic) {
if (!spec.static.type) {
errors.push('static.type is required');
}
if (spec.static.type === 'basic') {
if (!spec.static.username) {
errors.push('static.username is required for basic auth');
}
if (!spec.static.password) {
errors.push('static.password is required for basic auth');
}
}
}
}
return errors;
} catch (error) {
return [`Error during validation: ${error.message}`];
}
}
// Run the tests
console.log("=== Testing SetAuthorization Schema Validation ===\n");
function runTest(testCase, description) {
console.log(`Testing: ${description}`);
const errors = validateResource(testCase);
if (errors.length === 0) {
console.log("✅ Validation passed\n");
} else {
console.log("❌ Validation failed:");
errors.forEach(error => {
console.log(` - ${error}`);
});
console.log("");
}
}
// Run all tests
runTest(testWithEmptyUseIncoming, "useIncoming as an empty object (should pass)");
runTest(testWithStaticAuth, "static authorization (should pass)");
runTest(testWithBoth, "both useIncoming and static (should fail)");
runTest(testWithNeither, "neither useIncoming nor static (should fail)");
runTest(testWithInvalidUseIncoming, "useIncoming as a boolean (should fail)");
// Write test cases to files for manual validation if needed
// writeTestCases();
console.log("=== Test Complete ===");
// Made with Bob