2019-04-17 05:36:30 +00:00
|
|
|
from tests.helpers import create_ctfd, destroy_ctfd, login_as_user, register_user
|
|
|
|
from CTFd.utils.events import ServerSentEvent, EventManager, RedisEventManager
|
|
|
|
from CTFd.config import TestingConfig
|
|
|
|
from mock import patch
|
|
|
|
from six.moves.queue import Queue
|
|
|
|
from collections import defaultdict
|
|
|
|
from redis.exceptions import ConnectionError
|
|
|
|
import json
|
|
|
|
import redis
|
|
|
|
|
|
|
|
|
|
|
|
def test_event_manager_installed():
|
|
|
|
"""Test that EventManager is installed on the Flask app"""
|
|
|
|
app = create_ctfd()
|
|
|
|
assert type(app.events_manager) == EventManager
|
|
|
|
destroy_ctfd(app)
|
|
|
|
|
|
|
|
|
|
|
|
def test_event_manager_subscription():
|
|
|
|
"""Test that EventManager subscribing works"""
|
2019-05-12 01:09:37 +00:00
|
|
|
with patch.object(Queue, "get") as fake_queue:
|
2019-04-17 05:36:30 +00:00
|
|
|
saved_data = {
|
2019-05-12 01:09:37 +00:00
|
|
|
"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,
|
2019-04-17 05:36:30 +00:00
|
|
|
}
|
2019-05-12 01:09:37 +00:00
|
|
|
saved_event = {"type": "notification", "data": saved_data}
|
2019-04-17 05:36:30 +00:00
|
|
|
|
|
|
|
fake_queue.return_value = saved_event
|
|
|
|
event_manager = EventManager()
|
|
|
|
for message in event_manager.subscribe():
|
|
|
|
assert message.to_dict() == saved_event
|
2019-05-12 01:09:37 +00:00
|
|
|
assert message.__str__().startswith("event:notification\ndata:")
|
2019-04-17 05:36:30 +00:00
|
|
|
assert len(event_manager.clients) == 1
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
def test_event_manager_publish():
|
|
|
|
"""Test that EventManager publishing to clients works"""
|
|
|
|
saved_data = {
|
2019-05-12 01:09:37 +00:00
|
|
|
"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,
|
2019-04-17 05:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
event_manager = EventManager()
|
2019-05-12 01:09:37 +00:00
|
|
|
event_manager.clients.append(defaultdict(Queue))
|
|
|
|
event_manager.publish(data=saved_data, type="notification", channel="ctf")
|
2019-04-17 05:36:30 +00:00
|
|
|
|
2019-05-12 01:09:37 +00:00
|
|
|
event = event_manager.clients[0]["ctf"].get()
|
2019-04-17 05:36:30 +00:00
|
|
|
event = ServerSentEvent(**event)
|
|
|
|
assert event.data == saved_data
|
|
|
|
|
|
|
|
|
|
|
|
def test_event_endpoint_is_event_stream():
|
|
|
|
"""Test that the /events endpoint is text/event-stream"""
|
|
|
|
app = create_ctfd()
|
2019-05-12 01:09:37 +00:00
|
|
|
with patch.object(Queue, "get") as fake_queue:
|
2019-04-17 05:36:30 +00:00
|
|
|
saved_data = {
|
2019-05-12 01:09:37 +00:00
|
|
|
"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,
|
2019-04-17 05:36:30 +00:00
|
|
|
}
|
2019-05-12 01:09:37 +00:00
|
|
|
saved_event = {"type": "notification", "data": saved_data}
|
2019-04-17 05:36:30 +00:00
|
|
|
|
|
|
|
fake_queue.return_value = saved_event
|
|
|
|
with app.app_context():
|
|
|
|
register_user(app)
|
|
|
|
with login_as_user(app) as client:
|
2019-05-12 01:09:37 +00:00
|
|
|
r = client.get("/events")
|
|
|
|
assert "text/event-stream" in r.headers["Content-Type"]
|
2019-04-17 05:36:30 +00:00
|
|
|
destroy_ctfd(app)
|
|
|
|
|
|
|
|
|
|
|
|
def test_redis_event_manager_installed():
|
|
|
|
"""Test that RedisEventManager is installed on the Flask app"""
|
|
|
|
# TODO: This test is flaky.
|
|
|
|
class RedisConfig(TestingConfig):
|
2019-05-12 01:09:37 +00:00
|
|
|
REDIS_URL = "redis://localhost:6379"
|
|
|
|
CACHE_REDIS_URL = "redis://localhost:6379"
|
|
|
|
CACHE_TYPE = "redis"
|
2019-04-17 05:36:30 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
app = create_ctfd(config=RedisConfig)
|
2019-07-30 03:15:26 +00:00
|
|
|
except ConnectionError:
|
|
|
|
print("Failed to connect to redis. Skipping test.")
|
|
|
|
else:
|
2019-04-17 05:36:30 +00:00
|
|
|
with app.app_context():
|
|
|
|
assert isinstance(app.events_manager, RedisEventManager)
|
|
|
|
destroy_ctfd(app)
|
|
|
|
|
|
|
|
|
|
|
|
def test_redis_event_manager_subscription():
|
|
|
|
"""Test that RedisEventManager subscribing works."""
|
|
|
|
# TODO: This test is flaky.
|
|
|
|
class RedisConfig(TestingConfig):
|
2019-05-12 01:09:37 +00:00
|
|
|
REDIS_URL = "redis://localhost:6379"
|
|
|
|
CACHE_REDIS_URL = "redis://localhost:6379"
|
|
|
|
CACHE_TYPE = "redis"
|
2019-04-17 05:36:30 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
app = create_ctfd(config=RedisConfig)
|
2019-07-30 03:15:26 +00:00
|
|
|
except ConnectionError:
|
|
|
|
print("Failed to connect to redis. Skipping test.")
|
|
|
|
else:
|
2019-04-17 05:36:30 +00:00
|
|
|
with app.app_context():
|
2019-05-12 01:09:37 +00:00
|
|
|
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",
|
|
|
|
}
|
2019-04-17 05:36:30 +00:00
|
|
|
|
|
|
|
saved_event = {
|
2019-05-12 01:09:37 +00:00
|
|
|
"pattern": None,
|
|
|
|
"type": "message",
|
|
|
|
"channel": "ctf",
|
|
|
|
"data": json.dumps(saved_data),
|
2019-04-17 05:36:30 +00:00
|
|
|
}
|
2019-05-12 01:09:37 +00:00
|
|
|
with patch.object(redis.client.PubSub, "listen") as fake_pubsub_listen:
|
2019-04-17 05:36:30 +00:00
|
|
|
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
|
2019-05-12 01:09:37 +00:00
|
|
|
assert message.__str__().startswith("event:notification\ndata:")
|
2019-04-17 05:36:30 +00:00
|
|
|
break
|
|
|
|
destroy_ctfd(app)
|
|
|
|
|
|
|
|
|
|
|
|
def test_redis_event_manager_publish():
|
|
|
|
"""Test that RedisEventManager publishing to clients works."""
|
|
|
|
# TODO: This test is flaky.
|
|
|
|
class RedisConfig(TestingConfig):
|
2019-05-12 01:09:37 +00:00
|
|
|
REDIS_URL = "redis://localhost:6379"
|
|
|
|
CACHE_REDIS_URL = "redis://localhost:6379"
|
|
|
|
CACHE_TYPE = "redis"
|
|
|
|
|
2019-04-17 05:36:30 +00:00
|
|
|
try:
|
|
|
|
app = create_ctfd(config=RedisConfig)
|
2019-07-30 03:15:26 +00:00
|
|
|
except ConnectionError:
|
|
|
|
print("Failed to connect to redis. Skipping test.")
|
|
|
|
else:
|
2019-04-17 05:36:30 +00:00
|
|
|
with app.app_context():
|
|
|
|
saved_data = {
|
2019-05-12 01:09:37 +00:00
|
|
|
"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,
|
2019-04-17 05:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
event_manager = RedisEventManager()
|
2019-05-12 01:09:37 +00:00
|
|
|
event_manager.publish(data=saved_data, type="notification", channel="ctf")
|
2019-04-17 05:36:30 +00:00
|
|
|
destroy_ctfd(app)
|