def test_time_remaining_one_message(self): # if there are any messages at all, should still return a True value for the template batch = BatchFactory(status=Batch.PENDING) BulkMessageFactory(batch=batch, sms=None) # using assertTrue because the key is that we want the return value to be boolean True # for the template to NOT display the 'Finished' default self.assertTrue(batch.time_remaining())
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)
def test_add_messages_to_batch(self): batch = BatchFactory() msg = 'this is my message' shortcode = None gen = [("1", msg, shortcode), ("2", msg, shortcode), ("3", msg, shortcode)] batch.add_messages(gen) self.assertEqual(3, batch.messages.all().count()) for message in batch.messages.all(): self.assertEqual(msg, message.message) # from_shortcode should get set to default value (REGISTRATION_SHORT_CODE) self.assertEqual(settings.REGISTRATION_SHORT_CODE, message.from_shortcode)
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)
def test_get_random_messages_from_batch(self): batch = BatchFactory(status=Batch.PENDING) BatchFactory(status=Batch.PENDING, deleted=True) msgs = [BulkMessageFactory(batch=batch, sms=None) for i in range(10)] # the random message is one of our messages first_random_msg = batch.random_n_messages(n=1)[0] self.assertIn(first_random_msg, msgs) # get random message up to 10 times: shouldn't all be the same for i in range(10): next_random_msg = batch.random_n_messages(n=1)[0] if first_random_msg != next_random_msg: break self.assertNotEqual(first_random_msg, next_random_msg) # if we ask for more than exist, we only get the number that actually exist self.assertTrue(len(batch.random_n_messages(n=20)), 10)
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_add_broadcast_perm_sees_detail_page_but_cant_review(self): self.staff_user.user_permissions.remove( Permission.objects.get(codename='approve_broadcast')) self.staff_user.user_permissions.add(Permission.objects.get(codename='read_broadcast')) batch = BatchFactory(status=Batch.PENDING) broadcast = Broadcast.objects.create( created_by=batch.created_by, batch=batch, audience=Broadcast.CUSTOM, message=batch.description ) review_url = reverse('approve_reject_broadcast', kwargs=dict(broadcast_id=broadcast.id)) # can see detail page rsp = self.client.get(reverse('read_broadcast', kwargs=dict(pk=broadcast.id))) self.assertEqual(200, rsp.status_code) # and can't manually go to review url rsp = self.client.post(review_url) self.assertEqual(403, rsp.status_code)
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)
def test_unicode_method(self): batch = BatchFactory(name=u'foo') self.assertEqual(str(batch), u'foo')
def test_time_remaining_zero_messages(self): batch = BatchFactory(status=Batch.PENDING) self.assertEqual(batch.time_remaining(), 0)
def test_time_remaining(self): batch = BatchFactory(status=Batch.PENDING) [BulkMessageFactory(deleted=True, batch=batch, sms=None) for i in range(5)] msgs = [BulkMessageFactory(batch=batch, sms=None) for i in range(10)] self.assertEqual(batch.time_remaining(), datetime.timedelta(seconds=len(msgs) / 2))
def test_dont_get_deleted_batch_by_default(self): batch = BatchFactory(deleted=True) self.assertNotIn(batch, Batch.objects.all())
def setUp(self): # create an approved Batch self.batch = BatchFactory(status=Batch.APPROVED)
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)