Repository navigation
feature/INT-1751 - Security updates #257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| requests>=2.27.1 | ||
| pytest>=6.1.2 | ||
| pytest-mock>=3.6.1 | ||
| flake8>=4.0.1 | ||
| python-dateutil>=2.8.2 | ||
| pylint>=2.12.2 | ||
| Deprecated>=1.2.14 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,2 @@ | ||
| requests>=2.27.1 | ||
| pytest>=6.1.2 | ||
| pytest-mock>=3.6.1 | ||
| flake8>=4.0.1 | ||
| python-dateutil>=2.8.2 | ||
| -r requirements-ci.txt | ||
| pre-commit>=2.17.0 | ||
| pylint>=2.12.2 | ||
| Deprecated>=1.2.14 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import pytest | ||
| from requests import ConnectionError, HTTPError | ||
|
|
||
| from checkout_sdk.api_client import ApiClient | ||
| from checkout_sdk.exception import CheckoutApiException, CheckoutException | ||
| from checkout_sdk.files.files import FileRequest | ||
|
|
||
|
|
||
| # Exercises the REAL ApiClient upload path (invoke -> get_file_request -> http_client.request). | ||
| # get_file_request opens the file and hands the open handle to requests, which reads it but never | ||
| # closes it, so invoke has to close it whatever the outcome of the request. | ||
| def _build(mock_sdk_configuration, request_side_effect=None, status=200): | ||
| api_client = ApiClient(configuration=mock_sdk_configuration, | ||
| base_uri=mock_sdk_configuration.environment.base_uri) | ||
| response = MagicMock() | ||
| response.status_code = status | ||
| response.text = '{"id": "file_123"}' | ||
| response.headers = {'Content-Type': 'application/json'} | ||
| response.json.return_value = {'id': 'file_123'} | ||
| if status >= 400: | ||
| response.raise_for_status.side_effect = HTTPError(response=response) | ||
| http_client = MagicMock() | ||
| http_client.request.return_value = response | ||
| http_client.request.side_effect = request_side_effect | ||
| api_client._http_client = http_client | ||
|
|
||
| authorization = MagicMock() | ||
| authorization.get_authorization_header.return_value = 'Bearer test' | ||
| return api_client, http_client, authorization | ||
|
|
||
|
|
||
| def _file_request(tmp_path): | ||
| path = tmp_path / 'evidence.pdf' | ||
| path.write_bytes(b'%PDF-1.4') | ||
| file_request = FileRequest() | ||
| file_request.file = str(path) | ||
| file_request.purpose = 'dispute_evidence' | ||
| return file_request | ||
|
|
||
|
|
||
| def _sent_file(http_client): | ||
| files = http_client.request.call_args.kwargs['files'] | ||
| return files['file'][1] | ||
|
|
||
|
|
||
| class TestApiClientFileUpload: | ||
|
|
||
| def test_should_close_the_uploaded_file_after_a_successful_request(self, mock_sdk_configuration, tmp_path): | ||
| api_client, http_client, authorization = _build(mock_sdk_configuration) | ||
|
|
||
| response = api_client.submit_file('files', authorization, _file_request(tmp_path)) | ||
|
|
||
| assert response.id == 'file_123' | ||
| assert _sent_file(http_client).closed | ||
|
|
||
| def test_should_close_the_uploaded_file_when_the_api_returns_an_error(self, mock_sdk_configuration, tmp_path): | ||
| api_client, http_client, authorization = _build(mock_sdk_configuration, status=422) | ||
|
|
||
| with pytest.raises(CheckoutApiException): | ||
| api_client.submit_file('files', authorization, _file_request(tmp_path)) | ||
|
|
||
| assert _sent_file(http_client).closed | ||
|
|
||
| def test_should_close_the_uploaded_file_when_the_connection_fails(self, mock_sdk_configuration, tmp_path): | ||
| api_client, http_client, authorization = _build(mock_sdk_configuration, | ||
| request_side_effect=ConnectionError('unreachable')) | ||
|
|
||
| with pytest.raises(CheckoutException): | ||
| api_client.submit_file('files', authorization, _file_request(tmp_path)) | ||
|
|
||
| assert _sent_file(http_client).closed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import json | ||
|
|
||
| import pytest | ||
| from requests import Response, Session | ||
| from requests.adapters import HTTPAdapter | ||
|
|
||
| from checkout_sdk.environment import Environment | ||
| from checkout_sdk.oauth_credentials import OAuthSdkCredentials | ||
|
|
||
|
|
||
| def token_response(request): | ||
| response = Response() | ||
| response.status_code = 200 | ||
| response.request = request | ||
| response.url = request.url | ||
| response._content = json.dumps( | ||
| {'access_token': 'access_token', 'token_type': 'Bearer', 'expires_in': 3600}).encode('utf-8') | ||
| return response | ||
|
|
||
|
|
||
| class TestOAuthSdkCredentials: | ||
| """The token request carries the client id and secret, so it must never go out with TLS | ||
| certificate verification turned off. The SDK leaves verification to the http_client the | ||
| caller passed in, and to requests' defaults, instead of overriding it per request. | ||
|
|
||
| Only the transport is stubbed: a real Session merges its settings into the request, so the | ||
| test sees the same verify value the TLS handshake would. | ||
| """ | ||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def no_ca_bundle_from_environment(self, monkeypatch): | ||
| # requests reads these when verification is on, which would make the asserted value | ||
| # depend on the machine running the tests. | ||
| monkeypatch.delenv('REQUESTS_CA_BUNDLE', raising=False) | ||
| monkeypatch.delenv('CURL_CA_BUNDLE', raising=False) | ||
|
|
||
| @staticmethod | ||
| def request_token(mocker, http_client: Session): | ||
| send = mocker.patch.object(HTTPAdapter, 'send', autospec=True, | ||
| side_effect=lambda adapter, request, **kwargs: token_response(request)) | ||
| credentials = OAuthSdkCredentials(http_client, Environment.sandbox(), 'client_id', 'client_secret', | ||
| None, ['vault']) | ||
| assert credentials.get_access_token().token == 'access_token' | ||
| return send | ||
|
|
||
| def test_should_verify_the_server_certificate_when_requesting_the_token(self, mocker): | ||
| send = self.request_token(mocker, Session()) | ||
|
|
||
| assert send.call_count == 1 | ||
| assert send.call_args.kwargs['verify'] is True | ||
|
|
||
| def test_should_use_the_ca_bundle_configured_on_the_http_client(self, mocker): | ||
| http_client = Session() | ||
| http_client.verify = '/path/to/ca-bundle.pem' | ||
|
|
||
| send = self.request_token(mocker, http_client) | ||
|
|
||
| assert send.call_args.kwargs['verify'] == '/path/to/ca-bundle.pem' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.