Commit 4e6fbe2f authored by Jens Langhammer's avatar Jens Langhammer

pyazo: add more unittest

parent 0412be9e
Pipeline #3202 passed with stages
in 2 minutes and 57 seconds
......@@ -6,6 +6,9 @@ omit =
manage.py
*/migrations/*
*/apps.py
pyazo/management/commands/nexus_upload.py
pyazo/management/commands/web.py
pyazo/management/commands/worker.py
docs/
[report]
......
"""test upload views"""
import os
from django.conf import settings
from django.contrib.auth.models import User
from django.shortcuts import reverse
from django.test import TestCase, override_settings
from pyazo.models import Collection, Upload
from pyazo.tests.utils import test_auth
class UploadViewTests(TestCase):
"""Test all client upload views"""
def setUp(self):
super().setUp()
with open(settings.MEDIA_ROOT + 'test2.txt', 'w') as _file:
_file.write('updating existing upload')
self.upload = Upload.objects.create(file=settings.MEDIA_ROOT + 'test2.txt')
with open(settings.MEDIA_ROOT + 'test3.txt', 'w') as _file:
_file.write('updating qwerqwerqwer upload')
def tearDown(self):
super().tearDown()
os.unlink(settings.MEDIA_ROOT + 'test2.txt')
os.unlink(settings.MEDIA_ROOT + 'test3.txt')
def test_upload_view(self):
"""Test UploadView"""
self.client.login(**test_auth())
response = self.client.get(reverse('upload_view', kwargs={'file_hash': self.upload.sha512}))
self.assertEqual(response.status_code, 200)
def test_upload_view_post(self):
"""Test UploadView's post"""
auth = test_auth()
self.client.login(**auth)
test_user = User.objects.get(username=auth.get('username'))
test_collection = Collection.objects.create(name='test', owner=test_user)
form_data = {
'collection': test_collection
}
response = self.client.post(
reverse('upload_view', kwargs={'file_hash': self.upload.sha512}), data=form_data)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, reverse(
'upload_view', kwargs={'file_hash': self.upload.sha512}))
def test_claim_view(self):
"""Test ClaimView"""
self.client.login(**test_auth())
response = self.client.get(reverse('upload_claim', kwargs={
'file_hash': self.upload.sha512}))
self.assertEqual(response.status_code, 200)
def test_claim_view_post(self):
"""Test ClaimView"""
self.client.login(**test_auth())
response = self.client.post(reverse('upload_claim', kwargs={
'file_hash': self.upload.sha512}))
self.assertEqual(response.status_code, 302)
self.client.login(**test_auth(superuser=False))
response = self.client.post(reverse('upload_claim', kwargs={
'file_hash': self.upload.sha512}))
self.assertEqual(response.status_code, 302)
def test_delete_view(self):
"""Test DeleteView"""
self.client.login(**test_auth())
response = self.client.get(reverse('upload_delete', kwargs={
'file_hash': self.upload.sha512}))
self.assertEqual(response.status_code, 200)
def test_delete_view_post(self):
"""Test DeleteView's post"""
self.client.login(**test_auth())
response = self.client.post(reverse('upload_delete', kwargs={
'file_hash': self.upload.sha512}))
self.assertEqual(response.status_code, 302)
def test_delete_view_invalid(self):
"""Test DeleteView's post but with invalid permissions"""
self.client.login(**test_auth(superuser=False))
response = self.client.post(reverse('upload_delete', kwargs={
'file_hash': self.upload.sha512}))
self.assertEqual(response.status_code, 302)
@override_settings(AUTO_CLAIM_ENABLED=True)
def test_legacy_upload(self):
"""Test legacy upload view"""
test_auth()
with open(settings.MEDIA_ROOT + 'test3.txt') as _file:
response = self.client.post(reverse('upload'), data={
'id': 'test', 'imagedata': _file, 'username': 'superuser'})
self.assertEqual(response.status_code, 200)
def test_legacy_upload_exist(self):
"""Test legacy upload view with existing file"""
self.client.logout()
with open(self.upload.file.path) as _file:
response = self.client.post(reverse('upload'), data={'id': 'test', 'imagedata': _file})
self.assertEqual(response.status_code, 200)
def test_legacy_upload_invalid(self):
"""Test legacy upload view with invalid data"""
response = self.client.post(reverse('upload'), data={})
self.assertEqual(response.status_code, 400)
def test_browser_upload(self):
"""Test browser upload"""
self.client.login(**test_auth())
with open(settings.MEDIA_ROOT + 'test3.txt') as _file:
response = self.client.post(reverse('upload_browser'), data={'test': _file})
self.assertEqual(response.status_code, 204)
......@@ -13,7 +13,7 @@ class UploadViewTests(TestCase):
def setUp(self):
super().setUp()
self.test_file_path = settings.MEDIA_ROOT + 'test'
self.test_file_path = settings.MEDIA_ROOT + 'test.txt'
with open(self.test_file_path, 'w') as _file:
self._test_data = 'testdatafewrqwer'
_file.write(self._test_data)
......
......@@ -5,15 +5,20 @@ from django.contrib.auth.models import User
from django.core.management import call_command
def test_auth():
def test_auth(superuser=True):
"""Create a test user and return credentials to use with client.login"""
creds = {
credentials = {
'username': 'test',
'password': 'test',
'email': 'test@test.test',
}
User.objects.create_superuser(**creds)
return creds
if superuser:
credentials['username'] = 'superuser'
User.objects.create_superuser(**credentials)
else:
credentials['username'] = 'user'
User.objects.create_user(**credentials)
return credentials
def call_command_ret(*args, **kwargs):
......
......@@ -94,9 +94,9 @@ class DeleteUploadView(LoginRequiredMixin, TemplateView):
template_name = 'core/generic_delete.html'
def post(self, request: HttpRequest, file_hash: str) -> HttpResponse:
"""Claim upload to user (only if upload has no owner yet or user is superuser)"""
"""Delete upload"""
upload = get_object_or_404(Upload, sha512=file_hash)
if request.user.is_superuser or not upload.user:
if request.user.is_superuser or upload.user == request.user:
upload.delete()
messages.success(request, _('Upload successfully deleted'))
else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment