Skip to content

Commit f3ed5bf

Browse files
committed
refactor folder unittests out of demoapp
1 parent 7c1580f commit f3ed5bf

File tree

13 files changed

+418
-201
lines changed

13 files changed

+418
-201
lines changed

.coveragerc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
branch = True
33
source =
44
finder
5-
demoapp/unittests
65
omit =
76
migrations/*
8-
tests/*
97

108
[report]
119
exclude_lines =
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,22 @@ jobs:
2323
matrix:
2424
python-version: ["3.11", "3.12"]
2525
django-version: ["5.2.*"]
26-
node-version: ["18.x"]
26+
with-cte: [true, false]
2727

2828
steps:
2929
- uses: actions/checkout@v3
30-
- name: Use Node.js ${{ matrix.node-version }}
31-
uses: actions/setup-node@v2
32-
with:
33-
node-version: ${{ matrix.node-version }}
3430
- name: Set up Python ${{ matrix.python-version }}
3531
uses: actions/setup-python@v2
3632
with:
3733
python-version: ${{ matrix.python-version }}
3834
- name: Install dependencies
3935
run: |
40-
npm install --include=dev
4136
python -m pip install --upgrade pip
4237
python -m pip install https://github.com/django/django/archive/refs/heads/main.zip
43-
python -m pip install django-cte django-entangled ffmpeg-python pillow reportlab svglib
38+
python -m pip install django-entangled ffmpeg-python pillow reportlab svglib
4439
python -m pip install beautifulsoup4 coverage Faker lxml pytest pytest-django pytest-cov
45-
- name: Build Client
46-
run: |
47-
npm run compilescss
48-
npm run esbuild
40+
- if: ${{ matrix.with-cte }}
41+
run: python -m pip install django-cte
4942
- name: Test with pytest
5043
run: |
51-
python -m pytest -v demoapp/unittests
44+
python -m pytest -v unittests

demoapp/unittests/test_folder_admin.py

Lines changed: 0 additions & 153 deletions
This file was deleted.

finder/admin/folder.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from django.db.models import QuerySet, Subquery
66

77
from django.forms.widgets import Media
8-
from django.http.response import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound, JsonResponse
8+
from django.http.response import HttpResponse, HttpResponseNotAllowed, HttpResponseNotFound, JsonResponse
99
from django.templatetags.static import static
1010
from django.urls import path, reverse
1111
from django.utils.translation import gettext
@@ -175,8 +175,12 @@ def get_model_admin(self, mime_type):
175175
return self._model_admin_cache[mime_type]
176176

177177
def fetch_inodes(self, request, folder_id):
178-
if not (current_folder := self.get_object(request, folder_id)):
179-
return HttpResponseNotFound(f"Folder {folder_id} not found.")
178+
if request.method != 'GET':
179+
return HttpResponseNotAllowed(f"Method {request.method} not allowed. Only GET requests are allowed.")
180+
try:
181+
current_folder = self.get_object(request, folder_id)
182+
except ObjectDoesNotExist:
183+
return HttpResponseNotFound(f"FolderModel<{folder_id}> not found.")
180184
if search_query := request.GET.get('q'):
181185
inode_qs = self.search_for_inodes(request, current_folder, search_query)
182186
else:
@@ -204,11 +208,13 @@ def search_for_inodes(self, request, current_folder, search_query, **lookup):
204208

205209
def upload_file(self, request, folder_id):
206210
if request.method != 'POST':
207-
return HttpResponseBadRequest(f"Method {request.method} not allowed. Only POST requests are allowed.")
208-
if not (folder := self.get_object(request, folder_id)):
209-
return HttpResponseNotFound(f"Folder {folder_id} not found.")
211+
return HttpResponseNotAllowed(f"Method {request.method} not allowed. Only POST requests are allowed.")
212+
try:
213+
folder = self.get_object(request, folder_id)
214+
except ObjectDoesNotExist:
215+
return HttpResponseNotFound(f"FolderModel<{folder_id}> not found.")
210216
if request.content_type != 'multipart/form-data' or 'upload_file' not in request.FILES:
211-
return HttpResponseBadRequest("Bad encoding type or missing payload.")
217+
return HttpResponse("Bad encoding type or missing payload.", status=415)
212218
model = FileModel.objects.get_model_for(request.FILES['upload_file'].content_type)
213219
new_file = model.objects.create_from_upload(
214220
request.FILES['upload_file'],
@@ -223,33 +229,27 @@ def update_inode(self, request, folder_id):
223229
body = json.loads(request.body)
224230
try:
225231
obj = self.get_object(request, body['id'])
226-
except (InodeModel.DoesNotExist, KeyError):
227-
return HttpResponseNotFound(f"Inode(id={body.get('id', '<missing>')}) not found.")
232+
except (ObjectDoesNotExist, KeyError):
233+
return HttpResponseNotFound(f"InodeModel<id={body.get('id', '<missing>')}> not found.")
228234
current_folder = self.get_object(request, folder_id)
229235
inode_name = body['name']
230236
try:
231237
filename_validator(inode_name)
232238
except ValidationError as exc:
233-
return HttpResponseBadRequest(exc.messages[0], status=409)
239+
return HttpResponse(exc.messages[0], status=409)
234240
if current_folder.listdir(name=inode_name, is_folder=True).exists():
235241
msg = gettext("A folder named “{name}” already exists.")
236-
return HttpResponseBadRequest(msg.format(name=inode_name), status=409)
242+
return HttpResponse(msg.format(name=inode_name), status=409)
237243
update_values = {}
238244
for field in self.get_fields(request, obj):
239245
if field in body and body[field] != getattr(obj, field):
240246
setattr(obj, field, body[field])
241247
update_values[field] = body[field]
242248
if update_values:
243249
obj.save(update_fields=list(update_values.keys()))
244-
favorite_folders = self.get_favorite_folders(request, current_folder)
245-
if update_values:
246-
for folder in favorite_folders:
247-
if folder['id'] == obj.id:
248-
folder.update(update_values)
249-
break
250250
return JsonResponse({
251251
'new_inode': self.serialize_inode(obj),
252-
'favorite_folders': favorite_folders,
252+
'favorite_folders': self.get_favorite_folders(request, current_folder),
253253
})
254254

255255
def copy_inodes(self, request, folder_id):
@@ -263,7 +263,7 @@ def copy_inodes(self, request, folder_id):
263263
try:
264264
inode.copy_to(current_folder, owner=request.user)
265265
except RecursionError as exc:
266-
return HttpResponseBadRequest(str(exc), status=409)
266+
return HttpResponse(str(exc), status=409)
267267
return JsonResponse({
268268
'inodes': list(self.get_inodes(request, parent=current_folder)),
269269
})
@@ -291,7 +291,7 @@ def move_inodes(self, request, folder_id):
291291
proxy_obj.validate_constraints()
292292
proxy_obj._meta.model.objects.filter(id=entry['id']).update(parent=target_folder)
293293
except ValidationError as exc:
294-
return HttpResponseBadRequest(exc.messages[0], status=409)
294+
return HttpResponse(exc.messages[0], status=409)
295295
return JsonResponse({
296296
'inodes': list(self.get_inodes(request, parent=target_folder)),
297297
})
@@ -303,7 +303,7 @@ def delete_inodes(self, request, folder_id):
303303
current_folder = self.get_object(request, folder_id)
304304
trash_folder = self.get_trash_folder(request)
305305
if current_folder.id == trash_folder.id:
306-
return HttpResponseBadRequest("Cannot move inodes from trash folder into itself.")
306+
return HttpResponse("Cannot move inodes from trash folder into itself.", status=409)
307307
inode_ids = body.get('inode_ids', [])
308308
for entry in FolderModel.objects.filter_unified(id__in=inode_ids):
309309
inode = FolderModel.objects.get_inode(id=entry['id'])
@@ -324,7 +324,7 @@ def delete_inodes(self, request, folder_id):
324324

325325
def undo_discarded_inodes(self, request):
326326
if request.method != 'POST':
327-
return HttpResponseBadRequest(f"Method {request.method} not allowed. Only POST requests are allowed.")
327+
return HttpResponseNotAllowed(f"Method {request.method} not allowed. Only POST requests are allowed.")
328328
body = json.loads(request.body)
329329
trash_folder = self.get_trash_folder(request)
330330
discarded_inodes = DiscardedInode.objects.filter(inode__in=body.get('inode_ids', []))
@@ -337,7 +337,7 @@ def undo_discarded_inodes(self, request):
337337

338338
def erase_trash_folder(self, request):
339339
if request.method != 'DELETE':
340-
return HttpResponseBadRequest(f"Method {request.method} not allowed. Only DELETE requests are allowed.")
340+
return HttpResponseNotAllowed(f"Method {request.method} not allowed. Only DELETE requests are allowed.")
341341
trash_folder_entries = self.get_trash_folder(request).listdir()
342342
DiscardedInode.objects.filter(inode__in=list(trash_folder_entries.values_list('id', flat=True))).delete()
343343
for entry in trash_folder_entries:
@@ -360,7 +360,7 @@ def add_folder(self, request, folder_id):
360360
body = json.loads(request.body)
361361
if parent_folder.listdir(name=body['name'], is_folder=True).exists():
362362
msg = gettext("A folder named “{name}” already exists.")
363-
return HttpResponseBadRequest(msg.format(name=body['name']), status=409)
363+
return HttpResponse(msg.format(name=body['name']), status=409)
364364
new_folder = FolderModel.objects.create(
365365
name=body['name'],
366366
parent=parent_folder,

finder/admin/inode.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import json
22

3+
from django.core.exceptions import ObjectDoesNotExist
4+
35
from django.contrib import admin
46
from django.contrib.sites.shortcuts import get_current_site
57
from django.db.models.expressions import F, Value
68
from django.db.models.fields import BooleanField
7-
from django.http.response import HttpResponseBadRequest, HttpResponseNotFound, HttpResponseRedirect, JsonResponse
9+
from django.http.response import (
10+
HttpResponse, HttpResponseBadRequest, HttpResponseNotAllowed, HttpResponseNotFound, HttpResponseRedirect,
11+
JsonResponse,
12+
)
813
from django.middleware.csrf import get_token
914
from django.template.response import TemplateResponse
1015
from django.urls import path, reverse
@@ -32,10 +37,15 @@ def get_object(self, request, inode_id, *args):
3237

3338
def check_for_valid_post_request(self, request, folder_id):
3439
if request.method != 'POST':
35-
return HttpResponseBadRequest(f"Method {request.method} not allowed. Only POST requests are allowed.")
40+
return HttpResponseNotAllowed(f"Method {request.method} not allowed. Only POST requests are allowed.")
3641
if request.content_type != 'application/json':
37-
return HttpResponseBadRequest(f"Invalid content-type {request.content_type}. Only application/json is allowed.")
38-
if self.get_object(request, folder_id) is None:
42+
return HttpResponse(
43+
f"Invalid content-type {request.content_type}. Only application/json is allowed.",
44+
status=415,
45+
)
46+
try:
47+
self.get_object(request, folder_id)
48+
except ObjectDoesNotExist:
3949
return HttpResponseNotFound(f"Folder with id “{folder_id}” not found.")
4050

4151
def toggle_pin(self, request, folder_id):
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[pytest]
2-
DJANGO_SETTINGS_MODULE = demoapp.settings
2+
DJANGO_SETTINGS_MODULE = unittests.settings
33
django_find_project = false
44
addopts = --tb=native

0 commit comments

Comments
 (0)