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_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
+
18
31
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 )
20
39
21
40
22
41
@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
+ ):
24
46
"""
25
47
A user not connected to the websocket and no other user have already updated the document,
26
48
the document can be updated.
@@ -34,6 +56,7 @@ def test_api_documents_can_edit_authenticated_no_websocket(settings):
34
56
35
57
settings .COLLABORATION_API_URL = "http://example.com/"
36
58
settings .COLLABORATION_SERVER_SECRET = "secret-token"
59
+ settings .COLLABORATION_WS_NOT_CONNECTED_READY_ONLY = ws_not_connected_ready_only
37
60
endpoint_url = (
38
61
f"{ settings .COLLABORATION_API_URL } get-connections/"
39
62
f"?room={ document .id } &sessionKey={ session_key } "
@@ -49,7 +72,7 @@ def test_api_documents_can_edit_authenticated_no_websocket(settings):
49
72
assert response .status_code == 200
50
73
51
74
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 )
53
76
54
77
55
78
@responses .activate
@@ -69,6 +92,7 @@ def test_api_documents_can_edit_authenticated_no_websocket_user_already_editing(
69
92
70
93
settings .COLLABORATION_API_URL = "http://example.com/"
71
94
settings .COLLABORATION_SERVER_SECRET = "secret-token"
95
+ settings .COLLABORATION_WS_NOT_CONNECTED_READY_ONLY = True
72
96
endpoint_url = (
73
97
f"{ settings .COLLABORATION_API_URL } get-connections/"
74
98
f"?room={ document .id } &sessionKey={ session_key } "
@@ -103,6 +127,7 @@ def test_api_documents_can_edit_no_websocket_other_user_connected_to_websocket(
103
127
104
128
settings .COLLABORATION_API_URL = "http://example.com/"
105
129
settings .COLLABORATION_SERVER_SECRET = "secret-token"
130
+ settings .COLLABORATION_WS_NOT_CONNECTED_READY_ONLY = True
106
131
endpoint_url = (
107
132
f"{ settings .COLLABORATION_API_URL } get-connections/"
108
133
f"?room={ document .id } &sessionKey={ session_key } "
@@ -134,6 +159,7 @@ def test_api_documents_can_edit_user_connected_to_websocket(settings):
134
159
135
160
settings .COLLABORATION_API_URL = "http://example.com/"
136
161
settings .COLLABORATION_SERVER_SECRET = "secret-token"
162
+ settings .COLLABORATION_WS_NOT_CONNECTED_READY_ONLY = True
137
163
endpoint_url = (
138
164
f"{ settings .COLLABORATION_API_URL } get-connections/"
139
165
f"?room={ document .id } &sessionKey={ session_key } "
@@ -168,6 +194,7 @@ def test_api_documents_can_edit_websocket_server_unreachable_fallback_to_no_webs
168
194
169
195
settings .COLLABORATION_API_URL = "http://example.com/"
170
196
settings .COLLABORATION_SERVER_SECRET = "secret-token"
197
+ settings .COLLABORATION_WS_NOT_CONNECTED_READY_ONLY = True
171
198
endpoint_url = (
172
199
f"{ settings .COLLABORATION_API_URL } get-connections/"
173
200
f"?room={ document .id } &sessionKey={ session_key } "
@@ -202,6 +229,7 @@ def test_api_documents_can_edit_websocket_server_unreachable_fallback_to_no_webs
202
229
203
230
settings .COLLABORATION_API_URL = "http://example.com/"
204
231
settings .COLLABORATION_SERVER_SECRET = "secret-token"
232
+ settings .COLLABORATION_WS_NOT_CONNECTED_READY_ONLY = True
205
233
endpoint_url = (
206
234
f"{ settings .COLLABORATION_API_URL } get-connections/"
207
235
f"?room={ document .id } &sessionKey={ session_key } "
0 commit comments