def test_basic_broadcast(self, ws1, ws2): WebSocketDispatcher.broadcast('bar', trigger='manual') ws1.trigger.assert_called_with(client='client-1', callback='callback-1', trigger='manual') assert not ws2.trigger.called assert not ws1.unsubscribe_all.called and not ws2.unsubscribe_all.called
def wsd(monkeypatch): WebSocketDispatcher.instances.clear() monkeypatch.setattr(WebSocket, 'send', Mock()) monkeypatch.setattr(WebSocket, 'closed', Mock()) monkeypatch.setattr(WebSocketDispatcher, 'is_closed', False) monkeypatch.setattr(WebSocketDispatcher, 'check_authentication', lambda cls: mock_session_data) monkeypatch.setattr(WebSocketDispatcher, 'fetch_headers', lambda cls: mock_header_data) return WebSocketDispatcher(None)
def check_authentication(cls): host, origin = cherrypy.request.headers[ 'host'], cherrypy.request.headers['origin'] if ('//' + host.split(':')[0]) not in origin: log.error( 'Javascript websocket connections must follow same-origin policy; origin {!r} does not match host {!r}', origin, host) raise WebSocketAuthError('Origin and Host headers do not match') if config['ws.auth_required'] and not cherrypy.session.get( config['ws.auth_field']): log.warning( 'websocket connections to this address must have a valid session' ) raise WebSocketAuthError('You are not logged in') return WebSocketDispatcher.check_authentication()
def orig_ws(monkeypatch): monkeypatch.setattr(ws4py.websocket.WebSocket, '__init__', Mock(return_value=None)) monkeypatch.setattr(WebSocketDispatcher, 'check_authentication', Mock(return_value={'username': '******'})) return WebSocketDispatcher()
def test_broadcast_error(ws4, monkeypatch): monkeypatch.setattr(log, 'warn', Mock()) WebSocketDispatcher.broadcast('foo') assert not ws4.trigger.called and not log.warn.called WebSocketDispatcher.broadcast('baf') assert ws4.trigger.called and log.warn.called
def test_multi_broadcast(ws1, ws2, ws3, ws4): WebSocketDispatcher.broadcast(['foo', 'bar']) assert ws1.trigger.called and ws2.trigger.called and not ws3.trigger.called and not ws4.trigger.called
def test_broadcast_with_originating_client(ws1, ws2): WebSocketDispatcher.broadcast('foo', originating_client='client-1') assert ws2.trigger.called and not ws1.trigger.called
def test_get_all_subscribed(wsd, ws1, ws2, ws3, ws4): assert WebSocketDispatcher.get_all_subscribed() == { wsd, ws1, ws2, ws3, ws4 }
def wsd(monkeypatch): monkeypatch.setattr(WebSocket, 'send', Mock()) monkeypatch.setattr(WebSocket, 'closed', Mock()) monkeypatch.setattr(WebSocketDispatcher, 'check_authentication', lambda self: 'mock_user') return WebSocketDispatcher(None)
def test_basic_broadcast(ws1, ws2): WebSocketDispatcher.broadcast('bar', trigger='manual') ws1.trigger.assert_called_with(client='client-1', callback='callback-1', trigger='manual') assert not ws2.trigger.called
def test_get_all_subscribed(wsd, ws1, ws2, ws3, ws4): assert WebSocketDispatcher.get_all_subscribed() == {wsd, ws1, ws2, ws3, ws4}
def test_broadcast_closed(self, ws1, ws2): ws1.is_closed = True WebSocketDispatcher.broadcast('foo') assert ws2.trigger.called and not ws1.trigger.called assert ws1.unsubscribe_all.called and not ws2.unsubscribe_all.called