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