Skip to content

Commit

Permalink
refactor test to work with file instead of image
Browse files Browse the repository at this point in the history
  • Loading branch information
jrief committed Jan 9, 2025
1 parent f3ed5bf commit fd12c52
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 39 deletions.
12 changes: 6 additions & 6 deletions finder/admin/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def update_inode(self, request, folder_id):
return response
body = json.loads(request.body)
try:
obj = self.get_object(request, body['id'])
inode_obj = self.get_object(request, body['id'])
except (ObjectDoesNotExist, KeyError):
return HttpResponseNotFound(f"InodeModel<id={body.get('id', '<missing>')}> not found.")
current_folder = self.get_object(request, folder_id)
Expand All @@ -241,14 +241,14 @@ def update_inode(self, request, folder_id):
msg = gettext("A folder named “{name}” already exists.")
return HttpResponse(msg.format(name=inode_name), status=409)
update_values = {}
for field in self.get_fields(request, obj):
if field in body and body[field] != getattr(obj, field):
setattr(obj, field, body[field])
for field in self.get_fields(request, inode_obj):
if field in body and body[field] != getattr(inode_obj, field):
setattr(inode_obj, field, body[field])
update_values[field] = body[field]
if update_values:
obj.save(update_fields=list(update_values.keys()))
inode_obj.save(update_fields=list(update_values.keys()))
return JsonResponse({
'new_inode': self.serialize_inode(obj),
'new_inode': self.serialize_inode(inode_obj),
'favorite_folders': self.get_favorite_folders(request, current_folder),
})

Expand Down
2 changes: 1 addition & 1 deletion finder/contrib/archive/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def archive_selected(self, request, folder_id):
for inode_id in body['inode_ids']:
try:
inode_obj = FolderModel.objects.get_inode(id=inode_id)
except FolderModel.DoesNotExist:
except ObjectDoesNotExist:
return HttpResponseBadRequest(f"Inode with ID “{inode_id}” not found")
else:
inode_objects.append(inode_obj)
Expand Down
9 changes: 9 additions & 0 deletions unittests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import pytest
import random

from django.conf import settings
from django.core.management import call_command
Expand All @@ -16,13 +17,21 @@
@pytest.fixture(autouse=True, scope='session')
def create_assets():
os.makedirs(settings.BASE_DIR / 'workdir/assets', exist_ok=True)
with open(settings.BASE_DIR / 'workdir/assets/small_file.bin', 'wb') as handle:
handle.write(random.randbytes(1000))
with open(settings.BASE_DIR / 'workdir/assets/huge_file.bin', 'wb') as handle:
handle.write(random.randbytes(100000))
image = create_random_image()
image.save(settings.BASE_DIR / 'workdir/assets/demo_image.png')


@pytest.fixture(scope='session')
def django_db_setup(django_db_blocker):
database_file = settings.BASE_DIR / 'workdir/test_db.sqlite3'
try:
os.remove(database_file)
except FileNotFoundError:
pass
settings.DATABASES['default']['NAME'] = database_file
with django_db_blocker.unblock():
call_command('migrate', verbosity=0)
Expand Down
64 changes: 32 additions & 32 deletions unittests/test_folder_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.urls import reverse

from filer import settings as filer_settings
from finder.contrib.image.models import ImageFileModel
from finder.models.file import FileModel
from finder.models.folder import FolderModel
from finder.models.realm import RealmModel

Expand Down Expand Up @@ -72,13 +72,14 @@ def test_access_folder_not_found(admin_client):


@pytest.mark.django_db
def test_folder_upload_file(realm, admin_client):
@pytest.mark.parametrize('binary_file', ['small_file.bin', 'huge_file.bin'])
def test_folder_upload_file(realm, admin_client, binary_file):
admin_url = reverse('admin:finder_inodemodel_change', kwargs={'inode_id': realm.root_folder.id})
sha1 = hashlib.sha1()
with open(settings.BASE_DIR / 'workdir/assets/demo_image.png', 'rb') as file_handle:
with open(settings.BASE_DIR / 'workdir/assets' / binary_file, 'rb') as file_handle:
response = admin_client.post(
f'{admin_url}/upload',
{'upload_file': file_handle, 'filename': 'demo_image.png'},
{'upload_file': file_handle, 'filename': binary_file},
content_type=MULTIPART_CONTENT % {'boundary': 'BoUnDaRyStRiNg'},
)
file_handle.seek(0)
Expand All @@ -87,15 +88,14 @@ def test_folder_upload_file(realm, admin_client):
assert response.status_code == 200
file_info = response.json()['file_info']
id = file_info['id']
assert file_info['name'] == 'demo_image.png'
assert file_info['file_size'] == os.stat(settings.BASE_DIR / 'workdir/assets/demo_image.png').st_size
assert file_info['name'] == binary_file
assert file_info['file_size'] == os.stat(settings.BASE_DIR / 'workdir/assets' / binary_file).st_size
assert file_info['sha1'] == sha1.hexdigest()
assert file_info['mime_type'] == 'image/png'
assert file_info['mime_type'] == 'application/octet-stream'
filer_public = filer_settings.FILER_STORAGES['public']['main']['UPLOAD_TO_PREFIX']
assert file_info['download_url'] == f'{settings.MEDIA_URL}{filer_public}/{id[0:2]}/{id[2:4]}/{id}/demo_image.png'
filer_public_thumbnails = filer_settings.FILER_STORAGES['public']['thumbnails']['THUMBNAIL_OPTIONS']['base_dir']
assert file_info['thumbnail_url'] == f'{settings.MEDIA_URL}{filer_public_thumbnails}/{id[0:2]}/{id[2:4]}/{id}/demo_image__180x180.png'
assert ImageFileModel.objects.filter(id=id).exists()
assert file_info['download_url'] == f'{settings.MEDIA_URL}{filer_public}/{id[0:2]}/{id[2:4]}/{id}/{binary_file}'
assert file_info['thumbnail_url'] == f'{settings.STATIC_URL}filer/icons/file-unknown.svg'
assert FileModel.objects.filter(id=id).exists()

# with wrong method
response = admin_client.get(admin_url + '/upload')
Expand All @@ -114,11 +114,11 @@ def test_folder_upload_file(realm, admin_client):


@pytest.fixture
def uploaded_image(realm, admin_user):
file_name = 'demo_image.png'
def uploaded_file(realm, admin_user):
file_name = 'small_file.bin'
with open(settings.BASE_DIR / 'workdir/assets' / file_name, 'rb') as file_handle:
uploaded_file = SimpleUploadedFile(file_name, file_handle.read(), content_type='image/png')
return ImageFileModel.objects.create_from_upload(
uploaded_file = SimpleUploadedFile(file_name, file_handle.read(), content_type='application/octet-stream')
return FileModel.objects.create_from_upload(
uploaded_file,
folder=realm.root_folder,
owner=admin_user,
Expand All @@ -135,21 +135,21 @@ def sub_folder(realm, admin_user):


@pytest.mark.django_db
def test_folder_fetch(realm, uploaded_image, admin_client):
def test_folder_fetch(realm, uploaded_file, admin_client):
admin_url = reverse('admin:finder_inodemodel_change', kwargs={'inode_id': realm.root_folder.id})
response = admin_client.get(f'{admin_url}/fetch')
assert response.status_code == 200
inodes = response.json()['inodes']
assert ImageFileModel.objects.filter(parent=realm.root_folder, id=inodes[0]['id']).exists()
assert FileModel.objects.filter(parent=realm.root_folder, id=inodes[0]['id']).exists()

# found using search query
response = admin_client.get(f'{admin_url}/fetch?q=demo')
response = admin_client.get(f'{admin_url}/fetch?q=small')
assert response.status_code == 200
inodes = response.json()['inodes']
assert ImageFileModel.objects.filter(parent=realm.root_folder, id=inodes[0]['id']).exists()
assert FileModel.objects.filter(parent=realm.root_folder, id=inodes[0]['id']).exists()

# not found using search query
response = admin_client.get(f'{admin_url}/fetch?q=nemo')
response = admin_client.get(f'{admin_url}/fetch?q=foobar')
assert response.status_code == 200
inodes = response.json()['inodes']
assert len(inodes) == 0
Expand All @@ -172,42 +172,42 @@ def update_inode_url(realm):


@pytest.mark.django_db
def test_update_inode_nothing_changed(update_inode_url, uploaded_image, admin_client):
def test_update_inode_change_nothing(update_inode_url, uploaded_file, admin_client):
response = admin_client.post(
update_inode_url,
{'id': str(uploaded_image.id), 'name': 'demo_image.png'},
{'id': str(uploaded_file.id), 'name': uploaded_file.name},
content_type='application/json',
)
assert response.status_code == 200


@pytest.mark.django_db
def test_update_inode_update_filename(update_inode_url, uploaded_image, admin_client):
def test_update_inode_rename_file(update_inode_url, uploaded_file, admin_client):
response = admin_client.post(
update_inode_url,
{'id': str(uploaded_image.id), 'name': 'renamed_image.png'},
{'id': str(uploaded_file.id), 'name': "renamed_file.bin"},
content_type='application/json',
)
assert response.status_code == 200
uploaded_image.refresh_from_db()
assert uploaded_image.name == 'renamed_image.png'
uploaded_file.refresh_from_db()
assert uploaded_file.name == "renamed_file.bin"


@pytest.mark.django_db
def test_update_inode_update_using_invalid_filename(update_inode_url, uploaded_image, admin_client):
def test_update_inode_update_using_invalid_filename(update_inode_url, uploaded_file, admin_client):
response = admin_client.post(
update_inode_url,
{'id': str(uploaded_image.id), 'name': 'invalid:name'},
{'id': str(uploaded_file.id), 'name': 'invalid:name'},
content_type='application/json',
)
assert response.status_code == 409


@pytest.mark.django_db
def test_update_inode_using_existing_folder_name(update_inode_url, uploaded_image, sub_folder, admin_client):
def test_update_inode_using_existing_folder_name(update_inode_url, uploaded_file, sub_folder, admin_client):
response = admin_client.post(
update_inode_url,
{'id': str(uploaded_image.id), 'name': "Sub Folder"},
{'id': str(uploaded_file.id), 'name': "Sub Folder"},
content_type='application/json',
)
assert response.status_code == 409
Expand All @@ -226,10 +226,10 @@ def test_update_inode_rename_folder(update_inode_url, sub_folder, admin_client):


@pytest.mark.django_db
def test_update_inode_update_with_missing_content_type(update_inode_url, uploaded_image, admin_client):
def test_update_inode_update_with_missing_content_type(update_inode_url, uploaded_file, admin_client):
response = admin_client.post(
update_inode_url,
{'id': str(uploaded_image.id), 'name': 'renamed_image.png'},
{'id': str(uploaded_file.id), 'name': 'renamed_file.bin'},
)
assert response.status_code == 415

Expand Down
Empty file added unittests/test_image_png.py
Empty file.

0 comments on commit fd12c52

Please sign in to comment.