Example #1
0
    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
Example #2
0
 def test_presence_message_w_users(self, test_uuids):
     server_state = get_state()
     user = User("test_user")
     user.state_from_dict({"key": "1", "key2": "2"})
     user.state_public_keys = ["key2"]
     server_state.users[user.username] = user
     connection = Connection("test_user", conn_id=test_uuids[1])
     user.add_connection(connection)
     user2 = User("test_user2")
     user2.state_from_dict({"key": "1", "key2": "2"})
     server_state.users[user2.username] = user2
     connection2 = Connection("test_user2", conn_id=test_uuids[2])
     user2.add_connection(connection2)
     config = {
         "notify_presence": True,
         "broadcast_presence_with_user_lists": True
     }
     channel = Channel("test", channel_config=config)
     channel.add_connection(connection)
     channel.add_connection(connection2)
     payload = channel.send_notify_presence_info("test_user", "join")
     assert len(payload["users"]) == 2
     sorted_users = sorted(payload["users"], key=lambda x: x["user"])
     assert sorted_users == [
         {
             "state": {
                 "key2": "2"
             },
             "user": "******"
         },
         {
             "state": {},
             "user": "******"
         },
     ]
Example #3
0
    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"
Example #4
0
def subscribe(connection=None, channels=None, channel_configs=None):
    """

    :param connection:
    :param channels:
    :param channel_configs:
    :return:
    """
    server_state = get_state()
    user = server_state.users.get(connection.username)
    subscribed_to = []
    with server_state.lock:
        if user:
            for channel_name in channels:
                if channel_name not in server_state.channels:
                    channel = Channel(
                        channel_name, channel_config=channel_configs.get(channel_name)
                    )
                    server_state.channels[channel_name] = channel
                is_found = server_state.channels[channel_name].add_connection(
                    connection
                )
                if is_found:
                    subscribed_to.append(channel_name)
    return subscribed_to
Example #5
0
 def test_add_connection(self, test_uuids):
     connection = Connection("test_user", conn_id=test_uuids[1])
     channel = Channel("test")
     channel.add_connection(connection)
     assert len(channel.connections["test_user"]) == 1
     assert "test_user" in channel.connections
     assert connection in channel.connections["test_user"]
     assert repr(channel) == "<Channel: test, connections:1>"
Example #6
0
 def test_gc_connections_collecting(self, test_uuids):
     server_state = get_state()
     channel = Channel("test")
     server_state.channels[channel.name] = channel
     channel2 = Channel("test2")
     server_state.channels[channel2.name] = channel2
     user = User("test_user")
     server_state.users[user.username] = user
     user2 = User("test_user2")
     server_state.users[user2.username] = user2
     connection = Connection("test_user", test_uuids[1])
     server_state.connections[connection.id] = connection
     connection2 = Connection("test_user", test_uuids[2])
     connection2.mark_for_gc()
     server_state.connections[connection2.id] = connection2
     connection3 = Connection("test_user2", test_uuids[3])
     connection3.mark_for_gc()
     server_state.connections[connection3.id] = connection3
     connection4 = Connection("test_user2", test_uuids[4])
     server_state.connections[connection4.id] = connection4
     user.add_connection(connection)
     user.add_connection(connection2)
     channel.add_connection(connection)
     channel.add_connection(connection2)
     user2.add_connection(connection3)
     user2.add_connection(connection4)
     channel2.add_connection(connection3)
     channel2.add_connection(connection4)
     channelstream.gc.gc_conns()
     assert len(server_state.connections.items()) == 2
     conns = server_state.channels["test"].connections["test_user"]
     assert len(conns) == 1
     assert conns == [connection]
     conns = server_state.channels["test2"].connections["test_user2"]
     assert len(conns) == 1
     assert conns == [connection4]
     assert len(user.connections) == 1
     assert len(user2.connections) == 1
     connection.mark_for_gc()
     connection4.mark_for_gc()
     channelstream.gc.gc_conns()
     assert "test_user" not in server_state.channels["test"].connections
     assert "test_user2" not in server_state.channels["test2"].connections
     assert len(server_state.channels["test"].connections.items()) == 0
     assert len(server_state.channels["test2"].connections.items()) == 0
Example #7
0
 def test_user_multi_assignment(self, test_uuids):
     server_state = get_state()
     user = User("test_user")
     connection = Connection("test_user", conn_id=test_uuids[1])
     connection2 = Connection("test_user", conn_id=test_uuids[2])
     connection3 = Connection("test_user", conn_id=test_uuids[3])
     user.add_connection(connection)
     user.add_connection(connection2)
     user.add_connection(connection3)
     channel = Channel("test")
     channel2 = Channel("test2")
     server_state.channels[channel.name] = channel
     server_state.channels[channel2.name] = channel2
     channel.add_connection(connection)
     channel.add_connection(connection2)
     channel2.add_connection(connection3)
     assert ["test",
             "test2"] == sorted([c.name for c in user.get_channels()])
Example #8
0
 def test_user_single_assignment(self, test_uuids):
     server_state = get_state()
     user = User("test_user")
     connection = Connection("test_user", conn_id=test_uuids[1])
     user.add_connection(connection)
     channel = Channel("test")
     server_state.channels[channel.name] = channel
     channel.add_connection(connection)
     assert [channel] == user.get_channels()
Example #9
0
 def test_create_defaults(self):
     channel = Channel("test", long_name="long name")
     assert channel.name == "test"
     assert channel.long_name == "long name"
     assert channel.connections == {}
     assert channel.notify_presence is False
     assert channel.broadcast_presence_with_user_lists is False
     assert channel.salvageable is False
     assert channel.store_history is False
     assert channel.history_size == 10
     assert channel.history == []
Example #10
0
 def test_presence_message(self, test_uuids):
     user = User("test_user")
     connection = Connection("test_user", conn_id=test_uuids[1])
     user.add_connection(connection)
     channel = Channel("test")
     channel.add_connection(connection)
     payload = channel.send_notify_presence_info("test_user", "join")
     assert payload["user"] == "test_user"
     assert payload["message"]["action"] == "join"
     assert payload["type"] == "presence"
     assert payload["channel"] == "test"
     assert len(payload["users"]) == 0
Example #11
0
 def test_remove_connection_w_presence(self, test_uuids):
     server_state = get_state()
     user = User("test_user")
     server_state.users[user.username] = user
     connection = Connection("test_user", conn_id=test_uuids[1])
     user.add_connection(connection)
     config = {
         "notify_presence": True,
         "broadcast_presence_with_user_lists": True
     }
     channel = Channel("test", channel_config=config)
     channel.add_connection(connection)
     channel.remove_connection(connection)
Example #12
0
 def test_user_state(self, test_uuids):
     user = User("test_user")
     changed = user.state_from_dict({"key": "1", "key2": "2"})
     user.state_public_keys = ["key2"]
     connection = Connection("test_user", conn_id=test_uuids[1])
     user.add_connection(connection)
     channel = Channel("test")
     channel.add_connection(connection)
     payload = channel.send_user_state(user, changed)
     assert payload["user"] == "test_user"
     assert payload["message"]["state"] == {"key2": "2"}
     assert payload["message"]["changed"] == [{"key": "key2", "value": "2"}]
     assert payload["type"] == "user_state_change"
     assert payload["channel"] == "test"
Example #13
0
 def test_remove_connection(self, test_uuids):
     connection = Connection("test_user", conn_id=test_uuids[1])
     connection2 = Connection("test_user2", conn_id=test_uuids[2])
     connection3 = Connection("test_user", conn_id=test_uuids[3])
     channel = Channel("test")
     channel.add_connection(connection)
     channel.add_connection(connection2)
     channel.remove_connection(connection)
     assert "test_user" not in channel.connections
     assert len(channel.connections["test_user2"]) == 1
     channel.add_connection(connection)
     channel.add_connection(connection3)
     channel.remove_connection(connection)
     assert len(channel.connections["test_user"]) == 1
Example #14
0
 def test_add_connection_w_presence(self):
     server_state = get_state()
     user = User("test_user")
     server_state.users[user.username] = user
     connection = Connection("test_user", conn_id=test_uuids[1])
     user.add_connection(connection)
     config = {
         "notify_presence": True,
         "broadcast_presence_with_user_lists": True
     }
     channel = Channel("test", channel_config=config)
     channel.add_connection(connection)
     assert len(channel.connections["test_user"]) == 1
     assert "test_user" in channel.connections
     assert connection in channel.connections["test_user"]
Example #15
0
 def test_gc_connections_active(self, test_uuids):
     server_state = get_state()
     channel = Channel("test")
     server_state.channels[channel.name] = channel
     channel2 = Channel("test2")
     server_state.channels[channel2.name] = channel2
     user = User("test_user")
     server_state.users[user.username] = user
     user2 = User("test_user2")
     server_state.users[user2.username] = user2
     connection = Connection("test_user", test_uuids[1])
     server_state.connections[connection.id] = connection
     connection2 = Connection("test_user", test_uuids[2])
     server_state.connections[connection2.id] = connection2
     connection3 = Connection("test_user2", test_uuids[3])
     server_state.connections[connection3.id] = connection3
     connection4 = Connection("test_user2", test_uuids[4])
     server_state.connections[connection4.id] = connection4
     user.add_connection(connection)
     user.add_connection(connection2)
     channel.add_connection(connection)
     channel.add_connection(connection2)
     user2.add_connection(connection3)
     user2.add_connection(connection4)
     channel2.add_connection(connection3)
     channel2.add_connection(connection4)
     channelstream.gc.gc_conns()
     conns = server_state.channels["test"].connections["test_user"]
     assert len(conns) == 2
     assert len(server_state.connections.items()) == 4
     conns = server_state.channels["test2"].connections["test_user2"]
     assert len(conns) == 2
     assert len(user.connections) == 2
     assert len(user2.connections) == 2
     assert sorted(channel.connections.keys()) == ["test_user"]
     assert sorted(channel2.connections.keys()) == ["test_user2"]
Example #16
0
def set_channel_config(channel_configs):
    """

    :param channel_configs:
    :return:
    """
    server_state = get_state()
    with server_state.lock:
        for channel_name, config in channel_configs.items():
            if not server_state.channels.get(channel_name):
                channel = Channel(
                    channel_name, channel_config=channel_configs.get(channel_name)
                )
                server_state.channels[channel_name] = channel
            else:
                channel = server_state.channels[channel_name]
                channel.reconfigure_from_dict(channel_configs.get(channel_name))
Example #17
0
def connect(
    username=None,
    fresh_user_state=None,
    state_public_keys=None,
    update_user_state=None,
    conn_id=None,
    channels=None,
    channel_configs=None,
):
    """

    :param username:
    :param fresh_user_state:
    :param state_public_keys:
    :param update_user_state:
    :param conn_id:
    :param channels:
    :param channel_configs:
    :return:
    """
    server_state = get_state()
    with server_state.lock:
        if username not in server_state.users:
            user = User(username)
            user.state_from_dict(fresh_user_state)
            server_state.users[username] = user
        else:
            user = server_state.users[username]
        if state_public_keys is not None:
            user.state_public_keys = state_public_keys

        user.state_from_dict(update_user_state)
        connection = Connection(username, conn_id)
        if connection.id not in server_state.connections:
            server_state.connections[connection.id] = connection
        user.add_connection(connection)
        for channel_name in channels:
            # user gets assigned to a channel
            if channel_name not in server_state.channels:
                channel = Channel(
                    channel_name, channel_config=channel_configs.get(channel_name)
                )
                server_state.channels[channel_name] = channel
            server_state.channels[channel_name].add_connection(connection)
        log.info("connecting %s with uuid %s" % (username, connection.id))
        return connection, user
Example #18
0
    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"]
Example #19
0
 def test_remove_non_existant_connection(self, test_uuids):
     channel = Channel("test")
     connection = Connection("test_user", conn_id=test_uuids[1])
     channel.remove_connection(connection)
     assert "test_user" not in channel.connections
Example #20
0
    def test_create_set_config(self, prop, value):
        channel_config = {prop: value}
        channel = Channel("test", channel_config=channel_config)

        assert getattr(channel, prop) == value
Example #21
0
 def test_repr(self):
     channel = Channel("test", long_name="long name")
     assert repr(channel) == "<Channel: test, connections:0>"
Example #22
0
    def test_history(self):
        config = {"store_history": True, "history_size": 3}
        channel = Channel("test", long_name="long name", channel_config=config)
        channel.add_message({
            "channel": "test",
            "message": "test1",
            "type": "message",
            "no_history": False,
            "pm_users": [],
            "exclude_users": [],
        })
        channel.add_message({
            "channel": "test",
            "message": "test2",
            "type": "message",
            "no_history": False,
            "pm_users": [],
            "exclude_users": [],
        })
        channel.add_message({
            "channel": "test",
            "message": "test3",
            "type": "message",
            "no_history": False,
            "pm_users": [],
            "exclude_users": [],
        })
        channel.add_message({
            "channel": "test",
            "message": "test4",
            "type": "message",
            "no_history": True,
            "pm_users": [],
            "exclude_users": [],
        })
        channel.add_message({
            "channel": "test",
            "message": "test5",
            "type": "message",
            "no_history": False,
            "pm_users": [],
            "exclude_users": [],
        })

        assert len(channel.history) == 3
        assert channel.history == [
            {
                "channel": "test",
                "message": "test2",
                "type": "message",
                "no_history": False,
                "pm_users": [],
                "exclude_users": [],
            },
            {
                "channel": "test",
                "message": "test3",
                "type": "message",
                "no_history": False,
                "pm_users": [],
                "exclude_users": [],
            },
            {
                "channel": "test",
                "message": "test5",
                "type": "message",
                "no_history": False,
                "pm_users": [],
                "exclude_users": [],
            },
        ]