def test_channel_events(self): url = reverse('api.v2.channel_events') self.assertEndpointAccess(url) call1 = Call.create_call(self.channel, "0788123123", timezone.now(), 0, Call.TYPE_CALL_IN_MISSED) call2 = Call.create_call(self.channel, "0788124124", timezone.now(), 36, Call.TYPE_CALL_IN) call3 = Call.create_call(self.channel, "0788124124", timezone.now(), 0, Call.TYPE_CALL_OUT_MISSED) call4 = Call.create_call(self.channel, "0788123123", timezone.now(), 15, Call.TYPE_CALL_OUT) # 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 _create_calls(self, count, channel, contacts): """ Creates the given number of calls """ calls = [] date = timezone.now() for c in range(0, count): duration = random.randint(10, 30) contact = contacts[c % len(contacts)] calls.append(Call(channel=channel, org=self.org, contact=contact, time=date, duration=duration, call_type='mo_call', created_by=self.user, modified_by=self.user)) Call.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, MISSED_CALL_TRIGGER).first() flow = self.create_flow() contact = self.create_contact("Ali", "250788739305") self.assertFalse(missed_call_trigger) Call.create_call(self.channel, contact.get_urn(TEL_SCHEME).path, timezone.now(), 0, CALL_IN_MISSED) self.assertEquals(1, Call.objects.all().count()) self.assertEquals(0, flow.runs.all().count()) 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, MISSED_CALL_TRIGGER) self.assertEquals(trigger.flow.pk, flow.pk) missed_call_trigger = Trigger.get_triggers_of_type(self.org, MISSED_CALL_TRIGGER).first() self.assertEquals(missed_call_trigger.pk, trigger.pk) Call.create_call(self.channel, contact.get_urn(TEL_SCHEME).path, timezone.now(), 0, CALL_IN_MISSED) self.assertEquals(2, Call.objects.all().count()) self.assertEquals(1, flow.runs.all().count()) self.assertEquals(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.assertEquals(i+2, Trigger.objects.all().count()) self.assertEquals(1, Trigger.objects.filter(is_archived=False, trigger_type=MISSED_CALL_TRIGGER).count()) # even unarchiving we only have one acive trigger at a time triggers = Trigger.objects.filter(trigger_type=MISSED_CALL_TRIGGER, is_archived=True) active_trigger = Trigger.objects.get(trigger_type=MISSED_CALL_TRIGGER, is_archived=False) post_data = dict() post_data['action'] = 'restore' post_data['objects'] = [_.pk for _ in triggers] response = self.client.post(reverse("triggers.trigger_archived"), post_data) self.assertEquals(1, Trigger.objects.filter(is_archived=False, trigger_type=MISSED_CALL_TRIGGER).count()) self.assertFalse(active_trigger.pk == Trigger.objects.filter(is_archived=False, trigger_type=MISSED_CALL_TRIGGER)[0].pk)