|
| 1 | +"""PyOData Client tests""" |
| 2 | +from unittest.mock import patch, AsyncMock |
| 3 | + |
| 4 | +import aiohttp |
| 5 | +import pytest |
| 6 | +import requests.adapters |
| 7 | + |
| 8 | +import pyodata.v2.service |
| 9 | +from pyodata import Client |
| 10 | +from pyodata.exceptions import PyODataException, HttpError |
| 11 | +from pyodata.v2.model import ParserError, PolicyWarning, PolicyFatal, PolicyIgnore, Config |
| 12 | + |
| 13 | +SERVICE_URL = 'http://example.com' |
| 14 | + |
| 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 | + |
| 27 | +@pytest.mark.asyncio |
| 28 | +async def test_create_client_for_local_metadata(metadata): |
| 29 | + """Check client creation for valid use case with local metadata""" |
| 30 | + |
| 31 | + async with aiohttp.ClientSession() as client: |
| 32 | + service_client = await Client.build_async_client(SERVICE_URL, client, metadata=metadata) |
| 33 | + |
| 34 | + assert isinstance(service_client, pyodata.v2.service.Service) |
| 35 | + assert service_client.schema.is_valid == True |
| 36 | + |
| 37 | + assert len(service_client.schema.entity_sets) != 0 |
| 38 | + |
| 39 | + |
| 40 | +@patch("pyodata.client._async_fetch_metadata") |
| 41 | +@pytest.mark.parametrize("content_type", ['application/xml', 'application/atom+xml', 'text/xml']) |
| 42 | +@pytest.mark.asyncio |
| 43 | +async def test_create_service_application(mock_fetch_metadata, metadata, content_type): |
| 44 | + """Check client creation for valid MIME types""" |
| 45 | + mock_fetch_metadata.return_value = metadata |
| 46 | + |
| 47 | + async with aiohttp.ClientSession() as client: |
| 48 | + service_client = await Client.build_async_client(SERVICE_URL, client) |
| 49 | + |
| 50 | + assert isinstance(service_client, pyodata.v2.service.Service) |
| 51 | + |
| 52 | + # one more test for '/' terminated url |
| 53 | + |
| 54 | + service_client = await Client.build_async_client(SERVICE_URL + '/', requests) |
| 55 | + |
| 56 | + assert isinstance(service_client, pyodata.v2.service.Service) |
| 57 | + assert service_client.schema.is_valid |
| 58 | + |
| 59 | + |
| 60 | +@patch("aiohttp.client.ClientSession.get") |
| 61 | +@pytest.mark.asyncio |
| 62 | +async def test_metadata_not_reachable(mock): |
| 63 | + """Check handling of not reachable service metadata""" |
| 64 | + |
| 65 | + response = AsyncMock() |
| 66 | + response.status = 404 |
| 67 | + response.headers = {'content-type': 'text/html'} |
| 68 | + mock.return_value.__aenter__.return_value = response |
| 69 | + |
| 70 | + with pytest.raises(HttpError) as e_info: |
| 71 | + async with aiohttp.ClientSession() as client: |
| 72 | + await Client.build_async_client(SERVICE_URL, client) |
| 73 | + |
| 74 | + assert str(e_info.value).startswith('Metadata request failed') |
| 75 | + |
| 76 | + |
| 77 | +@patch("aiohttp.client.ClientSession.get") |
| 78 | +@pytest.mark.asyncio |
| 79 | +async def test_metadata_saml_not_authorized(mock): |
| 80 | + """Check handling of not SAML / OAuth unauthorized response""" |
| 81 | + |
| 82 | + response = AsyncMock() |
| 83 | + response.status = 200 |
| 84 | + response.headers = {'content-type': 'text/html; charset=utf-8'} |
| 85 | + mock.return_value.__aenter__.return_value = response |
| 86 | + |
| 87 | + with pytest.raises(HttpError) as e_info: |
| 88 | + async with aiohttp.ClientSession() as client: |
| 89 | + await Client.build_async_client(SERVICE_URL, client) |
| 90 | + |
| 91 | + assert str(e_info.value).startswith('Metadata request did not return XML, MIME type:') |
| 92 | + |
| 93 | + |
| 94 | +@patch("pyodata.client._async_fetch_metadata") |
| 95 | +@patch('warnings.warn') |
| 96 | +@pytest.mark.asyncio |
| 97 | +async def test_client_custom_configuration(mock_warning, mock_fetch_metadata, metadata): |
| 98 | + """Check client creation for custom configuration""" |
| 99 | + |
| 100 | + mock_fetch_metadata.return_value = metadata |
| 101 | + |
| 102 | + namespaces = { |
| 103 | + 'edmx': "customEdmxUrl.com", |
| 104 | + 'edm': 'customEdmUrl.com' |
| 105 | + } |
| 106 | + |
| 107 | + custom_config = Config( |
| 108 | + xml_namespaces=namespaces, |
| 109 | + default_error_policy=PolicyFatal(), |
| 110 | + custom_error_policies={ |
| 111 | + ParserError.ANNOTATION: PolicyWarning(), |
| 112 | + ParserError.ASSOCIATION: PolicyIgnore() |
| 113 | + }) |
| 114 | + |
| 115 | + with pytest.raises(PyODataException) as e_info: |
| 116 | + async with aiohttp.ClientSession() as client: |
| 117 | + await Client.build_async_client(SERVICE_URL, client, config=custom_config, namespaces=namespaces) |
| 118 | + |
| 119 | + assert str(e_info.value) == 'You cannot pass namespaces and config at the same time' |
| 120 | + |
| 121 | + async with aiohttp.ClientSession() as client: |
| 122 | + service = await Client.build_async_client(SERVICE_URL, client, namespaces=namespaces) |
| 123 | + |
| 124 | + mock_warning.assert_called_with( |
| 125 | + 'Passing namespaces directly is deprecated. Use class Config instead', |
| 126 | + DeprecationWarning |
| 127 | + ) |
| 128 | + assert isinstance(service, pyodata.v2.service.Service) |
| 129 | + assert service.schema.config.namespaces == namespaces |
| 130 | + |
| 131 | + async with aiohttp.ClientSession() as client: |
| 132 | + service = await Client.build_async_client(SERVICE_URL, client, config=custom_config) |
| 133 | + |
| 134 | + assert isinstance(service, pyodata.v2.service.Service) |
| 135 | + assert service.schema.config == custom_config |
0 commit comments