Skip to content

Commit 2c27a77

Browse files
iulian03Iulian Masar
and
Iulian Masar
authored
[release] 3.45.1 (#423)
* handle user data validation (#421) Co-authored-by: Iulian Masar <iulian.masar@codegile.com> * updated tests (#419) Co-authored-by: Iulian Masar <iulian.masar@codegile.com> --------- Co-authored-by: Iulian Masar <iulian.masar@codegile.com>
1 parent e72f8ab commit 2c27a77

File tree

5 files changed

+102
-4
lines changed

5 files changed

+102
-4
lines changed

mangopay/fields.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
ScopeBlocked, BrowserInfo, Shipping, CurrentState, FallbackReason, InstantPayout, CountryAuthorizationData, \
1313
PayinsLinked, ConversionRate, CardInfo, LocalAccountDetails, InternationalAccountDetails, \
1414
VirtualAccountCapabilities, PaymentRef, PendingUserAction, LegalRepresentative, IndividualRecipient, \
15-
BusinessRecipient, RecipientPropertySchema, IndividualRecipientPropertySchema, BusinessRecipientPropertySchema
15+
BusinessRecipient, RecipientPropertySchema, IndividualRecipientPropertySchema, BusinessRecipientPropertySchema, \
16+
CompanyNumberValidation
1617

1718

1819
class FieldDescriptor(object):
@@ -1180,3 +1181,27 @@ def api_value(self, value):
11801181
}
11811182

11821183
return value
1184+
1185+
1186+
class CompanyNumberValidationField(Field):
1187+
def python_value(self, value):
1188+
if value is not None:
1189+
return CompanyNumberValidation(company_number=value.get('CompanyNumber', None),
1190+
country_code=value.get('CountryCode', None),
1191+
is_valid=value.get('IsValid', None),
1192+
validation_rules=value.get('ValidationRules', None))
1193+
1194+
return value
1195+
1196+
def api_value(self, value):
1197+
value = super(CompanyNumberValidationField, self).api_value(value)
1198+
1199+
if isinstance(value, CompanyNumberValidation):
1200+
value = {
1201+
'CompanyNumber': value.company_number,
1202+
'CountryCode': value.country_code,
1203+
'IsValid': value.is_valid,
1204+
'ValidationRules': value.validation_rules
1205+
}
1206+
1207+
return value

mangopay/resources.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
LocalAccountDetailsField, VirtualAccountCapabilitiesField, PaymentRefField, PendingUserActionField,
1919
LegalRepresentativeField, IndividualRecipientField, BusinessRecipientField,
2020
RecipientPropertySchemaField, IndividualRecipientPropertySchemaField,
21-
BusinessRecipientPropertySchemaField)
21+
BusinessRecipientPropertySchemaField, CompanyNumberValidationField)
2222
from .query import InsertQuery, UpdateQuery, SelectQuery, ActionQuery, DeleteQuery
2323

2424

@@ -2800,3 +2800,15 @@ def get(cls, country, currency, *args, **kwargs):
28002800
kwargs['currency'] = currency
28012801
select = SelectQuery(PayoutMethod, *args, **kwargs)
28022802
return select.get("", *args, **kwargs)
2803+
2804+
2805+
class UserDataFormatValidation(BaseModel):
2806+
company_number = CompanyNumberValidationField(api_name='CompanyNumber')
2807+
2808+
class Meta:
2809+
verbose_name = 'user_data_format_validation'
2810+
verbose_name_plural = 'user_data_format_validations'
2811+
2812+
url = {
2813+
InsertQuery.identifier: '/users/data-formats/validation'
2814+
}

mangopay/utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,3 +1295,33 @@ def to_api_json(self):
12951295
"BusinessName": self.business_name,
12961296
"Address": self.address
12971297
}
1298+
1299+
1300+
@add_camelcase_aliases
1301+
class CompanyNumberValidation(object):
1302+
def __init__(self, company_number=None, country_code=None, is_valid=None, validation_rules=None):
1303+
self.company_number = company_number
1304+
self.country_code = country_code
1305+
self.is_valid = is_valid
1306+
self.validation_rules = validation_rules
1307+
1308+
def __str__(self):
1309+
return 'CompanyNumberValidation: %s , %s, %s, %s' % \
1310+
(self.company_number, self.country_code, self.is_valid, self.validation_rules)
1311+
1312+
def __eq__(self, other):
1313+
if isinstance(other, CompanyNumberValidation):
1314+
stat = ((self.company_number == other.company_number) and
1315+
(self.country_code == other.country_code) and
1316+
(self.is_valid == other.is_valid) and
1317+
(self.validation_rules == other.validation_rules))
1318+
return stat
1319+
return False
1320+
1321+
def to_api_json(self):
1322+
return {
1323+
"CompanyNumber": self.company_number,
1324+
"CountryCode": self.country_code,
1325+
"IsValid": self.is_valid,
1326+
"ValidationRules": self.validation_rules
1327+
}

tests/test_recipients.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ def test_get_user_recipients(self):
4141
self.assertIsInstance(fetched.data, list)
4242
self.assertTrue(len(fetched.data) > 0)
4343

44+
def test_get_user_recipients_filtered_payout(self):
45+
self.create_new_recipient()
46+
john = BaseTestLive.get_john_sca_owner()
47+
fetched = Recipient.get_user_recipients(john.id, RecipientScope='PAYOUT')
48+
49+
self.assertIsNotNone(fetched)
50+
self.assertIsInstance(fetched.data, list)
51+
self.assertTrue(len(fetched.data) > 0)
52+
53+
def test_get_user_recipients_filtered_payin(self):
54+
self.create_new_recipient()
55+
john = BaseTestLive.get_john_sca_owner()
56+
fetched = Recipient.get_user_recipients(john.id, RecipientScope='PAYIN')
57+
58+
self.assertIsNotNone(fetched)
59+
self.assertIsInstance(fetched.data, list)
60+
self.assertTrue(len(fetched.data) == 0)
61+
4462
def test_get_recipient_schema_local_bank_transfer_individual(self):
4563
schema = RecipientSchema.get('LocalBankTransfer', 'Individual', 'GBP', 'GB')
4664

tests/test_users.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
from mangopay.exceptions import APIError
1010
from mangopay.resources import (User, NaturalUser, Wallet,
11-
LegalUser, Transfer, Transaction, NaturalUserSca, LegalUserSca)
12-
from mangopay.utils import Money, Address
11+
LegalUser, Transfer, Transaction, NaturalUserSca, LegalUserSca,
12+
UserDataFormatValidation)
13+
from mangopay.utils import Money, Address, CompanyNumberValidation
1314
from tests import settings
1415
from tests.mocks import today, today_timestamp
1516
from tests.test_base import BaseTest, BaseTestLive
@@ -1000,3 +1001,15 @@ def test_users_GetTransactionsSca(self):
10001001
self.assertTrue('PendingUserAction RedirectUrl' in ex.headers.get('www-authenticate'))
10011002
except Exception as ex:
10021003
self.assertTrue('PendingUserAction RedirectUrl' in ex.headers.get('www-authenticate'))
1004+
1005+
def test_validate_user_data_format(self):
1006+
validation = UserDataFormatValidation()
1007+
validation.company_number = CompanyNumberValidation(company_number='AB123456', country_code='IT')
1008+
result = validation.save()
1009+
self.assertIsNotNone(result['company_number'])
1010+
1011+
try:
1012+
validation.company_number = CompanyNumberValidation(company_number='123')
1013+
validation.save()
1014+
except APIError as e:
1015+
self.assertTrue("One or several required parameters are missing or incorrect" in e.content['Message'])

0 commit comments

Comments
 (0)