Skip to content

Commit

Permalink
Merge pull request #2 from Som-Energia/FIX_decode_unexpected_param
Browse files Browse the repository at this point in the history
Fix decode unexpected param
  • Loading branch information
BenjiRS94 committed Dec 22, 2021
2 parents 5e0065a + 2934e70 commit eb2b17d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ redsystest = dict( # Clave para tests
```

## Changelog
### sermepa 1.0.4-dev

- When sermepa revice an unexpected param in `decodeSignedData` method not will raise `SignatureError`

### sermepa 1.0.2 (2020-09-16)

- Accept `Ds_ProcessedPayMethod` parameter set
Expand Down
4 changes: 0 additions & 4 deletions sermepa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,6 @@ def error(message):
camell = _notification_fields_upper[key]
data[camell]=data[key]
del data[key]
continue

if key not in _notification_fields:
error("Bad parameter '{}'".format(key))

return data

Expand Down
35 changes: 17 additions & 18 deletions sermepa/sermepa_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import unicode_literals

import unittest
import random

import base64
import json
Expand All @@ -21,11 +22,6 @@
except ImportError:
config = None

def randhex(nbytes):
import random
return "{1:0{0}x}".format(nbytes*2,random.getrandbits(nbytes*8))


class Generator_Test(unittest.TestCase):

# back2back data taken from PHP example
Expand Down Expand Up @@ -57,6 +53,9 @@ class Generator_Test(unittest.TestCase):
signature = 'Ejse86yr96Xbr1mf6UvQLoTPwwTyFiLXM+2uT09i9nY='
signatureversion = 'HMAC_SHA256_V1'

def new_order_id(self):
nbytes=6
return "{1:0{0}x}".format(nbytes*2,random.getrandbits(nbytes*8))

def test_encodePayload(self):
self.assertEqual(
Expand Down Expand Up @@ -179,7 +178,7 @@ def test_sendingPost_testing(self):
Ds_Merchant_MerchantData = "COBRAMENT QUOTA SOCI",
Ds_Merchant_MerchantName = "SOM ENERGIA, SCCL",
Ds_Merchant_MerchantURL = "https://testing.somenergia.coop:5001/pagament/notificacio",
Ds_Merchant_Order = randhex(nbytes=6), # "20167db2f375",
Ds_Merchant_Order = self.new_order_id(),
Ds_Merchant_ProductDescription = "Alta de soci SOMENERGIA",
Ds_Merchant_SumTotal = "10000",
Ds_Merchant_Terminal = "1",
Expand Down Expand Up @@ -210,7 +209,7 @@ def test_sendingPost_testing_invalidSignature(self):
Ds_Merchant_MerchantData = "COBRAMENT QUOTA SOCI",
Ds_Merchant_MerchantName = "SOM ENERGIA, SCCL",
Ds_Merchant_MerchantURL = "https://testing.somenergia.coop:5001/pagament/notificacio",
Ds_Merchant_Order = randhex(nbytes=6), # "20167db2f375",
Ds_Merchant_Order = self.new_order_id(),
Ds_Merchant_ProductDescription = "Alta de soci SOMENERGIA",
Ds_Merchant_SumTotal = "10000",
Ds_Merchant_Terminal = "1",
Expand Down Expand Up @@ -241,7 +240,7 @@ def test_sendingPost_testingFails_invalidUser(self):
Ds_Merchant_MerchantData = "COBRAMENT QUOTA SOCI",
Ds_Merchant_MerchantName = "SOM ENERGIA, SCCL",
Ds_Merchant_MerchantURL = "https://testing.somenergia.coop:5001/pagament/notificacio",
Ds_Merchant_Order = randhex(nbytes=6), # "20167db2f375",
Ds_Merchant_Order = self.new_order_id(),
Ds_Merchant_ProductDescription = "Alta de soci SOMENERGIA",
Ds_Merchant_SumTotal = "10000",
Ds_Merchant_Terminal = "1",
Expand Down Expand Up @@ -412,19 +411,19 @@ def test_decodeSignedData_badSignature(self):
msg = cm.exception.args[0]
self.assertEqual(msg, 'Bad signature')

def test_decodeSignedData_badParam(self):
def test_decodeSignedData_unexpectedParam(self):
json_data = '{"Ds_Order":"666", "Bad":"value"}'
base64_data = base64.urlsafe_b64encode(b(json_data))
signature = signPayload(self.secret, base64_data, urlsafe=True)
with self.assertRaises(SignatureError) as cm:
decodeSignedData(
self.merchantkey,
Ds_MerchantParameters = base64_data,
Ds_Signature = signature,
Ds_SignatureVersion = self.signatureversion,
)
msg = cm.exception.args[0]
self.assertEqual(msg, "Bad parameter 'Bad'")

data = decodeSignedData(
self.merchantkey,
Ds_MerchantParameters=base64_data,
Ds_Signature=signature,
Ds_SignatureVersion=self.signatureversion,
)

self.assertEqual(data, dict(Ds_Order='666', Bad='value'))

def test_decodeSignedData_upperCaseOrder(self):
json_data = '{"DS_ORDER":"666"}'
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name='sermepa',
version='1.0.3',
version='1.0.4-dev',
description = __doc__.strip(),
author='GISCE Enginyeria',
author_email='devel@gisce.net',
Expand Down

0 comments on commit eb2b17d

Please sign in to comment.