-
-
Notifications
You must be signed in to change notification settings - Fork 46
[feature] Add websocket endpoint for all location updates #191 #193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dee077
wants to merge
5
commits into
master
Choose a base branch
from
feature/191-new-ws-endpoint-for-all-location
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+259
−38
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
99392cb
[feature] Add websocket endpoint for bulk location updates #191
dee077 71176da
[fix] Qa
dee077 cdb67fe
[fix] Add tests and docs
dee077 d58772f
[fix] Change naming conventions for paths and tests
dee077 3044979
[chores] Added a page for viewing location broadcasts
pandafy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,10 @@ | ||
| from ..models import Location | ||
| from .base import BaseLocationBroadcast | ||
| from .base import BaseCommonLocationBroadcast, BaseLocationBroadcast | ||
|
|
||
|
|
||
| class LocationBroadcast(BaseLocationBroadcast): | ||
| model = Location | ||
|
|
||
|
|
||
| class CommonLocationBroadcast(BaseCommonLocationBroadcast): | ||
| model = Location |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ | |
| from django.contrib.auth.models import Permission | ||
| from django.http.request import HttpRequest | ||
|
|
||
| from django_loci.channels.consumers import LocationBroadcast | ||
| from django_loci.channels.consumers import CommonLocationBroadcast, LocationBroadcast | ||
|
|
||
| from ...channels.base import _get_object_or_none | ||
| from .. import TestAdminMixin, TestLociMixin | ||
|
|
@@ -30,7 +30,7 @@ async def _force_login(self, user, backend=None): | |
| request.session.save | ||
| return request.session | ||
|
|
||
| async def _get_request_dict(self, pk=None, user=None): | ||
| async def _get_specific_location_request_dict(self, pk=None, user=None): | ||
| if not pk: | ||
| location = await database_sync_to_async(self._create_location)( | ||
| is_mobile=True | ||
|
|
@@ -39,26 +39,48 @@ async def _get_request_dict(self, pk=None, user=None): | |
| location=location | ||
| ) | ||
| pk = location.pk | ||
| path = "/ws/loci/location/{0}/".format(pk) | ||
| path = "/ws/loci/locations/{0}/".format(pk) | ||
| session = None | ||
| if user: | ||
| session = await self._force_login(user) | ||
| return {"pk": pk, "path": path, "session": session} | ||
|
|
||
| def _get_communicator(self, request_vars, user=None): | ||
| async def _get_common_location_request_dict(self, user=None): | ||
| location = await database_sync_to_async(self._create_location)(is_mobile=True) | ||
| await database_sync_to_async(self._create_object_location)(location=location) | ||
| pk = location.pk | ||
| path = "/ws/loci/locations/" | ||
| session = None | ||
| if user: | ||
| session = await self._force_login(user) | ||
| return {"pk": pk, "path": path, "session": session} | ||
|
|
||
| def _get_location_communicator( | ||
| self, consumer_cls, request_vars, user=None, include_pk=False | ||
| ): | ||
| communicator = WebsocketCommunicator( | ||
| LocationBroadcast.as_asgi(), request_vars["path"] | ||
| consumer_cls.as_asgi(), request_vars["path"] | ||
| ) | ||
| if user: | ||
| communicator.scope.update( | ||
| { | ||
| "user": user, | ||
| "session": request_vars["session"], | ||
| "url_route": {"kwargs": {"pk": request_vars["pk"]}}, | ||
| } | ||
| ) | ||
| scope = { | ||
| "user": user, | ||
| "session": request_vars["session"], | ||
| } | ||
| if include_pk: | ||
| scope["url_route"] = {"kwargs": {"pk": request_vars["pk"]}} | ||
| communicator.scope.update(scope) | ||
| return communicator | ||
|
|
||
| def _get_specific_location_communicator(self, request_vars, user=None): | ||
| return self._get_location_communicator( | ||
| LocationBroadcast, request_vars, user=user, include_pk=True | ||
| ) | ||
|
|
||
| def _get_common_location_communicator(self, request_vars, user=None): | ||
| return self._get_location_communicator( | ||
| CommonLocationBroadcast, request_vars, user=user, include_pk=False | ||
| ) | ||
|
|
||
| @pytest.mark.django_db(transaction=True) | ||
| def test_object_or_none(self): | ||
| result = _get_object_or_none(self.location_model, pk=1) | ||
|
|
@@ -70,8 +92,17 @@ def test_object_or_none(self): | |
| @pytest.mark.asyncio | ||
| @pytest.mark.django_db(transaction=True) | ||
| async def test_consumer_unauthenticated(self): | ||
| request_vars = await self._get_request_dict() | ||
| communicator = self._get_communicator(request_vars) | ||
| request_vars = await self._get_specific_location_request_dict() | ||
| communicator = self._get_specific_location_communicator(request_vars) | ||
| connected, _ = await communicator.connect() | ||
| assert not connected | ||
| await communicator.disconnect() | ||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.django_db(transaction=True) | ||
| async def test_common_location_consumer_unauthenticated(self): | ||
| request_vars = await self._get_common_location_request_dict() | ||
| communicator = self._get_common_location_communicator(request_vars) | ||
| connected, _ = await communicator.connect() | ||
| assert not connected | ||
| await communicator.disconnect() | ||
|
|
@@ -80,8 +111,18 @@ async def test_consumer_unauthenticated(self): | |
| @pytest.mark.django_db(transaction=True) | ||
| async def test_connect_admin(self): | ||
| test_user = await database_sync_to_async(self._create_admin)() | ||
| request_vars = await self._get_request_dict(user=test_user) | ||
| communicator = self._get_communicator(request_vars, test_user) | ||
| request_vars = await self._get_specific_location_request_dict(user=test_user) | ||
| communicator = self._get_specific_location_communicator(request_vars, test_user) | ||
| connected, _ = await communicator.connect() | ||
| assert connected | ||
| await communicator.disconnect() | ||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.django_db(transaction=True) | ||
| async def test_common_location_connect_admin(self): | ||
| test_user = await database_sync_to_async(self._create_admin)() | ||
| request_vars = await self._get_common_location_request_dict(user=test_user) | ||
| communicator = self._get_common_location_communicator(request_vars, test_user) | ||
| connected, _ = await communicator.connect() | ||
| assert connected | ||
| await communicator.disconnect() | ||
|
|
@@ -92,8 +133,20 @@ async def test_consumer_not_staff(self): | |
| test_user = await database_sync_to_async(self.user_model.objects.create_user)( | ||
| username="user", password="password", email="[email protected]" | ||
| ) | ||
| request_vars = await self._get_request_dict(user=test_user) | ||
| communicator = self._get_communicator(request_vars, test_user) | ||
| request_vars = await self._get_specific_location_request_dict(user=test_user) | ||
| communicator = self._get_specific_location_communicator(request_vars, test_user) | ||
| connected, _ = await communicator.connect() | ||
| assert not connected | ||
| await communicator.disconnect() | ||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.django_db(transaction=True) | ||
| async def test_common_location_consumer_not_staff(self): | ||
| test_user = await database_sync_to_async(self.user_model.objects.create_user)( | ||
| username="user", password="password", email="[email protected]" | ||
| ) | ||
| request_vars = await self._get_common_location_request_dict(user=test_user) | ||
| communicator = self._get_common_location_communicator(request_vars, test_user) | ||
| connected, _ = await communicator.connect() | ||
| assert not connected | ||
| await communicator.disconnect() | ||
|
|
@@ -103,8 +156,8 @@ async def test_consumer_not_staff(self): | |
| async def test_consumer_404(self): | ||
| pk = self.location_model().pk | ||
| admin = await database_sync_to_async(self._create_admin)() | ||
| request_vars = await self._get_request_dict(pk=pk, user=admin) | ||
| communicator = self._get_communicator(request_vars, admin) | ||
| request_vars = await self._get_specific_location_request_dict(pk=pk, user=admin) | ||
| communicator = self._get_specific_location_communicator(request_vars, admin) | ||
| connected, _ = await communicator.connect() | ||
| assert not connected | ||
|
|
||
|
|
@@ -119,16 +172,45 @@ async def test_consumer_staff_but_no_change_permission(self): | |
| location=location | ||
| ) | ||
| pk = ol.location.pk | ||
| request_vars = await self._get_request_dict(pk=pk, user=test_user) | ||
| communicator = self._get_communicator(request_vars, test_user) | ||
| request_vars = await self._get_specific_location_request_dict( | ||
| pk=pk, user=test_user | ||
| ) | ||
| communicator = self._get_specific_location_communicator(request_vars, test_user) | ||
| connected, _ = await communicator.connect() | ||
| assert not connected | ||
| await communicator.disconnect() | ||
|
|
||
| # add permission to change location and repeat | ||
| loc_perm = await Permission.objects.filter( | ||
| codename=f"change_{self.location_model._meta.model_name}" | ||
| ).afirst() | ||
| await test_user.user_permissions.aadd(loc_perm) | ||
| test_user = await self.user_model.objects.aget(pk=test_user.pk) | ||
| communicator = self._get_specific_location_communicator(request_vars, test_user) | ||
| connected, _ = await communicator.connect() | ||
| assert connected | ||
| await communicator.disconnect() | ||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.django_db(transaction=True) | ||
| async def test_common_location_consumer_staff_but_no_change_permission(self): | ||
| test_user = await database_sync_to_async(self.user_model.objects.create_user)( | ||
| username="user", password="password", email="[email protected]", is_staff=True | ||
| ) | ||
| location = await database_sync_to_async(self._create_location)(is_mobile=True) | ||
| await database_sync_to_async(self._create_object_location)(location=location) | ||
| request_vars = await self._get_common_location_request_dict(user=test_user) | ||
| communicator = self._get_common_location_communicator(request_vars, test_user) | ||
| connected, _ = await communicator.connect() | ||
| assert not connected | ||
| await communicator.disconnect() | ||
| # add permission to change location and repeat | ||
dee077 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| loc_perm = await Permission.objects.filter(name="Can change location").afirst() | ||
| loc_perm = await Permission.objects.filter( | ||
| codename=f"change_{self.location_model._meta.model_name}" | ||
| ).afirst() | ||
| await test_user.user_permissions.aadd(loc_perm) | ||
| test_user = await self.user_model.objects.aget(pk=test_user.pk) | ||
| communicator = self._get_communicator(request_vars, test_user) | ||
| communicator = self._get_common_location_communicator(request_vars, test_user) | ||
| connected, _ = await communicator.connect() | ||
| assert connected | ||
| await communicator.disconnect() | ||
|
|
@@ -137,15 +219,35 @@ async def test_consumer_staff_but_no_change_permission(self): | |
| @pytest.mark.django_db(transaction=True) | ||
| async def test_location_update(self): | ||
| test_user = await database_sync_to_async(self._create_admin)() | ||
| request_vars = await self._get_request_dict(user=test_user) | ||
| communicator = self._get_communicator(request_vars, test_user) | ||
| request_vars = await self._get_specific_location_request_dict(user=test_user) | ||
| communicator = self._get_specific_location_communicator(request_vars, test_user) | ||
| connected, _ = await communicator.connect() | ||
| assert connected | ||
| await self._save_location(request_vars["pk"]) | ||
| response = await communicator.receive_json_from() | ||
| assert response == { | ||
| "geometry": {"type": "Point", "coordinates": [12.513124, 41.897903]}, | ||
| "address": "Via del Corso, Roma, Italia", | ||
| } | ||
| await communicator.disconnect() | ||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.django_db(transaction=True) | ||
| async def test_common_location_update(self): | ||
| test_user = await database_sync_to_async(self._create_admin)() | ||
| request_vars = await self._get_common_location_request_dict(user=test_user) | ||
| communicator = self._get_common_location_communicator(request_vars, test_user) | ||
| connected, _ = await communicator.connect() | ||
| assert connected | ||
| await self._save_location(request_vars["pk"]) | ||
| response = await communicator.receive_json_from() | ||
| assert response == { | ||
| "id": str(request_vars["pk"]), | ||
| "geometry": {"type": "Point", "coordinates": [12.513124, 41.897903]}, | ||
| "address": "Via del Corso, Roma, Italia", | ||
| "name": "test-location", | ||
| "type": "outdoor", | ||
| "is_mobile": True, | ||
| } | ||
| await communicator.disconnect() | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.