11
11
pytestmark = pytest .mark .django_db
12
12
13
13
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 ):
15
18
"""Anonymous users can not edit documents."""
16
- document = factories .DocumentFactory ()
19
+ document = factories .DocumentFactory (link_reach = "public" , link_role = role )
17
20
client = APIClient ()
21
+ session_key = client .session .session_key
22
+ settings .COLLABORATION_WS_NOT_CONNECTED_READY_ONLY = ws_not_connected_ready_only
23
+ endpoint_url = (
24
+ f"{ settings .COLLABORATION_API_URL } get-connections/"
25
+ f"?room={ document .id } &sessionKey={ session_key } "
26
+ )
27
+ ws_resp = responses .get (endpoint_url , json = {"count" : 0 , "exists" : False })
28
+
18
29
response = client .get (f"/api/v1.0/documents/{ document .id !s} /can-edit/" )
19
- assert response .status_code == 401
30
+
31
+ if role == "reader" :
32
+ assert response .status_code == 401
33
+ else :
34
+ assert response .status_code == 200
35
+ assert response .json () == {"can_edit" : True }
36
+ assert ws_resp .call_count == (1 if ws_not_connected_ready_only else 0 )
20
37
21
38
22
39
@responses .activate
23
- def test_api_documents_can_edit_authenticated_no_websocket (settings ):
40
+ @pytest .mark .parametrize ("ws_not_connected_ready_only" , [True , False ])
41
+ def test_api_documents_can_edit_authenticated_no_websocket (
42
+ settings , ws_not_connected_ready_only
43
+ ):
24
44
"""
25
45
A user not connected to the websocket and no other user have already updated the document,
26
46
the document can be updated.
@@ -34,6 +54,7 @@ def test_api_documents_can_edit_authenticated_no_websocket(settings):
34
54
35
55
settings .COLLABORATION_API_URL = "http://example.com/"
36
56
settings .COLLABORATION_SERVER_SECRET = "secret-token"
57
+ settings .COLLABORATION_WS_NOT_CONNECTED_READY_ONLY = ws_not_connected_ready_only
37
58
endpoint_url = (
38
59
f"{ settings .COLLABORATION_API_URL } get-connections/"
39
60
f"?room={ document .id } &sessionKey={ session_key } "
@@ -49,7 +70,7 @@ def test_api_documents_can_edit_authenticated_no_websocket(settings):
49
70
assert response .status_code == 200
50
71
51
72
assert response .json () == {"can_edit" : True }
52
- assert ws_resp .call_count == 1
73
+ assert ws_resp .call_count == ( 1 if ws_not_connected_ready_only else 0 )
53
74
54
75
55
76
@responses .activate
@@ -69,6 +90,7 @@ def test_api_documents_can_edit_authenticated_no_websocket_user_already_editing(
69
90
70
91
settings .COLLABORATION_API_URL = "http://example.com/"
71
92
settings .COLLABORATION_SERVER_SECRET = "secret-token"
93
+ settings .COLLABORATION_WS_NOT_CONNECTED_READY_ONLY = True
72
94
endpoint_url = (
73
95
f"{ settings .COLLABORATION_API_URL } get-connections/"
74
96
f"?room={ document .id } &sessionKey={ session_key } "
@@ -103,6 +125,7 @@ def test_api_documents_can_edit_no_websocket_other_user_connected_to_websocket(
103
125
104
126
settings .COLLABORATION_API_URL = "http://example.com/"
105
127
settings .COLLABORATION_SERVER_SECRET = "secret-token"
128
+ settings .COLLABORATION_WS_NOT_CONNECTED_READY_ONLY = True
106
129
endpoint_url = (
107
130
f"{ settings .COLLABORATION_API_URL } get-connections/"
108
131
f"?room={ document .id } &sessionKey={ session_key } "
@@ -134,6 +157,7 @@ def test_api_documents_can_edit_user_connected_to_websocket(settings):
134
157
135
158
settings .COLLABORATION_API_URL = "http://example.com/"
136
159
settings .COLLABORATION_SERVER_SECRET = "secret-token"
160
+ settings .COLLABORATION_WS_NOT_CONNECTED_READY_ONLY = True
137
161
endpoint_url = (
138
162
f"{ settings .COLLABORATION_API_URL } get-connections/"
139
163
f"?room={ document .id } &sessionKey={ session_key } "
@@ -168,6 +192,7 @@ def test_api_documents_can_edit_websocket_server_unreachable_fallback_to_no_webs
168
192
169
193
settings .COLLABORATION_API_URL = "http://example.com/"
170
194
settings .COLLABORATION_SERVER_SECRET = "secret-token"
195
+ settings .COLLABORATION_WS_NOT_CONNECTED_READY_ONLY = True
171
196
endpoint_url = (
172
197
f"{ settings .COLLABORATION_API_URL } get-connections/"
173
198
f"?room={ document .id } &sessionKey={ session_key } "
@@ -202,6 +227,7 @@ def test_api_documents_can_edit_websocket_server_unreachable_fallback_to_no_webs
202
227
203
228
settings .COLLABORATION_API_URL = "http://example.com/"
204
229
settings .COLLABORATION_SERVER_SECRET = "secret-token"
230
+ settings .COLLABORATION_WS_NOT_CONNECTED_READY_ONLY = True
205
231
endpoint_url = (
206
232
f"{ settings .COLLABORATION_API_URL } get-connections/"
207
233
f"?room={ document .id } &sessionKey={ session_key } "
0 commit comments