|
| 1 | +""" Test the pyodata integration with aiohttp client, based on asyncio |
| 2 | +
|
| 3 | +https://docs.aiohttp.org/en/stable/ |
| 4 | +""" |
| 5 | +import aiohttp |
| 6 | +from aiohttp import web |
| 7 | +import pytest |
| 8 | + |
| 9 | +import pyodata.v2.service |
| 10 | +from pyodata import Client |
| 11 | +from pyodata.exceptions import PyODataException, HttpError |
| 12 | +from pyodata.v2.model import ParserError, PolicyWarning, PolicyFatal, PolicyIgnore, Config |
| 13 | + |
| 14 | +SERVICE_URL = '' |
| 15 | + |
| 16 | +@pytest.mark.asyncio |
| 17 | +async def test_invalid_odata_version(): |
| 18 | + """Check handling of request for invalid OData version implementation""" |
| 19 | + |
| 20 | + with pytest.raises(PyODataException) as e_info: |
| 21 | + async with aiohttp.ClientSession() as client: |
| 22 | + await Client.build_async_client(SERVICE_URL, client, 'INVALID VERSION') |
| 23 | + |
| 24 | + assert str(e_info.value).startswith('No implementation for selected odata version') |
| 25 | + |
| 26 | +@pytest.mark.asyncio |
| 27 | +async def test_create_client_for_local_metadata(metadata): |
| 28 | + """Check client creation for valid use case with local metadata""" |
| 29 | + |
| 30 | + async with aiohttp.ClientSession() as client: |
| 31 | + service_client = await Client.build_async_client(SERVICE_URL, client, metadata=metadata) |
| 32 | + |
| 33 | + assert isinstance(service_client, pyodata.v2.service.Service) |
| 34 | + assert service_client.schema.is_valid == True |
| 35 | + |
| 36 | + assert len(service_client.schema.entity_sets) != 0 |
| 37 | + |
| 38 | +@pytest.mark.asyncio |
| 39 | +def generate_metadata_response(headers=None, body=None, status=200): |
| 40 | + |
| 41 | + async def metadata_response(request): |
| 42 | + return web.Response(status=status, headers=headers, body=body) |
| 43 | + |
| 44 | + return metadata_response |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize("content_type", ['application/xml', 'application/atom+xml', 'text/xml']) |
| 48 | +@pytest.mark.asyncio |
| 49 | +async def test_create_service_application(aiohttp_client, metadata, content_type): |
| 50 | + """Check client creation for valid MIME types""" |
| 51 | + |
| 52 | + app = web.Application() |
| 53 | + app.router.add_get('/$metadata', generate_metadata_response(headers={'content-type': content_type}, body=metadata)) |
| 54 | + client = await aiohttp_client(app) |
| 55 | + |
| 56 | + service_client = await Client.build_async_client(SERVICE_URL, client) |
| 57 | + |
| 58 | + assert isinstance(service_client, pyodata.v2.service.Service) |
| 59 | + |
| 60 | + # one more test for '/' terminated url |
| 61 | + |
| 62 | + service_client = await Client.build_async_client(SERVICE_URL + '/', client) |
| 63 | + |
| 64 | + assert isinstance(service_client, pyodata.v2.service.Service) |
| 65 | + assert service_client.schema.is_valid |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.asyncio |
| 69 | +async def test_metadata_not_reachable(aiohttp_client): |
| 70 | + """Check handling of not reachable service metadata""" |
| 71 | + |
| 72 | + app = web.Application() |
| 73 | + app.router.add_get('/$metadata', generate_metadata_response(headers={'content-type': 'text/html'}, status=404)) |
| 74 | + client = await aiohttp_client(app) |
| 75 | + |
| 76 | + with pytest.raises(HttpError) as e_info: |
| 77 | + await Client.build_async_client(SERVICE_URL, client) |
| 78 | + |
| 79 | + assert str(e_info.value).startswith('Metadata request failed') |
| 80 | + |
| 81 | +@pytest.mark.asyncio |
| 82 | +async def test_metadata_saml_not_authorized(aiohttp_client): |
| 83 | + """Check handling of not SAML / OAuth unauthorized response""" |
| 84 | + |
| 85 | + app = web.Application() |
| 86 | + app.router.add_get('/$metadata', generate_metadata_response(headers={'content-type': 'text/html; charset=utf-8'})) |
| 87 | + client = await aiohttp_client(app) |
| 88 | + |
| 89 | + with pytest.raises(HttpError) as e_info: |
| 90 | + await Client.build_async_client(SERVICE_URL, client) |
| 91 | + |
| 92 | + assert str(e_info.value).startswith('Metadata request did not return XML, MIME type:') |
| 93 | + |
| 94 | + |
| 95 | +@pytest.mark.asyncio |
| 96 | +async def test_client_custom_configuration(aiohttp_client, metadata): |
| 97 | + """Check client creation for custom configuration""" |
| 98 | + |
| 99 | + namespaces = { |
| 100 | + 'edmx': "customEdmxUrl.com", |
| 101 | + 'edm': 'customEdmUrl.com' |
| 102 | + } |
| 103 | + |
| 104 | + custom_config = Config( |
| 105 | + xml_namespaces=namespaces, |
| 106 | + default_error_policy=PolicyFatal(), |
| 107 | + custom_error_policies={ |
| 108 | + ParserError.ANNOTATION: PolicyWarning(), |
| 109 | + ParserError.ASSOCIATION: PolicyIgnore() |
| 110 | + }) |
| 111 | + |
| 112 | + app = web.Application() |
| 113 | + app.router.add_get('/$metadata', |
| 114 | + generate_metadata_response(headers={'content-type': 'application/xml'}, body=metadata)) |
| 115 | + client = await aiohttp_client(app) |
| 116 | + |
| 117 | + with pytest.raises(PyODataException) as e_info: |
| 118 | + await Client.build_async_client(SERVICE_URL, client, config=custom_config, namespaces=namespaces) |
| 119 | + |
| 120 | + assert str(e_info.value) == 'You cannot pass namespaces and config at the same time' |
| 121 | + |
| 122 | + with pytest.warns(DeprecationWarning,match='Passing namespaces directly is deprecated. Use class Config instead'): |
| 123 | + service = await Client.build_async_client(SERVICE_URL, client, namespaces=namespaces) |
| 124 | + |
| 125 | + assert isinstance(service, pyodata.v2.service.Service) |
| 126 | + assert service.schema.config.namespaces == namespaces |
| 127 | + |
| 128 | + service = await Client.build_async_client(SERVICE_URL, client, config=custom_config) |
| 129 | + |
| 130 | + assert isinstance(service, pyodata.v2.service.Service) |
| 131 | + assert service.schema.config == custom_config |
0 commit comments