Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Instamojo/feature/tests
Feature/tests
- Loading branch information
Showing
10 changed files
with
630 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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,13 +1,9 @@ | ||
language: python | ||
|
||
python: | ||
- "2.6" | ||
- "2.7" | ||
- "3.2" | ||
- "3.3" | ||
- "3.4" | ||
- "3.5" | ||
|
||
install: "pip install -r requirements.txt" | ||
|
||
install: "pip install mock requests responses" | ||
script: nosetests |
This file was deleted.
Oops, something went wrong.
This file contains 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,66 @@ | ||
import difflib | ||
import pprint | ||
from os.path import commonprefix | ||
from unittest import TestCase | ||
|
||
_MAX_LENGTH = 80 | ||
_PLACEHOLDER_LEN = 12 | ||
_MIN_BEGIN_LEN = 5 | ||
_MIN_END_LEN = 5 | ||
_MIN_COMMON_LEN = 5 | ||
_MIN_DIFF_LEN = _MAX_LENGTH - \ | ||
(_MIN_BEGIN_LEN + _PLACEHOLDER_LEN + _MIN_COMMON_LEN + | ||
_PLACEHOLDER_LEN + _MIN_END_LEN) | ||
|
||
|
||
class BaseTestClass(TestCase): | ||
def assertDictEqual(self, d1, d2, msg=None): | ||
assert isinstance(d1, dict), 'First argument is not a dictionary' | ||
assert isinstance(d2, dict), 'Second argument is not a dictionary' | ||
|
||
if d1 != d2: | ||
standardMsg = '%s != %s' % _common_shorten_repr(d1, d2) | ||
diff = ('\n' + '\n'.join(difflib.ndiff( | ||
pprint.pformat(d1).splitlines(), | ||
pprint.pformat(d2).splitlines()))) | ||
standardMsg = self._truncateMessage(standardMsg, diff) | ||
self.fail(self._formatMessage(msg, standardMsg)) | ||
|
||
|
||
def safe_repr(obj, short=False): | ||
try: | ||
result = repr(obj) | ||
except Exception: | ||
result = object.__repr__(obj) | ||
if not short or len(result) < _MAX_LENGTH: | ||
return result | ||
return result[:_MAX_LENGTH] + ' [truncated]...' | ||
|
||
|
||
def _shorten(s, prefixlen, suffixlen): | ||
skip = len(s) - prefixlen - suffixlen | ||
if skip > _PLACEHOLDER_LEN: | ||
s = '%s[%d chars]%s' % (s[:prefixlen], skip, s[len(s) - suffixlen:]) | ||
return s | ||
|
||
|
||
def _common_shorten_repr(*args): | ||
args = tuple(map(safe_repr, args)) | ||
maxlen = max(map(len, args)) | ||
if maxlen <= _MAX_LENGTH: | ||
return args | ||
|
||
prefix = commonprefix(args) | ||
prefixlen = len(prefix) | ||
|
||
common_len = _MAX_LENGTH - \ | ||
(maxlen - prefixlen + _MIN_BEGIN_LEN + _PLACEHOLDER_LEN) | ||
if common_len > _MIN_COMMON_LEN: | ||
assert _MIN_BEGIN_LEN + _PLACEHOLDER_LEN + _MIN_COMMON_LEN + \ | ||
(maxlen - prefixlen) < _MAX_LENGTH | ||
prefix = _shorten(prefix, _MIN_BEGIN_LEN, common_len) | ||
return tuple(prefix + s[prefixlen:] for s in args) | ||
|
||
prefix = _shorten(prefix, _MIN_BEGIN_LEN, _MIN_COMMON_LEN) | ||
return tuple(prefix + _shorten(s[prefixlen:], _MIN_DIFF_LEN, _MIN_END_LEN) | ||
for s in args) |
This file contains 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,3 @@ | ||
from .payment_requests import * | ||
from .products import * | ||
from .refunds import * |
This file contains 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,38 @@ | ||
payment_requests_payload = { | ||
'payment_request_create': { | ||
'request': { | ||
'purpose': 'FIFA 16', | ||
'amount': '2500', | ||
'buyer_name': 'Jon Snow', | ||
'email': 'jon+snow@mailinator.com', | ||
'phone': '+919999999990', | ||
'send_email': True, | ||
'send_sms': True, | ||
'redirect_url': 'https://www.instamojo.com/redirect-handler', | ||
'webhook': 'https://www.instamojo.com/webhook-handler', | ||
'allow_repeated_payments': 'allow_repeated_payments' | ||
} | ||
}, | ||
'payment_request_status': { | ||
'request': { | ||
'id': '92e58bd771414d05a5e443b0a85f8b43' | ||
} | ||
}, | ||
'payment_requests_list': { | ||
'request': {} | ||
}, | ||
'payment_requests_list_optional_params': { | ||
'request': { | ||
'min_created_at': '2016-08-10T13:23:20Z', | ||
'max_created_at': '2016-08-12T13:23:20Z', | ||
'min_modified_at': '2016-08-10T13:23:20Z', | ||
'max_modified_at': '2016-08-12T13:23:20Z' | ||
} | ||
}, | ||
'payment_request_payment_status': { | ||
'request': { | ||
'id': '92e58bd771414d05a5e443b0a85f8b43', | ||
'payment_id': 'MOJO5a06005J21512197' | ||
} | ||
} | ||
} |
This file contains 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,96 @@ | ||
products_payload = { | ||
'basic_product_creation': { | ||
'request': { | ||
'currency': 'INR', | ||
'base_price': '50.00', | ||
'description': 'This is an example product.', | ||
'title': 'Hello API' | ||
} | ||
}, | ||
'event_product_creation': { | ||
'request': { | ||
'currency': 'INR', | ||
'base_price': '50.00', | ||
'description': 'This is an example product.', | ||
'title': 'Hello API', | ||
'start_date': '2016-08-03 16:30', | ||
'end_date': '2016-08-04 18:14', | ||
'venue': 'A galaxy far, far away', | ||
'timezone': 'Asia/Kolkata' | ||
} | ||
}, | ||
'digital_product_creation': { | ||
'request': { | ||
'currency': 'INR', | ||
'base_price': '50.00', | ||
'description': 'This is an example product.', | ||
'title': 'Hello API', | ||
'file_upload': '/Users/DarthVader/Padme.jpg' | ||
} | ||
}, | ||
'product_creation_with_cover_image': { | ||
'request': { | ||
'currency': 'INR', | ||
'base_price': '50.00', | ||
'description': 'This is an example product.', | ||
'title': 'Hello API', | ||
'cover_image': '/Users/DarthVader/Padme.jpg' | ||
} | ||
}, | ||
'product_creation_with_all_params': { | ||
'request': { | ||
'currency': 'INR', | ||
'base_price': '50.00', | ||
'description': 'This is an example product.', | ||
'title': 'Hello API', | ||
'quantity': 100, | ||
'start_date': '2016-08-03 16:30', | ||
'end_date': '2016-08-04 18:14', | ||
'venue': 'A galaxy far, far away', | ||
'timezone': 'Asia/Kolkata', | ||
'redirect_url': 'https://www.instamojo.com/redirect_handler', | ||
'webhook_url': 'https://www.instamojo.com/webhook_handler', | ||
'note': 'Thanks for buying things.', | ||
'enable_pwyw': True, | ||
'enable_sign': True, | ||
'socialpay_platforms': 'twitter', | ||
'file_upload': '/Users/DarthVader/Padme.jpg', | ||
'cover_image': '/Users/DarthVader/Padme.jpg' | ||
} | ||
}, | ||
'product_edit_with_all_params': { | ||
'request': { | ||
'slug': 'hello-api', | ||
'currency': 'INR', | ||
'base_price': '50.00', | ||
'description': 'This is an example product.', | ||
'title': 'Hello API', | ||
'quantity': 100, | ||
'start_date': '2016-08-03 16:30', | ||
'end_date': '2016-08-04 18:14', | ||
'venue': 'A galaxy far, far away', | ||
'timezone': 'Asia/Kolkata', | ||
'redirect_url': 'https://www.instamojo.com/redirect_handler', | ||
'webhook_url': 'https://www.instamojo.com/webhook_handler', | ||
'note': 'Thanks for buying things.', | ||
'enable_pwyw': True, | ||
'enable_sign': True, | ||
'socialpay_platforms': 'twitter', | ||
'file_upload': '/Users/DarthVader/Padme.jpg', | ||
'cover_image': '/Users/DarthVader/Padme.jpg' | ||
} | ||
}, | ||
'product_delete': { | ||
'request': { | ||
'slug': 'hello-api' | ||
} | ||
}, | ||
'product_details': { | ||
'request': { | ||
'slug': 'hello-api' | ||
} | ||
}, | ||
'products_list': { | ||
'request': {} | ||
} | ||
} |
This file contains 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,18 @@ | ||
refunds_payload = { | ||
'refund_create': { | ||
'request': { | ||
'payment_id': 'MOJO5a06005J21512197', | ||
'type': 'QFL', | ||
'body': 'Product didn\'t satisfy customer.', | ||
'refund_amount': '2500' | ||
} | ||
}, | ||
'refund_detail': { | ||
'request': { | ||
'id': 'C5c0751272' | ||
} | ||
}, | ||
'refunds_list': { | ||
'request': {} | ||
} | ||
} |
This file contains 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,98 @@ | ||
from . import BaseTestClass | ||
try: | ||
from urllib import urlencode | ||
from urlparse import parse_qsl, urlparse | ||
except ImportError: | ||
from urllib.parse import parse_qsl, urlencode, urlparse | ||
|
||
import responses | ||
|
||
from instamojo_wrapper import Instamojo | ||
from tests.payloads import payment_requests_payload | ||
|
||
|
||
class TestPaymentRequests(BaseTestClass): | ||
def setUp(self): | ||
self.api_endpoint = 'https://www.instamojo.com/api/1.1/' | ||
self.api = Instamojo('API-KEY', 'AUTH-TOKEN', self.api_endpoint) | ||
|
||
@responses.activate | ||
def test_payment_request_create(self): | ||
data = payment_requests_payload['payment_request_create'] | ||
endpoint = self.api_endpoint + 'payment-requests/' | ||
responses.add( | ||
responses.POST, | ||
endpoint, | ||
body='{}', | ||
content_type='application/json' | ||
) | ||
resp = self.api.payment_request_create(**data['request']) | ||
self.assertEqual(resp, {}) | ||
self.assertEqual(len(responses.calls), 1) | ||
self.assertEqual( | ||
responses.calls[0].request.url, endpoint) | ||
|
||
@responses.activate | ||
def test_payment_request_status(self): | ||
data = payment_requests_payload['payment_request_status'] | ||
endpoint = self.api_endpoint + 'payment-requests/{id}/'.format(**data['request']) | ||
responses.add( | ||
responses.GET, | ||
endpoint, | ||
body='{}', | ||
content_type='application/json' | ||
) | ||
resp = self.api.payment_request_status(**data['request']) | ||
self.assertEqual(resp, {}) | ||
self.assertEqual(len(responses.calls), 1) | ||
self.assertEqual( | ||
responses.calls[0].request.url, endpoint) | ||
|
||
@responses.activate | ||
def test_payment_requests_list(self): | ||
data = payment_requests_payload['payment_requests_list'] | ||
endpoint = self.api_endpoint + 'payment-requests/' | ||
responses.add( | ||
responses.GET, | ||
endpoint, | ||
body='{}', | ||
content_type='application/json' | ||
) | ||
resp = self.api.payment_requests_list(**data['request']) | ||
self.assertEqual(resp, {}) | ||
self.assertEqual(len(responses.calls), 1) | ||
self.assertEqual( | ||
responses.calls[0].request.url, endpoint) | ||
|
||
@responses.activate | ||
def test_payment_requests_list_optional_params(self): | ||
data = payment_requests_payload['payment_requests_list_optional_params'] | ||
endpoint = self.api_endpoint + 'payment-requests/' | ||
responses.add( | ||
responses.GET, | ||
endpoint, | ||
body='{}', | ||
content_type='application/json' | ||
) | ||
resp = self.api.payment_requests_list(**data['request']) | ||
self.assertEqual(resp, {}) | ||
self.assertEqual(len(responses.calls), 1) | ||
parsed_url = urlparse(responses.calls[0].request.url) | ||
self.assertTrue(endpoint.endswith(parsed_url.path)) | ||
self.assertDictEqual(dict(parse_qsl(parsed_url.query.strip('/'))), data['request']) | ||
|
||
@responses.activate | ||
def test_payment_request_payment_status(self): | ||
data = payment_requests_payload['payment_request_payment_status'] | ||
endpoint = self.api_endpoint + 'payment-requests/{id}/{payment_id}/'.format(**data['request']) | ||
responses.add( | ||
responses.GET, | ||
endpoint, | ||
body='{}', | ||
content_type='application/json' | ||
) | ||
resp = self.api.payment_request_payment_status(**data['request']) | ||
self.assertEqual(resp, {}) | ||
self.assertEqual(len(responses.calls), 1) | ||
self.assertEqual( | ||
responses.calls[0].request.url, endpoint) |
Oops, something went wrong.