From cf9afd5aff6b674dbd185e18f056a01ccda9d2f3 Mon Sep 17 00:00:00 2001 From: Zaki Ibrahim Date: Thu, 17 Mar 2022 12:20:04 +0700 Subject: [PATCH 01/10] Update gitignore for PyCharm IDE --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 01f3a95..d803b5e 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ push_to_twine.sh ./Pipfile env/ .DS_STORE +.idea \ No newline at end of file From 28ec626c0e8e9908609aaf5ad45114eb5a7a429d Mon Sep 17 00:00:00 2001 From: Zaki Ibrahim Date: Thu, 17 Mar 2022 12:20:27 +0700 Subject: [PATCH 02/10] Fix unit test for custom header --- tests/test_http_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_http_client.py b/tests/test_http_client.py index 8d3e7b9..27f9aee 100644 --- a/tests/test_http_client.py +++ b/tests/test_http_client.py @@ -68,7 +68,7 @@ def test_is_custom_headers_applied(): custom_headers=custom_headers) # Fetch the headers from requests.request arguments - headers = mock_request.call_args.kwargs['headers'] + headers = mock_request.call_args[1]['headers'] # Make sure default header still exist assert headers.get('content-type') == 'application/json' From 3dd698c0def37dc5660b27c351abe43ac15f501c Mon Sep 17 00:00:00 2001 From: Zaki Ibrahim Date: Thu, 17 Mar 2022 13:01:00 +0700 Subject: [PATCH 03/10] Increased module version --- midtransclient/__init__.py | 2 +- midtransclient/http_client.py | 2 +- setup.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/midtransclient/__init__.py b/midtransclient/__init__.py index e90f0f8..0d8ff59 100644 --- a/midtransclient/__init__.py +++ b/midtransclient/__init__.py @@ -4,4 +4,4 @@ from .error_midtrans import MidtransAPIError from .error_midtrans import JSONDecodeError -__version__ = '1.4.0' +__version__ = '1.5.0' diff --git a/midtransclient/http_client.py b/midtransclient/http_client.py index a72bd08..58ca1d5 100644 --- a/midtransclient/http_client.py +++ b/midtransclient/http_client.py @@ -39,7 +39,7 @@ def request(self, method, server_key, request_url, parameters=dict(), default_headers = { 'content-type': 'application/json', 'accept': 'application/json', - 'user-agent': 'midtransclient-python/1.2.0' + 'user-agent': 'midtransclient-python/1.5.0' } headers = default_headers diff --git a/setup.py b/setup.py index b21ba9d..05f30fe 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ setup( name="midtransclient", - version="1.4.0", + version="1.5.0", author="Midtrans - Integration Support Team", author_email="support@midtrans.com", license='MIT', From 436cba6398ce509ee833102c7d749ca39aacac45 Mon Sep 17 00:00:00 2001 From: Zaki Ibrahim Date: Thu, 17 Mar 2022 16:22:26 +0700 Subject: [PATCH 04/10] implement error handling for server-key format --- midtransclient/http_client.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/midtransclient/http_client.py b/midtransclient/http_client.py index 58ca1d5..ce1509f 100644 --- a/midtransclient/http_client.py +++ b/midtransclient/http_client.py @@ -47,6 +47,22 @@ def request(self, method, server_key, request_url, parameters=dict(), if custom_headers: headers = {**default_headers, **custom_headers} + # raise Midtrans error if server_key is not valid format + if server_key == "" or server_key is None: + raise MidtransAPIError( + message='The ServerKey is invalid, as it is an empty string or Null. Please double-check your API key. You can check the ServerKey from the Midtrans Dashboard. See https://docs.midtrans.com/en/midtrans-account/overview?id=retrieving-api-access-keys for the details or contact support at support@midtrans.com if you have any questions.', + api_response_dict=None, + http_status_code=0, + raw_http_client_data=None + ) + if " " in server_key: + raise MidtransAPIError( + message='The ServerKey is contains white-space. Please double-check your API key. You can check the ServerKey from the Midtrans Dashboard. See https://docs.midtrans.com/en/midtrans-account/overview?id=retrieving-api-access-keys for the details or contact support at support@midtrans.com if you have any questions.', + api_response_dict=None, + http_status_code=0, + raw_http_client_data=None + ) + response_object = self.http_client.request( method, request_url, From c473565e19d5e1bad88e7380dce294f06f91714b Mon Sep 17 00:00:00 2001 From: Zaki Ibrahim Date: Thu, 17 Mar 2022 16:22:37 +0700 Subject: [PATCH 05/10] fix several unit test for valid test after implement error for server-key format --- tests/test_snap.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_snap.py b/tests/test_snap.py index b16ddc3..5fa2d8c 100644 --- a/tests/test_snap.py +++ b/tests/test_snap.py @@ -58,7 +58,7 @@ def test_snap_status_fail_404(): def test_snap_request_fail_401(): snap = generate_snap_instance() - snap.api_config.server_key='' + snap.api_config.server_key='dummy' param = generate_param_min() err = '' try: @@ -94,7 +94,7 @@ def test_snap_request_fail_zero_gross_amount(): def test_snap_exception_MidtransAPIError(): snap = generate_snap_instance() - snap.api_config.server_key='' + snap.api_config.server_key='dummy' param = generate_param_min() err = '' try: From 72b34539a7f84f01eca1bf665ffc78bdc7f9392c Mon Sep 17 00:00:00 2001 From: Zaki Ibrahim Date: Thu, 17 Mar 2022 16:22:53 +0700 Subject: [PATCH 06/10] Add unit test for server-key format --- tests/test_http_client.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/test_http_client.py b/tests/test_http_client.py index 27f9aee..4b83b82 100644 --- a/tests/test_http_client.py +++ b/tests/test_http_client.py @@ -77,6 +77,42 @@ def test_is_custom_headers_applied(): assert 'X-Override-Notification' in headers assert headers.get('X-Override-Notification') == 'https://example.org' +def test_serverkey_empty(): + http_client = HttpClient() + err = '' + try: + response_dict, response_object = http_client.request(method='post', + server_key='', + request_url='https://app.sandbox.midtrans.com/snap/v1/transactions', + parameters=generate_param_min()) + except Exception as e: + err = e + assert 'The ServerKey is invalid, as it is an empty string or Null' in err.message + +def test_serverkey_none(): + http_client = HttpClient() + err = '' + try: + response_dict, response_object = http_client.request(method='post', + server_key=None, + request_url='https://app.sandbox.midtrans.com/snap/v1/transactions', + parameters=generate_param_min()) + except Exception as e: + err = e + assert 'The ServerKey is invalid, as it is an empty string or Null' in err.message + +def test_serverkey_contain_whitespace(): + http_client = HttpClient() + err = '' + try: + response_dict, _ = http_client.request(method='post', + server_key='test ', + request_url='https://app.sandbox.midtrans.com/snap/v1/transactions', + parameters=generate_param_min()) + except Exception as e: + err = e + assert 'The ServerKey is contains white-space.' in err.message + # TODO test GET request # ======== HELPER FUNCTIONS BELOW ======== # From 56dd1aceb6055e537470e71e52162a9b466aa6e5 Mon Sep 17 00:00:00 2001 From: Zaki Ibrahim Date: Thu, 17 Mar 2022 16:23:23 +0700 Subject: [PATCH 07/10] update maintain how to increase version on user-agent value --- Maintaining.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Maintaining.md b/Maintaining.md index d5dad64..6874f39 100644 --- a/Maintaining.md +++ b/Maintaining.md @@ -12,8 +12,9 @@ - Install project as local package `pip install -e .` - Make your code changes - Increase `version` value on: - - `./setup.py` file - - `./midtransclient/__init__.py` file + - `./setup.py` file + - `./midtransclient/__init__.py` file + - `./midtransclient/http_client.py` file on User-Agent value - To install the package locally with a symlink `pip install -e .` - To run test, run `pytest` - To run specific test, e.g: `pytest -k "test_core_api_charge_fail_401"` From 4f5340a229cf4f4c37823593f2cb8d926807d164 Mon Sep 17 00:00:00 2001 From: Zaki Ibrahim Date: Fri, 18 Mar 2022 13:21:10 +0700 Subject: [PATCH 08/10] remove handling error format server-key --- midtransclient/http_client.py | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/midtransclient/http_client.py b/midtransclient/http_client.py index ce1509f..58ca1d5 100644 --- a/midtransclient/http_client.py +++ b/midtransclient/http_client.py @@ -47,22 +47,6 @@ def request(self, method, server_key, request_url, parameters=dict(), if custom_headers: headers = {**default_headers, **custom_headers} - # raise Midtrans error if server_key is not valid format - if server_key == "" or server_key is None: - raise MidtransAPIError( - message='The ServerKey is invalid, as it is an empty string or Null. Please double-check your API key. You can check the ServerKey from the Midtrans Dashboard. See https://docs.midtrans.com/en/midtrans-account/overview?id=retrieving-api-access-keys for the details or contact support at support@midtrans.com if you have any questions.', - api_response_dict=None, - http_status_code=0, - raw_http_client_data=None - ) - if " " in server_key: - raise MidtransAPIError( - message='The ServerKey is contains white-space. Please double-check your API key. You can check the ServerKey from the Midtrans Dashboard. See https://docs.midtrans.com/en/midtrans-account/overview?id=retrieving-api-access-keys for the details or contact support at support@midtrans.com if you have any questions.', - api_response_dict=None, - http_status_code=0, - raw_http_client_data=None - ) - response_object = self.http_client.request( method, request_url, From d279d0ebb6af783c88ffbced4ebabd8a1e5e32d1 Mon Sep 17 00:00:00 2001 From: Zaki Ibrahim Date: Fri, 18 Mar 2022 13:21:45 +0700 Subject: [PATCH 09/10] fix version for fix bug custom header --- midtransclient/__init__.py | 2 +- midtransclient/http_client.py | 2 +- setup.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/midtransclient/__init__.py b/midtransclient/__init__.py index 0d8ff59..6caff84 100644 --- a/midtransclient/__init__.py +++ b/midtransclient/__init__.py @@ -4,4 +4,4 @@ from .error_midtrans import MidtransAPIError from .error_midtrans import JSONDecodeError -__version__ = '1.5.0' +__version__ = '1.4.1' diff --git a/midtransclient/http_client.py b/midtransclient/http_client.py index 58ca1d5..1a0bffd 100644 --- a/midtransclient/http_client.py +++ b/midtransclient/http_client.py @@ -39,7 +39,7 @@ def request(self, method, server_key, request_url, parameters=dict(), default_headers = { 'content-type': 'application/json', 'accept': 'application/json', - 'user-agent': 'midtransclient-python/1.5.0' + 'user-agent': 'midtransclient-python/1.4.1' } headers = default_headers diff --git a/setup.py b/setup.py index 05f30fe..6e8158f 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ setup( name="midtransclient", - version="1.5.0", + version="1.4.1", author="Midtrans - Integration Support Team", author_email="support@midtrans.com", license='MIT', From 9823f7c89060550d7817a2262272aed8480f4d96 Mon Sep 17 00:00:00 2001 From: Zaki Ibrahim Date: Fri, 18 Mar 2022 13:23:39 +0700 Subject: [PATCH 10/10] remove server-key error handling format unit test --- tests/test_http_client.py | 36 ------------------------------------ 1 file changed, 36 deletions(-) diff --git a/tests/test_http_client.py b/tests/test_http_client.py index 4b83b82..27f9aee 100644 --- a/tests/test_http_client.py +++ b/tests/test_http_client.py @@ -77,42 +77,6 @@ def test_is_custom_headers_applied(): assert 'X-Override-Notification' in headers assert headers.get('X-Override-Notification') == 'https://example.org' -def test_serverkey_empty(): - http_client = HttpClient() - err = '' - try: - response_dict, response_object = http_client.request(method='post', - server_key='', - request_url='https://app.sandbox.midtrans.com/snap/v1/transactions', - parameters=generate_param_min()) - except Exception as e: - err = e - assert 'The ServerKey is invalid, as it is an empty string or Null' in err.message - -def test_serverkey_none(): - http_client = HttpClient() - err = '' - try: - response_dict, response_object = http_client.request(method='post', - server_key=None, - request_url='https://app.sandbox.midtrans.com/snap/v1/transactions', - parameters=generate_param_min()) - except Exception as e: - err = e - assert 'The ServerKey is invalid, as it is an empty string or Null' in err.message - -def test_serverkey_contain_whitespace(): - http_client = HttpClient() - err = '' - try: - response_dict, _ = http_client.request(method='post', - server_key='test ', - request_url='https://app.sandbox.midtrans.com/snap/v1/transactions', - parameters=generate_param_min()) - except Exception as e: - err = e - assert 'The ServerKey is contains white-space.' in err.message - # TODO test GET request # ======== HELPER FUNCTIONS BELOW ======== #