Commit 13c36bc3 authored by Jens Langhammer's avatar Jens Langhammer

pyazo: more unit tests, fix some small bugs

parent 4e6fbe2f
Pipeline #3203 passed with stages
in 2 minutes and 56 seconds
......@@ -23,15 +23,11 @@ class Celery(celery.Celery):
# pylint: disable=method-hidden
def on_configure(self):
"""Update raven client"""
try:
client = Client(settings.RAVEN_CONFIG.get('dsn'))
# register a custom filter to filter out duplicate logs
register_logger_signal(client)
# hook into the Celery error handler
register_signal(client)
except RecursionError: # This error happens when pdoc is running
pass
client = Client(settings.RAVEN_CONFIG.get('dsn'))
# register a custom filter to filter out duplicate logs
register_logger_signal(client)
# hook into the Celery error handler
register_signal(client)
# pylint: disable=unused-argument
@celery.signals.setup_logging.connect
......@@ -45,7 +41,7 @@ def config_loggers(*args, **kwags):
def after_task_publish(sender=None, headers=None, body=None, **kwargs):
"""Log task_id after it was published"""
info = headers if 'task' in headers else body
LOGGER.debug('%-40s published (name=%s)', info.get('id'), info.get('task'))
LOGGER.debug('%-40s published (name=%s)', info.get('id', ''), info.get('task', ''))
# pylint: disable=unused-argument
......
"""pyazo view decorators"""
from functools import wraps
from django.shortcuts import redirect
from django.urls import reverse
def anonymous_required(view_function):
"""Decorator to only allow a view for anonymous users"""
@wraps(view_function)
def wrap(*args, **kwargs):
"""Check if request's user is authenticated and route back to index"""
req = args[0] if args else None
if req and req.user is not None and req.user.is_authenticated:
return redirect(reverse('common-index'))
requests = args[0] if args else None
if requests and requests.user is not None and requests.user.is_authenticated:
return redirect(reverse('index'))
return view_function(*args, **kwargs)
wrap.__doc__ = view_function.__doc__
wrap.__name__ = view_function.__name__
return wrap
......@@ -289,7 +289,10 @@ STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
THUMBNAIL_ROOT = os.path.join(BASE_DIR, 'media/thumbnail/')
if TEST:
LOGGING = None
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
}
CELERY_TASK_ALWAYS_EAGER = True
MEDIA_ROOT = os.path.join(BASE_DIR, 'media_test/')
THUMBNAIL_ROOT = os.path.join(BASE_DIR, 'media_test/thumbnail/')
......
......@@ -3,6 +3,8 @@ from django.contrib.auth.models import User
from django.shortcuts import reverse
from django.test import TestCase
from pyazo.tests.utils import test_auth
class AccountViewTests(TestCase):
"""Test views in views/accounts.py"""
......@@ -28,6 +30,12 @@ class AccountViewTests(TestCase):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
def test_login_auth(self):
"""Test login view while being authenticated"""
self.client.login(**test_auth())
response = self.client.get(self.url)
self.assertEqual(response.status_code, 302)
def test_login_post_valid(self):
"""Test valid login"""
response = self.client.post(self.url, self.valid_data)
......
......@@ -16,6 +16,12 @@ class CoreViewTests(TestCase):
response = self.client.get(reverse('index'))
self.assertEqual(response.status_code, 200)
def test_index_not_super(self):
"""Test default index page as a normal user"""
self.client.login(**test_auth(superuser=False))
response = self.client.get(reverse('index'))
self.assertEqual(response.status_code, 200)
def test_index_paginator(self):
"""Test invalid paginator page"""
self.client.login(**test_auth())
......
"""test tasks"""
import glob
import os
from django.conf import settings
from django.test import TestCase
from pyazo.models import Upload
from pyazo.tasks import make_thumbnail
class TaskTests(TestCase):
"""Test tasks"""
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')
def tearDown(self):
super().tearDown()
os.unlink(settings.MEDIA_ROOT + 'test2.txt')
# delete all files generated by uploads
for file in glob.glob(settings.MEDIA_ROOT + '*txt'):
os.remove(file)
def test_make_thumbnail(self):
"""test make_thumbnail"""
# pylint gets confused by celery tasks
make_thumbnail(self.upload.pk) # pylint: disable=no-value-for-parameter
"""test template tags commands"""
from django.shortcuts import reverse
from django.test import RequestFactory, TestCase
from pyazo.templatetags.pyazo import back
class TemplateTagTest(TestCase):
"""Test django template tags"""
def setUp(self):
super().setUp()
self.factory = RequestFactory()
def test_back(self):
"""Test back"""
initial_request = self.factory.get(reverse('index'))
self.assertEqual(back({'request': initial_request}), '')
get_back_request = self.factory.get(reverse('index') + '?back=external')
self.assertEqual(back({'request': get_back_request}), 'external')
meta_request = self.factory.get(reverse('index'))
meta_request.META['HTTP_REFERER'] = 'external'
self.assertEqual(back({'request': meta_request}), 'external')
"""test upload views"""
import glob
import os
from django.conf import settings
......@@ -25,6 +26,9 @@ class UploadViewTests(TestCase):
super().tearDown()
os.unlink(settings.MEDIA_ROOT + 'test2.txt')
os.unlink(settings.MEDIA_ROOT + 'test3.txt')
# delete all files generated by uploads
for file in glob.glob(settings.MEDIA_ROOT + '*txt'):
os.remove(file)
def test_upload_view(self):
"""Test UploadView"""
......
......@@ -4,6 +4,7 @@ from django.shortcuts import reverse
from django.test import TestCase
from django.test.client import RequestFactory
from pyazo.celery import after_task_publish, config_loggers
from pyazo.utils import get_remote_ip, get_reverse_dns
......@@ -23,3 +24,8 @@ class UtilsTest(TestCase):
def test_reverse_dns(self):
"""Test get_reverse_dns"""
self.assertEqual(get_reverse_dns('erqwer'), '')
def test_celery(self):
"""Test celery setup"""
config_loggers()
after_task_publish(headers={}, body={})
......@@ -159,6 +159,8 @@ class BrowserUploadView(LoginRequiredMixin, TemplateView):
"""Create Upload objects from request"""
for __, _file in request.FILES.items():
__, ext = os.path.splitext(_file.name)
# Remove leading dot from extension
ext = ext[1:] if ext.startswith('.') else ext
new_upload = Upload(
file=save_from_post(_file.read(), extension=ext),
user=request.user)
......
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