Beispiel #1
0
 def test_multiple_active_batches(self):
     # Our code should use the highest priority batch
     batch1 = BatchFactory(status=Batch.APPROVED, priority=5)
     batch2 = BatchFactory(status=Batch.APPROVED, priority=2)
     BatchFactory(status=Batch.PENDING, deleted=True)
     self.assertEqual(Batch.objects.get_next_batch(), batch1)
     # dealing with batch1 will then make batch2 the highest priority one
     batch1.status = Batch.REJECTED
     batch1.save()
     self.assertEqual(Batch.objects.get_next_batch(), batch2)
Beispiel #2
0
 def test_multiple_active_batches(self):
     # Our code should use the highest priority batch
     batch1 = BatchFactory(status=Batch.APPROVED, priority=5)
     batch2 = BatchFactory(status=Batch.APPROVED, priority=2)
     BatchFactory(status=Batch.PENDING, deleted=True)
     self.assertEqual(Batch.objects.get_next_batch(), batch1)
     # dealing with batch1 will then make batch2 the highest priority one
     batch1.status = Batch.REJECTED
     batch1.save()
     self.assertEqual(Batch.objects.get_next_batch(), batch2)
Beispiel #3
0
 def test_active_batches(self):
     BatchFactory(status=Batch.PENDING, deleted=True)
     batch = BatchFactory(status=Batch.PENDING)
     self.assertEqual(Batch.objects.get_next_batch(), None)
     batch.status = Batch.APPROVED
     batch.save()
     self.assertEqual(Batch.objects.get_next_batch(), batch)
     batch.status = Batch.REJECTED
     batch.save()
     self.assertEqual(Batch.objects.get_next_batch(), None)
     batch.status = Batch.COMPLETED
     batch.save()
     self.assertEqual(Batch.objects.get_next_batch(), None)
Beispiel #4
0
 def test_active_batches(self):
     BatchFactory(status=Batch.PENDING, deleted=True)
     batch = BatchFactory(status=Batch.PENDING)
     self.assertEqual(Batch.objects.get_next_batch(), None)
     batch.status = Batch.APPROVED
     batch.save()
     self.assertEqual(Batch.objects.get_next_batch(), batch)
     batch.status = Batch.REJECTED
     batch.save()
     self.assertEqual(Batch.objects.get_next_batch(), None)
     batch.status = Batch.COMPLETED
     batch.save()
     self.assertEqual(Batch.objects.get_next_batch(), None)
Beispiel #5
0
class SendingTest(LowLevelRouterMixin, TestCase):
    def setUp(self):
        # create an approved Batch
        self.batch = BatchFactory(status=Batch.APPROVED)
        # add message to batch
        self.bulk_msg = BulkMessageFactory(
            batch=self.batch,
            phone_number=get_random_number_string(),
            message=u'.نآسف، مرحلة التسجيل عن طريق الرسائل النصية ليست متاحة',
            sms=None
        )

    def test_send_one_bulk_message(self):
        # send message
        send_message_by_id(self.bulk_msg.pk)
        # -> sms is no longer 'None'
        sms = BulkMessage.objects.get(id=self.bulk_msg.id).sms
        self.assertTrue(sms)
        # -> from_number is REGISTRATION_SHORT_CODE (default)
        self.assertEqual(sms.from_number, settings.REGISTRATION_SHORT_CODE)
        # -> to_number is the phone number from the BulkMessage
        self.assertEqual(sms.to_number, self.bulk_msg.phone_number)
        # -> message is the message from the BulkMessage
        self.assertEqual(sms.message, self.bulk_msg.message)
        self.assertEqual(len(self.outbound), 1)

    @patch.object(LowLevelTestRouter, 'send_to_backend')
    def test_endpoint_is_passed_to_backend(self, mock_send_to_backend):
        # mock sending the message to check what params we send to the backend
        send_message_by_id(self.bulk_msg.pk)
        self.assertTrue(mock_send_to_backend.called)
        call_args, call_kwargs = mock_send_to_backend.call_args
        # by default, we should send endpoint=REGISTRATION_SHORT_CODE
        self.assertEqual(call_kwargs['context'], {'endpoint': settings.REGISTRATION_SHORT_CODE})

    def test_endpoint_passed_to_backend_is_customizable(self):
        self.bulk_msg.from_shortcode = settings.REPORTS_SHORT_CODE
        self.bulk_msg.save()
        # send message
        with patch.object(LowLevelTestRouter, 'send_to_backend') as mock_send_to_backend:
            send_message_by_id(self.bulk_msg.pk)
        self.assertTrue(mock_send_to_backend.called)
        call_args, call_kwargs = mock_send_to_backend.call_args
        self.assertEqual(call_kwargs['context'], {'endpoint': settings.REPORTS_SHORT_CODE})

    def test_endpoint_gets_saved_to_sms_object(self):
        self.bulk_msg.from_shortcode = settings.REPORTS_SHORT_CODE
        self.bulk_msg.save()
        # send message
        send_message_by_id(self.bulk_msg.pk)
        sms = BulkMessage.objects.get(id=self.bulk_msg.id).sms
        self.assertEqual(sms.from_number, settings.REPORTS_SHORT_CODE)

    def test_dont_send_msg_if_sms_field_set(self):
        # set the sms field
        self.bulk_msg.sms = SMSFactory()
        self.bulk_msg.save()
        send_message_by_id(self.bulk_msg.pk)
        # no message sent
        self.assertEqual(len(self.outbound), 0)

    def test_dont_send_msg_if_batch_inactive(self):
        self.batch.status = Batch.PENDING
        self.batch.save()
        send_message_by_id(self.bulk_msg.pk)
        # no message sent
        self.assertEqual(len(self.outbound), 0)

    def test_bulk_sms_send_one_proc(self):
        for i in range(30):
            BulkMessageFactory(batch=self.batch, sms=None)
        for j in range(10):
            BulkMessageFactory(batch=self.batch, sms=None, deleted=True)
        num_sent = send_messages(self.batch)
        # 31 messages sent (30 + 1 from setUp)
        self.assertEqual(len(self.outbound), 31)
        # assert that we report the right number sent too
        self.assertEqual(num_sent, 31)

    def test_sent_messages_dont_get_resent(self):
        for i in range(20):
            BulkMessageFactory(batch=self.batch, sms=None)
        for i in range(10):
            sms = SMSFactory()
            BulkMessageFactory(batch=self.batch, sms=sms)
            BulkMessageFactory(batch=self.batch, sms=SMSFactory(), deleted=True)
        send_messages(self.batch)
        # only 21 messages get sent out (20 + 1 from setUp)
        self.assertEqual(len(self.outbound), 21)

    def test_send_finite_number_each_time(self):
        for i in range(20):
            BulkMessageFactory(batch=self.batch, sms=None)
        send_messages(self.batch, num_to_send=10)
        # only 10 messages should be sent out each call
        self.assertEqual(len(self.outbound), 10)

    def test_once_all_are_sent_next_send_completes_batch(self):
        for i in range(20):
            BulkMessageFactory(batch=self.batch, sms=None)
        BulkMessageFactory(batch=self.batch, sms=None, deleted=True)
        # send out all the messages (21)
        send_messages(self.batch)
        self.assertEqual(len(self.outbound), 21)
        # send again
        BulkMessageFactory(batch=self.batch, sms=None, deleted=True)
        send_messages(self.batch)
        # now batch should be completed
        batch = Batch.objects.get(pk=self.batch.pk)
        self.assertEqual(batch.status, Batch.COMPLETED)

    def test_dont_send_batch_if_batch_inactive(self):
        self.batch.status = Batch.PENDING
        self.batch.save()
        send_messages(self.batch)
        # no message sent
        self.assertEqual(len(self.outbound), 0)

    @patch('bulk_sms.sending.get_router', autospec=True)
    def test_if_message_fails_error_incremented(self, mock_get_router):
        self.assertEqual(self.batch.errors, 0)
        # send the message, mocking an exception
        mock_get_router.return_value.send_to_backend.side_effect = MessageSendingError
        send_messages(self.batch)
        batch = Batch.objects.get(pk=self.batch.pk)
        self.assertEqual(batch.errors, 1)
        self.assertFalse(self.bulk_msg.sms)

    def test_send_to_invalid_id(self):
        """
        Ensure sending to an invalid ID returns gracefully
        """
        self.assertEqual(send_message_by_id(9999999), 0)

    @patch.object(LowLevelTestRouter, 'send_to_backend')
    def test_send_to_existing_connection(self, send_to_backend):
        """
        Ensure bulk messages to phone numbers with existing connections use that
        connection. This helps ensure (but does not guarantee) that messages to
        Thuraya phones won't be sent over Madar or Libyana.
        """
        # use a backend that's available in the test environment but not
        # in BULKSMS_BACKENDS
        conn = ConnectionFactory(identity=self.bulk_msg.phone_number,
                                 backend__name=settings.HTTPTESTER_BACKEND)
        old_count = Connection.objects.all().count()
        # send message
        send_message_by_id(self.bulk_msg.pk)
        # make sure send_to_backend was called with the correct backend name and
        # identity
        self.assertEqual(send_to_backend.call_count, 1)
        self.assertEqual(send_to_backend.call_args[1]['backend_name'], conn.backend.name)
        self.assertEqual(send_to_backend.call_args[1]['identities'], [conn.identity])
        # make sure no new connection object was created:
        new_count = Connection.objects.all().count()
        self.assertEqual(old_count, new_count)
Beispiel #6
0
class SendingTest(LowLevelRouterMixin, TestCase):
    def setUp(self):
        # create an approved Batch
        self.batch = BatchFactory(status=Batch.APPROVED)
        # add message to batch
        self.bulk_msg = BulkMessageFactory(
            batch=self.batch,
            phone_number=get_random_number_string(),
            message=u'.نآسف، مرحلة التسجيل عن طريق الرسائل النصية ليست متاحة',
            sms=None)

    def test_send_one_bulk_message(self):
        # send message
        send_message_by_id(self.bulk_msg.pk)
        # -> sms is no longer 'None'
        sms = BulkMessage.objects.get(id=self.bulk_msg.id).sms
        self.assertTrue(sms)
        # -> from_number is REGISTRATION_SHORT_CODE (default)
        self.assertEqual(sms.from_number, settings.REGISTRATION_SHORT_CODE)
        # -> to_number is the phone number from the BulkMessage
        self.assertEqual(sms.to_number, self.bulk_msg.phone_number)
        # -> message is the message from the BulkMessage
        self.assertEqual(sms.message, self.bulk_msg.message)
        self.assertEqual(len(self.outbound), 1)

    @patch.object(LowLevelTestRouter, 'send_to_backend')
    def test_endpoint_is_passed_to_backend(self, mock_send_to_backend):
        # mock sending the message to check what params we send to the backend
        send_message_by_id(self.bulk_msg.pk)
        self.assertTrue(mock_send_to_backend.called)
        call_args, call_kwargs = mock_send_to_backend.call_args
        # by default, we should send endpoint=REGISTRATION_SHORT_CODE
        self.assertEqual(call_kwargs['context'],
                         {'endpoint': settings.REGISTRATION_SHORT_CODE})

    def test_endpoint_passed_to_backend_is_customizable(self):
        self.bulk_msg.from_shortcode = settings.REPORTS_SHORT_CODE
        self.bulk_msg.save()
        # send message
        with patch.object(LowLevelTestRouter,
                          'send_to_backend') as mock_send_to_backend:
            send_message_by_id(self.bulk_msg.pk)
        self.assertTrue(mock_send_to_backend.called)
        call_args, call_kwargs = mock_send_to_backend.call_args
        self.assertEqual(call_kwargs['context'],
                         {'endpoint': settings.REPORTS_SHORT_CODE})

    def test_endpoint_gets_saved_to_sms_object(self):
        self.bulk_msg.from_shortcode = settings.REPORTS_SHORT_CODE
        self.bulk_msg.save()
        # send message
        send_message_by_id(self.bulk_msg.pk)
        sms = BulkMessage.objects.get(id=self.bulk_msg.id).sms
        self.assertEqual(sms.from_number, settings.REPORTS_SHORT_CODE)

    def test_dont_send_msg_if_sms_field_set(self):
        # set the sms field
        self.bulk_msg.sms = SMSFactory()
        self.bulk_msg.save()
        send_message_by_id(self.bulk_msg.pk)
        # no message sent
        self.assertEqual(len(self.outbound), 0)

    def test_dont_send_msg_if_batch_inactive(self):
        self.batch.status = Batch.PENDING
        self.batch.save()
        send_message_by_id(self.bulk_msg.pk)
        # no message sent
        self.assertEqual(len(self.outbound), 0)

    def test_bulk_sms_send_one_proc(self):
        for i in range(30):
            BulkMessageFactory(batch=self.batch, sms=None)
        for j in range(10):
            BulkMessageFactory(batch=self.batch, sms=None, deleted=True)
        num_sent = send_messages(self.batch)
        # 31 messages sent (30 + 1 from setUp)
        self.assertEqual(len(self.outbound), 31)
        # assert that we report the right number sent too
        self.assertEqual(num_sent, 31)

    def test_sent_messages_dont_get_resent(self):
        for i in range(20):
            BulkMessageFactory(batch=self.batch, sms=None)
        for i in range(10):
            sms = SMSFactory()
            BulkMessageFactory(batch=self.batch, sms=sms)
            BulkMessageFactory(batch=self.batch,
                               sms=SMSFactory(),
                               deleted=True)
        send_messages(self.batch)
        # only 21 messages get sent out (20 + 1 from setUp)
        self.assertEqual(len(self.outbound), 21)

    def test_send_finite_number_each_time(self):
        for i in range(20):
            BulkMessageFactory(batch=self.batch, sms=None)
        send_messages(self.batch, num_to_send=10)
        # only 10 messages should be sent out each call
        self.assertEqual(len(self.outbound), 10)

    def test_once_all_are_sent_next_send_completes_batch(self):
        for i in range(20):
            BulkMessageFactory(batch=self.batch, sms=None)
        BulkMessageFactory(batch=self.batch, sms=None, deleted=True)
        # send out all the messages (21)
        send_messages(self.batch)
        self.assertEqual(len(self.outbound), 21)
        # send again
        BulkMessageFactory(batch=self.batch, sms=None, deleted=True)
        send_messages(self.batch)
        # now batch should be completed
        batch = Batch.objects.get(pk=self.batch.pk)
        self.assertEqual(batch.status, Batch.COMPLETED)

    def test_dont_send_batch_if_batch_inactive(self):
        self.batch.status = Batch.PENDING
        self.batch.save()
        send_messages(self.batch)
        # no message sent
        self.assertEqual(len(self.outbound), 0)

    @patch('bulk_sms.sending.get_router', autospec=True)
    def test_if_message_fails_error_incremented(self, mock_get_router):
        self.assertEqual(self.batch.errors, 0)
        # send the message, mocking an exception
        mock_get_router.return_value.send_to_backend.side_effect = MessageSendingError
        send_messages(self.batch)
        batch = Batch.objects.get(pk=self.batch.pk)
        self.assertEqual(batch.errors, 1)
        self.assertFalse(self.bulk_msg.sms)

    def test_send_to_invalid_id(self):
        """
        Ensure sending to an invalid ID returns gracefully
        """
        self.assertEqual(send_message_by_id(9999999), 0)

    @patch.object(LowLevelTestRouter, 'send_to_backend')
    def test_send_to_existing_connection(self, send_to_backend):
        """
        Ensure bulk messages to phone numbers with existing connections use that
        connection. This helps ensure (but does not guarantee) that messages to
        Thuraya phones won't be sent over Madar or Libyana.
        """
        # use a backend that's available in the test environment but not
        # in BULKSMS_BACKENDS
        conn = ConnectionFactory(identity=self.bulk_msg.phone_number,
                                 backend__name=settings.HTTPTESTER_BACKEND)
        old_count = Connection.objects.all().count()
        # send message
        send_message_by_id(self.bulk_msg.pk)
        # make sure send_to_backend was called with the correct backend name and
        # identity
        self.assertEqual(send_to_backend.call_count, 1)
        self.assertEqual(send_to_backend.call_args[1]['backend_name'],
                         conn.backend.name)
        self.assertEqual(send_to_backend.call_args[1]['identities'],
                         [conn.identity])
        # make sure no new connection object was created:
        new_count = Connection.objects.all().count()
        self.assertEqual(old_count, new_count)