def test_invalid_filter_error(self, validate, socket): message = websocket.Message( socket=socket, payload={"type": "filter", "filter": {"wibble": "giraffe"}} ) validate.side_effect = ValidationError("kaboom!") with mock.patch.object(websocket.Message, "reply") as mock_reply: websocket.handle_filter_message(message) mock_reply.assert_called_once_with(Any.dict.containing(["error"]), ok=False)
def test_pong(self): message = websocket.Message(socket=mock.sentinel.socket, payload={ 'type': 'ping', }) with mock.patch.object(websocket.Message, 'reply') as mock_reply: websocket.handle_ping_message(message) mock_reply.assert_called_once_with({'type': 'pong'})
def test_missing_filter_error(self, matchers, socket): message = websocket.Message(socket=socket, payload={ 'type': 'filter', }) with mock.patch.object(websocket.Message, 'reply') as mock_reply: websocket.handle_filter_message(message) mock_reply.assert_called_once_with(matchers.MappingContaining('error'), ok=False)
def test_sets_client_id(self, socket): """Updates the socket client_id if valid.""" message = websocket.Message(socket=socket, payload={ 'messageType': 'client_id', 'value': 'abcd1234', }) websocket.handle_client_id_message(message) assert socket.client_id == 'abcd1234'
def test_error(self, matchers): message = websocket.Message(socket=mock.sentinel.socket, payload={ 'type': 'wibble', }) with mock.patch.object(websocket.Message, 'reply') as mock_reply: websocket.handle_unknown_message(message) mock_reply.assert_called_once_with(matchers.MappingContaining('error'), ok=False)
def test_missing_value_error(self, matchers, socket): message = websocket.Message(socket=socket, payload={ 'messageType': 'client_id', }) with mock.patch.object(websocket.Message, 'reply') as mock_reply: websocket.handle_client_id_message(message) mock_reply.assert_called_once_with( matchers.mapping_containing('error'), ok=False)
def test_reply_adds_ok(self, ok, socket): """Adds an appropriate `ok` field to the sent message.""" message = websocket.Message(socket=socket, payload={'id': 123}) message.reply({'foo': 'bar'}, ok=ok) socket.send_json.assert_called_once_with({ 'ok': ok, 'reply_to': 123, 'foo': 'bar' })
def test_handle_message_sets_socket_client_id_for_client_id_messages(): socket = mock.Mock() socket.client_id = None message = websocket.Message(socket=socket, payload=json.dumps({ 'messageType': 'client_id', 'value': 'abcd1234', })) websocket.handle_message(message) assert socket.client_id == 'abcd1234'
def test_reply_overrides_ok_and_reply_to_fields_in_payload(self, socket): """Payload `ok` and `reply_to` fields should be ignored.""" message = websocket.Message(socket=socket, payload={'id': 123}) message.reply({'foo': 'bar', 'ok': False, 'reply_to': 'wibble'}) socket.send_json.assert_called_once_with({ 'ok': True, 'reply_to': 123, 'foo': 'bar' })
def test_reply_overrides_ok_and_reply_to_fields_in_payload(self, socket): """Payload `ok` and `reply_to` fields should be ignored.""" message = websocket.Message(socket=socket, payload={"id": 123}) message.reply({"foo": "bar", "ok": False, "reply_to": "wibble"}) socket.send_json.assert_called_once_with({ "ok": True, "reply_to": 123, "foo": "bar" })
def test_reply_adds_ok(self, ok, socket): """Adds an appropriate `ok` field to the sent message.""" message = websocket.Message(socket=socket, payload={"id": 123}) message.reply({"foo": "bar"}, ok=ok) socket.send_json.assert_called_once_with({ "ok": ok, "reply_to": 123, "foo": "bar" })
def test_sets_client_id(self, socket): """Updates the socket client_id if valid.""" message = websocket.Message(socket=socket, payload={ "messageType": "client_id", "value": "abcd1234" }) websocket.handle_client_id_message(message) assert socket.client_id == "abcd1234"
def test_uses_unknown_handler_for_unknown_type(self, unknown_handler): """If the type is unknown, call the `None` handler.""" socket = mock.Mock(spec_set=['close']) message = websocket.Message(socket, payload={ "type": "donkeys", "foo": "bar" }) websocket.handle_message(message) unknown_handler.assert_called_once_with(message, session=None)
def test_uses_appropriate_handler_for_known_type(self, foo_handler): """If the type is recognised, call the relevant handler.""" socket = mock.Mock(spec_set=['close']) message = websocket.Message(socket, payload={ "type": "foo", "foo": "bar" }) websocket.handle_message(message) foo_handler.assert_called_once_with(message, session=None)
def test_replies_with_whoyouare_message(self, socket, userid): """Send back a `whoyouare` message with a userid (which may be null).""" socket.authenticated_userid = userid message = websocket.Message(socket=socket, payload={ 'type': 'whoami', }) with mock.patch.object(websocket.Message, 'reply') as mock_reply: websocket.handle_whoami_message(message) mock_reply.assert_called_once_with({'type': 'whoyouare', 'userid': userid})
def test_invalid_filter_error(self, validate, matchers, socket): message = websocket.Message(socket=socket, payload={ 'type': 'filter', 'filter': {'wibble': 'giraffe'}, }) validate.side_effect = ValidationError('kaboom!') with mock.patch.object(websocket.Message, 'reply') as mock_reply: websocket.handle_filter_message(message) mock_reply.assert_called_once_with(matchers.MappingContaining('error'), ok=False)
def test_sets_socket_filter(self, socket, SocketFilter): filter = { "actions": {}, "match_policy": "include_any", "clauses": [ {"field": "/uri", "operator": "equals", "value": "http://example.com"} ], } message = websocket.Message(socket=socket, payload={"filter": filter}) websocket.handle_filter_message(message) SocketFilter.set_filter.assert_called_once_with(socket, filter)
def test_replies_with_whoyouare_message(self, socket, with_identity): if not with_identity: socket.identity = False message = websocket.Message(socket=socket, payload={"type": "whoami"}) with mock.patch.object(websocket.Message, "reply") as mock_reply: websocket.handle_whoami_message(message) mock_reply.assert_called_once_with({ "type": "whoyouare", "userid": socket.identity.user.userid if with_identity else None, })
def test_sets_socket_filter(self, socket): message = websocket.Message(socket=socket, payload={ 'filter': { 'actions': {}, 'match_policy': 'include_all', 'clauses': [{ 'field': '/uri', 'operator': 'equals', 'value': 'http://example.com', }], } }) websocket.handle_filter_message(message) assert socket.filter is not None
def test_expands_uris_using_passed_session(self, expand_uri, socket): expand_uri.return_value = ['http://example.com', 'http://example.org/'] session = mock.sentinel.db_session message = websocket.Message(socket=socket, payload={ 'filter': { 'actions': {}, 'match_policy': 'include_all', 'clauses': [{ 'field': '/uri', 'operator': 'equals', 'value': 'http://example.com', }], } }) websocket.handle_filter_message(message, session=session) expand_uri.assert_called_once_with(session, 'http://example.com')
def test_handle_message_sets_socket_filter_for_filter_messages(): socket = mock.Mock() socket.filter = None message = websocket.Message(socket=socket, payload=json.dumps({ 'filter': { 'actions': {}, 'match_policy': 'include_all', 'clauses': [{ 'field': '/uri', 'operator': 'equals', 'value': 'http://example.com', }], } })) websocket.handle_message(message) assert socket.filter is not None
def test_sets_socket_filter(self, socket): message = websocket.Message( socket=socket, payload={ "filter": { "actions": {}, "match_policy": "include_any", "clauses": [{ "field": "/uri", "operator": "equals", "value": "http://example.com", }], } }, ) websocket.handle_filter_message(message) assert socket.filter is not None
def test_expands_uris_using_passed_session(self, expand_uri, socket): expand_uri.return_value = ["http://example.com", "http://example.org/"] session = mock.sentinel.db_session message = websocket.Message( socket=socket, payload={ "filter": { "actions": {}, "match_policy": "include_any", "clauses": [{ "field": "/uri", "operator": "equals", "value": "http://example.com", }], } }, ) websocket.handle_filter_message(message, session=session) expand_uri.assert_called_once_with(session, "http://example.com")
def test_expands_uris_in_uri_filter_with_session( self, expand_uri, socket, SocketFilter ): expand_uri.return_value = [ "http://example.com", "http://example.com/alter", "http://example.com/print", ] session = mock.sentinel.db_session message = websocket.Message( socket=socket, payload={ "filter": { "actions": {}, "match_policy": "include_any", "clauses": [ { "field": "/uri", "operator": "equals", "value": "http://example.com", } ], } }, ) websocket.handle_filter_message(message, session=session) # Use Any.list to do order insensitive matching expected_values = Any.list.containing(expand_uri.return_value).only() SocketFilter.set_filter.assert_called_once_with( socket, Any.dict.containing( {"clauses": [Any.dict.containing({"value": expected_values})]} ), )
def ws_message(self): return websocket.Message(socket=mock.sentinel.SOCKET, payload="bar")