def setUpClass(cls):
     """Setup a User, BTS and Subscriber."""
     cls.user = models.User(username="******", email="*****@*****.**")
     cls.user.save()
     cls.user_profile = models.UserProfile.objects.get(user=cls.user)
     cls.bts = models.BTS(uuid="88332", nickname="test-bts-name",
                          inbound_url="http://localhost/88332/test",
                          network=cls.user_profile.network)
     cls.bts.save()
     cls.imsi = 'IMSI000553'
     cls.subscriber = models.Subscriber.objects.create(
         balance=20000, name='test-sub-name', imsi=cls.imsi,
         network=cls.bts.network)
     cls.subscriber.save()
     cls.incoming_event = {
         'date': '2015-03-15 13:32:17',
         'imsi': cls.imsi,
         'oldamt': 1000,
         'newamt': 1000,
         'change': 0,
         'reason': 'GPRS usage',
         'kind': 'gprs',
         'up_bytes': 120,
         'down_bytes': 430,
         'timespan': 60,
         'seq': 6,
     }
     checkin.handle_event(cls.bts, cls.incoming_event)
     cls.new_usage_event = models.UsageEvent.objects.all()[0]
 def setUp(self):
     # Delete all pre-existing events.
     for usage_event in models.UsageEvent.objects.all():
         usage_event.delete()
     # For event generation, see core.events._create_event in the client
     # repo.
     self.incoming_event = {
         'date': '2015-02-15 15:32:43',
         'imsi': self.subscriber_imsi,
         'oldamt': 1000,
         'newamt': 900,
         'change': 100,
         'reason': 'test reason',
         'kind': 'outside_call',
         'billsec': 45,
         'call_duration': 55,
         'from_imsi': 'IMSI000123',
         'from_number': '5550123',
         'to_imsi': 'IMSI000987',
         'to_number': '6285550987',
         'tariff': 350,
         'seq': 3,
     }
     checkin.handle_event(self.bts, self.incoming_event)
     self.new_usage_event = models.UsageEvent.objects.all()[0]
 def test_bill_operator_for_local_sent_sms(self):
     """We can bill operators for local_sms events."""
     # Tweak the local tier's operator cost to make this more interesting.
     tier = models.BillingTier.objects.get(
         network=self.user_profile.network,
         directionality='on_network_send')
     tier.cost_to_operator_per_sms = 1000
     tier.save()
     # Add a new event.
     self.event_template['kind'] = 'local_sms'
     checkin.handle_event(self.bts, self.event_template)
     # The operator's ledger's balance should have a deduction.
     self.reload_user_profile()
     expected_credit = -1000
     self.assertEqual(expected_credit, self.user_profile.network.ledger.balance)
 def test_call_with_no_billsec(self):
     """Events can be generated with no 'billsec' key."""
     incoming_event = {
         'date': '2015-02-15 15:32:43',
         'imsi': self.subscriber_imsi,
         'oldamt': 800,
         'newamt': 700,
         'change': 100,
         'reason': '70 sec call',
         'kind': 'local_call',
         'seq': 101,
     }
     checkin.handle_event(self.bts, incoming_event)
     new_usage_event = models.UsageEvent.objects.filter(
         kind__exact='local_call')[:1].get()
     self.assertEqual(70, new_usage_event.voice_sec())
 def test_bill_operator_for_local_received_call(self):
     """We can bill operators for local_recv_call events."""
     self.event_template['to_number'] = ''.join(['855', '9195554433'])
     self.event_template['kind'] = 'local_recv_call'
     self.event_template['billsec'] = 100
     # Tweak the on-network receive tier's operator cost to make this more
     # interesting than the default (zero).
     tier = models.BillingTier.objects.get(
         network=self.user_profile.network,
         directionality='on_network_receive')
     tier.cost_to_operator_per_min = 2000
     tier.save()
     checkin.handle_event(self.bts, self.event_template)
     # The operator's ledger's balance should have a deduction.
     self.reload_user_profile()
     expected_credit = int(-2000 * 100 / 60.)
     self.assertEqual(expected_credit, self.user_profile.network.ledger.balance)
 def test_voice_sec_sans_call_duration(self):
     """We can still parse voice seconds without the call duration key."""
     incoming_event = {
         'date': '2015-02-15 15:32:43',
         'imsi': self.subscriber_imsi,
         'oldamt': 800,
         'newamt': 700,
         'change': 100,
         'reason': '30 sec call',
         'kind': 'local_call',
         'billsec': 30,
         'seq': 100,
     }
     checkin.handle_event(self.bts, incoming_event)
     new_usage_event = models.UsageEvent.objects.filter(
         kind__exact='local_call')[:1].get()
     self.assertEqual(30, new_usage_event.voice_sec())