def test_get_other_participant(fresh_session): """ The 'get_other_participant' method is able to traverse the ProxySession associated to the VirtualTN and sender number. """ new_tn, new_session = fresh_session other_participant, session_id = ProxySession.get_other_participant(new_tn.value, new_session.participant_a) assert other_participant == new_session.participant_b assert session_id == new_session.id other_participant, session_id = ProxySession.get_other_participant(new_tn.value, new_session.participant_b) assert other_participant == new_session.participant_a assert session_id == new_session.id
def test_terminate_session(fresh_session): """ The 'terminate' method delete's the ProxySession and releases the associated VirtualTN back to the pool. """ new_tn, new_session = fresh_session sessions = ProxySession.query.all() assert new_session.virtual_TN == new_tn.value assert len(sessions) == 1 ProxySession.terminate(new_session.id) released_tn = VirtualTN.query.filter_by(value=new_tn.value).one() assert released_tn.session_id is None sessions = ProxySession.query.all() assert len(sessions) == 0
def test_clean_expired_sessions(fresh_session): """ The 'clean_expired' method does clear expired ProxySessions """ new_tn, new_session = fresh_session new_session.expiry_date = datetime.utcnow() db_session.add(new_session) db_session.commit() sessions = ProxySession.query.all() assert len(sessions) == 1 ProxySession.clean_expired() sessions = ProxySession.query.all() assert new_tn.session_id is None assert len(sessions) == 0
def test_get_other_participant(fresh_session): """ The 'get_other_participant' method is able to traverse the ProxySession associated to the VirtualTN and sender number. """ new_tn, new_session = fresh_session other_participant, session_id = ProxySession.get_other_participant( new_tn.value, new_session.participant_a) assert other_participant == new_session.participant_b assert session_id == new_session.id other_participant, session_id = ProxySession.get_other_participant( new_tn.value, new_session.participant_b) assert other_participant == new_session.participant_a assert session_id == new_session.id
def test_delete_tn(): """ Creates a new virtual tn attached to a session, and requests to delete that number which is an illegal operation. The VirtualTN is then released, and the request is made again - this time succeeding. """ client = app.test_client() test_num = '12223334444' session = ProxySession(test_num, '12223334444', '12223335555', expiry_window=None) vnum = VirtualTN(test_num) vnum.session_id = 'fake_session_id' db_session.add(session) db_session.add(vnum) db_session.commit() resp = client.delete('/tn', data=json.dumps({'value': test_num}), content_type='application/json') data = json.loads(resp.data) assert data['status'] == 'failed' assert "Cannot delete the number." in data['message'] assert session.id in data['message'] assert resp.status_code == 400 db_session.delete(session) vnum.session_id = None db_session.add(vnum) db_session.commit() resp = client.delete('/tn', data=json.dumps({'value': test_num}), content_type='application/json') data = json.loads(resp.data) assert 'Successfully removed TN from pool' in data['message'] assert resp.status_code == 200
def valid_session(virtual_tn): first_num = '12223334444' sec_num = '12223335555' proxy_sess = ProxySession(virtual_tn.value, first_num, sec_num) virtual_tn.session_id = proxy_sess.id db_session.add(virtual_tn) db_session.add(proxy_sess) db_session.commit() return proxy_sess
def test_get_session(): """ Ensures the '/session' GET method returns json reflecting the state of the database. """ client = app.test_client() test_num_1 = '12223334444' test_num_2 = '12223335555' resp = client.get('/session') data = json.loads(resp.data) assert data['total_sessions'] == 0 assert data['sessions'] == [] sess_1 = ProxySession(test_num_1, 'cust_1_num', 'cust_2_num') sess_2 = ProxySession(test_num_2, 'cust_1_num', 'cust_2_num') db_session.add(sess_1) db_session.add(sess_2) db_session.commit() resp = client.get('/session') data = json.loads(resp.data) assert data['total_sessions'] == 2
def fresh_session(): """ Clears out any outstanding rows in the VirtualTN and ProxySession tables. Creates and returns a linked VirtualTN and ProxySession. """ VirtualTN.query.delete() ProxySession.query.delete() new_tn = VirtualTN('1234567897') db_session.add(new_tn) db_session.commit() new_session = ProxySession(new_tn.value, '12223334444', '12223335555', expiry_window=1) new_tn.session_id = new_session.id db_session.add(new_tn) db_session.add(new_session) db_session.commit() return new_tn, new_session
def test_delete_session(): """ Initially tries to delete a session from an id that is unknown. The service responds with a 404, and helpful message. A session is created an persisted to the database. A delete request for that session is executed, and SMS's are dispatched to the participants. """ mock_controller = MockController() app.sms_controller = mock_controller client = app.test_client() resp = client.delete('/session', data=json.dumps({'session_id': 'fake_id'}), content_type='application/json') data = json.loads(resp.data) assert resp.status_code == 404 msg = ("ProxySession {} could not be deleted because" " it does not exist".format('fake_id')) assert data['message'] == msg test_num_1 = '12223334444' vnum_1 = VirtualTN(test_num_1) sess_1 = ProxySession(test_num_1, 'cust_1_num', 'cust_2_num') vnum_1.session_id = sess_1.id db_session.add(sess_1) db_session.add(vnum_1) db_session.commit() resp = client.delete('/session', data=json.dumps({'session_id': sess_1.id}), content_type='application/json') assert resp.status_code == 200 data = json.loads(resp.data) assert data['message'] == 'Successfully ended the session.' assert data['session_id'] == sess_1.id sms = mock_controller.requests[0] msg = "[{}]: {}".format(ORG_NAME.upper(), SESSION_END_MSG) assert sms.content == msg assert len(mock_controller.requests) == 2