1111pytestmark = pytest .mark .django_db
1212
1313
14- def test_api_documents_can_edit_anonymous ():
14+ @responses .activate
15+ @pytest .mark .parametrize ("ws_not_connected_ready_only" , [True , False ])
16+ @pytest .mark .parametrize ("role" , ["editor" , "reader" ])
17+ def test_api_documents_can_edit_anonymous (settings , ws_not_connected_ready_only , role ):
1518 """Anonymous users can not edit documents."""
16- document = factories .DocumentFactory ()
19+ document = factories .DocumentFactory (link_reach = "public" , link_role = role )
1720 client = APIClient ()
21+ session_key = client .session .session_key
22+ settings .COLLABORATION_API_URL = "http://example.com/"
23+ settings .COLLABORATION_SERVER_SECRET = "secret-token"
24+ settings .COLLABORATION_WS_NOT_CONNECTED_READY_ONLY = ws_not_connected_ready_only
25+ endpoint_url = (
26+ f"{ settings .COLLABORATION_API_URL } get-connections/"
27+ f"?room={ document .id } &sessionKey={ session_key } "
28+ )
29+ ws_resp = responses .get (endpoint_url , json = {"count" : 0 , "exists" : False })
30+
1831 response = client .get (f"/api/v1.0/documents/{ document .id !s} /can-edit/" )
19- assert response .status_code == 401
32+
33+ if role == "reader" :
34+ assert response .status_code == 401
35+ else :
36+ assert response .status_code == 200
37+ assert response .json () == {"can_edit" : True }
38+ assert ws_resp .call_count == (1 if ws_not_connected_ready_only else 0 )
2039
2140
2241@responses .activate
23- def test_api_documents_can_edit_authenticated_no_websocket (settings ):
42+ @pytest .mark .parametrize ("ws_not_connected_ready_only" , [True , False ])
43+ def test_api_documents_can_edit_authenticated_no_websocket (
44+ settings , ws_not_connected_ready_only
45+ ):
2446 """
2547 A user not connected to the websocket and no other user have already updated the document,
2648 the document can be updated.
@@ -34,6 +56,7 @@ def test_api_documents_can_edit_authenticated_no_websocket(settings):
3456
3557 settings .COLLABORATION_API_URL = "http://example.com/"
3658 settings .COLLABORATION_SERVER_SECRET = "secret-token"
59+ settings .COLLABORATION_WS_NOT_CONNECTED_READY_ONLY = ws_not_connected_ready_only
3760 endpoint_url = (
3861 f"{ settings .COLLABORATION_API_URL } get-connections/"
3962 f"?room={ document .id } &sessionKey={ session_key } "
@@ -49,7 +72,7 @@ def test_api_documents_can_edit_authenticated_no_websocket(settings):
4972 assert response .status_code == 200
5073
5174 assert response .json () == {"can_edit" : True }
52- assert ws_resp .call_count == 1
75+ assert ws_resp .call_count == ( 1 if ws_not_connected_ready_only else 0 )
5376
5477
5578@responses .activate
@@ -69,6 +92,7 @@ def test_api_documents_can_edit_authenticated_no_websocket_user_already_editing(
6992
7093 settings .COLLABORATION_API_URL = "http://example.com/"
7194 settings .COLLABORATION_SERVER_SECRET = "secret-token"
95+ settings .COLLABORATION_WS_NOT_CONNECTED_READY_ONLY = True
7296 endpoint_url = (
7397 f"{ settings .COLLABORATION_API_URL } get-connections/"
7498 f"?room={ document .id } &sessionKey={ session_key } "
@@ -103,6 +127,7 @@ def test_api_documents_can_edit_no_websocket_other_user_connected_to_websocket(
103127
104128 settings .COLLABORATION_API_URL = "http://example.com/"
105129 settings .COLLABORATION_SERVER_SECRET = "secret-token"
130+ settings .COLLABORATION_WS_NOT_CONNECTED_READY_ONLY = True
106131 endpoint_url = (
107132 f"{ settings .COLLABORATION_API_URL } get-connections/"
108133 f"?room={ document .id } &sessionKey={ session_key } "
@@ -134,6 +159,7 @@ def test_api_documents_can_edit_user_connected_to_websocket(settings):
134159
135160 settings .COLLABORATION_API_URL = "http://example.com/"
136161 settings .COLLABORATION_SERVER_SECRET = "secret-token"
162+ settings .COLLABORATION_WS_NOT_CONNECTED_READY_ONLY = True
137163 endpoint_url = (
138164 f"{ settings .COLLABORATION_API_URL } get-connections/"
139165 f"?room={ document .id } &sessionKey={ session_key } "
@@ -168,6 +194,7 @@ def test_api_documents_can_edit_websocket_server_unreachable_fallback_to_no_webs
168194
169195 settings .COLLABORATION_API_URL = "http://example.com/"
170196 settings .COLLABORATION_SERVER_SECRET = "secret-token"
197+ settings .COLLABORATION_WS_NOT_CONNECTED_READY_ONLY = True
171198 endpoint_url = (
172199 f"{ settings .COLLABORATION_API_URL } get-connections/"
173200 f"?room={ document .id } &sessionKey={ session_key } "
@@ -202,6 +229,7 @@ def test_api_documents_can_edit_websocket_server_unreachable_fallback_to_no_webs
202229
203230 settings .COLLABORATION_API_URL = "http://example.com/"
204231 settings .COLLABORATION_SERVER_SECRET = "secret-token"
232+ settings .COLLABORATION_WS_NOT_CONNECTED_READY_ONLY = True
205233 endpoint_url = (
206234 f"{ settings .COLLABORATION_API_URL } get-connections/"
207235 f"?room={ document .id } &sessionKey={ session_key } "
0 commit comments