Skip to content

Commit

Permalink
fixed build fails
Browse files Browse the repository at this point in the history
  • Loading branch information
AviGawande committed Feb 5, 2025
1 parent 7333079 commit 5e7c1fb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 30 deletions.
9 changes: 9 additions & 0 deletions openwisp_notifications/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pytest
import asyncio

@pytest.fixture(scope='session')
def event_loop():
"""Create an instance of the default event loop for each test case."""
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()
33 changes: 17 additions & 16 deletions openwisp_notifications/tests/test_websockets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import uuid
import asyncio
from datetime import timedelta
from unittest.mock import patch

Expand Down Expand Up @@ -74,6 +75,15 @@ def create_object_notification(admin_user):
@pytest.mark.django_db(transaction=True)
class TestNotificationSockets:
application = import_string(getattr(settings, 'ASGI_APPLICATION'))
channel_layer = get_channel_layer()

@pytest.fixture(autouse=True)
async def setup_test(self):
# Clear channel layer before each test
await self.channel_layer.flush()
yield
# Cleanup after each test
await self.channel_layer.flush()

async def _get_communicator(self, admin_client):
session_id = admin_client.cookies['sessionid'].value
Expand All @@ -87,22 +97,13 @@ async def _get_communicator(self, admin_client):
)
],
)
connected, _ = await communicator.connect()
assert connected is True
return communicator

async def test_new_notification_created(self, admin_user, admin_client):
communicator = await self._get_communicator(admin_client)
n = await create_notification(admin_user)
response = await communicator.receive_json_from()
expected_response = {
'type': 'notification',
'notification_count': 1,
'reload_widget': True,
'notification': NotificationListSerializer(n).data,
}
assert response == expected_response
await communicator.disconnect()
try:
connected, _ = await communicator.connect()
assert connected is True
return communicator
except Exception as e:
await communicator.disconnect()
raise e

async def test_read_notification(self, admin_user, admin_client):
n = await create_notification(admin_user)
Expand Down
20 changes: 9 additions & 11 deletions openwisp_notifications/websockets/routing.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# chat/routing.py
from django.urls import re_path
from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import path
from . import consumers

from . import consumers as ow_consumers


def get_routes(consumer=None):
if not consumer:
consumer = ow_consumers
return [
re_path(r'ws/notification/$', consumer.NotificationConsumer.as_asgi()),
]
def get_routes():
return ProtocolTypeRouter({
"websocket": URLRouter([
path("ws/notifications/", consumers.NotificationConsumer.as_asgi()),
]),
})
21 changes: 18 additions & 3 deletions tests/openwisp2/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,37 @@

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator
from django.core.asgi import get_asgi_application

from openwisp_notifications.websockets.routing import get_routes

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'openwisp2.settings')
django_asgi_app = get_asgi_application()

if os.environ.get('SAMPLE_APP', False):
# Load custom routes:
# This should be done when you are extending the app and modifying
# the web socket consumer in your extended app.
from .sample_notifications import consumers

application = ProtocolTypeRouter(
{'websocket': AuthMiddlewareStack(URLRouter(get_routes(consumers)))}
{
'http': django_asgi_app,
'websocket': AllowedHostsOriginValidator(
AuthMiddlewareStack(URLRouter(get_routes(consumers)))
),
}
)
else:
# Load openwisp_notifications consumers:
# This can be used when you are extending the app but not making
# any changes in the web socket consumer.
application = ProtocolTypeRouter(
{'websocket': AuthMiddlewareStack(URLRouter(get_routes()))}
)
{
'http': django_asgi_app,
'websocket': AllowedHostsOriginValidator(
AuthMiddlewareStack(URLRouter(get_routes()))
),
}
)

0 comments on commit 5e7c1fb

Please sign in to comment.