def test_send_default_url(self): joe = self.create_contact("Joe", "+250788383383") self.create_group("Reporters", [joe]) inbound = Msg.create_incoming(self.channel, "tel:+250788383383", "Send an inbound message", external_id='vumi-message-id') msg = inbound.reply("Test message", self.admin, trigger_send=False) # our outgoing message msg.refresh_from_db() try: settings.SEND_MESSAGES = True with patch('requests.put') as mock: mock.return_value = MockResponse(200, '{ "message_id": "1515" }') # manually send it off Channel.send_message( dict_to_struct('MsgStruct', msg.as_task_json())) self.assertEqual( mock.call_args[0][0], 'https://go.vumi.org/api/v1/go/http_api_nostream/key/messages.json' ) self.clear_cache() finally: settings.SEND_MESSAGES = False
def test_ack(self): joe = self.create_contact("Joe", "+250788383383") self.create_group("Reporters", [joe]) inbound = Msg.create_incoming(self.channel, "tel:+250788383383", "Send an inbound message", external_id='vumi-message-id', msg_type=USSD) msg = inbound.reply("Test message", self.admin, trigger_send=False)[0] # our outgoing message msg.refresh_from_db() try: settings.SEND_MESSAGES = True with patch('requests.put') as mock: mock.return_value = MockResponse(200, '{ "message_id": "1515" }') # manually send it off Channel.send_message( dict_to_struct('MsgStruct', msg.as_task_json())) # check the status of the message is now sent msg.refresh_from_db() self.assertEquals(WIRED, msg.status) self.assertTrue(msg.sent_on) self.assertEquals("1515", msg.external_id) self.assertEquals(1, mock.call_count) # simulate Vumi calling back to us sending an ACK event data = { "transport_name": "ussd_transport", "event_type": "ack", "event_id": six.text_type(uuid.uuid4()), "sent_message_id": six.text_type(uuid.uuid4()), "helper_metadata": {}, "routing_metadata": {}, "message_version": "20110921", "timestamp": six.text_type(timezone.now()), "transport_metadata": {}, "user_message_id": msg.external_id, "message_type": "event" } callback_url = reverse('handlers.vumi_handler', args=['event', self.channel.uuid]) self.client.post(callback_url, json.dumps(data), content_type="application/json") # it should be SENT now msg.refresh_from_db() self.assertEquals(SENT, msg.status) self.clear_cache() finally: settings.SEND_MESSAGES = False
def test_nack(self): joe = self.create_contact("Joe", "+250788383383") self.create_group("Reporters", [joe]) msg = joe.send("Test message", self.admin, trigger_send=False) # our outgoing message msg.refresh_from_db() r = get_redis_connection() try: settings.SEND_MESSAGES = True with patch('requests.put') as mock: mock.return_value = MockResponse(200, '{ "message_id": "1515" }') # manually send it off Channel.send_message( dict_to_struct('MsgStruct', msg.as_task_json())) # check the status of the message is now sent msg.refresh_from_db() self.assertEquals(WIRED, msg.status) self.assertTrue(msg.sent_on) self.assertEquals("1515", msg.external_id) self.assertEquals(1, mock.call_count) # should have a failsafe that it was sent self.assertTrue( r.sismember(timezone.now().strftime(MSG_SENT_KEY), str(msg.id))) # simulate Vumi calling back to us sending an NACK event data = { "transport_name": "ussd_transport", "event_type": "nack", "nack_reason": "Unknown address.", "event_id": six.text_type(uuid.uuid4()), "timestamp": six.text_type(timezone.now()), "message_version": "20110921", "transport_metadata": {}, "user_message_id": msg.external_id, "message_type": "event" } callback_url = reverse('handlers.vumi_handler', args=['event', self.channel.uuid]) response = self.client.post(callback_url, json.dumps(data), content_type="application/json") self.assertEqual(response.status_code, 200) self.assertTrue( self.create_contact("Joe", "+250788383383").is_stopped) self.clear_cache() finally: settings.SEND_MESSAGES = False
def test_send(self): joe = self.create_contact("Joe", "+250788383383") self.create_group("Reporters", [joe]) inbound = Msg.create_incoming(self.channel, "tel:+250788383383", "Send an inbound message", external_id='vumi-message-id', msg_type=USSD) msg = inbound.reply("Test message", self.admin, trigger_send=False)[0] self.assertEqual(inbound.msg_type, USSD) self.assertEqual(msg.msg_type, USSD) # our outgoing message msg.refresh_from_db() r = get_redis_connection() try: settings.SEND_MESSAGES = True with patch('requests.put') as mock: mock.return_value = MockResponse(200, '{ "message_id": "1515" }') # manually send it off Channel.send_message( dict_to_struct('MsgStruct', msg.as_task_json())) # check the status of the message is now sent msg.refresh_from_db() self.assertEquals(WIRED, msg.status) self.assertTrue(msg.sent_on) self.assertEquals("1515", msg.external_id) self.assertEquals(1, mock.call_count) # should have a failsafe that it was sent self.assertTrue( r.sismember(timezone.now().strftime(MSG_SENT_KEY), str(msg.id))) # try sending again, our failsafe should kick in Channel.send_message( dict_to_struct('MsgStruct', msg.as_task_json())) # we shouldn't have been called again self.assertEquals(1, mock.call_count) self.clear_cache() finally: settings.SEND_MESSAGES = False