Overview
Custom test templates allow you to:- Create reusable test patterns for your specific API security needs
- Auto-generate tests from endpoint metadata
- Standardize security testing across your organization
- Extend Metlo’s built-in templates with custom logic
Template Structure
A test template is a TypeScript or JavaScript module that exports:- name - Template identifier
- version - Template version number
- builder - Function that generates a test configuration
Setting Up Custom Templates
Initialize Template Project
Create a new custom template project:This creates:
package.jsonwith@metlo/testingdependencytemplates/directory for your templates
Template Builder API
TestBuilder
TheTestBuilder class helps construct test configurations:
TestStepBuilder
Build individual test steps:Built-in Template Examples
Broken Authentication Template
Tests that authentication is properly enforced:BOLA (Broken Object Level Authorization) Template
Tests that users can’t access other users’ resources:Custom Template Examples
Rate Limiting Test
Input Validation Test
Custom Header Validation
Using Templates
Generate Test from Template
Generate Without Saving
Print the generated test to stdout:Generate with Specific Version
Template Configuration
TheTemplateConfig object provides access to:
- authConfig - Authentication configuration per host
- userConfig - User credentials for testing
- entityMapping - Entity ID mappings for BOLA tests
Validation
Metlo validates custom templates to ensure they:- Export a default object
- Include
name,version, andbuilderproperties - Return a valid
TestConfigfrom the builder
Best Practices
Version your templates
Version your templates
Increment version numbers when making changes:
Use descriptive names
Use descriptive names
Choose clear template names that indicate what they test:
Add detailed assertions
Add detailed assertions
Include descriptions to make failures clear:
Handle missing configuration gracefully
Handle missing configuration gracefully
Check for required configuration and throw helpful errors:
Use TypeScript for better type safety
Use TypeScript for better type safety
TypeScript catches errors before runtime:
Sharing Templates
Share templates with your team:- Version Control - Commit templates to your repository
- NPM Package - Publish as an npm package for easy distribution
- Documentation - Document what each template tests and when to use it
Next Steps
Writing Tests
Learn the YAML test format in detail
Running Tests
Execute your generated tests