From e5f70b6968486f6b21385349f9a63c08270c46fb Mon Sep 17 00:00:00 2001 From: Gagan Deep Date: Wed, 17 May 2023 03:47:51 +0530 Subject: [PATCH] [fix] Fixed WebSocket routes and JS WebSocket routes should start with "/ws/". Fixed real-time update bug in JS. --- openwisp_network_topology/routing.py | 6 +++--- .../templates/netjsongraph/netjsongraph-script.html | 4 ++-- openwisp_network_topology/tests/test_websockets.py | 12 +----------- 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/openwisp_network_topology/routing.py b/openwisp_network_topology/routing.py index 1a24b6a0..2ea0c44d 100644 --- a/openwisp_network_topology/routing.py +++ b/openwisp_network_topology/routing.py @@ -1,12 +1,12 @@ -from django.urls import re_path +from django.urls import path from . import consumers websocket_urlpatterns = [ # This route is used by both # the admin and non-admin topology view - re_path( - r'^network-topology/topology/(?P[^/]+)/$', + path( + 'ws/network-topology/topology//', consumers.TopologyConsumer.as_asgi(), ), ] diff --git a/openwisp_network_topology/templates/netjsongraph/netjsongraph-script.html b/openwisp_network_topology/templates/netjsongraph/netjsongraph-script.html index 0adcb82e..70d32df5 100644 --- a/openwisp_network_topology/templates/netjsongraph/netjsongraph-script.html +++ b/openwisp_network_topology/templates/netjsongraph/netjsongraph-script.html @@ -4,7 +4,7 @@ loadNetJsonGraph = (el='body', url='{{ graph_url }}') => { const history_url = '{{ history_url }}'; const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; - const wsUrl = `${wsProtocol}//${window.location.host}/network-topology/topology/${getTopologyIdFromUrl()}/`; + const wsUrl = `${wsProtocol}//${window.location.host}/ws/network-topology/topology/${getTopologyIdFromUrl()}/`; const socket = new WebSocket(wsUrl); const getDataParseOptions = (data) => { @@ -190,7 +190,7 @@ const data=JSON.parse(e.data); if(data.type==="broadcast_topology"){ const topology=JSON.parse(data.topology); - window.__njg_graph__.utils.JSONDataUpdate.call(window.__njg_graph__, topology); + window.graph.utils.JSONDataUpdate.call(window.graph, topology); } } diff --git a/openwisp_network_topology/tests/test_websockets.py b/openwisp_network_topology/tests/test_websockets.py index d221010b..531d34e8 100644 --- a/openwisp_network_topology/tests/test_websockets.py +++ b/openwisp_network_topology/tests/test_websockets.py @@ -31,7 +31,7 @@ async def _get_communicator(self, admin_client, topology_id): session_id = admin_client.cookies['sessionid'].value communicator = WebsocketCommunicator( self.application, - path=f'network-topology/topology/{topology_id}/', + path=f'ws/network-topology/topology/{topology_id}/', headers=[ ( b'cookie', @@ -104,16 +104,6 @@ async def test_consumer_connection_auth_disabled_org_manager(self, client): async def test_consumer_connection_auth_disabled_unauth_user(self, client): await self._assert_connection_unauth_user(client) - async def test_consumer_connection_invalid_topology_pk( - self, admin_user, admin_client - ): - org = await database_sync_to_async(self._create_org)() - await database_sync_to_async(self._create_topology)(organization=org) - communicator = await self._get_communicator(admin_client, 'invalid_topology_pk') - connected, _ = await communicator.connect() - assert connected is False - await communicator.disconnect() - async def test_consumer_connection_unexisting_topology( self, admin_user, admin_client ):