|
| 1 | +"""Test the can_edit endpoint in the viewset DocumentViewSet.""" |
| 2 | + |
| 3 | +from django.core.cache import cache |
| 4 | + |
| 5 | +import pytest |
| 6 | +import responses |
| 7 | +from rest_framework.test import APIClient |
| 8 | + |
| 9 | +from core import factories |
| 10 | + |
| 11 | +pytestmark = pytest.mark.django_db |
| 12 | + |
| 13 | + |
| 14 | +def test_api_documents_can_edit_anonymous(): |
| 15 | + """Anonymous users can not edit documents.""" |
| 16 | + document = factories.DocumentFactory() |
| 17 | + client = APIClient() |
| 18 | + response = client.get(f"/api/v1.0/documents/{document.id!s}/can-edit/") |
| 19 | + assert response.status_code == 401 |
| 20 | + |
| 21 | + |
| 22 | +@responses.activate |
| 23 | +def test_api_documents_can_edit_authenticated_no_websocket(settings): |
| 24 | + """ |
| 25 | + A user not connected to the websocket and no other user have already updated the document, |
| 26 | + the document can be updated. |
| 27 | + """ |
| 28 | + user = factories.UserFactory(with_owned_document=True) |
| 29 | + client = APIClient() |
| 30 | + client.force_login(user) |
| 31 | + session_key = client.session.session_key |
| 32 | + |
| 33 | + document = factories.DocumentFactory(users=[(user, "editor")]) |
| 34 | + |
| 35 | + settings.COLLABORATION_API_URL = "http://example.com/" |
| 36 | + settings.COLLABORATION_SERVER_SECRET = "secret-token" |
| 37 | + endpoint_url = ( |
| 38 | + f"{settings.COLLABORATION_API_URL}get-connections/" |
| 39 | + f"?room={document.id}&sessionKey={session_key}" |
| 40 | + ) |
| 41 | + |
| 42 | + ws_resp = responses.get(endpoint_url, json={"count": 0, "exists": False}) |
| 43 | + |
| 44 | + assert cache.get(f"docs:no-websocket:{document.id}") is None |
| 45 | + |
| 46 | + response = client.get( |
| 47 | + f"/api/v1.0/documents/{document.id!s}/can-edit/", |
| 48 | + ) |
| 49 | + assert response.status_code == 200 |
| 50 | + |
| 51 | + assert response.json() == {"can_edit": True} |
| 52 | + assert ws_resp.call_count == 1 |
| 53 | + |
| 54 | + |
| 55 | +@responses.activate |
| 56 | +def test_api_documents_can_edit_authenticated_no_websocket_user_already_editing( |
| 57 | + settings, |
| 58 | +): |
| 59 | + """ |
| 60 | + A user not connected to the websocket and another user have already updated the document, |
| 61 | + the document can not be updated. |
| 62 | + """ |
| 63 | + user = factories.UserFactory(with_owned_document=True) |
| 64 | + client = APIClient() |
| 65 | + client.force_login(user) |
| 66 | + session_key = client.session.session_key |
| 67 | + |
| 68 | + document = factories.DocumentFactory(users=[(user, "editor")]) |
| 69 | + |
| 70 | + settings.COLLABORATION_API_URL = "http://example.com/" |
| 71 | + settings.COLLABORATION_SERVER_SECRET = "secret-token" |
| 72 | + endpoint_url = ( |
| 73 | + f"{settings.COLLABORATION_API_URL}get-connections/" |
| 74 | + f"?room={document.id}&sessionKey={session_key}" |
| 75 | + ) |
| 76 | + ws_resp = responses.get(endpoint_url, json={"count": 0, "exists": False}) |
| 77 | + |
| 78 | + cache.set(f"docs:no-websocket:{document.id}", "other_session_key") |
| 79 | + |
| 80 | + response = client.get( |
| 81 | + f"/api/v1.0/documents/{document.id!s}/can-edit/", |
| 82 | + ) |
| 83 | + assert response.status_code == 200 |
| 84 | + assert response.json() == {"can_edit": False} |
| 85 | + |
| 86 | + assert ws_resp.call_count == 1 |
| 87 | + |
| 88 | + |
| 89 | +@responses.activate |
| 90 | +def test_api_documents_can_edit_no_websocket_other_user_connected_to_websocket( |
| 91 | + settings, |
| 92 | +): |
| 93 | + """ |
| 94 | + A user not connected to the websocket and another user is connected to the websocket, |
| 95 | + the document can not be updated. |
| 96 | + """ |
| 97 | + user = factories.UserFactory(with_owned_document=True) |
| 98 | + client = APIClient() |
| 99 | + client.force_login(user) |
| 100 | + session_key = client.session.session_key |
| 101 | + |
| 102 | + document = factories.DocumentFactory(users=[(user, "editor")]) |
| 103 | + |
| 104 | + settings.COLLABORATION_API_URL = "http://example.com/" |
| 105 | + settings.COLLABORATION_SERVER_SECRET = "secret-token" |
| 106 | + endpoint_url = ( |
| 107 | + f"{settings.COLLABORATION_API_URL}get-connections/" |
| 108 | + f"?room={document.id}&sessionKey={session_key}" |
| 109 | + ) |
| 110 | + ws_resp = responses.get(endpoint_url, json={"count": 3, "exists": False}) |
| 111 | + |
| 112 | + assert cache.get(f"docs:no-websocket:{document.id}") is None |
| 113 | + |
| 114 | + response = client.get( |
| 115 | + f"/api/v1.0/documents/{document.id!s}/can-edit/", |
| 116 | + ) |
| 117 | + assert response.status_code == 200 |
| 118 | + assert response.json() == {"can_edit": False} |
| 119 | + assert cache.get(f"docs:no-websocket:{document.id}") is None |
| 120 | + assert ws_resp.call_count == 1 |
| 121 | + |
| 122 | + |
| 123 | +@responses.activate |
| 124 | +def test_api_documents_can_edit_user_connected_to_websocket(settings): |
| 125 | + """ |
| 126 | + A user connected to the websocket, the document can be updated. |
| 127 | + """ |
| 128 | + user = factories.UserFactory(with_owned_document=True) |
| 129 | + client = APIClient() |
| 130 | + client.force_login(user) |
| 131 | + session_key = client.session.session_key |
| 132 | + |
| 133 | + document = factories.DocumentFactory(users=[(user, "editor")]) |
| 134 | + |
| 135 | + settings.COLLABORATION_API_URL = "http://example.com/" |
| 136 | + settings.COLLABORATION_SERVER_SECRET = "secret-token" |
| 137 | + endpoint_url = ( |
| 138 | + f"{settings.COLLABORATION_API_URL}get-connections/" |
| 139 | + f"?room={document.id}&sessionKey={session_key}" |
| 140 | + ) |
| 141 | + ws_resp = responses.get(endpoint_url, json={"count": 3, "exists": True}) |
| 142 | + |
| 143 | + assert cache.get(f"docs:no-websocket:{document.id}") is None |
| 144 | + |
| 145 | + response = client.get( |
| 146 | + f"/api/v1.0/documents/{document.id!s}/can-edit/", |
| 147 | + ) |
| 148 | + assert response.status_code == 200 |
| 149 | + assert response.json() == {"can_edit": True} |
| 150 | + assert cache.get(f"docs:no-websocket:{document.id}") is None |
| 151 | + assert ws_resp.call_count == 1 |
| 152 | + |
| 153 | + |
| 154 | +@responses.activate |
| 155 | +def test_api_documents_can_edit_websocket_server_unreachable_fallback_to_no_websocket( |
| 156 | + settings, |
| 157 | +): |
| 158 | + """ |
| 159 | + When the websocket server is unreachable, the document can be updated like if the user was |
| 160 | + not connected to the websocket. |
| 161 | + """ |
| 162 | + user = factories.UserFactory(with_owned_document=True) |
| 163 | + client = APIClient() |
| 164 | + client.force_login(user) |
| 165 | + session_key = client.session.session_key |
| 166 | + |
| 167 | + document = factories.DocumentFactory(users=[(user, "editor")]) |
| 168 | + |
| 169 | + settings.COLLABORATION_API_URL = "http://example.com/" |
| 170 | + settings.COLLABORATION_SERVER_SECRET = "secret-token" |
| 171 | + endpoint_url = ( |
| 172 | + f"{settings.COLLABORATION_API_URL}get-connections/" |
| 173 | + f"?room={document.id}&sessionKey={session_key}" |
| 174 | + ) |
| 175 | + ws_resp = responses.get(endpoint_url, status=500) |
| 176 | + |
| 177 | + assert cache.get(f"docs:no-websocket:{document.id}") is None |
| 178 | + |
| 179 | + response = client.get( |
| 180 | + f"/api/v1.0/documents/{document.id!s}/can-edit/", |
| 181 | + ) |
| 182 | + assert response.status_code == 200 |
| 183 | + assert response.json() == {"can_edit": True} |
| 184 | + |
| 185 | + assert ws_resp.call_count == 1 |
| 186 | + |
| 187 | + |
| 188 | +@responses.activate |
| 189 | +def test_api_documents_can_edit_websocket_server_unreachable_fallback_to_no_websocket_other_users( |
| 190 | + settings, |
| 191 | +): |
| 192 | + """ |
| 193 | + When the websocket server is unreachable, the behavior fallback to the no websocket one. |
| 194 | + If an other user is already editing, the document can not be updated. |
| 195 | + """ |
| 196 | + user = factories.UserFactory(with_owned_document=True) |
| 197 | + client = APIClient() |
| 198 | + client.force_login(user) |
| 199 | + session_key = client.session.session_key |
| 200 | + |
| 201 | + document = factories.DocumentFactory(users=[(user, "editor")]) |
| 202 | + |
| 203 | + settings.COLLABORATION_API_URL = "http://example.com/" |
| 204 | + settings.COLLABORATION_SERVER_SECRET = "secret-token" |
| 205 | + endpoint_url = ( |
| 206 | + f"{settings.COLLABORATION_API_URL}get-connections/" |
| 207 | + f"?room={document.id}&sessionKey={session_key}" |
| 208 | + ) |
| 209 | + ws_resp = responses.get(endpoint_url, status=500) |
| 210 | + |
| 211 | + cache.set(f"docs:no-websocket:{document.id}", "other_session_key") |
| 212 | + |
| 213 | + response = client.get( |
| 214 | + f"/api/v1.0/documents/{document.id!s}/can-edit/", |
| 215 | + ) |
| 216 | + assert response.status_code == 200 |
| 217 | + assert response.json() == {"can_edit": False} |
| 218 | + |
| 219 | + assert cache.get(f"docs:no-websocket:{document.id}") == "other_session_key" |
| 220 | + assert ws_resp.call_count == 1 |
0 commit comments