From 0b77e2c1ecd062ed31c45755adf192ba16aee67b Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Fri, 6 Feb 2026 04:50:24 +0000 Subject: [PATCH] Add Jinja2 templates for test generation --- templates/jest/api.test.js.j2 | 101 ++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 templates/jest/api.test.js.j2 diff --git a/templates/jest/api.test.js.j2 b/templates/jest/api.test.js.j2 new file mode 100644 index 0000000..bf8bf64 --- /dev/null +++ b/templates/jest/api.test.js.j2 @@ -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 %} +}});