|
| 1 | +import json |
| 2 | +from asgiref.sync import async_to_sync, sync_to_async |
| 3 | +import pytest |
| 4 | +import jwt |
| 5 | +import redis |
| 6 | +from django.contrib.auth.models import AnonymousUser |
| 7 | +from django.conf import settings |
| 8 | + |
| 9 | +from ChatApp import views, audit, tasks |
| 10 | +from ChatApp.middleware import DLPWebSocketMiddleware, JWTAuthMiddleware |
| 11 | +from ChatApp.huddle import rooms as huddle_rooms |
| 12 | +from ChatApp.models import CustomUser, Conversation |
| 13 | + |
| 14 | +@pytest.mark.django_db |
| 15 | +def test_fetch_messages_from_redis_errors(monkeypatch): |
| 16 | + class DummyRedis: |
| 17 | + def __init__(self, fail=False, bad=False): |
| 18 | + self.fail = fail |
| 19 | + self.bad = bad |
| 20 | + def lrange(self, key, start, end): |
| 21 | + if self.fail: |
| 22 | + raise redis.ConnectionError("boom") |
| 23 | + if self.bad: |
| 24 | + return [b"not-json"] |
| 25 | + return [json.dumps({"message":"hi","timestamp":"2024-01-01 00:00:00.000000"}).encode()] |
| 26 | + def close(self): |
| 27 | + pass |
| 28 | + convo = Conversation.objects.create(user1=CustomUser.objects.create_user(email="a@b.com"), user2=CustomUser.objects.create_user(email="b@b.com")) |
| 29 | + monkeypatch.setattr(views.redis, "StrictRedis", lambda *a, **k: DummyRedis(fail=True)) |
| 30 | + assert views.fetch_messages_from_redis(convo) == [] |
| 31 | + monkeypatch.setattr(views.redis, "StrictRedis", lambda *a, **k: DummyRedis(bad=True)) |
| 32 | + assert views.fetch_messages_from_redis(convo) == [] |
| 33 | + monkeypatch.setattr(views.redis, "StrictRedis", lambda *a, **k: DummyRedis()) |
| 34 | + msgs = views.fetch_messages_from_redis(convo) |
| 35 | + assert msgs and msgs[0]["message"] == "hi" |
| 36 | + |
| 37 | +@pytest.mark.django_db |
| 38 | +def test_audit_producer_errors(monkeypatch): |
| 39 | + monkeypatch.setattr(audit, "_producer", None) |
| 40 | + settings.KAFKA_BROKER_URL = "kafka:9092" |
| 41 | + class Boom(Exception): |
| 42 | + pass |
| 43 | + def bad_kafka(*a, **k): |
| 44 | + raise Boom() |
| 45 | + monkeypatch.setattr(audit, "KafkaProducer", bad_kafka) |
| 46 | + user = CustomUser.objects.create_user(email="u@x.com") |
| 47 | + audit.record_audit_event(user, "act", {"x":1}) |
| 48 | + assert audit._producer is None |
| 49 | + class Dummy: |
| 50 | + def send(self, *a, **k): |
| 51 | + raise Boom() |
| 52 | + monkeypatch.setattr(audit, "_get_producer", lambda: Dummy()) |
| 53 | + audit.record_audit_event(user, "act2", {"y":2}) |
| 54 | + |
| 55 | +@pytest.mark.django_db |
| 56 | +def test_send_push_error_paths(monkeypatch, settings): |
| 57 | + settings.FCM_SERVER_KEY = "k" |
| 58 | + settings.APNS_CERT_FILE = "c" |
| 59 | + settings.APNS_TOPIC = "t" |
| 60 | + class BadFCM: |
| 61 | + def __init__(self, *a, **k): |
| 62 | + pass |
| 63 | + def notify_multiple_devices(self, *a, **k): |
| 64 | + raise RuntimeError("fail") |
| 65 | + class BadAPNS: |
| 66 | + def __init__(self, *a, **k): |
| 67 | + pass |
| 68 | + def send_notification(self, *a, **k): |
| 69 | + raise RuntimeError("fail") |
| 70 | + monkeypatch.setattr(tasks, "FCMNotification", lambda *a, **k: BadFCM()) |
| 71 | + monkeypatch.setattr(tasks, "APNsClient", lambda *a, **k: BadAPNS()) |
| 72 | + tasks.send_push("t", "b", [{"token":"a","platform":"android"},{"token":"b","platform":"ios"}]) |
| 73 | + |
| 74 | +@pytest.mark.django_db |
| 75 | +def test_erase_user_data_missing(monkeypatch): |
| 76 | + tasks.erase_user_data.run(999) # should not raise |
| 77 | + |
| 78 | +@pytest.mark.asyncio |
| 79 | +async def test_dlp_middleware_block(monkeypatch): |
| 80 | + sent = [] |
| 81 | + async def app(scope, receive, send): |
| 82 | + await send({"type":"websocket.send","text":json.dumps({"message":"hi"})}) |
| 83 | + middleware = DLPWebSocketMiddleware(app) |
| 84 | + async def fake_hook(*a, **k): |
| 85 | + return False |
| 86 | + monkeypatch.setattr('ChatApp.middleware.run_dlp_hook', fake_hook) |
| 87 | + async def capture(message): |
| 88 | + sent.append(message) |
| 89 | + await middleware({"user":object()}, lambda: None, capture) |
| 90 | + assert sent == [] |
| 91 | + |
| 92 | +@pytest.mark.asyncio |
| 93 | +async def test_dlp_middleware_pass(monkeypatch): |
| 94 | + sent = [] |
| 95 | + async def app(scope, receive, send): |
| 96 | + await send({"type":"websocket.send","text":"bad"}) |
| 97 | + middleware = DLPWebSocketMiddleware(app) |
| 98 | + async def capture(message): |
| 99 | + sent.append(message) |
| 100 | + await middleware({"user":object()}, lambda: None, capture) |
| 101 | + assert sent |
| 102 | + |
| 103 | +@pytest.mark.django_db |
| 104 | +def test_jwt_middleware(monkeypatch): |
| 105 | + user = CustomUser.objects.create_user(email="tok@ex.com") |
| 106 | + token = jwt.encode({"user_id": user.id}, "secret", algorithm="HS256") |
| 107 | + scope = {"query_string": f"token={token}".encode()} |
| 108 | + async def inner(scope, receive, send): |
| 109 | + pass |
| 110 | + mw = JWTAuthMiddleware(inner) |
| 111 | + monkeypatch.setattr(settings, "JWT_SECRET_KEY", "secret") |
| 112 | + monkeypatch.setattr(settings, "JWT_ALGORITHM", "HS256") |
| 113 | + async_to_sync(mw)(scope, lambda: None, lambda m: None) |
| 114 | + assert scope["user"].id == user.id |
| 115 | + def bad_decode(*a, **k): |
| 116 | + raise jwt.DecodeError("bad") |
| 117 | + monkeypatch.setattr(jwt, "decode", bad_decode) |
| 118 | + scope = {"headers": [(b"authorization", f"Bearer {token}".encode())]} |
| 119 | + async_to_sync(mw)(scope, lambda: None, lambda m: None) |
| 120 | + assert isinstance(scope["user"], AnonymousUser) |
| 121 | + |
| 122 | + |
| 123 | +def test_create_room(): |
| 124 | + room = huddle_rooms.create_room() |
| 125 | + assert room.id in huddle_rooms._rooms |
| 126 | + assert room.router_rtp_capabilities["codecs"] |
0 commit comments