mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-10-17 05:41:25 +02:00
Added unit test for quota list. (#1260)
* Added unit test for quota list.
This commit is contained in:
@@ -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=(
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user