From c7ff74a5266a84dfdd61d22309ef0ad5c9eb4abb Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Mon, 11 Sep 2023 13:02:46 -0400 Subject: [PATCH] Moar unit tests --- app/api/tasks.py | 1 + app/templates/app/dashboard.html | 13 ++++------ app/templates/app/logged_in_base.html | 2 +- app/templates/app/quota.html | 11 +++++++++ app/templatetags/settings.py | 8 +++---- app/tests/test_api_admin.py | 2 ++ app/tests/test_login.py | 34 +++++++++++++++++++++++++++ app/tests/test_quota.py | 34 +++++++++++++++++++++++++-- 8 files changed, 89 insertions(+), 16 deletions(-) create mode 100644 app/templates/app/quota.html create mode 100644 app/tests/test_login.py diff --git a/app/api/tasks.py b/app/api/tasks.py index 4e4da2da..bb2d4a7c 100644 --- a/app/api/tasks.py +++ b/app/api/tasks.py @@ -184,6 +184,7 @@ class TaskViewSet(viewsets.ViewSet): if task.images_count < 1: raise exceptions.ValidationError(detail=_("You need to upload at least 1 file before commit")) + task.update_size() task.save() worker_tasks.process_task.delay(task.id) diff --git a/app/templates/app/dashboard.html b/app/templates/app/dashboard.html index 668af63e..344981ad 100644 --- a/app/templates/app/dashboard.html +++ b/app/templates/app/dashboard.html @@ -13,6 +13,8 @@ {% if no_processingnodes %} + {% include "quota.html" %} +

{% trans 'Welcome!' %} ☺

{% trans 'Add a Processing Node' as add_processing_node %} {% with nodeodm_link='NodeODM' api_link='API' %} @@ -39,15 +41,8 @@

{% endif %} - - {% if user.profile.has_exceeded_quota_cached %} - {% with total=user.profile.quota|disk_size used=user.profile.used_quota_cached|disk_size %} - {% quota_exceeded_grace_period as when %} -
- {% blocktrans %}The disk quota is being exceeded ({{ used }} of {{ total }} used). The most recent tasks will be automatically deleted {{ when }}, until usage falls below {{ total }}.{% endblocktrans %} -
- {% endwith %} - {% endif %} + + {% include "quota.html" %}
diff --git a/app/templates/app/logged_in_base.html b/app/templates/app/logged_in_base.html index 8ba8d36f..914cb4b0 100644 --- a/app/templates/app/logged_in_base.html +++ b/app/templates/app/logged_in_base.html @@ -19,7 +19,7 @@ {% if user.profile.has_quota %}
  • - + {% with tot_quota=user.profile.quota used_quota=user.profile.used_quota_cached %} {% percentage used_quota tot_quota as perc_quota %} {% percentage used_quota tot_quota 100 as bar_width %} diff --git a/app/templates/app/quota.html b/app/templates/app/quota.html new file mode 100644 index 00000000..c52df023 --- /dev/null +++ b/app/templates/app/quota.html @@ -0,0 +1,11 @@ +{% load i18n %} +{% load settings %} + +{% if user.profile.has_exceeded_quota_cached %} + {% with total=user.profile.quota|disk_size used=user.profile.used_quota_cached|disk_size %} + {% quota_exceeded_grace_period as when %} +
    + {% blocktrans %}The disk quota is being exceeded ({{ used }} of {{ total }} used). The most recent tasks will be automatically deleted {{ when }}, until usage falls below {{ total }}.{% endblocktrans %} +
    + {% endwith %} +{% endif %} \ No newline at end of file diff --git a/app/templatetags/settings.py b/app/templatetags/settings.py index 96efd0e2..b0962cbd 100644 --- a/app/templatetags/settings.py +++ b/app/templatetags/settings.py @@ -46,11 +46,11 @@ def quota_exceeded_grace_period(context): deadline = now + settings.QUOTA_EXCEEDED_GRACE_PERIOD * 60 * 60 diff = max(0, deadline - now) if diff >= 60*60*24*2: - return _("within %(num)s days") % {"num": math.ceil(diff / (60*60*24))} - elif diff >= 60*60: - return _("within %(num)s hours") % {"num": math.ceil(diff / (60*60))} + return _("in %(num)s days") % {"num": math.floor(diff / (60*60*24))} + elif diff >= 60*60*2: + return _("in %(num)s hours") % {"num": math.floor(diff / (60*60))} elif diff > 1: - return _("within %(num)s minutes") % {"num": math.ceil(diff / 60)} + return _("in %(num)s minutes") % {"num": math.floor(diff / 60)} else: return _("very soon") diff --git a/app/tests/test_api_admin.py b/app/tests/test_api_admin.py index 7a0d1f7f..a2c46b38 100644 --- a/app/tests/test_api_admin.py +++ b/app/tests/test_api_admin.py @@ -246,6 +246,8 @@ class TestApi(BootTestCase): # Update quota deadlines + self.assertTrue(user.profile.get_quota_deadline() is None) + # Miss parameters res = client.post('/api/admin/profiles/%s/update_quota_deadline/' % user.id) self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) diff --git a/app/tests/test_login.py b/app/tests/test_login.py new file mode 100644 index 00000000..57f25a15 --- /dev/null +++ b/app/tests/test_login.py @@ -0,0 +1,34 @@ +import os +from django.test import Client +from webodm import settings +from .classes import BootTestCase + +class TestLogin(BootTestCase): + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_reset_password_render(self): + c = Client() + c.login(username="testuser", password="test1234") + + settings.RESET_PASSWORD_LINK = '' + + res = c.get('/login/', follow=True) + body = res.content.decode("utf-8") + + # The reset password link should show instructions + self.assertTrue("You can reset the administrator password" in body) + + settings.RESET_PASSWORD_LINK = 'http://0.0.0.0/reset_test' + + res = c.get('/login/', follow=True) + body = res.content.decode("utf-8") + + # The reset password link should show instructions + self.assertTrue('