def test_empty_json(self, dummy_request): from channelstream.wsgi_views.server import message dummy_request.json_body = {} with pytest.raises(marshmallow.exceptions.ValidationError) as excinfo: message(dummy_request) assert excinfo.value.messages == {"_schema": ["Invalid input type."]}
def test_good_json_no_channel(self, dummy_request): from channelstream.wsgi_views.server import message, messages_patch server_state = get_state() channel = Channel("test") channel.store_history = True server_state.channels[channel.name] = channel msg_payload = {"user": "******", "channel": "test", "message": {"text": "test"}} dummy_request.json_body = [msg_payload] message(dummy_request) # change context gevent.sleep(0) msg = channel.history[0] assert msg["message"] == msg_payload["message"] edit_payload = { "uuid": msg["uuid"], "user": "******", "channel": "test", "timestamp": "2010-01-01T01:01", "edited": "2010-01-01T01:02", "message": {"text": "edited_message"}, } dummy_request.json_body = [edit_payload] response = messages_patch(dummy_request)[0] gevent.sleep(0) assert msg["user"] == response["user"] assert msg["message"] == response["message"] assert msg["edited"] == response["edited"] assert msg["timestamp"] == response["timestamp"] frame = channel.frames[0][1] assert id(frame) == id(msg) assert frame["user"] == response["user"] assert frame["message"] == response["message"] assert frame["edited"] == response["edited"] assert frame["timestamp"] == response["timestamp"]
def test_good_json_no_channel(self, dummy_request): from channelstream.wsgi_views.server import message server_state = get_state() channel = Channel("test") channel.store_history = True server_state.channels[channel.name] = channel msg_payload = { "type": "message", "user": "******", "channel": "test", "message": {"text": "test"}, } dummy_request.json_body = [msg_payload] assert server_state.stats["total_unique_messages"] == 0 assert len(channel.history) == 0 message(dummy_request) # change context gevent.sleep(0) assert server_state.stats["total_unique_messages"] == 1 assert len(channel.history) == 1 msg = channel.history[0] assert msg["uuid"] is not None assert msg["user"] == msg_payload["user"] assert msg["message"] == msg_payload["message"] assert msg["type"] == msg_payload["type"] assert msg["channel"] == msg_payload["channel"] assert msg["timestamp"] is not None
def test_good_json_no_channel(self, dummy_request): from channelstream.wsgi_views.server import message server_state = get_state() channel = Channel("test") channel.store_history = True server_state.channels[channel.name] = channel msg_payload = { "type": "message", "user": "******", "channel": "test", "message": { "text": "test" }, } dummy_request.json_body = [msg_payload] assert server_state.stats["total_unique_messages"] == 0 assert len(channel.history) == 0 message(dummy_request) # change context gevent.sleep(0) assert server_state.stats["total_unique_messages"] == 1 assert len(channel.history) == 1 msg = channel.history[0] assert msg["uuid"] is not None assert msg["user"] == msg_payload["user"] assert msg["message"] == msg_payload["message"] assert msg["type"] == msg_payload["type"] assert msg["channel"] == msg_payload["channel"] assert msg["timestamp"] is not None
def test_good_json_no_channel(self, dummy_request): from channelstream.wsgi_views.server import message, messages_delete server_state = get_state() channel = Channel("test") channel.store_history = True server_state.channels[channel.name] = channel msg_payload = { "user": "******", "channel": "test", "message": { "text": "test" } } dummy_request.json_body = [msg_payload] message(dummy_request) # change context gevent.sleep(0) msg = channel.history[0] assert msg["message"] == msg_payload["message"] dummy_request.json_body = [{ "uuid": str(msg["uuid"]), "channel": "test" }] response = messages_delete(dummy_request) gevent.sleep(0) assert response[0]["uuid"] == msg["uuid"] assert len(channel.history) == 0 assert len(channel.frames) == 1 assert channel.frames[0][1]["type"] == "message:delete"
def test_empty_json(self, dummy_request): from channelstream.wsgi_views.server import message server_state = get_state() dummy_request.json_body = {} assert server_state.stats["total_unique_messages"] == 0 with pytest.raises(marshmallow.exceptions.ValidationError) as excinfo: message(dummy_request) assert excinfo.value.messages == {"_schema": ["Invalid input type."]}
def test_catchup_messages(self, dummy_request): from channelstream.wsgi_views.server import message, connect server_state = get_state() dummy_request.json_body = { "username": "******", "channels": ["test"], "channel_configs": { "test": { "store_history": True, "history_size": 2 } }, } connect(dummy_request) msg_payload = { "type": "message", "user": "******", "channel": "test", "message": { "text": "test3" }, } dummy_request.json_body = [msg_payload] message(dummy_request) # add pm message to non-existing user wrong_user_msg_payload = { "type": "message", "user": "******", "channel": "test", "message": { "text": "test1" }, "pm_users": ["test2"], } msg_payload = { "type": "message", "user": "******", "channel": "test", "message": { "text": "test2" }, "pm_users": ["test1"], } dummy_request.json_body = [wrong_user_msg_payload, msg_payload] message(dummy_request) # change context gevent.sleep(0) connection = server_state.users["test1"].connections[0] messages = connection.get_catchup_messages() assert len(messages) == 2 assert messages[0]["timestamp"] > connection.last_active assert messages[0]["message"]["text"] == "test3" assert messages[1]["timestamp"] > connection.last_active assert messages[1]["message"]["text"] == "test2"
def test_detailed_json(self, dummy_request, test_uuids): from channelstream.wsgi_views.server import connect, info, message dummy_request.json_body = { "username": "******", "conn_id": str(test_uuids[1]), "fresh_user_state": { "key": "foo" }, "user_state": { "bar": "baz", "private": "p1" }, "state_public_keys": ["bar"], "channels": ["a", "aB", "c", "D"], "channel_configs": { "a": { "store_history": True, "history_size": 2 } }, } connect(dummy_request) dummy_request.json_body = [{ "type": "message", "user": "******", "channel": "a", "message": { "text": "test" }, }] message(dummy_request) gevent.sleep(0) dummy_request.body = "value" dummy_request.json_body = { "info": { "exclude_channels": ["c"], "include_history": False, "include_users": True, "return_public_state": True, "include_connections": True, } } result = info(dummy_request) assert sorted(result["channels"].keys()) == sorted(["a", "aB", "D"]) assert "private" not in result["users"][0]["state"] assert len(result["channels"]["a"]["history"]) == 0
def test_empty_json(self, dummy_request): from channelstream.wsgi_views.server import message server_state = get_state() dummy_request.json_body = {} assert server_state.stats["total_unique_messages"] == 0 result = message(dummy_request) assert server_state.stats["total_unique_messages"] == 0
def test_catchup_messages(self, dummy_request): from channelstream.wsgi_views.server import message, connect server_state = get_state() dummy_request.json_body = { "username": "******", "channels": ["test"], "channel_configs": {"test": {"store_history": True, "history_size": 2}}, } connect(dummy_request) msg_payload = { "type": "message", "user": "******", "channel": "test", "message": {"text": "test3"}, } dummy_request.json_body = [msg_payload] message(dummy_request) # add pm message to non-existing user wrong_user_msg_payload = { "type": "message", "user": "******", "channel": "test", "message": {"text": "test1"}, "pm_users": ["test2"], } msg_payload = { "type": "message", "user": "******", "channel": "test", "message": {"text": "test2"}, "pm_users": ["test1"], } dummy_request.json_body = [wrong_user_msg_payload, msg_payload] message(dummy_request) # change context gevent.sleep(0) connection = server_state.users["test1"].connections[0] messages = connection.get_catchup_messages() assert len(messages) == 2 assert messages[0]["timestamp"] > connection.last_active assert messages[0]["message"]["text"] == "test3" assert messages[1]["timestamp"] > connection.last_active assert messages[1]["message"]["text"] == "test2"
def test_good_json_no_channel(self, dummy_request): from channelstream.wsgi_views.server import message, messages_patch server_state = get_state() channel = Channel("test") channel.store_history = True server_state.channels[channel.name] = channel msg_payload = { "user": "******", "channel": "test", "message": { "text": "test" } } dummy_request.json_body = [msg_payload] message(dummy_request) # change context gevent.sleep(0) msg = channel.history[0] assert msg["message"] == msg_payload["message"] edit_payload = { "uuid": msg["uuid"], "user": "******", "channel": "test", "timestamp": "2010-01-01T01:01", "edited": "2010-01-01T01:02", "message": { "text": "edited_message" }, } dummy_request.json_body = [edit_payload] response = messages_patch(dummy_request)[0] gevent.sleep(0) assert msg["user"] == response["user"] assert msg["message"] == response["message"] assert msg["edited"] == response["edited"] assert msg["timestamp"] == response["timestamp"] frame = channel.frames[0][1] assert id(frame) == id(msg) assert frame["user"] == response["user"] assert frame["message"] == response["message"] assert frame["edited"] == response["edited"] assert frame["timestamp"] == response["timestamp"]
def test_good_json_no_channel(self, dummy_request): from channelstream.wsgi_views.server import message, messages_delete server_state = get_state() channel = Channel("test") channel.store_history = True server_state.channels[channel.name] = channel msg_payload = {"user": "******", "channel": "test", "message": {"text": "test"}} dummy_request.json_body = [msg_payload] message(dummy_request) # change context gevent.sleep(0) msg = channel.history[0] assert msg["message"] == msg_payload["message"] dummy_request.json_body = [{"uuid": str(msg["uuid"]), "channel": "test"}] response = messages_delete(dummy_request) gevent.sleep(0) assert response[0]["uuid"] == msg["uuid"] assert len(channel.history) == 0 assert len(channel.frames) == 1 assert channel.frames[0][1]["type"] == "message:delete"
def test_detailed_json(self, dummy_request, test_uuids): from channelstream.wsgi_views.server import connect, info, message dummy_request.json_body = { "username": "******", "conn_id": str(test_uuids[1]), "fresh_user_state": {"key": "foo"}, "user_state": {"bar": "baz", "private": "p1"}, "state_public_keys": ["bar"], "channels": ["a", "aB", "c", "D"], "channel_configs": {"a": {"store_history": True, "history_size": 2}}, } connect(dummy_request) dummy_request.json_body = [ { "type": "message", "user": "******", "channel": "a", "message": {"text": "test"}, } ] message(dummy_request) gevent.sleep(0) dummy_request.body = "value" dummy_request.json_body = { "info": { "exclude_channels": ["c"], "include_history": False, "include_users": True, "return_public_state": True, "include_connections": True, } } result = info(dummy_request) assert sorted(result["channels"].keys()) == sorted(["a", "aB", "D"]) assert "private" not in result["users"][0]["state"] assert len(result["channels"]["a"]["history"]) == 0
def test_empty_json(self, dummy_request): from channelstream.wsgi_views.server import message dummy_request.json_body = {} result = message(dummy_request) assert result == []