Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Roost-README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

# RoostGPT generated pytest code for API Testing

RoostGPT generats code in `tests` folder within given project path.
Dependency file i.e. `requirements-roost.txt` is also created in the given project path

Below are the sample steps to run the generated tests. Sample commands contains use of package manager i.e. `uv`. Alternatively python and pip can be used directly.
1. ( Optional ) Create virtual Env .
2. Install dependencies
```
uv venv // Create virtual Env
uv pip install -r requirements-roost.txt // Install all dependencies

```

Test configurations and test_data is loaded from config.yml. e.g. API HOST, auth, common path parameters of endpoint.
Either set defalt value in this config.yml file OR use ENV. e.g. export API_HOST="https://example.com/api/v2"

Once configuration values are set, use below commands to run the tests.
```
// Run generated tests
uv run pytest -m smoke // Run only smoke tests
uv run pytest -s tests/generated-test.py // Run specific test file
```

14 changes: 14 additions & 0 deletions requirements-roost.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

connexion
Flask
flask_testing
jsonschema
pytest
python_dateutil
PyYAML
referencing
Requests
setuptools
six
urllib3
xmltodict
51,772 changes: 51,772 additions & 0 deletions tests/MEDUSA_STOREFRONT_API/api.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"auth_provider": "google",
"statusCode": 200,
"scenario": "Successful responses: OK"
},
{
"auth_provider": "facebook",
"statusCode": 200,
"scenario": "Successful responses: OK"
},
{
"statusCode": 400,
"scenario": "Client error responses: Bad Request"
},
{
"auth_provider": "invalid_provider",
"statusCode": 400,
"scenario": "Client error responses: Bad Request"
},
{
"auth_provider": "nonexistent_provider",
"statusCode": 404,
"scenario": "Client error responses: Not Found"
}
]
27 changes: 27 additions & 0 deletions tests/MEDUSA_STOREFRONT_API/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

# This config.yml contains user provided data for api testing. Allows to define values here or use ENV to load values. e.g. ENV[API_HOST] = "https://exampl2.com"
# api:
# host: "${API_HOST:-https://example.com/api/v2}" # includes base path
# auth:
# api_key: "${API_KEY:-}"
# api_key_header: "${KEYNAME:-DefaultValue}" # openapi.spec.security.KEY_NAME
# basic_auth: "${username:-}:${password:-}"
# test_data:
# id: "${TEST_ID:-282739-1238371-219393-2833}" # Any test data key value pair e.g. GET /api/v1/cart/:id
# context-id: "${TEST_context-id:-}" # GET /api/v1/{context-id}/summary



api:
host: "${MEDUSA_STOREFRONT_API_BASE_URL:-http://localhost:9000}"
auth:
jwt_token: "${BEARER_TOKEN:-}"
cookie_auth: "${connect.sid:-}"
reset_password: "${BEARER_TOKEN:-}"
test_data:
auth_provider: "${TEST_auth_provider:-}"
id: "${TEST_id:-}"
line_id: "${TEST_line_id:-}"
code: "${TEST_code:-}"
address_id: "${TEST_address_id:-}"
idOrCode: "${TEST_idOrCode:-}"
110 changes: 110 additions & 0 deletions tests/MEDUSA_STOREFRONT_API/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import pytest
import requests
import yaml
import json
import os
import re
from pathlib import Path
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

def _expand_env_vars(value):
pattern = r"\${([^}:s]+):-([^}]+)}"
def replacer(match):
env_var, default = match.groups()
return os.getenv(env_var, default)
return re.sub(pattern, replacer, value)

def load_config():
config_path = Path(__file__).parent / 'config.yml'
if not config_path.exists():
raise FileNotFoundError("config.yml not found")

with open(config_path, 'r') as file:
config = yaml.safe_load(file)

def expand_dict(d):
for k, v in d.items():
if isinstance(v, dict):
expand_dict(v)
elif isinstance(v, str):
d[k] = _expand_env_vars(v)

expand_dict(config)
return config

@pytest.fixture(scope='session')
def config():
return load_config()

@pytest.fixture
def load_endpoint_test_data():
def _load(path):
with open(path, 'r') as file:
return json.load(file)
return _load

@pytest.fixture
def merged_test_data(config, load_endpoint_test_data):
def _merge(path):
endpoint_data = load_endpoint_test_data(path)
merged_data = config.get('test_data', {}).copy()
merged_data.update(endpoint_data)
return merged_data
return _merge

class APIClient:
def __init__(self, base_url, timeout=5, retries=3):
self.base_url = base_url.strip()
self.session = requests.Session()
retry_strategy = Retry(
total=retries,
status_forcelist=[429, 500, 502, 503, 504],
method_whitelist=["HEAD", "GET", "OPTIONS", "POST", "PUT", "PATCH", "DELETE"],
backoff_factor=1
)
adapter = HTTPAdapter(max_retries=retry_strategy)
self.session.mount("http://", adapter)
self.session.mount("https://", adapter)
self.timeout = timeout

def make_request(self, endpoint, method="GET", headers=None, **kwargs):
url = f"{self.base_url}/{endpoint.lstrip('/')}"
response = self.session.request(method, url, headers=headers, timeout=self.timeout, **kwargs)
response.raise_for_status()
return response

def get(self, endpoint, **kwargs):
return self.make_request(endpoint, "GET", **kwargs)

def post(self, endpoint, **kwargs):
return self.make_request(endpoint, "POST", **kwargs)

def put(self, endpoint, **kwargs):
return self.make_request(endpoint, "PUT", **kwargs)

def patch(self, endpoint, **kwargs):
return self.make_request(endpoint, "PATCH", **kwargs)

def delete(self, endpoint, **kwargs):
return self.make_request(endpoint, "DELETE", **kwargs)

@pytest.fixture
def api_client(config):
base_url = config['api']['host']
return APIClient(base_url)

@pytest.fixture
def get_config(config):
def _get(key):
keys = key.split('.')
value = config
for k in keys:
value = value.get(k)
if value is None:
break
return value
return _get

def pytest_configure(config):
config.addinivalue_line("markers", "smoke: mark test as smoke test")
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# ********RoostGPT********

# Test generated by RoostGPT for test testAuth using AI Type Open AI and AI Model gpt-4o
#
# Test file generated for /auth/customer/{auth_provider}/update_post for http method type POST
# RoostTestHash=87bbaf440e
#
#

# ********RoostGPT********
# ********RoostGPT********

# Test generated by RoostGPT for test testAuth using AI Type Open AI and AI Model gpt-4o
#
# Test file generated for /auth/customer/{auth_provider}/update_post for http method type POST
# RoostTestHash=87bbaf440e
#
#

# ********RoostGPT********
"""
Pytest test suite for the /auth/customer/{auth_provider}/update endpoint.
Execute with: pytest <test_file_name>.py
"""

import pytest
from pathlib import Path
from validator import SwaggerSchemaValidator

_ENDPOINT = "/auth/customer/{auth_provider}/update"
_METHOD = "post"
_TEST_DATA_FILE = "auth_customer_auth_provider_update.json"
_here = Path(__file__).resolve().parent
_endpoint_data_path = _here / _TEST_DATA_FILE

@pytest.mark.smoke
def test_auth_customer_update_success(api_client, config, merged_test_data):
validator = SwaggerSchemaValidator(str(_here / "api.json"))
data = merged_test_data(_endpoint_data_path)

for test_case in data:
auth_provider = test_case["auth_provider"]
status_code = test_case["statusCode"]
scenario = test_case["scenario"]

# Prepare request
endpoint = _ENDPOINT.format(auth_provider=auth_provider)
headers = {
"Authorization": f"Bearer {config['auth']['reset_password']}",
"Content-Type": "application/json"
}
payload = {
"email": config['test_data'].get('email', 'customer@gmail.com'),
"password": config['test_data'].get('password', 'supersecret')
}

# Validate request schema
validation_result = validator.validate_json(payload, "input")
assert validation_result["valid"], f"Request validation failed: {validation_result['message']}"

# Make API request
response = api_client.post(endpoint, headers=headers, json=payload)

# Validate response
assert response.status_code == status_code, f"Unexpected status code: {response.status_code} for scenario: {scenario}"
validation_result = validator.validate_schema_by_response(endpoint, _METHOD, str(status_code), response)
assert validation_result["valid"], f"Response validation failed: {validation_result['message']}"

def test_auth_customer_update_invalid_auth(api_client, config):
validator = SwaggerSchemaValidator(str(_here / "api.json"))
endpoint = _ENDPOINT.format(auth_provider="google")
headers = {
"Authorization": "Bearer invalid_token",
"Content-Type": "application/json"
}
payload = {
"email": config['test_data'].get('email', 'customer@gmail.com'),
"password": config['test_data'].get('password', 'supersecret')
}

# Validate request schema
validation_result = validator.validate_json(payload, "input")
assert validation_result["valid"], f"Request validation failed: {validation_result['message']}"

# Make API request
response = api_client.post(endpoint, headers=headers, json=payload)

# Validate response
assert response.status_code == 401, f"Unexpected status code: {response.status_code}"
validation_result = validator.validate_schema_by_response(endpoint, _METHOD, "401", response)
assert validation_result["valid"], f"Response validation failed: {validation_result['message']}"

def test_auth_customer_update_missing_auth(api_client, config):
validator = SwaggerSchemaValidator(str(_here / "api.json"))
endpoint = _ENDPOINT.format(auth_provider="google")
headers = {
"Content-Type": "application/json"
}
payload = {
"email": config['test_data'].get('email', 'customer@gmail.com'),
"password": config['test_data'].get('password', 'supersecret')
}

# Validate request schema
validation_result = validator.validate_json(payload, "input")
assert validation_result["valid"], f"Request validation failed: {validation_result['message']}"

# Make API request
response = api_client.post(endpoint, headers=headers, json=payload)

# Validate response
assert response.status_code == 401, f"Unexpected status code: {response.status_code}"
validation_result = validator.validate_schema_by_response(endpoint, _METHOD, "401", response)
assert validation_result["valid"], f"Response validation failed: {validation_result['message']}"
Loading