Skip to content

Commit

Permalink
[change] Used single websocket route for both admin and non-admin #173
Browse files Browse the repository at this point in the history
Closes #173
  • Loading branch information
Aryamanz29 authored Apr 13, 2023
1 parent 3aed89f commit 6c2d8a4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
9 changes: 3 additions & 6 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/$',
r'^network-topology/topology/(?P<pk>[^/]+)/$',
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,8 @@
<script>
loadNetJsonGraph = (el='body', url='{{ graph_url }}') => {
const history_url = '{{ history_url }}';
const wsUrl = `ws://${window.location.host}${window.location.pathname}`;
const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsUrl = `${wsProtocol}//${window.location.host}/network-topology/topology/${getTopologyIdFromUrl()}/`;
const socket = new WebSocket(wsUrl);

const getDataParseOptions = (data) => {
Expand Down Expand Up @@ -168,6 +169,21 @@
graph.render();
window.initTopologyHistory(django.jQuery);
return graph

// Utility function
function getTopologyIdFromUrl() {
let topologyId;
try {
topologyId = /\/((\w{4,12}-?)){5}\//.exec(window.location)[0];
} catch (error) {
try {
topologyId = /\/(\d+)\//.exec(window.location)[0];
} catch (error) {
throw error;
}
}
return topologyId.replace(/\//g, '');
}
};

</script>
22 changes: 2 additions & 20 deletions openwisp_network_topology/tests/test_websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,11 @@ 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)
async def _get_communicator(self, admin_client, topology_id):
session_id = admin_client.cookies['sessionid'].value
communicator = WebsocketCommunicator(
self.application,
url,
path=f'network-topology/topology/{topology_id}/',
headers=[
(
b'cookie',
Expand Down Expand Up @@ -135,13 +127,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()

0 comments on commit 6c2d8a4

Please sign in to comment.