1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00

feat(admin): the user list shows how many API tokens each user has

Which users can reach the API with a token was seen only by opening each
user. The user list now has an API tokens column: the number of the
user's tokens that still work (expired ones are left out; they are
deleted only at the user's next token), linked to the user's token tab.
One query counts them for all users.
This commit is contained in:
catborise 2026-10-25 20:38:05 +03:00
parent f004552148
commit 8063dcec66
3 changed files with 23 additions and 1 deletions

View file

@ -12,6 +12,7 @@ from django.test import TestCase
from django.urls import reverse
from django.utils import timezone
from logs.models import Logs
from lxml import html
class ApiTokenTests(TestCase):
@ -176,3 +177,18 @@ class AdminApiTokenTests(TestCase):
self.client.force_login(self.admin)
self.assertEqual(self.client.post(self.delete_url(other)).status_code, 404)
self.assertTrue(ApiToken.objects.exists())
def test_the_user_list_counts_each_users_live_tokens(self):
self.client.force_login(self.user)
self.client.post(reverse("accounts:api_token_create"), {"name": "old", "days": "30"})
ApiToken.objects.filter(name="old").update(expiry=timezone.now() - timedelta(days=1))
self.client.force_login(self.admin)
page = html.fromstring(self.client.get(reverse("admin:user_list")).content)
headers = [th.text_content().strip() for th in page.xpath("//thead//th")]
column = headers.index("API tokens")
cells = {row.xpath("td")[0].text_content().strip(): row.xpath("td")[column] for row in page.xpath("//tbody/tr")}
# the expired one is not counted; the count opens the user's tokens
self.assertEqual(cells["tok-user"].text_content().strip(), "1")
self.assertEqual(cells["tok-user"].xpath(".//a/@href"),
[reverse("accounts:account", args=[self.user.id]) + "#apitokens"])
self.assertEqual(cells["tok-admin"].text_content().strip(), "")