def test_sms_to_chile(self):
     """We can get the price to a subscriber of an SMS sent to Chile."""
     expected_cost = 1000
     number = ''.join(['56', '1235554567'])
     actual_cost = get_sms_cost('off_network_send',
                                destination_number=number)
     self.assertEqual(expected_cost, actual_cost)
 def POST(self):
     """Handles POST requests."""
     data = web.input()
     if ('from_name' not in data or 'service_type' not in data or
             'destination' not in data):
         raise web.BadRequest()
     # Process the CDR data.
     cost_in_credits = billing.get_sms_cost(
         data.service_type, destination_number=data.destination)
     try:
         old_balance = subscriber.get_account_balance(data.from_name)
         subscriber.subtract_credit(data.from_name, str(cost_in_credits))
     except SubscriberNotFound:
         # The subscriber does not exist yet but has sent an SMS
         if data.service_type == 'free_sms':
             # But that is OK for a free service like provisioning
             old_balance = 0
         else:
             raise
     reason = "SMS sent to %s (%s)" % (data.destination, data.service_type)
     events.create_sms_event(
         data.from_name, old_balance, cost_in_credits, reason,
         data.destination, from_imsi=data.from_name,
         from_number=data.from_number)
     # If this was an in-network event, find the cost for the recipient.  We
     # can lookup the recipient by data.destination (the "to number").
     if 'local' in data.service_type:
         recipient_imsi = subscriber.get_imsi_from_number(data.destination)
         old_balance = subscriber.get_account_balance(recipient_imsi)
         cost_in_credits = billing.get_sms_cost(
             'local_recv_sms', destination_number=data.destination)
         subscriber.subtract_credit(recipient_imsi, str(cost_in_credits))
         reason = "SMS received from %s (local_recv_sms)" % data.from_name
         events.create_sms_event(
             recipient_imsi, old_balance, cost_in_credits, reason,
             data.destination, from_imsi=data.from_name,
             from_number=data.from_number)
     # Return 200 OK.
     headers = {
         'Content-type': 'text/plain'
     }
     raise web.OK(None, headers)
    def test_nonexistent_prefix(self):
        """If the prefix doesn't exist, it's free.

        The prefix price key might not exist if, say, the billing tier data
        has not yet been loaded.
        """
        expected_cost = 0
        number = ''.join(['9999', '1235554567'])
        actual_cost = get_sms_cost('off_network_send',
                                   destination_number=number)
        self.assertEqual(expected_cost, actual_cost)
Example #4
0
 def bill(self, to_number, from_number):
     try:
         if from_number == DASHBOARD_FROM_NUMBER:
             self.tariff_type = 'free_sms'
         tariff = billing.get_sms_cost(self.tariff_type,
                                       destination_number=to_number)
         username = subscriber.get_imsi_from_number(to_number)
         if username:
             reason = 'SMS from %s to %s (incoming_sms)' % (
                 from_number, username)
             old_balance = subscriber.get_account_balance(username)
             subscriber.subtract_credit(username, str(int(tariff)))
             events.create_sms_event(username, old_balance, tariff, reason,
                                     to_number, from_number=from_number)
     except Exception as e:
         logger.error("Endaga bill error:" + traceback.format_exc(e))
 def test_on_receive_sms(self):
     """We can get the subscriber price for an on-network received SMS."""
     expected_cost = 500
     self.assertEqual(expected_cost, get_sms_cost('on_network_receive'))
 def test_on_send_sms(self):
     """We can get the subscriber price for an on-network sent SMS."""
     expected_cost = 400
     self.assertEqual(expected_cost, get_sms_cost('on_network_send'))