Skip to content

Commit

Permalink
[change] Used single websocket route for both admin and non-admin views
Browse files Browse the repository at this point in the history
#173

Closes #173
  • Loading branch information
Aryamanz29 committed Apr 9, 2023
1 parent da8e099 commit b29c31a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 24 deletions.
7 changes: 2 additions & 5 deletions openwisp_network_topology/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
from . import consumers

websocket_urlpatterns = [
# For topology admin view
# This route is used by both
# the admin and non-admin topology view
re_path(
r'^admin/topology/topology/(?P<pk>[^/]+)/change/$',
consumers.TopologyConsumer.as_asgi(),
),
# For topology non admin view
re_path(
r'^topology/topology/(?P<pk>[^/]+)/$', consumers.TopologyConsumer.as_asgi()
),
]
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<script>
loadNetJsonGraph = (el='body', url='{{ graph_url }}') => {
const history_url = '{{ history_url }}';
const wsUrl = `ws://${window.location.host}${window.location.pathname}`;
const nonAdminView = {{ non_admin_view | yesno:"true,false" }};
const wsUrl = nonAdminView ?
`ws://${window.location.host}/admin${window.location.pathname}change/`:
`ws://${window.location.host}${window.location.pathname}`;
const socket = new WebSocket(wsUrl);

const getDataParseOptions = (data) => {
Expand Down
19 changes: 1 addition & 18 deletions openwisp_network_topology/tests/test_websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,8 @@ class TestTopologySockets(CreateGraphObjectsMixin, CreateOrgMixin):
topology_model = Topology
application = import_string(getattr(settings, 'ASGI_APPLICATION'))

async def _get_url(self, topology_id, admin_view):
non_admin_view_url = f'topology/topology/{topology_id}/'
admin_view_url = f'admin/topology/topology/{topology_id}/change/'
if not admin_view:
return non_admin_view_url
return admin_view_url

async def _get_communicator(self, admin_client, topology_id, admin_view=True):
url = await self._get_url(topology_id, admin_view)
url = f'admin/topology/topology/{topology_id}/change/'
session_id = admin_client.cookies['sessionid'].value
communicator = WebsocketCommunicator(
self.application,
Expand Down Expand Up @@ -135,13 +128,3 @@ async def test_topology_properties_update(self, admin_user, admin_client):
assert response['topology'] is not None
assert response['topology'] == expected_response
await communicator.disconnect()

async def test_non_admin_view_consumer_connection(self, admin_user, admin_client):
org = await database_sync_to_async(self._create_org)()
t = await database_sync_to_async(self._create_topology)(organization=org)
communicator = await self._get_communicator(
admin_client, t.pk, admin_view=False
)
connected, _ = await communicator.connect()
assert connected is True
await communicator.disconnect()
4 changes: 4 additions & 0 deletions openwisp_network_topology/visualizer/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def get(self, request, pk):
'graph_url': graph_url,
'history_url': history_url,
'VISUALIZER_CSS': VISUALIZER_CSS,
# This flag ensures that we consume
# the same WebSocket route for both
# the admin and non-admin topology view
'non_admin_view': True,
},
)

Expand Down

0 comments on commit b29c31a

Please sign in to comment.