Commit 88168820 authored by Rafael Monnerat's avatar Rafael Monnerat

slapos_subscription_request: Only cancel once the Subscription Request is at least 24 hours old

  Add tests for SubscriptionRequest_verifyPaymentTransaction
parent 49791ed9
...@@ -9,7 +9,11 @@ if hosting_subscription is None: ...@@ -9,7 +9,11 @@ if hosting_subscription is None:
if hosting_subscription is not None: if hosting_subscription is not None:
instance = hosting_subscription.getPredecessorValue() instance = hosting_subscription.getPredecessorValue()
# This ensure that the user has a valid cloud contract
# This ensure that the user has a valid cloud contract.
# At this stage he already have a paied invoice for the reservation,
# so the cloud contact will be just created.
instance.SoftwareInstance_requestValidationPayment() instance.SoftwareInstance_requestValidationPayment()
# create a Deduction for his fee # create a Deduction for his fee
......
from DateTime import DateTime
if context.getSimulationState() != "draft": if context.getSimulationState() != "draft":
return return
...@@ -5,6 +7,7 @@ sale_invoice_transaction = context.getCausalityValue( ...@@ -5,6 +7,7 @@ sale_invoice_transaction = context.getCausalityValue(
portal_type="Sale Invoice Transaction") portal_type="Sale Invoice Transaction")
if sale_invoice_transaction is None: if sale_invoice_transaction is None:
if context.getCreationDate() < DateTime() - 1:
context.cancel(comment="No sale invoice transaction attached, so subscription is cancelled") context.cancel(comment="No sale invoice transaction attached, so subscription is cancelled")
return return
......
...@@ -22,6 +22,7 @@ from erp5.component.test.SlapOSTestCaseMixin import \ ...@@ -22,6 +22,7 @@ from erp5.component.test.SlapOSTestCaseMixin import \
SlapOSTestCaseMixinWithAbort, simulate SlapOSTestCaseMixinWithAbort, simulate
from zExceptions import Unauthorized from zExceptions import Unauthorized
from Products.ERP5Type.TransactionalVariable import getTransactionalVariable from Products.ERP5Type.TransactionalVariable import getTransactionalVariable
from DateTime import DateTime
class TestSubscriptionSkinsMixin(SlapOSTestCaseMixinWithAbort): class TestSubscriptionSkinsMixin(SlapOSTestCaseMixinWithAbort):
...@@ -435,7 +436,6 @@ assert password""") ...@@ -435,7 +436,6 @@ assert password""")
self.assertNotEqual(subscriber_role.getStartDate(), None) self.assertNotEqual(subscriber_role.getStartDate(), None)
self.assertNotEqual(member_role.getStopDate(), None) self.assertNotEqual(member_role.getStopDate(), None)
from DateTime import DateTime
self.assertTrue(subscriber_role.getStartDate() < DateTime()) self.assertTrue(subscriber_role.getStartDate() < DateTime())
self.assertTrue(member_role.getStopDate() > DateTime() + 365*5) self.assertTrue(member_role.getStopDate() > DateTime() + 365*5)
self.assertTrue(subscriber_role.getStartDate() < DateTime()) self.assertTrue(subscriber_role.getStartDate() < DateTime())
...@@ -906,7 +906,167 @@ class TestSubscriptionRequest_notifyInstanceIsReady(TestSubscriptionSkinsMixin): ...@@ -906,7 +906,167 @@ class TestSubscriptionRequest_notifyInstanceIsReady(TestSubscriptionSkinsMixin):
# def test(self): # def test(self):
# raise # raise
class TestSubscriptionRequest_verifyPaymentTransaction(TestSubscriptionSkinsMixin):
def test_no_sale_invoice(self):
person = self.makePerson()
subscription_request = self.newSubscriptionRequest(
quantity=1, destination_section_value=person)
# Too early to cancel
self.assertEqual(subscription_request.SubscriptionRequest_verifyPaymentTransaction(),
None)
self.assertEqual(subscription_request.getSimulationState(),
"draft")
def getCreationDate(self):
return DateTime() - 1.1
from Products.ERP5Type.Base import Base
original_get_creation = Base.getCreationDate
Base.getCreationDate = getCreationDate
try:
self.assertEqual(subscription_request.SubscriptionRequest_verifyPaymentTransaction(),
None)
finally:
Base.getCreationDate = original_get_creation
self.assertEqual(subscription_request.getSimulationState(),
"cancelled")
def _test_cancel_due_payment_state(self, state="draft"):
email = "abc%s@nexedi.com" % self.new_id
name = "Cous Cous %s" % self.new_id
person, _ = self.portal.SubscriptionRequest_createUser(name=name, email=email)
self.tic()
subscription_request = self.newSubscriptionRequest(
quantity=1, destination_section_value=person,
default_email_text="abc%s@nexedi.com" % self.new_id)
invoice_template_path = "accounting_module/template_pre_payment_subscription_sale_invoice_transaction"
invoice_template = self.portal.restrictedTraverse(invoice_template_path)
payment_template = self.portal.restrictedTraverse("accounting_module/slapos_pre_payment_template")
# Too early to cancel
self.assertEqual(subscription_request.SubscriptionRequest_verifyPaymentTransaction(), None)
self.assertEqual(subscription_request.getSimulationState(), "draft")
current_invoice = invoice_template.Base_createCloneDocument(batch_mode=1)
current_payment = payment_template.Base_createCloneDocument(batch_mode=1)
current_invoice.start()
subscription_request.edit(causality_value=current_invoice)
current_payment.setCausalityValue(current_invoice)
if state == "cancelled":
current_payment.cancel()
elif state == "deleted":
current_payment.delete()
self.tic()
self.assertEqual(subscription_request.SubscriptionRequest_verifyPaymentTransaction(),
None)
self.assertEqual(current_invoice.getSimulationState(), "cancelled")
self.assertEqual(subscription_request.getSimulationState(),
"cancelled")
def test_draft_payment_state(self):
self._test_cancel_due_payment_state()
def test_cancelled_payment_state(self):
self._test_cancel_due_payment_state(state="cancelled")
def test_deleted_payment_state(self):
self._test_cancel_due_payment_state(state="deleted")
def test_stopped_payment_state(self, state="draft"):
email = "abc%s@nexedi.com" % self.new_id
name = "Cous Cous %s" % self.new_id
person, _ = self.portal.SubscriptionRequest_createUser(name=name, email=email)
self.tic()
subscription_request = self.newSubscriptionRequest(
quantity=1, destination_section_value=person,
default_email_text="abc%s@nexedi.com" % self.new_id)
invoice_template_path = "accounting_module/template_pre_payment_subscription_sale_invoice_transaction"
invoice_template = self.portal.restrictedTraverse(invoice_template_path)
payment_template = self.portal.restrictedTraverse("accounting_module/slapos_pre_payment_template")
# Too early to cancel
self.assertEqual(subscription_request.SubscriptionRequest_verifyPaymentTransaction(), None)
self.assertEqual(subscription_request.getSimulationState(), "draft")
current_invoice = invoice_template.Base_createCloneDocument(batch_mode=1)
current_payment = payment_template.Base_createCloneDocument(batch_mode=1)
current_invoice.confirm()
current_invoice.start()
current_invoice.stop()
subscription_request.edit(causality_value=current_invoice)
current_payment.setCausalityValue(current_invoice)
current_payment.confirm()
current_payment.start()
self.tic()
self.assertEqual(subscription_request.SubscriptionRequest_verifyPaymentTransaction(),
None)
self.assertEqual(subscription_request.getSimulationState(),
"draft")
current_payment.stop()
self.assertEqual(subscription_request.SubscriptionRequest_verifyPaymentTransaction(),
None)
self.assertEqual(subscription_request.getSimulationState(),
"planned")
def _test_cancel_due_sale_invoice_state(self, state="draft"):
email = "abc%s@nexedi.com" % self.new_id
name = "Cous Cous %s" % self.new_id
person, _ = self.portal.SubscriptionRequest_createUser(name=name, email=email)
self.tic()
subscription_request = self.newSubscriptionRequest(
quantity=1, destination_section_value=person,
default_email_text="abc%s@nexedi.com" % self.new_id)
invoice_template_path = "accounting_module/template_pre_payment_subscription_sale_invoice_transaction"
invoice_template = self.portal.restrictedTraverse(invoice_template_path)
# Too early to cancel
self.assertEqual(subscription_request.SubscriptionRequest_verifyPaymentTransaction(),
None)
self.assertEqual(subscription_request.getSimulationState(),
"draft")
current_invoice = invoice_template.Base_createCloneDocument(batch_mode=1)
subscription_request.edit(causality_value=current_invoice)
if state == "cancelled":
current_invoice.cancel()
elif state == "deleted":
current_invoice.delete()
self.assertEqual(subscription_request.SubscriptionRequest_verifyPaymentTransaction(),
None)
self.assertEqual(subscription_request.getSimulationState(),
"cancelled")
def test_draft_sale_invoice_state(self):
self._test_cancel_due_sale_invoice_state()
def test_cancelled_sale_invoice_state(self):
self._test_cancel_due_sale_invoice_state(state="cancelled")
def test_deleted_sale_invoice_state(self):
self._test_cancel_due_sale_invoice_state(state="deleted")
\ No newline at end of file
#class SubscriptionRequest_verifyPaymentTransaction(TestSubscriptionSkinsMixin):
# def test_not_implemented(self):
# raise
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