1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 11:16:47 +00:00
webvirtcloud/logs/test_logs.py
catborise c2fea2aa39 feat(accounts): failed logins lock a user for the address they come from
Nothing limited failed logins: a password could be guessed at the speed
the server answers. django-axes (8.3.1, Django 5.2, Python 3.10-3.13)
now counts them.

- A user is locked for the address the failed logins come from
  (username and IP together), so an attacker cannot lock someone out
  from elsewhere and users behind one NAT address do not share a lock.
  A successful login resets the count. axes sits in front of every
  authentication: the login page with or without OTP, LDAP and the API.
- The number of failed logins (default 5) and the lock time in minutes
  (default 15) are on the Settings page (LOGIN_FAILURE_LIMIT,
  LOGIN_LOCKOUT_MINUTES, added by migration appsettings.0014) and apply
  at once; an invalid value falls back to the default. Unlock with
  "manage.py axes_reset_username <username>".
- Behind the bundled nginx every request comes from 127.0.0.1; the
  client address is read from X-Real-IP, which nginx sets, but only for
  requests from a loopback address or from LOGIN_TRUSTED_PROXIES, so a
  forged header does not escape a lock.

settings.py.template gets the app, middleware, backend and AXES_*
settings; README tells existing installations what to copy. Tests that
call client.login now pass a request, which the axes backend needs.
The new tests fail with axes disabled.
2026-10-06 11:05:03 +03:00

61 lines
2.3 KiB
Python

import json
from django.contrib.auth import get_user_model
from django.test import Client, RequestFactory, TestCase
from django.urls import reverse
from computes.models import Compute
from instances.models import Instance
from logs.models import Logs
from logs.views import addlogmsg
class LogsTestCase(TestCase):
def setUp(self):
User = get_user_model()
self.superuser = User.objects.create_superuser(
username="admin_logs", email="admin@example.com", password="adminpassword"
)
self.normal_user = User.objects.create_user(
username="normal_logs", email="user@example.com", password="userpassword"
)
self.compute = Compute.objects.create(
name="local_compute",
hostname="localhost",
type=1,
)
self.instance = Instance.objects.create(
compute=self.compute,
name="test-vm",
uuid="12345678-1234-1234-1234-123456789012",
)
def test_addlogmsg(self):
addlogmsg("admin", "localhost", "test-vm", "Started VM")
log = Logs.objects.filter(instance="test-vm").first()
self.assertIsNotNone(log)
self.assertEqual(log.user, "admin")
self.assertEqual(log.host, "localhost")
self.assertEqual(log.message, "Started VM")
def test_vm_logs_superuser_access(self):
addlogmsg("admin", "localhost", "test-vm", "Created VM")
client = Client()
client.login(request=RequestFactory().get("/"), username="admin_logs", password="adminpassword")
response = client.get(reverse("vm_logs", args=["test-vm"]))
self.assertEqual(response.status_code, 200)
data = json.loads(response.content.decode("utf-8"))
self.assertIsInstance(data, list)
self.assertEqual(len(data), 1)
self.assertEqual(data[0]["user"], "admin")
self.assertEqual(data[0]["message"], "Created VM")
self.assertEqual(data[0]["instance"], "test-vm")
def test_vm_logs_non_superuser_forbidden(self):
client = Client()
client.login(request=RequestFactory().get("/"), username="normal_logs", password="userpassword")
response = client.get(reverse("vm_logs", args=["test-vm"]))
# superuser_only decorator redirects non-superusers to index or login
self.assertNotEqual(response.status_code, 200)