|
5 | 5 | import pytest
|
6 | 6 | from pyodata.exceptions import PyODataException, HttpError
|
7 | 7 | from pyodata.vendor import SAP
|
| 8 | +import responses |
| 9 | +import requests |
| 10 | +import json |
8 | 11 |
|
9 | 12 |
|
10 | 13 | class MockResponse(NamedTuple):
|
@@ -183,3 +186,88 @@ def test_vendor_http_error(response_with_error):
|
183 | 186 | sap_error = HttpError('Another foo bar', response_with_error)
|
184 | 187 | assert isinstance(sap_error, SAP.BusinessGatewayError)
|
185 | 188 | assert str(sap_error) == 'Gateway Error'
|
| 189 | + |
| 190 | + |
| 191 | +MOCK_AUTH_URL = "https://example.authentication.hana.ondemand.com" |
| 192 | +MOCK_BTP_USER = "[email protected]" |
| 193 | +MOCK_BTP_PASSWORD = "example_password" |
| 194 | +MOCK_KEY = { |
| 195 | + "uaa": { |
| 196 | + "url": MOCK_AUTH_URL, |
| 197 | + "clientid": "example-client-id", |
| 198 | + "clientsecret": "example-client-secret" |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | + |
| 203 | +@responses.activate |
| 204 | +def test_add_btp_token_to_session_valid(): |
| 205 | + """Valid username, password and key return a session with set token""" |
| 206 | + |
| 207 | + responses.add( |
| 208 | + responses.POST, |
| 209 | + MOCK_AUTH_URL + f'/oauth/token?grant_type=password&username={MOCK_BTP_USER}&password={MOCK_BTP_PASSWORD}', |
| 210 | + headers={'Content-type': 'application/json'}, |
| 211 | + json={ |
| 212 | + 'access_token': 'valid_access_token', |
| 213 | + 'token_type': 'bearer', |
| 214 | + 'id_token': 'valid_id_token', |
| 215 | + 'refresh_token': 'valid_refresh_token', |
| 216 | + 'expires_in': 43199, |
| 217 | + 'scope': 'openid uaa.user', |
| 218 | + 'jti': 'valid_jti' |
| 219 | + }, |
| 220 | + status=200) |
| 221 | + |
| 222 | + result = SAP.add_btp_token_to_session(requests.Session(), MOCK_KEY, MOCK_BTP_USER, MOCK_BTP_PASSWORD) |
| 223 | + assert result.headers['Authorization'] == 'Bearer valid_id_token' |
| 224 | + |
| 225 | + |
| 226 | +@responses.activate |
| 227 | +def test_add_btp_token_to_session_invalid_user(): |
| 228 | + """Invalid username returns an HttpError""" |
| 229 | + |
| 230 | + invalid_user = "[email protected]" |
| 231 | + |
| 232 | + responses.add( |
| 233 | + responses.POST, |
| 234 | + MOCK_AUTH_URL + f'/oauth/token?grant_type=password&username={invalid_user}&password={MOCK_BTP_PASSWORD}', |
| 235 | + headers={'Content-type': 'application/json'}, |
| 236 | + json={ |
| 237 | + 'error': 'unauthorized', |
| 238 | + 'error_description': { |
| 239 | + 'error': 'invalid_grant', |
| 240 | + 'error_description': 'User authentication failed.' |
| 241 | + } |
| 242 | + }, |
| 243 | + status=401) |
| 244 | + |
| 245 | + with pytest.raises(HttpError) as caught: |
| 246 | + SAP.add_btp_token_to_session(requests.Session(), MOCK_KEY, invalid_user, MOCK_BTP_PASSWORD) |
| 247 | + |
| 248 | + assert caught.value.response.status_code == 401 |
| 249 | + assert json.loads(caught.value.response.text)['error_description']['error'] == 'invalid_grant' |
| 250 | + |
| 251 | + |
| 252 | +@responses.activate |
| 253 | +def test_add_btp_token_to_session_invalid_clientid(): |
| 254 | + """Invalid clientid in key returns an HttpError""" |
| 255 | + |
| 256 | + invalid_key = MOCK_KEY.copy() |
| 257 | + invalid_key['uaa']['clientid'] = 'invalid-client-id' |
| 258 | + |
| 259 | + responses.add( |
| 260 | + responses.POST, |
| 261 | + MOCK_AUTH_URL + f'/oauth/token?grant_type=password&username={MOCK_BTP_USER}&password={MOCK_BTP_PASSWORD}', |
| 262 | + headers={'Content-type': 'application/json'}, |
| 263 | + json={ |
| 264 | + 'error': 'unauthorized', |
| 265 | + 'error_description': 'Bad credentials' |
| 266 | + }, |
| 267 | + status=401) |
| 268 | + |
| 269 | + with pytest.raises(HttpError) as caught: |
| 270 | + SAP.add_btp_token_to_session(requests.Session(), invalid_key, MOCK_BTP_USER, MOCK_BTP_PASSWORD) |
| 271 | + |
| 272 | + assert caught.value.response.status_code == 401 |
| 273 | + assert json.loads(caught.value.response.text)['error_description'] == 'Bad credentials' |
0 commit comments