fix: add Gitea Actions CI workflow for automated testing
This commit is contained in:
129
templates/go/api_test.go.j2
Normal file
129
templates/go/api_test.go.j2
Normal file
@@ -0,0 +1,129 @@
|
||||
package {{ package_name }}
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
baseURL = "{{ mock_server_url }}"
|
||||
testTimeout = 10 * time.Second
|
||||
)
|
||||
|
||||
{% if security_schemes %}
|
||||
{% for scheme_name, scheme in security_schemes.items() %}
|
||||
{% if scheme.type == "apiKey" %}
|
||||
func get{{ scheme_name|capitalize }}Headers() map[string]string {
|
||||
return map[string]string{
|
||||
"{{ scheme.name }}": os.Getenv("API_KEY"),
|
||||
}
|
||||
}
|
||||
|
||||
{% elif scheme.type == "http" and scheme.scheme == "bearer" %}
|
||||
func getBearerHeaders() map[string]string {
|
||||
return map[string]string{
|
||||
"Authorization": fmt.Sprintf("Bearer %s", os.Getenv("TOKEN")),
|
||||
}
|
||||
}
|
||||
|
||||
{% elif scheme.type == "http" and scheme.scheme == "basic" %}
|
||||
func getBasicHeaders(username, password string) map[string]string {
|
||||
auth := username + ":" + password
|
||||
encoded := base64.StdEncoding.EncodeToString([]byte(auth))
|
||||
return map[string]string{
|
||||
"Authorization": "Basic " + encoded,
|
||||
}
|
||||
}
|
||||
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
func contains(slice []int, item int) bool {
|
||||
for _, s := range slice {
|
||||
if s == item {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
{% for path, path_endpoints in grouped_endpoints.items() %}
|
||||
{% for endpoint in path_endpoints %}
|
||||
{% set test_name = (endpoint.method + "_" + path.strip("/").replace("/", "_").replace("{", "").replace("}", "")).title().replace("_", "") %}
|
||||
func Test{{ test_name }}(t *testing.T) {
|
||||
client := &http.Client{Timeout: testTimeout}
|
||||
url := baseURL + "{{ path }}"
|
||||
|
||||
{% for param in endpoint.parameters %}
|
||||
{% if param.in == "path" %}
|
||||
url = strings.Replace(url, "{+{{ param.name }}}", "{{ param.name }}", 1)
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
|
||||
{% if endpoint.method in ["post", "put", "patch"] %}
|
||||
body := `{}`
|
||||
req, err := http.NewRequest("{{ endpoint.method.upper() }}", url, strings.NewReader(body))
|
||||
{% else %}
|
||||
req, err := http.NewRequest("{{ endpoint.method.upper() }}", url, nil)
|
||||
{% endif %}
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create request: %v", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
{% if endpoint.security %}
|
||||
{% set scheme_name = endpoint.security[0].keys()|first %}
|
||||
{% set scheme = security_schemes[scheme_name] %}
|
||||
{% if scheme.type == "apiKey" %}
|
||||
for k, v := range get{{ scheme_name|capitalize }}Headers() {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
{% elif scheme.type == "http" and scheme.scheme == "bearer" %}
|
||||
for k, v := range getBearerHeaders() {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("Request failed: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if !contains([]int{200, 201, 204}, resp.StatusCode) {
|
||||
t.Errorf("Expected status in [200, 201, 204], got %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
if resp.StatusCode == 200 || resp.StatusCode == 201 {
|
||||
var data interface{}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
|
||||
t.Errorf("Failed to decode response: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
|
||||
func TestAPIHealth(t *testing.T) {
|
||||
client := &http.Client{Timeout: testTimeout}
|
||||
|
||||
resp, err := client.Get(baseURL + "/health")
|
||||
if err != nil {
|
||||
t.Fatalf("Health check failed: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
t.Errorf("Expected status 200, got %d", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
101
templates/jest/api.test.js.j2
Normal file
101
templates/jest/api.test.js.j2
Normal 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 %}
|
||||
});
|
||||
115
templates/pytest/test_base.py.j2
Normal file
115
templates/pytest/test_base.py.j2
Normal file
@@ -0,0 +1,115 @@
|
||||
"""
|
||||
Generated pytest tests for {{ api_title }} API v{{ api_version }}
|
||||
|
||||
This file was auto-generated by API TestGen.
|
||||
"""
|
||||
import pytest
|
||||
import requests
|
||||
import json
|
||||
from jsonschema import validate, ValidationError
|
||||
|
||||
|
||||
BASE_URL = "{{ mock_server_url }}"
|
||||
|
||||
|
||||
{% if security_schemes %}
|
||||
{% for scheme_name, scheme in security_schemes.items() %}
|
||||
{% if scheme.type == "apiKey" %}
|
||||
@pytest.fixture
|
||||
def {{ scheme_name }}_headers():
|
||||
"""API Key authentication headers."""
|
||||
return {"{{ scheme.name }}": "your-api-key"}
|
||||
|
||||
|
||||
{% elif scheme.type == "http" and scheme.scheme == "bearer" %}
|
||||
@pytest.fixture
|
||||
def bearer_headers():
|
||||
"""Bearer token authentication headers."""
|
||||
return {"Authorization": "Bearer your-token"}
|
||||
|
||||
|
||||
{% elif scheme.type == "http" and scheme.scheme == "basic" %}
|
||||
@pytest.fixture
|
||||
def basic_headers():
|
||||
"""Basic authentication headers."""
|
||||
import base64
|
||||
credentials = "username:password"
|
||||
encoded = base64.b64encode(credentials.encode()).decode()
|
||||
return {"Authorization": f"Basic {encoded}"}
|
||||
|
||||
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
|
||||
{% if definitions %}
|
||||
@pytest.fixture
|
||||
def base_url():
|
||||
"""Base URL for API requests."""
|
||||
return BASE_URL
|
||||
|
||||
|
||||
{% endif %}
|
||||
def validate_response(response, status_code=None):
|
||||
"""Validate API response.
|
||||
|
||||
Args:
|
||||
response: The response object.
|
||||
status_code: Expected status code (optional).
|
||||
"""
|
||||
if status_code:
|
||||
assert response.status_code == status_code, \
|
||||
f"Expected status {status_code}, got {response.status_code}"
|
||||
|
||||
assert response.headers.get("Content-Type", "").startswith("application/json"), \
|
||||
"Response Content-Type is not JSON"
|
||||
|
||||
|
||||
{% for endpoint in endpoints %}
|
||||
{% set endpoint_id = endpoint.operation_id or (endpoint.method + "_" + endpoint.path.strip("/").replace("/", "_").replace("{", "").replace("}", "")) %}
|
||||
def test_{{ endpoint_id }}(base_url{% if endpoint.parameters|selectattr("in", "equalto", "path")|list %}, {% for param in endpoint.parameters %}{% if param.in == "path" %}{{ param.name }}{% endif %}{% endfor %}{% endif %}):
|
||||
"""Test {{ endpoint.summary or endpoint.path }} endpoint.
|
||||
|
||||
{{ endpoint.description or "" }}
|
||||
"""
|
||||
url = f"{base_url}{{ endpoint.path }}"
|
||||
|
||||
headers = {"Content-Type": "application/json"}
|
||||
|
||||
{% if endpoint.security %}
|
||||
{% for security_requirement in endpoint.security %}
|
||||
{% for scheme_name in security_requirement.keys() %}
|
||||
{% if security_schemes[scheme_name].type == "apiKey" %}
|
||||
headers["{{ security_schemes[scheme_name].name }}"] = "test-api-key"
|
||||
{% elif security_schemes[scheme_name].type == "http" and security_schemes[scheme_name].scheme == "bearer" %}
|
||||
headers["Authorization"] = "Bearer test-token"
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
{% if endpoint.method in ["post", "put", "patch"] %}
|
||||
{% if endpoint.request_body %}
|
||||
request_body = {}
|
||||
{% else %}
|
||||
request_body = {}
|
||||
{% endif %}
|
||||
|
||||
response = requests.{{ endpoint["method"] }}(url, json=request_body, headers=headers)
|
||||
{% else %}
|
||||
response = requests.{{ endpoint["method"] }}(url, headers=headers{% if endpoint.parameters|selectattr("in", "equalto", "query")|list %}{% set query_params = endpoint.parameters|selectattr("in", "equalto", "query")|list %}{% if query_params %}, params={% for param in query_params %}"{{ param.name }}": "test"{% endfor %}{% endif %}{% endif %})
|
||||
{% endif %}
|
||||
|
||||
validate_response(response{% if endpoint.responses["200"] %}, {{ endpoint.responses["200"] }}{% endif %})
|
||||
|
||||
{% if endpoint.responses %}
|
||||
{% if endpoint.responses["200"] and endpoint.responses["200"].schema %}
|
||||
try:
|
||||
data = response.json()
|
||||
except json.JSONDecodeError:
|
||||
pytest.fail("Response is not valid JSON")
|
||||
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
Reference in New Issue
Block a user