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 DateTime import DateTime
from zExceptions import Unauthorized
import transaction
from Products.ERP5Type.tests.utils import createZODBPythonScript
class TestSlapOSCurrency_getIntegrationMapping(SlapOSTestCaseMixinWithAbort):
......@@ -644,27 +664,23 @@ return dict(vads_url_already_registered="%s/already_registered" % (payment_trans
self.login(person.getUserId())
self._simulatePaymentTransaction_getVADSUrlDict()
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:
self._dropPaymentTransaction_getVADSUrlDict()
payment_transaction_url = payment.getRelativeUrl()
for item in ["vads_site_id",
payment_transaction_url,
"vads_url_cancel",
"%s/cancel" % (payment_transaction_url),
"vads_url_error",
"%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))
payment_transaction_id = payment.getId()
expected = "http://example.org/#wechat_payment?trade_no=%s&price=1&payment_url=weixin://wxpay/bizpayurl?pr=" % payment_transaction_id
self.assertTrue(redirected_url.startswith(expected),
"%s do not start with %s" % (redirected_url, expected))
transaction.abort()
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 DateTime import DateTime
......@@ -7,13 +26,6 @@ import difflib
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):
def _simulatePaymentTransaction_getTotalPayablePrice(self):
......@@ -30,116 +42,40 @@ class TestSlapOSWechatInterfaceWorkflow(SlapOSTestCaseMixinWithAbort):
if script_name in self.portal.portal_skins.custom.objectIds():
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):
event = self.createWechatEvent()
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,
)
self.assertRaises(AttributeError, event.generateManualPaymentPage)
def test_generateManualPaymentPage_registeredTransaction(self):
event = self.createWechatEvent()
payment = self.createPaymentTransaction()
event.edit(destination_value=payment)
_ , _ = payment.PaymentTransaction_generateWechatId()
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,
)
self.assertRaises(ValueError, event.generateManualPaymentPage)
def test_generateManualPaymentPage_noPaymentService(self):
event = self.createWechatEvent()
payment = self.createPaymentTransaction()
event.edit(destination_value=payment)
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,
)
self.assertRaises(AttributeError, event.generateManualPaymentPage)
def test_generateManualPaymentPage_noCurrency(self):
event = self.createWechatEvent()
payment = self.createPaymentTransaction()
payment.edit(
resource_value=None
)
event.edit(
destination_value=payment,
source="portal_secure_payments/slapos_wechat_test",
)
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,
)
self.assertRaises(AttributeError, event.generateManualPaymentPage)
def test_generateManualPaymentPage_defaultUseCase(self):
event = self.createWechatEvent()
payment = self.createPaymentTransaction()
payment.edit(
resource="currency_module/EUR",
resource="currency_module/CNY",
)
event.edit(
destination_value=payment,
......@@ -149,14 +85,7 @@ class TestSlapOSWechatInterfaceWorkflow(SlapOSTestCaseMixinWithAbort):
before_date = DateTime()
self._simulatePaymentTransaction_getTotalPayablePrice()
try:
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,
)
event.generateManualPaymentPage()
finally:
self._dropPaymentTransaction_getTotalPayablePrice()
after_date = DateTime()
......@@ -175,19 +104,10 @@ class TestSlapOSWechatInterfaceWorkflow(SlapOSTestCaseMixinWithAbort):
self.assertEqual(event.getValidationState(), "acknowledged")
data_dict = {
'vads_language': 'en',
'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,
'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',
'out_trade_no': payment.getId().encode('utf-8'),
'total_fee': 1, #str(int(round((payment_transaction.PaymentTransaction_getTotalPayablePrice() * -100), 0))),
'fee_type': 'CNY',
'body': "Rapid Space Virtual Machine".encode('utf-8')
}
# Calculate the signature...
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
self.assertEqual(len(event_message_list), 2)
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)
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(),
sent_message.getRelativeUrl())
self.assertEqual(received_message.getTextContent(), mocked_received_text)
......
from DateTime import DateTime
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")
now = DateTime()
payment_transaction.AccountingTransaction_updateStartDate(now)
......@@ -25,7 +10,7 @@ if transaction_id is None:
raise ValueError, "Transaction already registered"
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))),
'fee_type': payment_transaction.getResourceValue().Currency_getIntegrationMapping(),
'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