def test_content_type_filtering(self): content_types = [ MessagingEvent.CONTENT_SMS, MessagingEvent.CONTENT_EMAIL, MessagingEvent.CONTENT_API_SMS, MessagingEvent.CONTENT_IVR_SURVEY, MessagingEvent.CONTENT_SMS_SURVEY ] for content_type in content_types: make_events_for_test(self.domain.name, datetime.utcnow(), content_type=content_type) url = f'{self.list_endpoint}?order_by=date&content_type=ivr-survey,sms' response = self._auth_get_resource(url) self.assertEqual(response.status_code, 200, response.content) actual = { event["content_type"] for event in json.loads(response.content)['objects'] } self.assertEqual(actual, {"sms", "api-sms", "ivr-survey"})
def test_source_filtering(self): sources = [ MessagingEvent.SOURCE_BROADCAST, MessagingEvent.SOURCE_KEYWORD, MessagingEvent.SOURCE_REMINDER, MessagingEvent.SOURCE_UNRECOGNIZED, MessagingEvent.SOURCE_CASE_RULE ] for source in sources: make_events_for_test(self.domain.name, datetime.utcnow(), source=source) url = f'{self.list_endpoint}?order_by=date&source=keyword,reminder' response = self._auth_get_resource(url) self.assertEqual(response.status_code, 200, response.content) actual = { event["source"]["type"] for event in json.loads(response.content)['objects'] } self.assertEqual(actual, {"keyword", "reminder", "conditional-alert"})
def test_contact_filter(self): user_ids = [] for i in range(2): user = CommCareUser.create(self.domain.name, f"user {i}", "123", None, None, email=f"user{i}@email.com") user_ids.append(user.get_id) self.addCleanup(user.delete, self.domain.name, deleted_by=None) make_email_event_for_test(self.domain.name, "test broadcast", user_ids) make_events_for_test(self.domain.name, datetime.utcnow(), phone_number='+99912345678') _create_sms_messages(self.domain, 1, False) self._check_contact_filtering("email_address", "*****@*****.**", "email_address") self._check_contact_filtering("email_address", "*****@*****.**", "email_address") self._check_contact_filtering("phone_number", "+99912345678", "phone_number")
def test_cursor(self): utcnow = datetime.utcnow() ids_and_dates = [] for offset in range(5): date = utcnow + timedelta(hours=offset) _, subevent, _ = make_events_for_test(self.domain.name, date) ids_and_dates.append((subevent.id, date.isoformat())) content = self._test_cursor_response(ids_and_dates[:2], extra_params={ "limit": 2, "order_by": "date" }) content = self._test_cursor_response(ids_and_dates[2:4], previous_content=content) self._test_cursor_response([ids_and_dates[-1]], previous_content=content)
def test_cursor_stuck_in_loop(self): """This demonstrates the limitation of the current cursor pagination implementation. If there are more objects with the same filter param than the batch size the API will get stuck in a loop. This should not be an issue for the message event API but is noted for completeness """ utcnow = datetime.utcnow() ids_and_dates = [] for i in range(5): _, subevent, _ = make_events_for_test(self.domain.name, utcnow) ids_and_dates.append((subevent.id, utcnow.isoformat())) content = self._test_cursor_response(ids_and_dates[:2], extra_params={"limit": 2}) content = self._test_cursor_response(ids_and_dates[:2], previous_content=content) self._test_cursor_response(ids_and_dates[:2], previous_content=content)
def test_status_filtering_error(self): make_events_for_test(self.domain.name, datetime.utcnow()) make_events_for_test(self.domain.name, datetime.utcnow(), status=MessagingEvent.STATUS_ERROR) make_events_for_test(self.domain.name, datetime.utcnow(), error=True) # sms with error url = f'{self.list_endpoint}?status=error' response = self._auth_get_resource(url) self.assertEqual(response.status_code, 200, response.content) events = json.loads(response.content)['objects'] self.assertEqual(len(events), 2) actual = {event["status"] for event in events} self.assertEqual(actual, {"error"})
def test_cursor_multiple_matching_dates(self): """Make sure that where we have multiple objects with matching dates at the intersection of pages we return the correct result set. This works because we are sorting the results by 'event.date' AND 'event.id' """ # events at 2, 3, 4 have the same dates ('[]' mark expected pages with limit=3) # [d1, d2, d3], [d3, d3, d4], [d5, d6] ids_and_dates = [] previous_date = None for i in range(8): event_date = previous_date if i in (3, 4) else datetime.utcnow() previous_date = event_date _, subevent, _ = make_events_for_test(self.domain.name, event_date) ids_and_dates.append((subevent.id, event_date.isoformat())) content = self._test_cursor_response(ids_and_dates[:3], extra_params={"limit": 3}) content = self._test_cursor_response(ids_and_dates[3:6], previous_content=content) self._test_cursor_response(ids_and_dates[6:], previous_content=content)