def test_get_and_close_all_open_sessions(self): domain = uuid.uuid4().hex contact = uuid.uuid4().hex for i in range(3): _make_session( domain=domain, connection_id=contact, end_time=None, session_type=XFORMS_SESSION_SMS, ) sql_sessions = SQLXFormsSession.get_all_open_sms_sessions(domain, contact) self.assertEqual(3, len(sql_sessions)) SQLXFormsSession.close_all_open_sms_sessions(domain, contact) self.assertEqual(0, len(SQLXFormsSession.get_all_open_sms_sessions(domain, contact)))
def test_get_and_close_all_open_sessions(self): domain = uuid.uuid4().hex contact = uuid.uuid4().hex for i in range(3): _make_session( domain=domain, connection_id=contact, end_time=None, session_type=XFORMS_SESSION_SMS, ) sql_sessions = SQLXFormsSession.get_all_open_sms_sessions(domain, contact) self.assertEqual(3, len(sql_sessions)) SQLXFormsSession.close_all_open_sms_sessions(domain, contact) self.assertEqual(0, len(SQLXFormsSession.get_all_open_sms_sessions(domain, contact)))
def test_get_all_open_sessions_wrong_type(self): domain = uuid.uuid4().hex contact = uuid.uuid4().hex _make_session( domain=domain, connection_id=contact, end_time=None, session_type=XFORMS_SESSION_IVR, ) self.assertEqual(0, len(SQLXFormsSession.get_all_open_sms_sessions(domain, contact)))
def test_get_all_open_sessions_already_ended(self): domain = uuid.uuid4().hex contact = uuid.uuid4().hex _make_session( domain=domain, connection_id=contact, end_time=datetime.utcnow(), session_type=XFORMS_SESSION_SMS, ) self.assertEqual(0, len(SQLXFormsSession.get_all_open_sms_sessions(domain, contact)))
def test_get_all_open_sessions_already_ended(self): domain = uuid.uuid4().hex contact = uuid.uuid4().hex _make_session( domain=domain, connection_id=contact, end_time=datetime.utcnow(), session_type=XFORMS_SESSION_SMS, ) self.assertEqual(0, len(SQLXFormsSession.get_all_open_sms_sessions(domain, contact)))
def test_get_all_open_sessions_wrong_type(self): domain = uuid.uuid4().hex contact = uuid.uuid4().hex _make_session( domain=domain, connection_id=contact, end_time=None, session_type=XFORMS_SESSION_IVR, ) self.assertEqual(0, len(SQLXFormsSession.get_all_open_sms_sessions(domain, contact)))
def test_get_all_open_sessions_contact_mismatch(self): domain = uuid.uuid4().hex contact = uuid.uuid4().hex _make_session( domain=domain, connection_id='wrong', end_time=None, session_is_open=True, session_type=XFORMS_SESSION_SMS, ) self.assertEqual(0, len(SQLXFormsSession.get_all_open_sms_sessions(domain, contact)))
def sms_keyword_handler(v, text, msg): text = text.strip() if text == "": return False sessions = SQLXFormsSession.get_all_open_sms_sessions(v.domain, v.owner_id) text_words = text.upper().split() if text.startswith("#"): return handle_global_keywords(v, text, msg, text_words, sessions) else: return handle_domain_keywords(v, text, msg, text_words, sessions)
def sms_keyword_handler(v, text, msg): text = text.strip() if text == "": return False sessions = SQLXFormsSession.get_all_open_sms_sessions(v.domain, v.owner_id) text_words = text.upper().split() if text.startswith("#"): return handle_global_keywords(v, text, msg, text_words, sessions) else: return handle_domain_keywords(v, text, msg, text_words, sessions)
def test_get_all_open_sessions_contact_mismatch(self): domain = uuid.uuid4().hex contact = uuid.uuid4().hex _make_session( domain=domain, connection_id='wrong', end_time=None, session_is_open=True, session_type=XFORMS_SESSION_SMS, ) self.assertEqual( 0, len(SQLXFormsSession.get_all_open_sms_sessions(domain, contact)))
def test_get_single_open_session(self): properties = _arbitrary_session_properties( end_time=None, session_is_open=True, session_type=XFORMS_SESSION_SMS, ) session = SQLXFormsSession(**properties) session.save() (mult, session) = get_single_open_session_or_close_multiple( session.domain, session.connection_id) self.assertEqual(False, mult) [session_back] = SQLXFormsSession.get_all_open_sms_sessions( session.domain, session.connection_id) self.assertEqual(session._id, session_back.couch_id)
def test_get_single_open_session(self): properties = _arbitrary_session_properties( end_time=None, session_type=XFORMS_SESSION_SMS, ) session = SQLXFormsSession(**properties) session.save() (mult, session) = get_single_open_session_or_close_multiple( session.domain, session.connection_id ) self.assertEqual(False, mult) [session_back] = SQLXFormsSession.get_all_open_sms_sessions( session.domain, session.connection_id ) self.assertEqual(session._id, session_back.couch_id)
def test_get_single_open_session_close_multiple(self): domain = uuid.uuid4().hex contact = uuid.uuid4().hex for i in range(3): _make_session( domain=domain, connection_id=contact, end_time=None, session_type=XFORMS_SESSION_SMS, ) (mult, session) = get_single_open_session_or_close_multiple(domain, contact) self.assertEqual(True, mult) self.assertEqual(None, session) self.assertEqual(0, len(SQLXFormsSession.get_all_open_sms_sessions(domain, contact)))
def test_get_single_open_session_close_multiple(self): domain = uuid.uuid4().hex contact = uuid.uuid4().hex for i in range(3): _make_session( domain=domain, connection_id=contact, end_time=None, session_type=XFORMS_SESSION_SMS, ) (mult, session) = get_single_open_session_or_close_multiple(domain, contact) self.assertEqual(True, mult) self.assertEqual(None, session) self.assertEqual(0, len(SQLXFormsSession.get_all_open_sms_sessions(domain, contact)))
def sms_keyword_handler(verified_number, text, msg): with critical_section_for_smsforms_sessions(verified_number.owner_id): text = text.strip() if text == "": return False sessions = SQLXFormsSession.get_all_open_sms_sessions( verified_number.domain, verified_number.owner_id) text_words = text.upper().split() if text.startswith("#"): return handle_global_keywords(verified_number, text, msg, text_words, sessions) else: return handle_domain_keywords(verified_number, text, msg, text_words, sessions)
def get_single_open_session_or_close_multiple(domain, contact_id): """ Retrieves the current open SQLXFormsSession for the given contact. If multiple sessions are open, it closes all of them and returns None for the session. The return value is a tuple of (multiple, session), where multiple is True if there were multiple sessions, and session is the session if there was a single open session available. """ sessions = SQLXFormsSession.get_all_open_sms_sessions(domain, contact_id) count = sessions.count() if count > 1: for session in sessions: session.mark_completed(False) session.save() return (True, None) session = sessions[0] if count == 1 else None return (False, session)
def get_single_open_session_or_close_multiple(domain, contact_id): """ Retrieves the current open SQLXFormsSession for the given contact. If multiple sessions are open, it closes all of them and returns None for the session. The return value is a tuple of (multiple, session), where multiple is True if there were multiple sessions, and session is the session if there was a single open session available. """ sessions = SQLXFormsSession.get_all_open_sms_sessions(domain, contact_id) count = sessions.count() if count > 1: for session in sessions: session.end(False) session.save() return (True, None) session = sessions[0] if count == 1 else None return (False, session)