def test_channel_events(self): url = reverse('api.v2.channel_events') self.assertEndpointAccess(url) call1 = ChannelEvent.create(self.channel, "tel:0788123123", ChannelEvent.TYPE_CALL_IN_MISSED, timezone.now(), 0) call2 = ChannelEvent.create(self.channel, "tel:0788124124", ChannelEvent.TYPE_CALL_IN, timezone.now(), 36) call3 = ChannelEvent.create(self.channel, "tel:0788124124", ChannelEvent.TYPE_CALL_OUT_MISSED, timezone.now(), 0) call4 = ChannelEvent.create(self.channel, "tel:0788123123", ChannelEvent.TYPE_CALL_OUT, timezone.now(), 15) # no filtering with self.assertNumQueries(NUM_BASE_REQUEST_QUERIES + 3): response = self.fetchJSON(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.json['next'], None) self.assertResultsById(response, [call4, call3, call2, call1]) self.assertEqual(response.json['results'][0], { 'id': call4.pk, 'channel': {'uuid': self.channel.uuid, 'name': "Test Channel"}, 'type': "call-out", 'contact': {'uuid': self.joe.uuid, 'name': self.joe.name}, 'time': format_datetime(call4.time), 'duration': 15, 'created_on': format_datetime(call4.created_on), }) # filter by id response = self.fetchJSON(url, 'id=%d' % call1.pk) self.assertResultsById(response, [call1]) # filter by contact response = self.fetchJSON(url, 'contact=%s' % self.joe.uuid) self.assertResultsById(response, [call4, call1]) # filter by invalid contact response = self.fetchJSON(url, 'contact=invalid') self.assertResultsById(response, []) # filter by before response = self.fetchJSON(url, 'before=%s' % format_datetime(call3.created_on)) self.assertResultsById(response, [call3, call2, call1]) # filter by after response = self.fetchJSON(url, 'after=%s' % format_datetime(call2.created_on)) self.assertResultsById(response, [call4, call3, call2])
def test_queue_mo_miss_event(self): get_redis_connection("default").flushall() event = ChannelEvent.create_relayer_event(self.channel, "tel:12065551212", ChannelEvent.TYPE_CALL_OUT, timezone.now()) r = get_redis_connection() # noop, this event isn't handled by mailroom self.assertEqual(0, r.zcard(f"handler:active")) self.assertEqual(0, r.zcard(f"handler:{self.org.id}")) self.assertEqual(0, r.llen(f"c:{self.org.id}:{event.contact_id}")) event = ChannelEvent.create_relayer_event( self.channel, "tel:12065551515", ChannelEvent.TYPE_CALL_IN_MISSED, timezone.now()) self.assert_org_queued(self.org, "handler") self.assert_contact_queued(event.contact) self.assert_queued_handler_task( event.contact, { "type": "mo_miss", "org_id": event.contact.org.id, "task": { "channel_id": self.channel.id, "contact_id": event.contact.id, "event_type": "mo_miss", "extra": None, "id": event.id, "new_contact": True, "org_id": event.contact.org.id, "urn": "tel:+12065551515", "urn_id": event.contact.urns.get().id, }, "queued_on": matchers.ISODate(), }, )
def fire_follow_triggers(channel_id, contact_urn_id, new_mage_contact=False): """ Fires a follow trigger """ urn = ContactURN.objects.select_related('contact').get(pk=contact_urn_id) contact = urn.contact # for now, flows start against contacts rather than URNs channel = Channel.objects.get(id=channel_id) # if contact was just created in Mage then.. # * its dynamic groups won't have been initialized # * we need to update our cached contact counts if new_mage_contact: handle_new_contact(contact.org, contact) event = ChannelEvent.create(channel, urn.identity, ChannelEvent.TYPE_FOLLOW, timezone.now()) event.handle()
def _create_calls(self, count, channel, contacts): """ Creates the given number of missed call events """ calls = [] date = timezone.now() for c in range(0, count): duration = random.randint(10, 30) contact = contacts[c % len(contacts)] contact_urn = contact.urn_objects.values()[0] calls.append( ChannelEvent(channel=channel, org=self.org, event_type='mt_miss', contact=contact, contact_urn=contact_urn, time=date, duration=duration, created_by=self.user, modified_by=self.user)) ChannelEvent.objects.bulk_create(calls) return calls
def test_missed_call_trigger(self): self.login(self.admin) missed_call_trigger = Trigger.get_triggers_of_type(self.org, Trigger.TYPE_MISSED_CALL).first() flow = self.create_flow() contact = self.create_contact("Ali", "250788739305") self.assertFalse(missed_call_trigger) ChannelEvent.create(self.channel, contact.get_urn(TEL_SCHEME).urn, ChannelEvent.TYPE_CALL_IN_MISSED, timezone.now(), 0) self.assertEqual(ChannelEvent.objects.all().count(), 1) self.assertEqual(flow.runs.all().count(), 0) trigger_url = reverse("triggers.trigger_missed_call") response = self.client.get(trigger_url) self.assertEquals(response.status_code, 200) post_data = dict(flow=flow.pk) response = self.client.post(trigger_url, post_data) trigger = Trigger.objects.all().order_by('-pk')[0] self.assertEquals(trigger.trigger_type, Trigger.TYPE_MISSED_CALL) self.assertEquals(trigger.flow.pk, flow.pk) missed_call_trigger = Trigger.get_triggers_of_type(self.org, Trigger.TYPE_MISSED_CALL).first() self.assertEquals(missed_call_trigger.pk, trigger.pk) ChannelEvent.create(self.channel, contact.get_urn(TEL_SCHEME).urn, ChannelEvent.TYPE_CALL_IN_MISSED, timezone.now(), 0) self.assertEqual(ChannelEvent.objects.all().count(), 2) self.assertEqual(flow.runs.all().count(), 1) self.assertEqual(flow.runs.all()[0].contact.pk, contact.pk) other_flow = Flow.copy(flow, self.admin) post_data = dict(flow=other_flow.pk) response = self.client.post(reverse("triggers.trigger_update", args=[trigger.pk]), post_data) trigger = Trigger.objects.get(pk=trigger.pk) self.assertEquals(trigger.flow.pk, other_flow.pk) # create ten missed call triggers for i in range(10): response = self.client.get(trigger_url) self.assertEquals(response.status_code, 200) post_data = dict(flow=flow.pk) response = self.client.post(trigger_url, post_data) self.assertEqual(i + 2, Trigger.objects.all().count()) self.assertEqual(1, Trigger.objects.filter(is_archived=False, trigger_type=Trigger.TYPE_MISSED_CALL).count()) # even unarchiving we only have one acive trigger at a time triggers = Trigger.objects.filter(trigger_type=Trigger.TYPE_MISSED_CALL, is_archived=True) active_trigger = Trigger.objects.get(trigger_type=Trigger.TYPE_MISSED_CALL, is_archived=False) post_data = dict() post_data['action'] = 'restore' post_data['objects'] = [t.pk for t in triggers] response = self.client.post(reverse("triggers.trigger_archived"), post_data) self.assertEquals(1, Trigger.objects.filter(is_archived=False, trigger_type=Trigger.TYPE_MISSED_CALL).count()) self.assertFalse(active_trigger.pk == Trigger.objects.filter(is_archived=False, trigger_type=Trigger.TYPE_MISSED_CALL)[0].pk)