|
| 1 | +"""Test for items API endpoint managing wopi init request.""" |
| 2 | + |
| 3 | +from django.contrib.auth.models import AnonymousUser |
| 4 | +from django.utils import timezone |
| 5 | + |
| 6 | +import pytest |
| 7 | +from rest_framework.test import APIClient |
| 8 | + |
| 9 | +from core import factories, models |
| 10 | + |
| 11 | +pytestmark = pytest.mark.django_db |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def timestamp_now(): |
| 16 | + """Timestamp now in milliseconds.""" |
| 17 | + return int(round(timezone.now().timestamp())) * 1000 |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def valid_mimetype(): |
| 22 | + """Valid mimetype for testing.""" |
| 23 | + return "application/vnd.oasis.opendocument.text" |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def valid_wopi_launch_url(): |
| 28 | + """Valid WOPI launch URL for testing.""" |
| 29 | + return "https://vendorA.com/launch_url" |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture(autouse=True) |
| 33 | +def configure_wopi_settings(settings, valid_mimetype, valid_wopi_launch_url): |
| 34 | + settings.WOPI_CLIENTS = ["vendorA"] |
| 35 | + settings.WOPI_CLIENTS_CONFIGURATION = { |
| 36 | + "vendorA": { |
| 37 | + "launch_url": valid_wopi_launch_url, |
| 38 | + "mimetypes": [valid_mimetype], |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | +def test_api_items_wopi_not_existing_item(): |
| 44 | + """ |
| 45 | + Anonymous user cannot generate wopi access token for non-existing item. |
| 46 | + """ |
| 47 | + |
| 48 | + client = APIClient() |
| 49 | + response = client.get("/api/v1.0/items/00000000-0000-0000-0000-000000000000/wopi/") |
| 50 | + |
| 51 | + assert response.status_code == 404 |
| 52 | + |
| 53 | + |
| 54 | +def test_api_items_wopi_anonymous_user_item_public( |
| 55 | + timestamp_now, valid_mimetype, valid_wopi_launch_url |
| 56 | +): |
| 57 | + """ |
| 58 | + Anonymous user can generate wopi access token for public item. |
| 59 | + """ |
| 60 | + |
| 61 | + item = factories.ItemFactory( |
| 62 | + link_reach=models.LinkReachChoices.PUBLIC, |
| 63 | + type=models.ItemTypeChoices.FILE, |
| 64 | + mimetype=valid_mimetype, |
| 65 | + ) |
| 66 | + item.upload_state = models.ItemUploadStateChoices.UPLOADED |
| 67 | + item.save() |
| 68 | + |
| 69 | + client = APIClient() |
| 70 | + response = client.get(f"/api/v1.0/items/{item.id!s}/wopi/") |
| 71 | + |
| 72 | + assert response.status_code == 200 |
| 73 | + data = response.json() |
| 74 | + assert data["access_token"] is not None |
| 75 | + assert data["access_token_ttl"] > timestamp_now |
| 76 | + assert data["launch_url"] == valid_wopi_launch_url |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.parametrize( |
| 80 | + "link_reach", |
| 81 | + [models.LinkReachChoices.AUTHENTICATED, models.LinkReachChoices.RESTRICTED], |
| 82 | +) |
| 83 | +def test_api_items_wopi_anonymous_user_item_not_public(link_reach): |
| 84 | + """Anymous user can not access not public item.""" |
| 85 | + item = factories.ItemFactory(link_reach=link_reach) |
| 86 | + |
| 87 | + client = APIClient() |
| 88 | + response = client.get(f"/api/v1.0/items/{item.id!s}/wopi/") |
| 89 | + |
| 90 | + assert response.status_code == 401 |
| 91 | + |
| 92 | + |
| 93 | +def test_api_items_wopi_anonymous_user_not_item_file(): |
| 94 | + """Anymous user can not access item that is not a file.""" |
| 95 | + item = factories.ItemFactory( |
| 96 | + type=models.ItemTypeChoices.FOLDER, link_reach=models.LinkReachChoices.PUBLIC |
| 97 | + ) |
| 98 | + |
| 99 | + client = APIClient() |
| 100 | + response = client.get(f"/api/v1.0/items/{item.id!s}/wopi/") |
| 101 | + |
| 102 | + assert response.status_code == 400 |
| 103 | + assert response.json() == {"detail": "This item does not suport WOPI integration."} |
| 104 | + |
| 105 | + |
| 106 | +def test_api_items_wopi_anonymous_item_file_mimetype_not_supported(): |
| 107 | + """Anymous user can not access item file with mimetype not supported.""" |
| 108 | + item = factories.ItemFactory( |
| 109 | + type=models.ItemTypeChoices.FILE, |
| 110 | + mimetype="image/png", |
| 111 | + link_reach=models.LinkReachChoices.PUBLIC, |
| 112 | + ) |
| 113 | + item.upload_state = models.ItemUploadStateChoices.UPLOADED |
| 114 | + item.save() |
| 115 | + |
| 116 | + client = APIClient() |
| 117 | + response = client.get(f"/api/v1.0/items/{item.id!s}/wopi/") |
| 118 | + |
| 119 | + assert response.status_code == 400 |
| 120 | + assert response.json() == {"detail": "This item does not suport WOPI integration."} |
| 121 | + |
| 122 | + |
| 123 | +def test_api_items_wopi_anonymous_user_item_not_uploaded(): |
| 124 | + """Anymous user can not access item file that is not uploaded.""" |
| 125 | + item = factories.ItemFactory( |
| 126 | + type=models.ItemTypeChoices.FILE, |
| 127 | + mimetype="image/png", |
| 128 | + link_reach=models.LinkReachChoices.PUBLIC, |
| 129 | + ) |
| 130 | + |
| 131 | + client = APIClient() |
| 132 | + response = client.get(f"/api/v1.0/items/{item.id!s}/wopi/") |
| 133 | + |
| 134 | + assert response.status_code == 400 |
| 135 | + assert response.json() == {"detail": "This item does not suport WOPI integration."} |
| 136 | + |
| 137 | + |
| 138 | +def test_api_items_wopi_authenticated_user_item_not_accessible(): |
| 139 | + """Authenticated user can not access item that is not accessible.""" |
| 140 | + user = factories.UserFactory() |
| 141 | + item = factories.ItemFactory(link_reach=models.LinkReachChoices.RESTRICTED) |
| 142 | + |
| 143 | + client = APIClient() |
| 144 | + client.force_login(user) |
| 145 | + response = client.get(f"/api/v1.0/items/{item.id!s}/wopi/") |
| 146 | + |
| 147 | + assert response.status_code == 403 |
| 148 | + |
| 149 | + |
| 150 | +def test_api_items_wopi_authenticated_can_access_retricted_item( |
| 151 | + timestamp_now, valid_mimetype, valid_wopi_launch_url |
| 152 | +): |
| 153 | + """Authenticated user can access item that is accessible.""" |
| 154 | + user = factories.UserFactory() |
| 155 | + item = factories.ItemFactory( |
| 156 | + link_reach=models.LinkReachChoices.RESTRICTED, |
| 157 | + type=models.ItemTypeChoices.FILE, |
| 158 | + mimetype=valid_mimetype, |
| 159 | + ) |
| 160 | + item.upload_state = models.ItemUploadStateChoices.UPLOADED |
| 161 | + item.save() |
| 162 | + factories.UserItemAccessFactory(user=user, item=item) |
| 163 | + |
| 164 | + client = APIClient() |
| 165 | + client.force_login(user) |
| 166 | + response = client.get(f"/api/v1.0/items/{item.id!s}/wopi/") |
| 167 | + |
| 168 | + assert response.status_code == 200 |
| 169 | + data = response.json() |
| 170 | + assert data["access_token"] is not None |
| 171 | + assert data["access_token_ttl"] > timestamp_now |
| 172 | + assert data["launch_url"] == valid_wopi_launch_url |
| 173 | + |
| 174 | + |
| 175 | +def test_api_items_wopi_authenticated_user_item_not_file(): |
| 176 | + """Authenticated user can not access item that is not a file.""" |
| 177 | + user = factories.UserFactory() |
| 178 | + item = factories.ItemFactory( |
| 179 | + link_reach=models.LinkReachChoices.RESTRICTED, |
| 180 | + type=models.ItemTypeChoices.FOLDER, |
| 181 | + ) |
| 182 | + factories.UserItemAccessFactory(user=user, item=item) |
| 183 | + |
| 184 | + client = APIClient() |
| 185 | + client.force_login(user) |
| 186 | + response = client.get(f"/api/v1.0/items/{item.id!s}/wopi/") |
| 187 | + |
| 188 | + assert response.status_code == 400 |
| 189 | + assert response.json() == {"detail": "This item does not suport WOPI integration."} |
| 190 | + |
| 191 | + |
| 192 | +def test_api_items_wopi_authenticated_user_item_mimetype_not_supported(): |
| 193 | + """Authenticated user can not access item that mimetype is not supported.""" |
| 194 | + user = factories.UserFactory() |
| 195 | + item = factories.ItemFactory( |
| 196 | + link_reach=models.LinkReachChoices.RESTRICTED, |
| 197 | + type=models.ItemTypeChoices.FILE, |
| 198 | + mimetype="image/png", |
| 199 | + ) |
| 200 | + item.upload_state = models.ItemUploadStateChoices.UPLOADED |
| 201 | + item.save() |
| 202 | + factories.UserItemAccessFactory(user=user, item=item) |
| 203 | + |
| 204 | + client = APIClient() |
| 205 | + client.force_login(user) |
| 206 | + response = client.get(f"/api/v1.0/items/{item.id!s}/wopi/") |
| 207 | + |
| 208 | + assert response.status_code == 400 |
| 209 | + assert response.json() == {"detail": "This item does not suport WOPI integration."} |
0 commit comments