def test_bad_auth_client(self): """ Tests a non-valid auth message for a client. """ with QdbServer( client_host='localhost', client_port=0, client_auth_fn=lambda _: False, # Fail all new clients. tracer_server=QdbNopServer()) as server: ws = create_connection('ws://localhost:%d%s' % (server.client_server.server_port, DEFAULT_ROUTE_FMT.format(uuid='test'))) send_client_event(ws, 'start', 'friendzoned-again') auth_failed_event = disable_event = None with gevent.Timeout(2, False): # The server should time us out in 1 second and send back these # two messages. auth_failed_event = recv_client_event(ws) disable_event = recv_client_event(ws) auth_failed_dict = fmt_err_msg('auth', 'Authentication failed') self.assertEqual(auth_failed_event, auth_failed_dict) self.assertEqual(disable_event['e'], 'disable') self.assertFalse('test' in server.session_store)
def test_client_auth_timeout(self): with QdbServer( client_host='localhost', client_port=0, auth_timeout=1, # Timeout after 1 second. tracer_server=QdbNopServer()) as server: ws = create_connection('ws://localhost:%d%s' % (server.client_server.server_port, DEFAULT_ROUTE_FMT.format(uuid='test'))) auth_failed_dict = fmt_err_msg('auth', 'No start event received') disable_dict = fmt_msg('disable') auth_failed_msg = '' disable_msg = '' with gevent.Timeout(2, False): # The server should time us out in 1 second and send back these # two messages. auth_failed_msg = ws.recv() disable_msg = ws.recv() self.assertEqual(auth_failed_msg, json.dumps(auth_failed_dict)) self.assertEqual(disable_msg, json.dumps(disable_dict)) self.assertFalse('test' in server.session_store)
def test_inactivity_timeout(self, mode): """ Tests that timeout sends a disable message with the proper mode.. """ with QdbServer( tracer_host='localhost', tracer_port=0, client_host='localhost', client_port=0, inactivity_timeout=0.01, # minutes sweep_time=0.01, # seconds timeout_disable_mode=mode) as server: tracer = gevent.socket.create_connection( ('localhost', server.tracer_server.server_port)) send_tracer_event(tracer, 'start', { 'uuid': 'test', 'auth': '', 'local': (0, 0), }) client = create_connection('ws://localhost:%d%s' % (server.client_server.server_port, DEFAULT_ROUTE_FMT.format(uuid='test'))) send_client_event(client, 'start', '') self.assertEqual({ 'e': 'start', 'p': '' }, recv_tracer_event(tracer)) self.assertEqual({ 'e': 'disable', 'p': mode }, recv_tracer_event(tracer)) self.assertEqual('disable', recv_client_event(client)['e']) self.assertFalse('test' in server.session_store)
def test_client_attach_timeout(self, mode): """ Tests the case when a client attaches but no tracer does. """ with QdbServer(tracer_server=QdbNopServer(), client_host='localhost', client_port=0, attach_timeout=0.01, timeout_disable_mode=mode) as server: client = create_connection( 'ws://localhost:%d%s' % (server.client_server.server_port, DEFAULT_ROUTE_FMT.format(uuid='test')) ) send_client_event(client, 'start', '') disable_event = None with Timeout(0.1, False): error_event = recv_client_event(client) disable_event = recv_client_event(client) error_dict = fmt_err_msg('tracer', 'No tracer') self.assertEqual(error_dict, error_event) self.assertEqual(fmt_msg('disable'), disable_event) self.assertNotIn('test', server.session_store)
def test_bad_auth_client(self): """ Tests a non-valid auth message for a client. """ with QdbServer(client_host='localhost', client_port=0, client_auth_fn=lambda _: False, # Fail all new clients. tracer_server=QdbNopServer()) as server: ws = create_connection( 'ws://localhost:%d%s' % (server.client_server.server_port, DEFAULT_ROUTE_FMT.format(uuid='test')) ) send_client_event(ws, 'start', 'friendzoned-again') auth_failed_event = disable_event = None with Timeout(2, False): # The server should time us out in 1 second and send back these # two messages. auth_failed_event = recv_client_event(ws) disable_event = recv_client_event(ws) auth_failed_dict = fmt_err_msg('auth', 'Authentication failed') self.assertEquals(auth_failed_event, auth_failed_dict) self.assertEquals(disable_event['e'], 'disable') self.assertFalse('test' in server.session_store)
def test_inactivity_timeout(self, mode): """ Tests that timeout sends a disable message with the proper mode.. """ with QdbServer(tracer_host='localhost', tracer_port=0, client_host='localhost', client_port=0, inactivity_timeout=0.01, # minutes sweep_time=0.01, # seconds timeout_disable_mode=mode) as server: tracer = socket.create_connection( ('localhost', server.tracer_server.server_port) ) send_tracer_event(tracer, 'start', { 'uuid': 'test', 'auth': '', 'local': (0, 0), }) client = create_connection( 'ws://localhost:%d%s' % (server.client_server.server_port, DEFAULT_ROUTE_FMT.format(uuid='test')) ) send_client_event(client, 'start', '') self.assertEqual({'e': 'start', 'p': ''}, recv_tracer_event(tracer)) self.assertEqual({'e': 'disable', 'p': mode}, recv_tracer_event(tracer)) self.assertEqual('disable', recv_client_event(client)['e']) self.assertFalse('test' in server.session_store)
def test_client_auth_timeout(self): with QdbServer(client_host='localhost', client_port=0, auth_timeout=1, # Timeout after 1 second. tracer_server=QdbNopServer()) as server: ws = create_connection( 'ws://localhost:%d%s' % (server.client_server.server_port, DEFAULT_ROUTE_FMT.format(uuid='test')) ) auth_failed_dict = fmt_err_msg('auth', 'No start event received') disable_dict = fmt_msg('disable') auth_failed_msg = '' disable_msg = '' with Timeout(2, False): # The server should time us out in 1 second and send back these # two messages. auth_failed_msg = ws.recv() disable_msg = ws.recv() self.assertEquals(auth_failed_msg, json.dumps(auth_failed_dict)) self.assertEquals(disable_msg, json.dumps(disable_dict)) self.assertFalse('test' in server.session_store)
def test_client_attach_timeout(self, mode): """ Tests the case when a client attaches but no tracer does. """ with QdbServer(tracer_server=QdbNopServer(), client_host='localhost', client_port=0, attach_timeout=0.01, timeout_disable_mode=mode) as server: client = create_connection( 'ws://localhost:%d%s' % (server.client_server.server_port, DEFAULT_ROUTE_FMT.format(uuid='test')) ) send_client_event(client, 'start', '') disable_event = None with gevent.Timeout(0.1, False): error_event = recv_client_event(client) disable_event = recv_client_event(client) error_dict = fmt_err_msg('tracer', 'No tracer') self.assertEqual(error_dict, error_event) self.assertEqual(fmt_msg('disable'), disable_event) self.assertNotIn('test', server.session_store)
def test_client_orphan_session(self): """ Tests that a client makes it into the session store without a tracer attaching if attach_timeout is set to ALLOW_ORPHANS or 0. """ with QdbServer(tracer_server=QdbNopServer(), client_host='localhost', client_port=0, attach_timeout=ALLOW_ORPHANS) as server: client = create_connection('ws://localhost:%d%s' % (server.client_server.server_port, DEFAULT_ROUTE_FMT.format(uuid='test'))) send_client_event(client, 'start', '') sleep(0.01) # yield to the session_store to let it get attached. self.assertIn('test', server.session_store)
def test_client_orphan_session(self): """ Tests that a client makes it into the session store without a tracer attaching if attach_timeout is set to ALLOW_ORPHANS or 0. """ with QdbServer(tracer_server=QdbNopServer(), client_host='localhost', client_port=0, attach_timeout=ALLOW_ORPHANS) as server: client = create_connection( 'ws://localhost:%d%s' % (server.client_server.server_port, DEFAULT_ROUTE_FMT.format(uuid='test')) ) send_client_event(client, 'start', '') sleep(0.01) # yield to the session_store to let it get attached. self.assertIn('test', server.session_store)