Skip to content

Initial tests #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
Empty file added tests/__init__.py
Empty file.
92 changes: 92 additions & 0 deletions tests/test_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import json
import tempfile
from unittest import mock

import pytest
from openapi_schema_validator import ValidationError

from your_module import har_to_openapi, validate_openapi_spec


def test_har_to_openapi():
# Create a sample HAR file
har_data = {
"entries": [
{
"request": {
"method": "GET",
"url": "https://api.example.com/users",
},
"response": {
"status": 200,
"content": "OK",
},
},
{
"request": {
"method": "POST",
"url": "https://api.example.com/users",
},
"response": {
"status": 201,
"content": "Created",
},
},
]
}

# Write the sample HAR data to a temporary file
with tempfile.NamedTemporaryFile(mode="w", delete=False) as temp_file:
json.dump(har_data, temp_file)
temp_file_path = temp_file.name

# Call the har_to_openapi function with the temporary file path
openapi_spec = har_to_openapi(temp_file_path)

# Assert the expected OpenAPI spec structure
assert "openapi" in openapi_spec
assert "info" in openapi_spec
assert "paths" in openapi_spec
assert "/users" in openapi_spec["paths"]
assert "get" in openapi_spec["paths"]["/users"]
assert "post" in openapi_spec["paths"]["/users"]
assert "responses" in openapi_spec["paths"]["/users"]["get"]
assert "responses" in openapi_spec["paths"]["/users"]["post"]
assert 200 in openapi_spec["paths"]["/users"]["get"]["responses"]
assert 201 in openapi_spec["paths"]["/users"]["post"]["responses"]


@mock.patch("requests.get")
def test_validate_openapi_spec_valid(mock_get):
# Create a valid OpenAPI spec
openapi_spec = {
"openapi": "3.0.0",
"info": {
"title": "Sample API",
"version": "1.0.0",
},
"paths": {},
}

# Mock the requests.get response
mock_get.return_value.json.return_value = {"valid": "schema"}

# Call the validate_openapi_spec function
validate_openapi_spec(openapi_spec)

# No assertion needed as the function should not raise an exception


@mock.patch("requests.get")
def test_validate_openapi_spec_invalid(mock_get):
# Create an invalid OpenAPI spec
openapi_spec = {
"invalid": "spec",
}

# Mock the requests.get response
mock_get.return_value.json.return_value = {"valid": "schema"}

# Call the validate_openapi_spec function and expect a ValidationError
with pytest.raises(ValidationError):
validate_openapi_spec(openapi_spec)