From 303b6a8adbe76211f850d1deacbe9a67ba864950 Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Fri, 6 Oct 2017 09:15:54 +0200 Subject: [PATCH] Added unit test for quota list. (#1260) * Added unit test for quota list. --- modoboa/admin/tests/test_account.py | 23 +++++++++++++ modoboa/admin/views/identity.py | 51 +++++++++++++++-------------- 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/modoboa/admin/tests/test_account.py b/modoboa/admin/tests/test_account.py index e1b94a6ea..a2cd9b300 100644 --- a/modoboa/admin/tests/test_account.py +++ b/modoboa/admin/tests/test_account.py @@ -414,6 +414,29 @@ class AccountTestCase(ModoTestCase): response = self.client.get(url) self.assertIn("Resources usage", response.content.decode()) + def test_quota_list_view(self): + """Test quota list view.""" + models.Quota.objects.filter(username="user@test.com").update( + bytes=5 * 1048576) + url = reverse("admin:quota_list") + response = self.ajax_get(url) + self.assertIn("5M", response["rows"]) + self.assertIn('title="50%"', response["rows"]) + self.assertIn("user@test.com", response["rows"]) + old_rows = response["rows"] + + response = self.ajax_get( + "{}?sort_order=-quota_value__bytes".format(url)) + self.assertNotEqual(old_rows, response["rows"]) + old_rows = response["rows"] + + response = self.ajax_get( + "{}?sort_order=-quota_usage".format(url)) + self.assertEqual(old_rows, response["rows"]) + + response = self.ajax_get( + "{}?sort_order=-unknown".format(url), status=400) + @skipIf(NO_LDAP, "No ldap module installed") @override_settings(AUTHENTICATION_BACKENDS=( diff --git a/modoboa/admin/views/identity.py b/modoboa/admin/views/identity.py index 0c0086741..9d31f3def 100644 --- a/modoboa/admin/views/identity.py +++ b/modoboa/admin/views/identity.py @@ -79,35 +79,36 @@ def list_quotas(request): mboxes = mboxes.exclude(quota=0) if sort_order in ["address", "quota"]: mboxes = mboxes.order_by("%s%s" % (sort_dir, sort_order)) - elif sort_order == "quota_value__bytes": - where = "admin_mailbox.address||'@'||admin_domain.name" - mboxes = mboxes.extra( - select={"quota_value__bytes": "admin_quota.bytes"}, - where=["admin_quota.username=%s" % where], - tables=["admin_quota", "admin_domain"], - order_by=["%s%s" % (sort_dir, sort_order)] - ) - elif sort_order == "quota_usage": - where = "admin_mailbox.address||'@'||admin_domain.name" + elif sort_order in ("quota_value__bytes", "quota_usage"): db_type = db_type() - if db_type == "postgres": - select = ( - "(admin_quota.bytes::float / (CAST(admin_mailbox.quota " - "AS BIGINT) * 1048576)) * 100" + if db_type == "mysql": + where = "CONCAT(admin_mailbox.address,'@',admin_domain.name)" + else: + where = "admin_mailbox.address||'@'||admin_domain.name" + if sort_order == "quota_value__bytes": + mboxes = mboxes.extra( + select={"quota_value__bytes": "admin_quota.bytes"}, + where=["admin_quota.username=%s" % where], + tables=["admin_quota", "admin_domain"], + order_by=["%s%s" % (sort_dir, sort_order)] ) else: - select = ( - "admin_quota.bytes / (admin_mailbox.quota " - "* 1048576) * 100" + if db_type == "postgres": + select = ( + "(admin_quota.bytes::float / (CAST(admin_mailbox.quota " + "AS BIGINT) * 1048576)) * 100" + ) + else: + select = ( + "(admin_quota.bytes * 1.0 / (admin_mailbox.quota " + "* 1048576)) * 100" + ) + mboxes = mboxes.extra( + select={'quota_usage': select}, + where=["admin_quota.username=%s" % where], + tables=["admin_quota", "admin_domain"], + order_by=["%s%s" % (sort_dir, sort_order)] ) - if db_type == "mysql": - where = "CONCAT(admin_mailbox.address,'@',admin_domain.name)" - mboxes = mboxes.extra( - select={'quota_usage': select}, - where=["admin_quota.username=%s" % where], - tables=["admin_quota", "admin_domain"], - order_by=["%s%s" % (sort_dir, sort_order)] - ) else: raise BadRequest(_("Invalid request")) page = get_listing_page(mboxes, request.GET.get("page", 1))