Commit ade9c891 authored by Rafael Monnerat's avatar Rafael Monnerat

slapos_wechat: Fix tests and some scripts

Add a mock for absolute_url as the test requires a valid url
parent 27baf711
# Copyright (c) 2002-2012 Nexedi SA and Contributors. All Rights Reserved. # -*- coding:utf-8 -*-
##############################################################################
#
# Copyright (c) 2019 Nexedi SA and Contributors. All Rights Reserved.
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
##############################################################################
from erp5.component.test.SlapOSTestCaseMixin import SlapOSTestCaseMixinWithAbort from erp5.component.test.SlapOSTestCaseMixin import SlapOSTestCaseMixinWithAbort
from DateTime import DateTime from DateTime import DateTime
from zExceptions import Unauthorized from zExceptions import Unauthorized
import transaction
from Products.ERP5Type.tests.utils import createZODBPythonScript from Products.ERP5Type.tests.utils import createZODBPythonScript
class TestSlapOSCurrency_getIntegrationMapping(SlapOSTestCaseMixinWithAbort): class TestSlapOSCurrency_getIntegrationMapping(SlapOSTestCaseMixinWithAbort):
...@@ -644,27 +664,23 @@ return dict(vads_url_already_registered="%s/already_registered" % (payment_trans ...@@ -644,27 +664,23 @@ return dict(vads_url_already_registered="%s/already_registered" % (payment_trans
self.login(person.getUserId()) self.login(person.getUserId())
self._simulatePaymentTransaction_getVADSUrlDict() self._simulatePaymentTransaction_getVADSUrlDict()
try: try:
text_content = payment.PaymentTransaction_redirectToManualWechatPayment() def mock_absolute_url():
return "http://example.org"
original_method = self.portal.absolute_url
self.portal.absolute_url = mock_absolute_url
try:
redirected_url = payment.PaymentTransaction_redirectToManualWechatPayment()
finally:
self.portal.absolute_url = original_method
finally: finally:
self._dropPaymentTransaction_getVADSUrlDict() self._dropPaymentTransaction_getVADSUrlDict()
payment_transaction_url = payment.getRelativeUrl()
for item in ["vads_site_id", payment_transaction_id = payment.getId()
payment_transaction_url, expected = "http://example.org/#wechat_payment?trade_no=%s&price=1&payment_url=weixin://wxpay/bizpayurl?pr=" % payment_transaction_id
"vads_url_cancel", self.assertTrue(redirected_url.startswith(expected),
"%s/cancel" % (payment_transaction_url), "%s do not start with %s" % (redirected_url, expected))
"vads_url_error", transaction.abort()
"%s/error" % (payment_transaction_url),
"vads_url_referral",
"%s/referral" % (payment_transaction_url),
"vads_url_refused",
"%s/refused" % (payment_transaction_url),
"vads_url_success",
"%s/success" % (payment_transaction_url),
"vads_url_return",
"%s/return" % (payment_transaction_url)]:
self.assertTrue(item in text_content,
"%s not in %s" % (item, text_content))
def test_PaymentTransaction_redirectToManualWechatPayment_already_registered(self): def test_PaymentTransaction_redirectToManualWechatPayment_already_registered(self):
......
# Copyright (c) 2002-2012 Nexedi SA and Contributors. All Rights Reserved. # -*- coding:utf-8 -*-
##############################################################################
#
# Copyright (c) 2002-2018 Nexedi SA and Contributors. All Rights Reserved.
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
##############################################################################
from erp5.component.test.SlapOSTestCaseMixin import SlapOSTestCaseMixinWithAbort from erp5.component.test.SlapOSTestCaseMixin import SlapOSTestCaseMixinWithAbort
from DateTime import DateTime from DateTime import DateTime
...@@ -7,13 +26,6 @@ import difflib ...@@ -7,13 +26,6 @@ import difflib
HARDCODED_PRICE = 99.6 HARDCODED_PRICE = 99.6
vads_url_cancel = 'http://example.org/cancel'
vads_url_error = 'http://example.org/error'
vads_url_referral = 'http://example.org/referral'
vads_url_refused = 'http://example.org/refused'
vads_url_success = 'http://example.org/success'
vads_url_return = 'http://example.org/return'
class TestSlapOSWechatInterfaceWorkflow(SlapOSTestCaseMixinWithAbort): class TestSlapOSWechatInterfaceWorkflow(SlapOSTestCaseMixinWithAbort):
def _simulatePaymentTransaction_getTotalPayablePrice(self): def _simulatePaymentTransaction_getTotalPayablePrice(self):
...@@ -30,116 +42,40 @@ class TestSlapOSWechatInterfaceWorkflow(SlapOSTestCaseMixinWithAbort): ...@@ -30,116 +42,40 @@ class TestSlapOSWechatInterfaceWorkflow(SlapOSTestCaseMixinWithAbort):
if script_name in self.portal.portal_skins.custom.objectIds(): if script_name in self.portal.portal_skins.custom.objectIds():
self.portal.portal_skins.custom.manage_delObjects(script_name) self.portal.portal_skins.custom.manage_delObjects(script_name)
def test_generateManualPaymentPage_mandatoryParameters(self):
event = self.createWechatEvent()
# vads_url_cancel
self.assertRaises(TypeError, event.generateManualPaymentPage,
vads_url_error=vads_url_error,
vads_url_referral=vads_url_referral,
vads_url_refused=vads_url_refused,
vads_url_success=vads_url_success,
vads_url_return=vads_url_return,
)
# vads_url_error
self.assertRaises(TypeError, event.generateManualPaymentPage,
vads_url_cancel=vads_url_cancel,
vads_url_referral=vads_url_referral,
vads_url_refused=vads_url_refused,
vads_url_success=vads_url_success,
vads_url_return=vads_url_return,
)
# vads_url_referral
self.assertRaises(TypeError, event.generateManualPaymentPage,
vads_url_cancel=vads_url_cancel,
vads_url_error=vads_url_error,
vads_url_refused=vads_url_refused,
vads_url_success=vads_url_success,
vads_url_return=vads_url_return,
)
# vads_url_refused
self.assertRaises(TypeError, event.generateManualPaymentPage,
vads_url_cancel=vads_url_cancel,
vads_url_error=vads_url_error,
vads_url_referral=vads_url_referral,
vads_url_success=vads_url_success,
vads_url_return=vads_url_return,
)
# vads_url_success
self.assertRaises(TypeError, event.generateManualPaymentPage,
vads_url_cancel=vads_url_cancel,
vads_url_error=vads_url_error,
vads_url_referral=vads_url_referral,
vads_url_refused=vads_url_refused,
vads_url_return=vads_url_return,
)
# vads_url_return
self.assertRaises(TypeError, event.generateManualPaymentPage,
vads_url_cancel=vads_url_cancel,
vads_url_error=vads_url_error,
vads_url_referral=vads_url_referral,
vads_url_refused=vads_url_refused,
vads_url_success=vads_url_success,
)
def test_generateManualPaymentPage_noAccountingTransaction(self): def test_generateManualPaymentPage_noAccountingTransaction(self):
event = self.createWechatEvent() event = self.createWechatEvent()
self.assertRaises(AttributeError, event.generateManualPaymentPage, self.assertRaises(AttributeError, event.generateManualPaymentPage)
vads_url_cancel=vads_url_cancel,
vads_url_error=vads_url_error,
vads_url_referral=vads_url_referral,
vads_url_refused=vads_url_refused,
vads_url_success=vads_url_success,
vads_url_return=vads_url_return,
)
def test_generateManualPaymentPage_registeredTransaction(self): def test_generateManualPaymentPage_registeredTransaction(self):
event = self.createWechatEvent() event = self.createWechatEvent()
payment = self.createPaymentTransaction() payment = self.createPaymentTransaction()
event.edit(destination_value=payment) event.edit(destination_value=payment)
_ , _ = payment.PaymentTransaction_generateWechatId() _ , _ = payment.PaymentTransaction_generateWechatId()
self.assertRaises(ValueError, event.generateManualPaymentPage, self.assertRaises(ValueError, event.generateManualPaymentPage)
vads_url_cancel=vads_url_cancel,
vads_url_error=vads_url_error,
vads_url_referral=vads_url_referral,
vads_url_refused=vads_url_refused,
vads_url_success=vads_url_success,
vads_url_return=vads_url_return,
)
def test_generateManualPaymentPage_noPaymentService(self): def test_generateManualPaymentPage_noPaymentService(self):
event = self.createWechatEvent() event = self.createWechatEvent()
payment = self.createPaymentTransaction() payment = self.createPaymentTransaction()
event.edit(destination_value=payment) event.edit(destination_value=payment)
self.assertRaises(AttributeError, event.generateManualPaymentPage, self.assertRaises(AttributeError, event.generateManualPaymentPage)
vads_url_cancel=vads_url_cancel,
vads_url_error=vads_url_error,
vads_url_referral=vads_url_referral,
vads_url_refused=vads_url_refused,
vads_url_success=vads_url_success,
vads_url_return=vads_url_return,
)
def test_generateManualPaymentPage_noCurrency(self): def test_generateManualPaymentPage_noCurrency(self):
event = self.createWechatEvent() event = self.createWechatEvent()
payment = self.createPaymentTransaction() payment = self.createPaymentTransaction()
payment.edit(
resource_value=None
)
event.edit( event.edit(
destination_value=payment, destination_value=payment,
source="portal_secure_payments/slapos_wechat_test", source="portal_secure_payments/slapos_wechat_test",
) )
self.assertRaises(AttributeError, event.generateManualPaymentPage, self.assertRaises(AttributeError, event.generateManualPaymentPage)
vads_url_cancel=vads_url_cancel,
vads_url_error=vads_url_error,
vads_url_referral=vads_url_referral,
vads_url_refused=vads_url_refused,
vads_url_success=vads_url_success,
vads_url_return=vads_url_return,
)
def test_generateManualPaymentPage_defaultUseCase(self): def test_generateManualPaymentPage_defaultUseCase(self):
event = self.createWechatEvent() event = self.createWechatEvent()
payment = self.createPaymentTransaction() payment = self.createPaymentTransaction()
payment.edit( payment.edit(
resource="currency_module/EUR", resource="currency_module/CNY",
) )
event.edit( event.edit(
destination_value=payment, destination_value=payment,
...@@ -149,14 +85,7 @@ class TestSlapOSWechatInterfaceWorkflow(SlapOSTestCaseMixinWithAbort): ...@@ -149,14 +85,7 @@ class TestSlapOSWechatInterfaceWorkflow(SlapOSTestCaseMixinWithAbort):
before_date = DateTime() before_date = DateTime()
self._simulatePaymentTransaction_getTotalPayablePrice() self._simulatePaymentTransaction_getTotalPayablePrice()
try: try:
event.generateManualPaymentPage( event.generateManualPaymentPage()
vads_url_cancel=vads_url_cancel,
vads_url_error=vads_url_error,
vads_url_referral=vads_url_referral,
vads_url_refused=vads_url_refused,
vads_url_success=vads_url_success,
vads_url_return=vads_url_return,
)
finally: finally:
self._dropPaymentTransaction_getTotalPayablePrice() self._dropPaymentTransaction_getTotalPayablePrice()
after_date = DateTime() after_date = DateTime()
...@@ -175,19 +104,10 @@ class TestSlapOSWechatInterfaceWorkflow(SlapOSTestCaseMixinWithAbort): ...@@ -175,19 +104,10 @@ class TestSlapOSWechatInterfaceWorkflow(SlapOSTestCaseMixinWithAbort):
self.assertEqual(event.getValidationState(), "acknowledged") self.assertEqual(event.getValidationState(), "acknowledged")
data_dict = { data_dict = {
'vads_language': 'en', 'out_trade_no': payment.getId().encode('utf-8'),
'vads_url_cancel': vads_url_cancel, 'total_fee': 1, #str(int(round((payment_transaction.PaymentTransaction_getTotalPayablePrice() * -100), 0))),
'vads_url_error': vads_url_error, 'fee_type': 'CNY',
'vads_url_referral': vads_url_referral, 'body': "Rapid Space Virtual Machine".encode('utf-8')
'vads_url_refused': vads_url_refused,
'vads_url_success': vads_url_success,
'vads_url_return': vads_url_return,
'vads_trans_date': payment.getStartDate().toZone('UTC')\
.asdatetime().strftime('%Y%m%d%H%M%S'),
'vads_amount': str(int(HARDCODED_PRICE * -100)),
'vads_currency': 978,
'vads_trans_id': transaction_id,
'vads_site_id': 'foo',
} }
# Calculate the signature... # Calculate the signature...
self.portal.portal_secure_payments.slapos_wechat_test._getFieldList(data_dict) self.portal.portal_secure_payments.slapos_wechat_test._getFieldList(data_dict)
...@@ -315,11 +235,11 @@ portal_workflow.doActionFor(context, action='edit_action', comment='Visited by W ...@@ -315,11 +235,11 @@ portal_workflow.doActionFor(context, action='edit_action', comment='Visited by W
self.assertEqual(len(event_message_list), 2) self.assertEqual(len(event_message_list), 2)
sent_message = [x for x in event_message_list \ sent_message = [x for x in event_message_list \
if x.getTitle() == 'Sent SOAP'][0] if x.getTitle() == 'Query Order Status'][0]
self.assertEqual(sent_message.getTextContent(), mocked_sent_text) self.assertEqual(sent_message.getTextContent(), mocked_sent_text)
received_message = [x for x in event_message_list \ received_message = [x for x in event_message_list \
if x.getTitle() == 'Received SOAP'][0] if x.getTitle() == 'Received Order Status'][0]
self.assertEqual(received_message.getPredecessor(), self.assertEqual(received_message.getPredecessor(),
sent_message.getRelativeUrl()) sent_message.getRelativeUrl())
self.assertEqual(received_message.getTextContent(), mocked_received_text) self.assertEqual(received_message.getTextContent(), mocked_received_text)
......
from DateTime import DateTime from DateTime import DateTime
wechat_event = state_change['object'] wechat_event = state_change['object']
# Get required arguments
#kwargs = state_change.kwargs
# Required args
# Raise TypeError if all parameters are not provided
#try:
# vads_url_cancel = kwargs['vads_url_cancel']
# vads_url_error = kwargs['vads_url_error']
# vads_url_referral = kwargs['vads_url_referral']
# vads_url_refused = kwargs['vads_url_refused']
# vads_url_success = kwargs['vads_url_success']
# vads_url_return = kwargs['vads_url_return']
#except KeyError:
# raise TypeError, "PayzenEvent_generateNavigationPage takes exactly 6 arguments"
payment_transaction = wechat_event.getDestinationValue(portal_type="Payment Transaction") payment_transaction = wechat_event.getDestinationValue(portal_type="Payment Transaction")
now = DateTime() now = DateTime()
payment_transaction.AccountingTransaction_updateStartDate(now) payment_transaction.AccountingTransaction_updateStartDate(now)
...@@ -25,7 +10,7 @@ if transaction_id is None: ...@@ -25,7 +10,7 @@ if transaction_id is None:
raise ValueError, "Transaction already registered" raise ValueError, "Transaction already registered"
wechat_dict = { wechat_dict = {
'out_trade_no': payment_transaction.getRelativeUrl().split('/')[1].encode('utf-8'), 'out_trade_no': payment_transaction.getId().encode('utf-8'),
'total_fee': str(int(round((payment_transaction.PaymentTransaction_getTotalPayablePrice() * -100), 0))), 'total_fee': str(int(round((payment_transaction.PaymentTransaction_getTotalPayablePrice() * -100), 0))),
'fee_type': payment_transaction.getResourceValue().Currency_getIntegrationMapping(), 'fee_type': payment_transaction.getResourceValue().Currency_getIntegrationMapping(),
'body': "Rapid Space Virtual Machine".encode('utf-8') 'body': "Rapid Space Virtual Machine".encode('utf-8')
......
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="TransitionDefinition" module="Products.DCWorkflow.Transitions"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>actbox_category</string> </key>
<value> <string>workflow</string> </value>
</item>
<item>
<key> <string>actbox_icon</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>actbox_name</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>actbox_url</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>after_script_name</string> </key>
<value> <string>WechatEvent_registerWechat</string> </value>
</item>
<item>
<key> <string>description</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>guard</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>register_wechat</string> </value>
</item>
<item>
<key> <string>new_state_id</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>script_name</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>trigger_type</string> </key>
<value> <int>2</int> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment