Example #1
0
def test_redis_event_manager_publish():
    """Test that RedisEventManager publishing to clients works."""

    # TODO: This test is flaky.
    class RedisConfig(TestingConfig):
        REDIS_URL = 'redis://localhost:6379'
        CACHE_REDIS_URL = 'redis://localhost:6379'
        CACHE_TYPE = 'redis'

    try:
        app = create_ctfd(config=RedisConfig)
        with app.app_context():
            saved_data = {
                'user_id': None,
                'title': 'asdf',
                'content': 'asdf',
                'team_id': None,
                'user': None,
                'team': None,
                'date': '2019-01-28T01:20:46.017649+00:00',
                'id': 10
            }

            event_manager = RedisEventManager()
            event_manager.publish(data=saved_data,
                                  type='notification',
                                  channel='ctf')
        destroy_ctfd(app)
    except ConnectionError:
        print("Failed to connect to redis. Skipping test.")
Example #2
0
def test_redis_event_manager_publish():
    """Test that RedisEventManager publishing to clients works."""
    class RedisConfig(TestingConfig):
        REDIS_URL = "redis://localhost:6379/3"
        CACHE_REDIS_URL = "redis://localhost:6379/3"
        CACHE_TYPE = "redis"

    try:
        app = create_ctfd(config=RedisConfig)
    except ConnectionError:
        print("Failed to connect to redis. Skipping test.")
    else:
        with app.app_context():
            saved_data = {
                "user_id": None,
                "title": "asdf",
                "content": "asdf",
                "team_id": None,
                "user": None,
                "team": None,
                "date": "2019-01-28T01:20:46.017649+00:00",
                "id": 10,
            }

            event_manager = RedisEventManager()
            event_manager.publish(data=saved_data,
                                  type="notification",
                                  channel="ctf")
        destroy_ctfd(app)
def test_redis_event_manager_listen():
    """Test that RedisEventManager listening pubsub works."""
    # This test is nob currently working properly
    # This test is sort of incomplete b/c we aren't also subscribing
    # I wasnt able to get listening and subscribing to work at the same time
    # But the code does work under gunicorn and serve.py
    try:
        # import importlib
        # from gevent.monkey import patch_time, patch_socket
        # from gevent import Timeout

        # patch_time()
        # patch_socket()

        class RedisConfig(TestingConfig):
            REDIS_URL = "redis://localhost:6379/4"
            CACHE_REDIS_URL = "redis://localhost:6379/4"
            CACHE_TYPE = "redis"

        try:
            app = create_ctfd(config=RedisConfig)
        except ConnectionError:
            print("Failed to connect to redis. Skipping test.")
        else:
            with app.app_context():
                # saved_event = {
                #     "data": {
                #         "team_id": None,
                #         "user_id": None,
                #         "content": "asdf",
                #         "title": "asdf",
                #         "id": 1,
                #         "team": None,
                #         "user": None,
                #         "date": "2020-08-31T23:57:27.193081+00:00",
                #         "type": "toast",
                #         "sound": None,
                #     },
                #     "type": "notification",
                # }

                event_manager = RedisEventManager()

                # def disable_retry(f, *args, **kwargs):
                #     return f()

                # with patch("tenacity.retry", side_effect=disable_retry):
                #     with Timeout(10):
                #         event_manager.listen()
                event_manager.listen()

                # event_manager.publish(
                #     data=saved_event["data"], type="notification", channel="ctf"
                # )
            destroy_ctfd(app)
    finally:
        pass
Example #4
0
def test_redis_event_manager_subscription():
    """Test that RedisEventManager subscribing works."""
    class RedisConfig(TestingConfig):
        REDIS_URL = "redis://localhost:6379/2"
        CACHE_REDIS_URL = "redis://localhost:6379/2"
        CACHE_TYPE = "redis"

    try:
        app = create_ctfd(config=RedisConfig)
    except ConnectionError:
        print("Failed to connect to redis. Skipping test.")
    else:
        with app.app_context():
            saved_data = {
                u"data": {
                    u"content": u"asdf",
                    u"date": u"2019-01-28T05:02:19.830906+00:00",
                    u"id": 13,
                    u"team": None,
                    u"team_id": None,
                    u"title": u"asdf",
                    u"user": None,
                    u"user_id": None,
                },
                u"type": u"notification",
            }

            saved_event = {
                "pattern": None,
                "type": "message",
                "channel": "ctf",
                "data": json.dumps(saved_data),
            }
            with patch.object(redis.client.PubSub,
                              "listen") as fake_pubsub_listen:
                fake_pubsub_listen.return_value = [saved_event]
                event_manager = RedisEventManager()

                events = event_manager.subscribe()
                message = next(events)
                assert isinstance(message, ServerSentEvent)
                assert message.to_dict() == {"data": "", "type": "ping"}
                assert message.__str__().startswith("event:ping")

                message = next(events)
                assert isinstance(message, ServerSentEvent)
                assert message.to_dict() == saved_data
                assert message.__str__().startswith(
                    "event:notification\ndata:")
        destroy_ctfd(app)
Example #5
0
def init_events(app):
    if app.config.get("CACHE_TYPE") == "redis":
        app.events_manager = RedisEventManager()
    elif app.config.get("CACHE_TYPE") == "filesystem":
        app.events_manager = EventManager()
    else:
        app.events_manager = EventManager()
Example #6
0
def init_events(app):
    if app.config.get('CACHE_TYPE') == 'redis':
        app.events_manager = RedisEventManager()
    elif app.config.get('CACHE_TYPE') == 'filesystem':
        app.events_manager = EventManager()
    else:
        app.events_manager = EventManager()
Example #7
0
def test_redis_event_manager_subscription():
    """Test that RedisEventManager subscribing works."""

    # TODO: This test is flaky.
    class RedisConfig(TestingConfig):
        REDIS_URL = 'redis://localhost:6379'
        CACHE_REDIS_URL = 'redis://localhost:6379'
        CACHE_TYPE = 'redis'

    try:
        app = create_ctfd(config=RedisConfig)
        with app.app_context():
            saved_data = {
                u'data': {
                    u'content': u'asdf',
                    u'date': u'2019-01-28T05:02:19.830906+00:00',
                    u'id': 13,
                    u'team': None,
                    u'team_id': None,
                    u'title': u'asdf',
                    u'user': None,
                    u'user_id': None
                },
                u'type': u'notification'
            }

            saved_event = {
                'pattern': None,
                'type': 'message',
                'channel': 'ctf',
                'data': json.dumps(saved_data)
            }
            with patch.object(redis.client.PubSub,
                              'listen') as fake_pubsub_listen:
                fake_pubsub_listen.return_value = [saved_event]
                event_manager = RedisEventManager()
                for message in event_manager.subscribe():
                    assert isinstance(message, ServerSentEvent)
                    assert message.to_dict() == saved_data
                    assert message.__str__().startswith(
                        'event:notification\ndata:')
                    break
        destroy_ctfd(app)
    except ConnectionError:
        print("Failed to connect to redis. Skipping test.")
def test_redis_event_manager_subscription():
    """Test that RedisEventManager subscribing works."""
    class RedisConfig(TestingConfig):
        REDIS_URL = "redis://localhost:6379/2"
        CACHE_REDIS_URL = "redis://localhost:6379/2"
        CACHE_TYPE = "redis"

    try:
        app = create_ctfd(config=RedisConfig)
    except ConnectionError:
        print("Failed to connect to redis. Skipping test.")
    else:
        with app.app_context():
            saved_data = {
                "user_id": None,
                "title": "asdf",
                "content": "asdf",
                "team_id": None,
                "user": None,
                "team": None,
                "date": "2019-01-28T01:20:46.017649+00:00",
                "id": 10,
            }
            saved_event = {"type": "notification", "data": saved_data}

            with patch.object(Queue, "get") as fake_queue:
                fake_queue.return_value = saved_event
                event_manager = RedisEventManager()

                events = event_manager.subscribe()
                message = next(events)
                assert isinstance(message, ServerSentEvent)
                assert message.to_dict() == {"data": "", "type": "ping"}
                assert message.__str__().startswith("event:ping")

                message = next(events)
                assert isinstance(message, ServerSentEvent)
                assert message.to_dict() == saved_event
                assert message.__str__().startswith(
                    "event:notification\ndata:")
        destroy_ctfd(app)