summaryrefslogtreecommitdiffstats
path: root/tests/test_base_payment.py
blob: bb23959b7a45813da1250418bd1019cab8889e17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from datetime import date, datetime, timedelta

import mock
import pytest


def do_mock_backend(monkeypatch):

    class MockBackend(object):

        request = mock.Mock()

        description = {
            'parameters': [
                {
                    'name': 'capture_day',
                },
                {
                    'name': 'manual_validation',
                    'caption': 'Validation manuelle',
                    'type': bool,
                    'default': False,
                    'scope': 'transaction'
                },
                {
                    'name': 'global_param',
                    'caption': 'Global Param',
                    'type': bool,
                    'default': False,
                    'scope': 'global'
                },

            ]
        }

    def get_backend(*args, **kwargs):
        def backend(*args, **kwargs):
            return MockBackend
        return backend

    import eopayment
    monkeypatch.setattr(eopayment, 'get_backend', get_backend)
    return MockBackend, eopayment.Payment('kind', None)


def test_deferred_payment(monkeypatch):
    mock_backend, payment = do_mock_backend(monkeypatch)

    capture_date = (datetime.now().date() + timedelta(days=3))
    payment.request(amount=12.2, capture_date=capture_date)
    mock_backend.request.assert_called_with(12.2, **{'capture_day': u'3'})

    # capture date can't be inferior to the transaction date
    capture_date = (datetime.now().date() - timedelta(days=3))
    with pytest.raises(
            ValueError, match='capture_date needs to be superior to the transaction date.'):
        payment.request(amount=12.2, capture_date=capture_date)

    # capture date should be a date object
    capture_date = 'not a date'
    with pytest.raises(ValueError, match='capture_date should be a datetime.date object.'):
        payment.request(amount=12.2, capture_date=capture_date)

    # using capture date on a backend that does not support it raise an error
    capture_date = (datetime.now().date() + timedelta(days=3))
    mock_backend.description['parameters'] = []
    with pytest.raises(ValueError, match='capture_date is not supported by the backend.'):
        payment.request(amount=12.2, capture_date=capture_date)


def test_paris_timezone(freezer, monkeypatch):
    freezer.move_to('2018-10-02 23:50:00')
    _, payment = do_mock_backend(monkeypatch)
    capture_date = date(year=2018, month=10, day=3)

    with pytest.raises(
            ValueError, match='capture_date needs to be superior to the transaction date'):
        # utcnow will return 2018-10-02 23:50:00,
        # converted to Europe/Paris it is already 2018-10-03
        # so 2018-10-03 for capture_date is invalid
        payment.request(amount=12.2, capture_date=capture_date)


def test_get_parameters(monkeypatch):
    _, payment = do_mock_backend(monkeypatch)

    global_parameters = payment.get_parameters()
    assert len(global_parameters) == 2
    assert global_parameters[0]['name'] == 'capture_day'
    assert global_parameters[1]['name'] == 'global_param'

    transaction_parameters = payment.get_parameters(scope='transaction')
    assert len(transaction_parameters) == 1
    assert transaction_parameters[0]['name'] == 'manual_validation'