Add Jinja2 templates for test generation
Some checks failed
CI / test (push) Has been cancelled
CI / build (push) Has been cancelled

This commit is contained in:
2026-02-06 04:50:24 +00:00
parent 2dacd28aee
commit 0b77e2c1ec

View File

@@ -0,0 +1,101 @@
/**
* Generated Jest tests for {{ api_title }} API v{{ api_version }}
*
* This file was auto-generated by API TestGen.
*/
const request = require('supertest');
const BASE_URL = process.env.MOCK_SERVER_URL || '{{ mock_server_url }}';
{% if security_schemes %}
{% for scheme_name, scheme in security_schemes.items() %}
{% if scheme.type == "apiKey" %}
const get{{ scheme_name|capitalize }}Headers = () => ({{
"{{ scheme.name }}": process.env.API_KEY || 'your-api-key',
}});
{% elif scheme.type == "http" and scheme.scheme == "bearer" %}
const getBearerHeaders = () => ({{
Authorization: `Bearer ${{process.env.TOKEN || 'your-token'}}`,
}});
{% elif scheme.type == "http" and scheme.scheme == "basic" %}
const getBasicHeaders = () => {{
const credentials = Buffer.from(`${{process.env.USERNAME || 'username'}}:${{process.env.PASSWORD || 'password'}}`).toString('base64');
return {{ Authorization: `Basic ${{credentials}}` }};
}};
{% endif %}
{% endfor %}
{% endif %}
const validateResponse = (response, expectedStatus) => {{
expect(response.headers['content-type']).toMatch(/application\\/json/);
if (expectedStatus) {{
expect(response.status).toBe(expectedStatus);
}}
}};
describe('{{ api_title }} API', () => {{
{% for endpoint in endpoints %}
{% set endpoint_id = endpoint.operation_id or (endpoint.method + "_" + endpoint.path.strip("/").replace("/", "_").replace("{", "").replace("}", "")) %}
describe('{{ endpoint.method.upper() }} {{ endpoint.path }}', () => {{
{{ endpoint.description or "" }}
it('should return valid response', async () => {{
{% if endpoint.security %}
{% set scheme_name = endpoint.security[0].keys()|first %}
{% set scheme = security_schemes[scheme_name] %}
{% if scheme.type == "apiKey" %}
const headers = get{{ scheme_name|capitalize }}Headers();
{% elif scheme.type == "http" and scheme.scheme == "bearer" %}
const headers = getBearerHeaders();
{% elif scheme.type == "http" and scheme.scheme == "basic" %}
const headers = getBasicHeaders();
{% else %}
const headers = {{}};
{% endif %}
{% else %}
const headers = {{ 'Content-Type': 'application/json' }};
{% endif %}
{% if endpoint.method in ["post", "put", "patch"] %}
const body = {{}};
const response = await request(BASE_URL)
.{{ endpoint["method"] }}('{{ endpoint["path"] }}')
.send(body)
.set(headers);
{% else %}
{% if endpoint.parameters|selectattr("in", "equalto", "query")|list %}
{% set query_params = endpoint.parameters|selectattr("in", "equalto", "query")|list %}
const queryParams = {{
{% for param in query_params %}
{{ param.name }}: 'test'{% if not loop.last %},
{% endif %}
{% endfor %}
}};
const response = await request(BASE_URL)
.{{ endpoint["method"] }}('{{ endpoint["path"] }}')
.query(queryParams)
.set(headers);
{% else %}
const response = await request(BASE_URL)
.{{ endpoint["method"] }}('{{ endpoint["path"] }}')
.set(headers);
{% endif %}
{% endif %}
expect([200, 201, 204]).toContain(response.status);
{% if endpoint.responses["200"] %}
validateResponse(response, {{ endpoint.responses["200"] }});
{% else %}
validateResponse(response);
{% endif %}
}});
}});
{% endfor %}
}});