mirror of
https://github.com/correl/openapi-core.git
synced 2024-12-28 19:19:23 +00:00
Creating tests that comply with readOnly and writeOnly specifications
This commit is contained in:
parent
b6fdd64e1a
commit
baf042cbb6
3 changed files with 167 additions and 0 deletions
39
tests/integration/data/v3.0/read_only_write_only.yaml
Normal file
39
tests/integration/data/v3.0/read_only_write_only.yaml
Normal file
|
@ -0,0 +1,39 @@
|
|||
openapi: "3.0.0"
|
||||
info:
|
||||
title: Specification Containing readOnly
|
||||
version: "0.1"
|
||||
paths:
|
||||
/users:
|
||||
post:
|
||||
operationId: createUser
|
||||
requestBody:
|
||||
description: Post data for creating a user
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/User'
|
||||
responses:
|
||||
default:
|
||||
description: Create a user
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/User'
|
||||
components:
|
||||
schemas:
|
||||
User:
|
||||
type: object
|
||||
required:
|
||||
- id
|
||||
- name
|
||||
properties:
|
||||
id:
|
||||
type: integer
|
||||
format: int32
|
||||
readOnly: true
|
||||
name:
|
||||
type: string
|
||||
hidden:
|
||||
type: boolean
|
||||
writeOnly: true
|
65
tests/integration/test_read_only.py
Normal file
65
tests/integration/test_read_only.py
Normal file
|
@ -0,0 +1,65 @@
|
|||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from openapi_core.schema.media_types.exceptions import InvalidMediaTypeValue
|
||||
from openapi_core.schema.schemas.enums import UnmarshalContext
|
||||
from openapi_core.schema.schemas.exceptions import InvalidSchemaProperty
|
||||
from openapi_core.shortcuts import create_spec
|
||||
from openapi_core.validation.response.validators import ResponseValidator
|
||||
from openapi_core.validation.request.validators import RequestValidator
|
||||
from openapi_core.wrappers.mock import MockRequest, MockResponse
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def response_validator(spec):
|
||||
return ResponseValidator(spec)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def request_validator(spec):
|
||||
return RequestValidator(spec)
|
||||
|
||||
|
||||
@pytest.fixture('class')
|
||||
def spec(factory):
|
||||
spec_dict = factory.spec_from_file("data/v3.0/read_only_write_only.yaml")
|
||||
return create_spec(spec_dict)
|
||||
|
||||
|
||||
class TestReadOnly(object):
|
||||
|
||||
def test_write_a_read_only_property(self, request_validator):
|
||||
data = json.dumps({
|
||||
'id': 10,
|
||||
'name': "Pedro"
|
||||
})
|
||||
|
||||
request = MockRequest(host_url='', method='POST',
|
||||
path='/users', data=data)
|
||||
|
||||
with pytest.raises(InvalidMediaTypeValue) as ex:
|
||||
request_validator.validate(request).raise_for_errors()
|
||||
assert isinstance(ex.value.original_exception, InvalidSchemaProperty)
|
||||
ex = ex.value.original_exception
|
||||
|
||||
assert ex.property_name == 'id'
|
||||
assert UnmarshalContext.REQUEST.value in str(ex.original_exception)
|
||||
|
||||
def test_read_only_property_response(self, response_validator):
|
||||
data = json.dumps({
|
||||
'id': 10,
|
||||
'name': "Pedro"
|
||||
})
|
||||
|
||||
request = MockRequest(host_url='', method='POST',
|
||||
path='/users')
|
||||
|
||||
response = MockResponse(data)
|
||||
|
||||
is_valid = response_validator.validate(request, response)
|
||||
is_valid.raise_for_errors()
|
||||
|
||||
assert len(is_valid.errors) == 0
|
||||
assert is_valid.data.id == 10
|
||||
assert is_valid.data.name == "Pedro"
|
63
tests/integration/test_write_only.py
Normal file
63
tests/integration/test_write_only.py
Normal file
|
@ -0,0 +1,63 @@
|
|||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from openapi_core.schema.media_types.exceptions import InvalidMediaTypeValue
|
||||
from openapi_core.schema.schemas.enums import UnmarshalContext
|
||||
from openapi_core.schema.schemas.exceptions import InvalidSchemaProperty
|
||||
from openapi_core.shortcuts import create_spec
|
||||
from openapi_core.validation.response.validators import ResponseValidator
|
||||
from openapi_core.validation.request.validators import RequestValidator
|
||||
from openapi_core.wrappers.mock import MockRequest, MockResponse
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def response_validator(spec):
|
||||
return ResponseValidator(spec)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def request_validator(spec):
|
||||
return RequestValidator(spec)
|
||||
|
||||
|
||||
@pytest.fixture('class')
|
||||
def spec(factory):
|
||||
spec_dict = factory.spec_from_file("data/v3.0/read_only_write_only.yaml")
|
||||
return create_spec(spec_dict)
|
||||
|
||||
|
||||
class TestWriteOnly(object):
|
||||
|
||||
def test_write_only_property(self, request_validator):
|
||||
data = json.dumps({
|
||||
'name': "Pedro",
|
||||
'hidden': False
|
||||
})
|
||||
|
||||
request = MockRequest(host_url='', method='POST',
|
||||
path='/users', data=data)
|
||||
|
||||
is_valid = request_validator.validate(request)
|
||||
is_valid.raise_for_errors()
|
||||
assert is_valid.body.name == "Pedro"
|
||||
assert is_valid.body.hidden is False
|
||||
|
||||
def test_read_a_write_only_property(self, response_validator):
|
||||
data = json.dumps({
|
||||
'id': 10,
|
||||
'name': "Pedro",
|
||||
'hidden': True
|
||||
})
|
||||
|
||||
request = MockRequest(host_url='', method='POST',
|
||||
path='/users')
|
||||
response = MockResponse(data)
|
||||
|
||||
with pytest.raises(InvalidMediaTypeValue) as ex:
|
||||
response_validator.validate(request, response).raise_for_errors()
|
||||
assert isinstance(ex.value.original_exception, InvalidSchemaProperty)
|
||||
ex = ex.value.original_exception
|
||||
|
||||
assert ex.property_name == 'hidden'
|
||||
assert UnmarshalContext.RESPONSE.value in str(ex.original_exception)
|
Loading…
Reference in a new issue