1- import importlib
21import os
32from unittest import skipIf
43
54import pytest
65from channels .db import database_sync_to_async
76from channels .routing import ProtocolTypeRouter
8- from channels .testing import WebsocketCommunicator
97from django .conf import settings
10- from django .contrib .auth import get_user_model , login
8+ from django .contrib .auth import get_user_model
119from django .contrib .auth .models import Permission
12- from django .http .request import HttpRequest
1310from django .utils .module_loading import import_string
11+ from django_loci .tests .base .test_channels import BaseTestChannels
1412from swapper import load_model
1513
1614from .utils import TestGeoMixin
2321
2422
2523@skipIf (os .environ .get ("SAMPLE_APP" , False ), "Running tests on SAMPLE_APP" )
26- class TestChannels (TestGeoMixin ):
24+ class TestChannels (TestGeoMixin , BaseTestChannels ):
2725 application = import_string (getattr (settings , "ASGI_APPLICATION" ))
2826 object_model = Device
2927 location_model = Location
3028 object_location_model = DeviceLocation
3129 user_model = get_user_model ()
3230
33- def _force_login (self , user , backend = None ):
34- engine = importlib .import_module (settings .SESSION_ENGINE )
35- request = HttpRequest ()
36- request .session = engine .SessionStore ()
37- login (request , user , backend )
38- request .session .save ()
39- return request .session
40-
41- async def _get_request_dict (self , pk = None , user = None ):
42- if not pk :
43- location = await database_sync_to_async (self ._create_location )(
44- is_mobile = True
45- )
46- await database_sync_to_async (self ._create_object_location )(
47- location = location
48- )
49- pk = location .pk
50- path = "/ws/loci/location/{0}/" .format (pk )
51- session = None
52- if user :
53- session = await database_sync_to_async (self ._force_login )(user )
54- return {"pk" : pk , "path" : path , "session" : session }
55-
56- def _get_communicator (self , request_vars , user = None ):
57- communicator = WebsocketCommunicator (self .application , request_vars ["path" ])
58- if user :
59- communicator .scope .update (
60- {
61- "user" : user ,
62- "session" : request_vars ["session" ],
63- "url_route" : {"kwargs" : {"pk" : request_vars ["pk" ]}},
64- }
65- )
66- return communicator
67-
6831 @pytest .mark .asyncio
6932 @pytest .mark .django_db (transaction = True )
7033 async def test_consumer_staff_but_no_change_permission (self ):
@@ -74,8 +37,51 @@ async def test_consumer_staff_but_no_change_permission(self):
7437 location = await database_sync_to_async (self ._create_location )(is_mobile = True )
7538 await database_sync_to_async (self ._create_object_location )(location = location )
7639 pk = location .pk
77- request_vars = await self ._get_request_dict (user = user , pk = pk )
78- communicator = self ._get_communicator (request_vars , user )
40+ request_vars = await self ._get_specific_location_request_dict (user = user , pk = pk )
41+ communicator = self ._get_specific_location_communicator (request_vars , user )
42+ connected , _ = await communicator .connect ()
43+ assert not connected
44+ await communicator .disconnect ()
45+ # add permission to change location and repeat
46+ perm = await database_sync_to_async (
47+ (
48+ await database_sync_to_async (Permission .objects .filter )(
49+ name = "Can change location"
50+ )
51+ ).first
52+ )()
53+ await database_sync_to_async (user .user_permissions .add )(perm )
54+ user = await database_sync_to_async (User .objects .get )(pk = user .pk )
55+ request_vars = await self ._get_specific_location_request_dict (user = user , pk = pk )
56+ communicator = self ._get_specific_location_communicator (request_vars , user )
57+ connected , _ = await communicator .connect ()
58+ assert not connected
59+ await communicator .disconnect ()
60+ # add user to organization
61+ await database_sync_to_async (OrganizationUser .objects .create )(
62+ organization = location .organization , user = user , is_admin = True
63+ )
64+ await database_sync_to_async (location .organization .save )()
65+ user = await database_sync_to_async (User .objects .get )(pk = user .pk )
66+ request_vars = await self ._ge_get_specific_location_request_dictt_request_dict (
67+ user = user , pk = pk
68+ )
69+ communicator = self ._get_specific_location_communicator (request_vars , user )
70+ connected , _ = await communicator .connect ()
71+ assert connected
72+ await communicator .disconnect ()
73+
74+ @pytest .mark .asyncio
75+ @pytest .mark .django_db (transaction = True )
76+ async def test_common_location_consumer_staff_but_no_change_permission (self ):
77+ user = await database_sync_to_async (User .objects .create_user )(
78+ username = "user" ,
password = "password" ,
email = "[email protected] " ,
is_staff = True 79+ )
80+ location = await database_sync_to_async (self ._create_location )(is_mobile = True )
81+ await database_sync_to_async (self ._create_object_location )(location = location )
82+ pk = location .pk
83+ request_vars = await self ._get_common_location_request_dict (user = user , pk = pk )
84+ communicator = self ._get_common_location_communicator (request_vars , user )
7985 connected , _ = await communicator .connect ()
8086 assert not connected
8187 await communicator .disconnect ()
@@ -89,8 +95,8 @@ async def test_consumer_staff_but_no_change_permission(self):
8995 )()
9096 await database_sync_to_async (user .user_permissions .add )(perm )
9197 user = await database_sync_to_async (User .objects .get )(pk = user .pk )
92- request_vars = await self ._get_request_dict (user = user , pk = pk )
93- communicator = self ._get_communicator (request_vars , user )
98+ request_vars = await self ._get_common_location_request_dict (user = user , pk = pk )
99+ communicator = self ._get_common_location_communicator (request_vars , user )
94100 connected , _ = await communicator .connect ()
95101 assert not connected
96102 await communicator .disconnect ()
@@ -100,8 +106,8 @@ async def test_consumer_staff_but_no_change_permission(self):
100106 )
101107 await database_sync_to_async (location .organization .save )()
102108 user = await database_sync_to_async (User .objects .get )(pk = user .pk )
103- request_vars = await self ._get_request_dict (user = user , pk = pk )
104- communicator = self ._get_communicator (request_vars , user )
109+ request_vars = await self ._get_common_location_request_dict (user = user , pk = pk )
110+ communicator = self ._get_common_location_communicator (request_vars , user )
105111 connected , _ = await communicator .connect ()
106112 assert connected
107113 await communicator .disconnect ()
0 commit comments