Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Commit

Permalink
Added tests for outbound credit cutoff messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudi Giesler committed Mar 4, 2015
1 parent 5d8bfb1 commit 0b665d3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
11 changes: 6 additions & 5 deletions go/vumitools/billing_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def process_outbound(self, config, msg, connector_name):
transaction = yield self.create_transaction_for_outbound(msg)
msg_mdh.set_paid()
if transaction.get('credit_cutoff_reached', False):
self._handle_credit_cutoff(msg)
msg = self._handle_credit_cutoff(msg)
except BillingError:
log.warning(
"BillingError for outbound message, sending without billing:"
Expand All @@ -280,11 +280,12 @@ def process_outbound(self, config, msg, connector_name):
msg, self.receive_inbound_connector, None)

def _handle_credit_cutoff(self, msg):
if msg.session_event is not None:
msg.session_event = SESSION_CLOSE
msg.content = self.credit_limit_message
if msg.get('session_event') is not None:
msg['session_event'] = SESSION_CLOSE
msg['content'] = self.credit_limit_message
return msg
else:
msg = None
return None

@inlineCallbacks
def process_event(self, config, event, connector_name):
Expand Down
40 changes: 39 additions & 1 deletion go/vumitools/tests/test_billing_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from twisted.internet.defer import inlineCallbacks, returnValue
from twisted.web.client import Agent, Request, Response

from vumi.message import TransportUserMessage
from vumi.tests.helpers import VumiTestCase
from vumi.tests.utils import LogCatcher
from vumi.utils import mkheaders, StringProducer
Expand All @@ -17,11 +18,14 @@
from go.billing.api import BillingError
from go.billing.utils import JSONEncoder

SESSION_CLOSE = TransportUserMessage.SESSION_CLOSE


class BillingApiMock(object):

def __init__(self):
def __init__(self, credit_cutoff=False):
self.transactions = []
self.credit_cutoff = credit_cutoff

def _record(self, items, vars):
del vars["self"]
Expand Down Expand Up @@ -50,6 +54,7 @@ def create_transaction(self, account_number, message_id, tag_pool_name,
"status": "Completed",
"transaction_type": transaction_type,
"session_length": session_length,
"credit_cutoff_reached": self.credit_cutoff
}


Expand Down Expand Up @@ -739,3 +744,36 @@ def test_outbound_message_session_length_custom_field(self):
"outbound",
session_created=False,
session_metadata_field='foo')

@inlineCallbacks
def test_outbound_message_credit_cutoff_session(self):
self.billing_api = BillingApiMock(credit_cutoff=True)
dispatcher = yield self.get_dispatcher()

yield self.make_dispatch_outbound(
"outbound",
user_account="12345",
tag=("pool1", "1234"),
helper_metadata={},
session_event='new'
)

published_msg = self.ri_helper.get_dispatched_outbound()[0]
self.assertEqual(published_msg['session_event'], SESSION_CLOSE)
self.assertEqual(
published_msg['content'], dispatcher.credit_limit_message)

@inlineCallbacks
def test_outbound_message_credit_cutoff_message(self):
self.billing_api = BillingApiMock(credit_cutoff=True)
yield self.get_dispatcher()

yield self.make_dispatch_outbound(
"outbound",
user_account="12345",
tag=("pool1", "1234"),
helper_metadata={},
)

published_msgs = self.ri_helper.get_dispatched_outbound()
self.assertEqual(len(published_msgs), 0)

0 comments on commit 0b665d3

Please sign in to comment.