Skip to content

Commit

Permalink
Moar unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pierotofy committed Sep 11, 2023
1 parent a709c8f commit c7ff74a
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 16 deletions.
1 change: 1 addition & 0 deletions app/api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ def commit(self, request, pk=None, project_pk=None):
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)

Expand Down
13 changes: 4 additions & 9 deletions app/templates/app/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<![endif]-->

{% if no_processingnodes %}
{% include "quota.html" %}

<h3>{% trans 'Welcome!' %} ☺</h3>
{% trans 'Add a Processing Node' as add_processing_node %}
{% with nodeodm_link='<a href="https://github.com/OpenDroneMap/NodeODM" target="_blank">NodeODM</a>' api_link='<a href="https://github.com/OpenDroneMap/NodeODM/blob/master/docs/index.adoc" target="_blank">API</a>' %}
Expand All @@ -39,15 +41,8 @@ <h3>{% trans 'Welcome!' %} ☺</h3>
</ul>
</p>
{% 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 %}
<div class="alert alert-warning alert-dismissible">
<i class="fas fa-info-circle"></i> {% 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 %}
</div>
{% endwith %}
{% endif %}

{% include "quota.html" %}

<div id="dashboard-app" data-dashboard></div>

Expand Down
2 changes: 1 addition & 1 deletion app/templates/app/logged_in_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</li>
{% if user.profile.has_quota %}
<li class="divider"></li>

{% 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 %}
Expand Down
11 changes: 11 additions & 0 deletions app/templates/app/quota.html
Original file line number Diff line number Diff line change
@@ -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 %}
<div class="alert alert-warning alert-dismissible">
<i class="fas fa-info-circle"></i> {% 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 %}
</div>
{% endwith %}
{% endif %}
8 changes: 4 additions & 4 deletions app/templatetags/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 2 additions & 0 deletions app/tests/test_api_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ def test_profile(self):

# 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)
Expand Down
34 changes: 34 additions & 0 deletions app/tests/test_login.py
Original file line number Diff line number Diff line change
@@ -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('<a href="http://0.0.0.0/reset_test' in body)


34 changes: 32 additions & 2 deletions app/tests/test_quota.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from rest_framework import status
from rest_framework.test import APIClient
from app.models import Task, Project
from nodeodm.models import ProcessingNode
from worker.tasks import check_quotas
from .classes import BootTestCase

class TestQuota(BootTestCase):
Expand Down Expand Up @@ -43,7 +45,9 @@ def test_quota(self):
# Create a task with size
p = Project.objects.create(owner=user, name='Test')
p.save()
t = Task.objects.create(project=p, name='Test', size=2005)
t = Task.objects.create(project=p, name='Test', size=1005)
t.save()
t = Task.objects.create(project=p, name='Test2', size=1010)
t.save()

# Simulate call to task.update_size which calls clear_used_quota_cache
Expand All @@ -55,4 +59,30 @@ def test_quota(self):
res = c.get('/dashboard/', follow=True)
body = res.content.decode("utf-8")

# self.assertTrue("disk quota is being exceeded" in body)
self.assertTrue("disk quota is being exceeded" in body)
self.assertTrue("in 8 hours" in body)

# Running the workers check_quota function will not remove tasks
check_quotas()
self.assertEqual(len(Task.objects.filter(project__owner=user)), 2)

# Update grace period
def check_quota_warning(hours, text):
user.profile.set_quota_deadline(hours)
res = c.get('/dashboard/', follow=True)
body = res.content.decode("utf-8")
self.assertTrue(text in body)

check_quota_warning(73, "in 3 days")
check_quota_warning(71, "in 2 days")
check_quota_warning(47.9, "in 47 hours")
check_quota_warning(3.1, "in 3 hours")
check_quota_warning(1.51, "in 90 minutes")
check_quota_warning(0.99, "in 59 minutes")
check_quota_warning(0, "very soon")

# Running the check_quotas function should remove the last task only
check_quotas()
tasks = Task.objects.filter(project__owner=user)
self.assertEqual(len(tasks), 1)
self.assertEqual(tasks[0].name, "Test")

0 comments on commit c7ff74a

Please sign in to comment.