Projet

Général

Profil

0001-ogone-use-pytest-style-tests-51305.patch

Benjamin Dauvergne, 20 février 2021 19:53

Télécharger (10 ko)

Voir les différences:

Subject: [PATCH 1/2] ogone: use pytest style tests (#51305)

 tests/test_ogone.py | 239 +++++++++++++++++++++++---------------------
 1 file changed, 124 insertions(+), 115 deletions(-)
tests/test_ogone.py
15 15
# You should have received a copy of the GNU Affero General Public License
16 16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 17

  
18
from unittest import TestCase
19 18
from xml.etree import ElementTree as ET
20 19

  
21
import six
20
import pytest
22 21
from six.moves.urllib import parse as urllib
23 22

  
24 23
import eopayment
......
27 26

  
28 27
PSPID = u'2352566ö'
29 28

  
30
BACKEND_PARAMS = {
31
    'environment': ogone.ENVIRONMENT_TEST,
32
    'pspid': PSPID,
33
    'sha_in': u'sécret',
34
    'sha_out': u'sécret',
35
    'automatic_return_url': u'http://example.com/autömatic_réturn_url'
36
}
37

  
38

  
39
class OgoneTests(TestCase):
40
    if six.PY2:
41
        def assertRaisesRegex(self, *args, **kwargs):
42
            return self.assertRaisesRegexp(*args, **kwargs)
43

  
44
    def test_request(self):
45
        ogone_backend = eopayment.Payment('ogone', BACKEND_PARAMS)
46
        amount = '42.42'
47
        order_id = u'my ordér'
48
        reference, kind, what = ogone_backend.request(
49
            amount=amount,
50
            orderid=order_id,
51
            email='foo@example.com')
52
        self.assertEqual(len(reference), 30)
53
        assert reference.startswith(order_id)
54
        root = ET.fromstring(str(what))
55
        self.assertEqual(root.tag, 'form')
56
        self.assertEqual(root.attrib['method'], 'POST')
57
        self.assertEqual(root.attrib['action'], ogone.ENVIRONMENT_TEST_URL)
58
        values = {
59
            'CURRENCY': u'EUR',
60
            'ORDERID': reference,
61
            'PSPID': PSPID,
62
            'EMAIL': 'foo@example.com',
63
            'AMOUNT': amount.replace('.', ''),
64
            'LANGUAGE': 'fr_FR',
65
        }
66
        values.update({'SHASIGN': ogone_backend.backend.sha_sign_in(values)})
67
        for node in root:
68
            self.assertIn(node.attrib['type'], ('hidden', 'submit'))
69
            self.assertEqual(set(node.attrib.keys()), set(['type', 'name', 'value']))
70
            name = node.attrib['name']
71
            if node.attrib['type'] == 'hidden':
72
                self.assertIn(name, values)
73
                self.assertEqual(node.attrib['value'], values[name])
74

  
75
    def test_unicode_response(self):
76
        ogone_backend = eopayment.Payment('ogone', BACKEND_PARAMS)
77
        order_id = 'myorder'
78
        data = {'orderid': u'myorder', 'status': u'9', 'payid': u'3011229363',
79
                'cn': u'Usér', 'ncerror': u'0',
80
                'trxdate': u'10/24/16', 'acceptance': u'test123',
81
                'currency': u'eur', 'amount': u'7.5',
82
                'shasign': u'CA4B3C2767B5EFAB33B9122A5D4CF6F27747303D'}
83
        # uniformize to utf-8 first
84
        for k in data:
85
            data[k] = eopayment.common.force_byte(data[k])
86
        response = ogone_backend.response(urllib.urlencode(data))
87
        assert response.signed
88
        self.assertEqual(response.order_id, order_id)
89

  
90
    def test_iso_8859_1_response(self):
91
        ogone_backend = eopayment.Payment('ogone', BACKEND_PARAMS)
92
        order_id = 'lRXK4Rl1N2yIR3R5z7Kc'
93
        backend_response = (
94
            'orderID=lRXK4Rl1N2yIR3R5z7Kc&currency=EUR&amount=7%2E5'
95
            '&PM=CreditCard&ACCEPTANCE=test123&STATUS=9'
96
            '&CARDNO=XXXXXXXXXXXX9999&ED=0118'
97
            '&CN=Miha%EF+Serghe%EF&TRXDATE=10%2F24%2F16'
98
            '&PAYID=3011228911&NCERROR=0&BRAND=MasterCard'
99
            '&IP=80%2E12%2E92%2E47&SHASIGN=C429BE892FACFBFCE5E2CC809B102D866DD3D48C'
100
        )
101
        response = ogone_backend.response(backend_response)
102
        assert response.signed
103
        self.assertEqual(response.order_id, order_id)
104

  
105
    def test_bad_response(self):
106
        ogone_backend = eopayment.Payment('ogone', BACKEND_PARAMS)
107
        data = {'payid': '32100123', 'status': 9, 'ncerror': 0}
108
        with self.assertRaisesRegex(ResponseError, 'missing ORDERID, PAYID, STATUS or NCERROR'):
109
            ogone_backend.response(urllib.urlencode(data))
110

  
111
    def test_bank_transfer_response(self):
112
        ogone_backend = eopayment.Payment('ogone', BACKEND_PARAMS)
113
        data = {
114
            'orderid': u'myorder',
115
            'status': u'41',
116
            'payid': u'3011229363',
117
            'cn': u'User',
118
            'ncerror': u'0',
119
            'trxdate': u'10/24/16',
120
            'brand': 'Bank transfer',
121
            'pm': 'bank transfer',
122
            'currency': u'eur',
123
            'amount': u'7.5',
124
            'shasign': u'944CBD1E010BA4945415AE4B16CC40FD533F6CE2',
125
        }
126
        # uniformize to utf-8 first
127
        for k in data:
128
            data[k] = eopayment.common.force_byte(data[k])
129
        response = ogone_backend.response(urllib.urlencode(data))
130
        assert response.signed
131
        assert response.result == eopayment.WAITING
132

  
133
        # check utf-8 based signature is also ok
134
        data['shasign'] = b'0E35F687ACBEAA6CA769E0ADDBD0863EB6C1678A'
135
        response = ogone_backend.response(urllib.urlencode(data))
136
        assert response.signed
137
        assert response.result == eopayment.WAITING
138

  
139
        # check invalid signature is not marked ok
140
        data['shasign'] = b'0000000000000000000000000000000000000000'
141
        response = ogone_backend.response(urllib.urlencode(data))
142
        assert not response.signed
29

  
30
@pytest.fixture
31
def params(request):
32
    params = {
33
        'environment': ogone.ENVIRONMENT_TEST,
34
        'pspid': PSPID,
35
        'sha_in': u'sécret',
36
        'sha_out': u'sécret',
37
        'automatic_return_url': u'http://example.com/autömatic_réturn_url'
38
    }
39
    return params
40

  
41

  
42
def test_request(params):
43
    ogone_backend = eopayment.Payment('ogone', params)
44
    amount = '42.42'
45
    order_id = u'my ordér'
46
    reference, kind, what = ogone_backend.request(
47
        amount=amount,
48
        orderid=order_id,
49
        email='foo@example.com')
50
    assert len(reference) == 30
51
    assert reference.startswith(order_id)
52
    root = ET.fromstring(str(what))
53
    assert root.tag == 'form'
54
    assert root.attrib['method'] == 'POST'
55
    assert root.attrib['action'] == ogone.ENVIRONMENT_TEST_URL
56
    values = {
57
        'CURRENCY': u'EUR',
58
        'ORDERID': reference,
59
        'PSPID': PSPID,
60
        'EMAIL': 'foo@example.com',
61
        'AMOUNT': amount.replace('.', ''),
62
        'LANGUAGE': 'fr_FR',
63
    }
64
    values.update({'SHASIGN': ogone_backend.backend.sha_sign_in(values)})
65
    for node in root:
66
        assert node.attrib['type'] in ('hidden', 'submit')
67
        assert set(node.attrib.keys()), set(['type', 'name' == 'value'])
68
        name = node.attrib['name']
69
        if node.attrib['type'] == 'hidden':
70
            assert name in values
71
            assert node.attrib['value'] == values[name]
72

  
73

  
74
def test_response(params):
75
    ogone_backend = eopayment.Payment('ogone', params)
76
    order_id = 'myorder'
77
    data = {'orderid': u'myorder', 'status': u'9', 'payid': u'3011229363',
78
            'cn': u'Usér', 'ncerror': u'0',
79
            'trxdate': u'10/24/16', 'acceptance': u'test123',
80
            'currency': u'eur', 'amount': u'7.5',
81
            'shasign': u'CA4B3C2767B5EFAB33B9122A5D4CF6F27747303D'}
82
    # uniformize to utf-8 first
83
    for k in data:
84
        data[k] = eopayment.common.force_byte(data[k])
85
    response = ogone_backend.response(urllib.urlencode(data))
86
    assert response.signed
87
    assert response.order_id == order_id
88

  
89

  
90
def test_iso_8859_1_response():
91
    params = {
92
        'environment': ogone.ENVIRONMENT_TEST,
93
        'pspid': PSPID,
94
        'sha_in': u'sécret',
95
        'sha_out': u'sécret',
96
        'automatic_return_url': u'http://example.com/autömatic_réturn_url'
97
    }
98
    ogone_backend = eopayment.Payment('ogone', params)
99
    order_id = 'lRXK4Rl1N2yIR3R5z7Kc'
100
    backend_response = (
101
        'orderID=lRXK4Rl1N2yIR3R5z7Kc&currency=EUR&amount=7%2E5'
102
        '&PM=CreditCard&ACCEPTANCE=test123&STATUS=9'
103
        '&CARDNO=XXXXXXXXXXXX9999&ED=0118'
104
        '&CN=Miha%EF+Serghe%EF&TRXDATE=10%2F24%2F16'
105
        '&PAYID=3011228911&NCERROR=0&BRAND=MasterCard'
106
        '&IP=80%2E12%2E92%2E47&SHASIGN=C429BE892FACFBFCE5E2CC809B102D866DD3D48C'
107
    )
108
    response = ogone_backend.response(backend_response)
109
    assert response.signed
110
    assert response.order_id == order_id
111

  
112

  
113
def test_bad_response(params):
114
    ogone_backend = eopayment.Payment('ogone', params)
115
    data = {'payid': '32100123', 'status': 9, 'ncerror': 0}
116
    with pytest.raises(ResponseError, match='missing ORDERID, PAYID, STATUS or NCERROR'):
117
        ogone_backend.response(urllib.urlencode(data))
118

  
119

  
120
def test_bank_transfer_response(params):
121
    ogone_backend = eopayment.Payment('ogone', params)
122
    data = {
123
        'orderid': u'myorder',
124
        'status': u'41',
125
        'payid': u'3011229363',
126
        'cn': u'User',
127
        'ncerror': u'0',
128
        'trxdate': u'10/24/16',
129
        'brand': 'Bank transfer',
130
        'pm': 'bank transfer',
131
        'currency': u'eur',
132
        'amount': u'7.5',
133
        'shasign': u'944CBD1E010BA4945415AE4B16CC40FD533F6CE2',
134
    }
135
    # uniformize to expected encoding
136
    for k in data:
137
        data[k] = eopayment.common.force_byte(data[k])
138
    response = ogone_backend.response(urllib.urlencode(data))
139
    assert response.signed
140
    assert response.result == eopayment.WAITING
141

  
142
    # check utf-8 based signature is also ok
143
    data['shasign'] = b'0E35F687ACBEAA6CA769E0ADDBD0863EB6C1678A'
144
    response = ogone_backend.response(urllib.urlencode(data))
145
    assert response.signed
146
    assert response.result == eopayment.WAITING
147

  
148
    # check invalid signature is not marked ok
149
    data['shasign'] = b'0000000000000000000000000000000000000000'
150
    response = ogone_backend.response(urllib.urlencode(data))
151
    assert not response.signed
143
-