def test_incoming_sms_linked_form_session(self): event, expected_subevent = self._create_message_event_and_subevent() msg = SMS( phone_number=self.number.phone_number, direction=INCOMING, date=datetime.utcnow(), text="test message", domain_scope=None, backend_api=None, backend_id=None, backend_message_id=None, raw_text=None, ) self.addCleanup(msg.delete) form_session_handler(self.number, msg.text, msg) self.assertEqual(expected_subevent, msg.messaging_subevent)
def test_incoming_sms_multiple_subevents_for_session(self): first_event, first_subevent = self._create_message_event_and_subevent() second_event, second_subevent = self._create_message_event_and_subevent( ) # no message event or subevent exist msg = SMS( phone_number=self.number.phone_number, direction=INCOMING, date=datetime.utcnow(), text="test message", domain_scope=None, backend_api=None, backend_id=None, backend_message_id=None, raw_text=None, ) self.addCleanup(msg.delete) form_session_handler(self.number, msg.text, msg) # assert that the most recent subevent is used self.assertNotEqual(first_subevent, msg.messaging_subevent) self.assertEqual(second_subevent, msg.messaging_subevent)
def test_incoming_sms_linked_form_session__session_contact_matches_incoming( self): session = self._make_session(self.number1) self._claim_channel(session) msg = self._get_sms() handled = form_session_handler(self.number1, msg.text, msg) self.assertTrue(handled) # message gets attached to the correct session self.assertEqual(msg.xforms_session_couch_id, session.couch_id) # sticky session is still 'open' session_info = XFormsSessionSynchronization.get_running_session_info_for_channel( session.get_channel()) self.assertEqual(session_info.session_id, session.session_id)
def test_incoming_sms_linked_form_session__session_contact_different_from_incoming( self): session = self._make_session(self.number2) self._claim_channel(session) msg = self._get_sms() # session belongs to `number2` but message comes from `number1` handled = form_session_handler(self.number1, msg.text, msg) self.assertTrue(handled) # msg does not get updated self.assertEqual(msg.xforms_session_couch_id, None) # session should be closed session.refresh_from_db() self.assertFalse(session.session_is_open) self.assertFalse(session.completed) # sticky session is removed session_info = XFormsSessionSynchronization.get_running_session_info_for_channel( session.get_channel()) self.assertIsNone(session_info.session_id)