Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
also enhance documentation.
  • Loading branch information
nwolff committed May 7, 2019
1 parent d88a2b5 commit e644995
Show file tree
Hide file tree
Showing 18 changed files with 38 additions and 46 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ Supports:


This implementation:
- Handles the exchanges with datatrans, including the signing of requests and the verification of the signature of notifications.
- Introduces persistent models for AliasRegistration, Payment, and Refund. These models record all exchanges with datatrans.
- Handles the exchanges with datatrans, including the signing of requests and the verification of the signature of notifications. All exchanges are logged as structured events, to make auditing easy.
- Introduces persistent models for AliasRegistration, Payment, and Refund. Both successes and failures are stored.
- Offers a rich admin interface, allowing ad-hoc payments using registered credit cards, as well as refunds.
- Sends signals whenever an AliasRegistration, Payment, or Refund is done. The signal is sent even if the operation failed,
the receiver should check the `success` flag received with the signal.

Expand Down Expand Up @@ -46,7 +47,7 @@ Include `datatrans-urls` to the urlpatterns in your urls.py, for instance:
url(r'^datatrans/', include('datatrans.urls')),
]

Configure the callback url in your upp
Configure the callback url in the datatrans UPP Administration page.

In your settings.py, enter the configuration for your web and mpo merchants. For instance:

Expand Down
2 changes: 1 addition & 1 deletion datatrans/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '1.0.5'
__version__ = '1.0.6'
__copyright__ = 'Copyright (c) 2017, skioo SA'
__licence__ = 'MIT'
__URL__ = 'https://github.com/skioo/django-datatrans-gateway'
File renamed without changes.
4 changes: 2 additions & 2 deletions datatrans/gateway/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from structlog import get_logger
from typing import Union

from .xml_helpers import parse_money
from .money_xml_converters import parse_money
from ..config import sign_web
from ..models import AliasRegistration, Payment

Expand All @@ -19,7 +19,7 @@ def handle_notification(xml: str) -> None:
def parse_notification_xml(xml: str) -> Union[AliasRegistration, Payment]:
""""
Both alias registration and payments are received here.
We can differentiate them by looking at the use-alias user-parameter (and verifying the amount is o).
We can differentiate them by looking at the use-alias user-parameter (and verifying the amount is 0).
"""
body = fromstring(xml).find('body')
Expand Down
3 changes: 2 additions & 1 deletion datatrans/gateway/payment_parameters.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from collections import namedtuple

from moneyed import Money
from structlog import get_logger

from .xml_helpers import money_to_amount_and_currency
from .money_xml_converters import money_to_amount_and_currency
from ..config import sign_web, web_merchant_id

logger = get_logger()
Expand Down
2 changes: 1 addition & 1 deletion datatrans/gateway/payment_with_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import requests
from structlog import get_logger

from .xml_helpers import money_to_amount_and_currency, parse_money
from .money_xml_converters import money_to_amount_and_currency, parse_money
from ..config import datatrans_authorize_url, mpo_merchant_id, sign_mpo
from ..models import AliasRegistration, Payment

Expand Down
2 changes: 1 addition & 1 deletion datatrans/gateway/refunding.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from moneyed import Money
from structlog import get_logger

from .xml_helpers import money_to_amount_and_currency, parse_money
from .money_xml_converters import money_to_amount_and_currency, parse_money
from ..config import datatrans_processor_url, sign_web
from ..models import Payment, Refund

Expand Down
Empty file added tests/gateway/__init__.py
Empty file.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from datatrans.gateway.notification import parse_notification_xml
from datatrans.models import AliasRegistration, Payment
from .utils import assertModelEqual
from .assertions import assertModelEqual


class ParseRegisterAliasTest(TestCase):
Expand Down Expand Up @@ -41,8 +41,7 @@ def test_success(self):
</userParameters>
</transaction>
</body>
</uppTransactionService>
"""
</uppTransactionService>"""
expected = AliasRegistration(
success=True,
transaction_id='170707111922838874',
Expand Down Expand Up @@ -99,9 +98,7 @@ def test_empty_response_code(self):
</userParameters>
</transaction>
</body>
</uppTransactionService>
"""

</uppTransactionService>"""
expected = AliasRegistration(
success=True,
transaction_id='170707111922838874',
Expand Down Expand Up @@ -156,8 +153,7 @@ def test_wrong_sign2(self):
</userParameters>
</transaction>
</body>
</uppTransactionService>
"""
</uppTransactionService>"""
with self.assertRaises(ValueError):
parse_notification_xml(xml)

Expand Down Expand Up @@ -192,8 +188,7 @@ def test_wrong_expiry_date(self):
</userParameters>
</transaction>
</body>
</uppTransactionService>
"""
</uppTransactionService>"""
expected = AliasRegistration(
success=False,
transaction_id='170710155947695609',
Expand Down Expand Up @@ -242,8 +237,7 @@ def test_card_number_and_alias_are_not_always_present(self):
</userParameters>
</transaction>
</body>
</uppTransactionService>
"""
</uppTransactionService>"""
expected = AliasRegistration(
success=False,
transaction_id='170710155947695609',
Expand Down Expand Up @@ -293,8 +287,7 @@ def test_success(self):
</userParameters>
</transaction>
</body>
</uppTransactionService>
"""
</uppTransactionService>"""
expected = Payment(
success=True,
transaction_id='170719094930353253',
Expand Down Expand Up @@ -396,8 +389,7 @@ def test_wrong_expiry(self):
</userParameters>
</transaction>
</body>
</uppTransactionService>
"""
</uppTransactionService>"""
expected = Payment(
success=False,
transaction_id='170720154219033737',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from datatrans.gateway import PaymentParameters, build_payment_parameters, build_register_credit_card_parameters


class PaymentTest(TestCase):
class PaymentParametersTest(TestCase):
def test_it_should_generate_payment_parameters(self):
parameters = build_payment_parameters(amount=Money(8.50, 'CHF'), client_ref='91827364')
expected = PaymentParameters(
Expand All @@ -17,8 +17,6 @@ def test_it_should_generate_payment_parameters(self):
)
assert parameters == expected


class RegisterCreditCardTest(TestCase):
def test_it_should_generate_register_credit_card_parameters(self):
parameters = build_register_credit_card_parameters(client_ref='someref')
expected = PaymentParameters(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

from datatrans.gateway.payment_with_alias import build_pay_with_alias_request_xml, parse_pay_with_alias_response_xml
from datatrans.models import AliasRegistration, Payment
from .utils import assertModelEqual
from .assertions import assertModelEqual


class BuildChargeRequestTest(TestCase):
class BuildPayWithAliasRequestTest(TestCase):
def test_build_request(self):
xml = build_pay_with_alias_request_xml(
amount=Money(123, "CHF"),
Expand Down Expand Up @@ -34,7 +34,7 @@ def test_build_request(self):
assert xml.decode() == expected


class ParseChargeResponseTest(TestCase):
class ParsePayWithAliasResponseTest(TestCase):
def test_success(self):
response = """<?xml version='1.0' encoding='utf8'?>
<authorizationService version='3'>
Expand Down
5 changes: 2 additions & 3 deletions tests/test_refund_xml.py → tests/gateway/test_refunding.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from datatrans.gateway.refunding import build_refund_request_xml, parse_refund_response_xml
from datatrans.models import Refund
from .utils import assertModelEqual
from .assertions import assertModelEqual


class BuildRefundRequestTest(TestCase):
Expand Down Expand Up @@ -111,8 +111,7 @@ def test_invalid_payment_transaction_id(self):
</error>
</transaction>
</body>
</paymentService>
"""
</paymentService>"""
expected = Refund(
success=False,
merchant_id='2222222222',
Expand Down
2 changes: 1 addition & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'django.contrib.sessions',
'django.contrib.admin',
'tests',
'datatrans',
'datatrans.apps.DatatransConfig',
]

MIDDLEWARE = [
Expand Down
11 changes: 0 additions & 11 deletions tests/test_gateway.py

This file was deleted.

2 changes: 2 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def test_save_many_errored_refund(self):
error_message='access denied',
error_detail='incorrect merchantId',
)
refund1.full_clean()
refund1.save()

refund2 = Refund(
Expand All @@ -155,4 +156,5 @@ def test_save_many_errored_refund(self):
error_message='access denied',
error_detail='incorrect merchantId',
)
refund2.full_clean()
refund2.save()
File renamed without changes.
10 changes: 10 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ def test_it_should_show_a_form_for_the_example_register_credit_card_page(self):
response = Client().get(reverse('example_register_credit_card'))
assert response.status_code == 200

def test_it_should_process_form_for_the_example_register_credit_card_page(self):
response = Client().post(reverse('example_register_credit_card'),
{'client_ref': 'a ref'})
assert response.status_code == 200

def test_it_should_show_a_form_for_the_example_pay_page(self):
response = Client().get(reverse('example_pay'))
assert response.status_code == 200

def test_it_should_process_form_for_the_example_pay_page(self):
response = Client().post(reverse('example_pay'),
{'amount_0': 1, 'amount_1': 'CHF', 'client_ref': 'a ref'})
assert response.status_code == 200

0 comments on commit e644995

Please sign in to comment.