2026-03-10 20:30:54 -04:00
|
|
|
import json
|
2026-11-24 20:11:34 -03:00
|
|
|
import shutil
|
|
|
|
|
import subprocess
|
|
|
|
|
import tempfile
|
|
|
|
|
import time
|
|
|
|
|
import uuid
|
|
|
|
|
from datetime import datetime, timedelta, timezone as dt_timezone
|
2026-03-10 20:30:54 -04:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
2026-11-23 14:29:26 -03:00
|
|
|
from django.test import TestCase, override_settings
|
2026-11-24 11:23:47 -03:00
|
|
|
from django.utils import timezone
|
2026-03-10 20:30:54 -04:00
|
|
|
|
|
|
|
|
from api_v2.models import ApiKey
|
2026-11-23 14:29:26 -03:00
|
|
|
from cluster.models import ClusterSettings, Worker
|
|
|
|
|
from cluster.tests import create_instance
|
2026-03-10 20:30:54 -04:00
|
|
|
from dns.models import DNSSettings, StaticHost
|
2026-11-24 20:11:34 -03:00
|
|
|
from vpn_invite.models import InviteSettings, PeerInvite
|
|
|
|
|
from wgrrd.functions import peer_rrd_path
|
2026-04-08 19:34:21 +02:00
|
|
|
from wireguard.models import Peer, WireGuardInstance
|
2026-03-10 20:30:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class ApiV2ManageDnsRecordTests(TestCase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.api_key = ApiKey.objects.create(name="dns-test-key", enabled=True)
|
|
|
|
|
self.url = "/api/v2/manage_dns_record/"
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _fake_export_dns_configuration():
|
|
|
|
|
dns_settings, dns_settings_created = DNSSettings.objects.get_or_create(name="dns_settings")
|
|
|
|
|
dns_settings.pending_changes = False
|
|
|
|
|
dns_settings.save(update_fields=["pending_changes", "updated"])
|
|
|
|
|
|
|
|
|
|
@patch("api_v2.views_api.export_dns_configuration")
|
|
|
|
|
def test_post_creates_record(self, mock_export):
|
|
|
|
|
response = self.client.post(
|
|
|
|
|
self.url,
|
|
|
|
|
data=json.dumps({
|
|
|
|
|
"hostname": "App.Example.com",
|
|
|
|
|
"ip_address": "10.20.30.40",
|
|
|
|
|
"skip_reload": True,
|
|
|
|
|
}),
|
|
|
|
|
content_type="application/json",
|
|
|
|
|
HTTP_TOKEN=str(self.api_key.token),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 201)
|
|
|
|
|
body = response.json()
|
|
|
|
|
self.assertEqual(body["hostname"], "app.example.com")
|
|
|
|
|
self.assertEqual(body["ip_address"], "10.20.30.40")
|
|
|
|
|
mock_export.assert_not_called()
|
|
|
|
|
self.assertTrue(StaticHost.objects.filter(hostname="app.example.com").exists())
|
|
|
|
|
|
|
|
|
|
@patch("api_v2.views_api.export_dns_configuration")
|
|
|
|
|
def test_post_fails_if_record_exists(self, mock_export):
|
|
|
|
|
StaticHost.objects.create(hostname="app.example.com", ip_address="10.20.30.40")
|
|
|
|
|
|
|
|
|
|
response = self.client.post(
|
|
|
|
|
self.url,
|
|
|
|
|
data=json.dumps({
|
|
|
|
|
"hostname": "app.example.com",
|
|
|
|
|
"ip_address": "10.20.30.99",
|
|
|
|
|
"skip_reload": False,
|
|
|
|
|
}),
|
|
|
|
|
content_type="application/json",
|
|
|
|
|
HTTP_TOKEN=str(self.api_key.token),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
|
|
self.assertIn("already exists", response.json()["error_message"])
|
|
|
|
|
self.assertEqual(str(StaticHost.objects.get(hostname="app.example.com").ip_address), "10.20.30.40")
|
|
|
|
|
mock_export.assert_not_called()
|
|
|
|
|
|
|
|
|
|
@patch("api_v2.views_api.export_dns_configuration")
|
|
|
|
|
def test_put_upserts_existing_record(self, mock_export):
|
|
|
|
|
mock_export.side_effect = self._fake_export_dns_configuration
|
|
|
|
|
StaticHost.objects.create(hostname="app.example.com", ip_address="10.20.30.40")
|
|
|
|
|
DNSSettings.objects.create(name="dns_settings", pending_changes=True)
|
|
|
|
|
|
2026-11-16 14:21:37 -03:00
|
|
|
with self.captureOnCommitCallbacks(execute=True):
|
|
|
|
|
response = self.client.put(
|
|
|
|
|
self.url,
|
|
|
|
|
data=json.dumps({
|
|
|
|
|
"hostname": "app.example.com",
|
|
|
|
|
"ip_address": "10.20.30.41",
|
|
|
|
|
"skip_reload": False,
|
|
|
|
|
}),
|
|
|
|
|
content_type="application/json",
|
|
|
|
|
HTTP_TOKEN=str(self.api_key.token),
|
|
|
|
|
)
|
2026-03-10 20:30:54 -04:00
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
self.assertEqual(str(StaticHost.objects.get(hostname="app.example.com").ip_address), "10.20.30.41")
|
|
|
|
|
self.assertEqual(StaticHost.objects.filter(hostname="app.example.com").count(), 1)
|
|
|
|
|
self.assertFalse(DNSSettings.objects.get(name="dns_settings").pending_changes)
|
|
|
|
|
mock_export.assert_called_once()
|
|
|
|
|
|
|
|
|
|
@patch("api_v2.views_api.export_dns_configuration")
|
|
|
|
|
def test_put_upserts_missing_record_as_create(self, mock_export):
|
|
|
|
|
mock_export.side_effect = self._fake_export_dns_configuration
|
|
|
|
|
|
2026-11-16 14:21:37 -03:00
|
|
|
with self.captureOnCommitCallbacks(execute=True):
|
|
|
|
|
response = self.client.put(
|
|
|
|
|
self.url,
|
|
|
|
|
data=json.dumps({
|
|
|
|
|
"hostname": "new.example.com",
|
|
|
|
|
"ip_address": "10.20.30.50",
|
|
|
|
|
"skip_reload": False,
|
|
|
|
|
"create_if_missing": True,
|
|
|
|
|
}),
|
|
|
|
|
content_type="application/json",
|
|
|
|
|
HTTP_TOKEN=str(self.api_key.token),
|
|
|
|
|
)
|
2026-03-10 20:30:54 -04:00
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 201)
|
|
|
|
|
self.assertTrue(StaticHost.objects.filter(hostname="new.example.com").exists())
|
|
|
|
|
mock_export.assert_called_once()
|
|
|
|
|
|
2026-11-16 14:21:37 -03:00
|
|
|
@patch("api_v2.views_api.export_dns_configuration")
|
|
|
|
|
def test_put_missing_record_without_create_if_missing_returns_404(self, mock_export):
|
|
|
|
|
with self.captureOnCommitCallbacks(execute=True):
|
|
|
|
|
response = self.client.put(
|
|
|
|
|
self.url,
|
|
|
|
|
data=json.dumps({
|
|
|
|
|
"hostname": "new.example.com",
|
|
|
|
|
"ip_address": "10.20.30.50",
|
|
|
|
|
"skip_reload": False,
|
|
|
|
|
}),
|
|
|
|
|
content_type="application/json",
|
|
|
|
|
HTTP_TOKEN=str(self.api_key.token),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
self.assertFalse(StaticHost.objects.filter(hostname="new.example.com").exists())
|
|
|
|
|
mock_export.assert_not_called()
|
|
|
|
|
|
2026-03-10 20:30:54 -04:00
|
|
|
@patch("api_v2.views_api.export_dns_configuration")
|
|
|
|
|
def test_delete_deletes_record_and_does_not_require_ip(self, mock_export):
|
|
|
|
|
StaticHost.objects.create(hostname="del.example.com", ip_address="10.1.2.3")
|
|
|
|
|
|
|
|
|
|
response = self.client.delete(
|
|
|
|
|
self.url,
|
|
|
|
|
data=json.dumps({
|
|
|
|
|
"hostname": "del.example.com",
|
|
|
|
|
"skip_reload": True,
|
|
|
|
|
}),
|
|
|
|
|
content_type="application/json",
|
|
|
|
|
HTTP_TOKEN=str(self.api_key.token),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
self.assertFalse(StaticHost.objects.filter(hostname="del.example.com").exists())
|
|
|
|
|
mock_export.assert_not_called()
|
2026-04-08 19:34:21 +02:00
|
|
|
|
2026-11-23 14:29:26 -03:00
|
|
|
|
|
|
|
|
@override_settings(WIREGUARD_STATUS_CACHE_ENABLED=True, CADDY_ENABLED=True)
|
|
|
|
|
class ApiV2ManageClusterTests(TestCase):
|
|
|
|
|
status_url = "/api/v2/manage_cluster/status/"
|
|
|
|
|
manage_url = "/api/v2/manage_cluster/manage_status/"
|
|
|
|
|
|
2026-04-08 19:34:21 +02:00
|
|
|
def setUp(self):
|
2026-11-23 14:29:26 -03:00
|
|
|
ClusterSettings.objects.update_or_create(name="cluster_settings", defaults={"enabled": True})
|
|
|
|
|
self.api_key = ApiKey.objects.create(name="cluster-key", manage_cluster=True)
|
|
|
|
|
self.node_a = Worker.objects.create(name="node-a")
|
|
|
|
|
self.node_b = Worker.objects.create(name="node-b", enabled=False)
|
|
|
|
|
self.instance = create_instance(0)
|
|
|
|
|
WireGuardInstance.objects.update(pending_changes=False)
|
2026-04-08 19:34:21 +02:00
|
|
|
|
2026-11-23 14:29:26 -03:00
|
|
|
self.calls = []
|
|
|
|
|
self.stop_errors = []
|
|
|
|
|
for name, value in (
|
|
|
|
|
("running_wireguard_interfaces", lambda: ["wg0"]),
|
|
|
|
|
("start_primary_interfaces", self.record("start")),
|
|
|
|
|
("stop_primary_interfaces", self.record("stop")),
|
|
|
|
|
("apply_caddy_config", self.record("export")),
|
|
|
|
|
):
|
|
|
|
|
patcher = patch(f"api_v2.views_api.{name}", value)
|
|
|
|
|
patcher.start()
|
|
|
|
|
self.addCleanup(patcher.stop)
|
|
|
|
|
|
|
|
|
|
def record(self, name):
|
|
|
|
|
def call():
|
|
|
|
|
self.calls.append(name)
|
|
|
|
|
return list(self.stop_errors) if name == "stop" else []
|
|
|
|
|
return call
|
|
|
|
|
|
|
|
|
|
def manage(self, payload, api_key=None, method="put"):
|
|
|
|
|
return getattr(self.client, method)(
|
|
|
|
|
self.manage_url, data=json.dumps(payload), content_type="application/json",
|
|
|
|
|
HTTP_TOKEN=str((api_key or self.api_key).token),
|
2026-04-08 19:34:21 +02:00
|
|
|
)
|
|
|
|
|
|
2026-11-23 14:29:26 -03:00
|
|
|
def assert_unchanged(self):
|
|
|
|
|
self.assertTrue(Worker.objects.get(pk=self.node_a.pk).enabled)
|
|
|
|
|
self.assertFalse(Worker.objects.get(pk=self.node_b.pk).enabled)
|
|
|
|
|
self.assertTrue(ClusterSettings.objects.get(name="cluster_settings").primary_enable_wireguard)
|
|
|
|
|
self.assertEqual(self.calls, [])
|
2026-04-08 19:34:21 +02:00
|
|
|
|
2026-11-23 14:29:26 -03:00
|
|
|
def test_api_key_needs_manage_cluster(self):
|
|
|
|
|
plain_key = ApiKey.objects.create(name="plain-key")
|
|
|
|
|
self.assertEqual(self.client.get(self.status_url).status_code, 403)
|
|
|
|
|
self.assertEqual(self.client.get(self.status_url, HTTP_TOKEN=str(plain_key.token)).status_code, 403)
|
|
|
|
|
response = self.manage({"workers": {str(self.node_a.uuid): False}}, api_key=plain_key)
|
|
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
self.assertIn("not allowed to manage the cluster", response.json()["error_message"])
|
|
|
|
|
self.assert_unchanged()
|
2026-04-08 19:34:21 +02:00
|
|
|
|
2026-11-23 14:29:26 -03:00
|
|
|
def test_status_lists_cluster_and_workers_by_uuid(self):
|
|
|
|
|
response = self.client.get(self.status_url, HTTP_TOKEN=str(self.api_key.token))
|
2026-04-08 19:34:21 +02:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
body = response.json()
|
2026-11-23 14:29:26 -03:00
|
|
|
self.assertEqual(body["cluster"], {
|
|
|
|
|
"enabled": True, "mode": "mirror", "primary_wireguard": True, "primary_running_interfaces": ["wg0"],
|
|
|
|
|
})
|
|
|
|
|
self.assertEqual(body["workers"][str(self.node_a.uuid)]["name"], "node-a")
|
|
|
|
|
self.assertFalse(body["workers"][str(self.node_b.uuid)]["enabled"])
|
|
|
|
|
self.assertIsNone(body["workers"][str(self.node_b.uuid)]["sync_state"])
|
2026-04-08 19:34:21 +02:00
|
|
|
|
2026-11-23 14:29:26 -03:00
|
|
|
def test_workers_and_primary_change_in_a_single_request(self):
|
|
|
|
|
response = self.manage({
|
|
|
|
|
"workers": {str(self.node_a.uuid): False, str(self.node_b.uuid): True}, "primary_wireguard": False,
|
|
|
|
|
})
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
body = response.json()
|
|
|
|
|
self.assertEqual(body["workers"][str(self.node_a.uuid)], {"name": "node-a", "enabled": False, "changed": True})
|
|
|
|
|
self.assertEqual(body["workers"][str(self.node_b.uuid)], {"name": "node-b", "enabled": True, "changed": True})
|
|
|
|
|
self.assertEqual(body["primary_wireguard"], {"enabled": False, "changed": True})
|
|
|
|
|
self.assertTrue(body["apply"]["success"])
|
|
|
|
|
self.assertFalse(Worker.objects.get(pk=self.node_a.pk).enabled)
|
|
|
|
|
self.assertTrue(Worker.objects.get(pk=self.node_b.pk).enabled)
|
|
|
|
|
self.assertFalse(ClusterSettings.primary_serves_clients())
|
|
|
|
|
self.assertEqual(self.calls, ["export", "stop"])
|
|
|
|
|
self.instance.refresh_from_db()
|
|
|
|
|
self.assertFalse(self.instance.pending_changes)
|
|
|
|
|
|
|
|
|
|
# The same request again changes nothing
|
|
|
|
|
self.calls.clear()
|
|
|
|
|
body = self.manage({"workers": {str(self.node_a.uuid): False}, "primary_wireguard": False}).json()
|
|
|
|
|
self.assertFalse(body["workers"][str(self.node_a.uuid)]["changed"])
|
|
|
|
|
self.assertFalse(body["primary_wireguard"]["changed"])
|
|
|
|
|
self.assertEqual(self.calls, [])
|
|
|
|
|
|
|
|
|
|
def test_enabling_the_primary_starts_its_interfaces(self):
|
|
|
|
|
ClusterSettings.objects.filter(name="cluster_settings").update(primary_enable_wireguard=False)
|
|
|
|
|
response = self.manage({"primary_wireguard": True}, method="post")
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
self.assertEqual(self.calls, ["export", "start"])
|
|
|
|
|
self.assertTrue(ClusterSettings.primary_serves_clients())
|
|
|
|
|
|
|
|
|
|
def test_skip_reload_only_marks_the_instances_pending(self):
|
|
|
|
|
response = self.manage({"primary_wireguard": False, "skip_reload": True})
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
self.assertEqual(self.calls, [])
|
|
|
|
|
self.instance.refresh_from_db()
|
|
|
|
|
self.assertTrue(self.instance.pending_changes)
|
|
|
|
|
|
|
|
|
|
def test_worker_changes_do_not_touch_the_primary_interfaces(self):
|
|
|
|
|
response = self.manage({"workers": {str(self.node_a.uuid): False}})
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
self.assertNotIn("primary_wireguard", response.json())
|
|
|
|
|
self.assertEqual(self.calls, [])
|
|
|
|
|
|
|
|
|
|
def test_invalid_requests_change_nothing(self):
|
|
|
|
|
for payload in (
|
|
|
|
|
{},
|
|
|
|
|
{"workers": {}},
|
|
|
|
|
{"workers": {"00000000-0000-0000-0000-000000000000": False}, "primary_wireguard": False},
|
|
|
|
|
{"workers": {"node-a": False}},
|
|
|
|
|
{"workers": {str(self.node_a.uuid): "no"}},
|
|
|
|
|
{"workers": {str(self.node_b.uuid): True}, "primary_wireguard": 0},
|
|
|
|
|
):
|
|
|
|
|
response = self.manage(payload)
|
|
|
|
|
self.assertEqual(response.status_code, 400, payload)
|
|
|
|
|
self.assert_unchanged()
|
|
|
|
|
|
|
|
|
|
def test_primary_follows_the_cluster_settings_form_rules(self):
|
|
|
|
|
ClusterSettings.objects.filter(name="cluster_settings").update(cluster_mode="distributed")
|
|
|
|
|
response = self.manage({"workers": {str(self.node_a.uuid): False}, "primary_wireguard": False})
|
|
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
|
|
self.assertIn("only be disabled in mirror mode", response.json()["error_message"])
|
|
|
|
|
self.assert_unchanged()
|
|
|
|
|
|
|
|
|
|
def test_changes_require_an_enabled_cluster(self):
|
|
|
|
|
ClusterSettings.objects.filter(name="cluster_settings").update(enabled=False)
|
|
|
|
|
response = self.manage({"workers": {str(self.node_a.uuid): False}})
|
|
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
|
|
self.assertEqual(response.json()["error_message"], "The cluster is not enabled.")
|
|
|
|
|
self.assert_unchanged()
|
|
|
|
|
|
|
|
|
|
def test_apply_failure_keeps_the_saved_changes_pending(self):
|
|
|
|
|
self.stop_errors = [("wg0", "device busy")]
|
|
|
|
|
response = self.manage({"primary_wireguard": False})
|
|
|
|
|
self.assertEqual(response.status_code, 500)
|
|
|
|
|
self.assertIn("saved but apply failed: wg0: device busy", response.json()["error_message"])
|
|
|
|
|
self.assertFalse(ClusterSettings.objects.get(name="cluster_settings").primary_enable_wireguard)
|
|
|
|
|
self.instance.refresh_from_db()
|
|
|
|
|
self.assertTrue(self.instance.pending_changes)
|
|
|
|
|
|
|
|
|
|
def test_other_methods_are_not_allowed(self):
|
|
|
|
|
response = self.client.delete(self.manage_url, HTTP_TOKEN=str(self.api_key.token))
|
|
|
|
|
self.assertEqual(response.status_code, 405)
|
|
|
|
|
response = self.client.put(self.status_url, HTTP_TOKEN=str(self.api_key.token))
|
|
|
|
|
self.assertEqual(response.status_code, 405)
|
2026-11-24 11:23:47 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class ApiV2PeerInviteTests(TestCase):
|
|
|
|
|
url = "/api/v2/peer_invite/"
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.instance = create_instance(0)
|
|
|
|
|
self.other_instance = create_instance(1)
|
|
|
|
|
self.api_key = ApiKey.objects.create(name="invite-test-key", enabled=True)
|
|
|
|
|
self.api_key.allowed_instances.add(self.instance)
|
|
|
|
|
self.peer = Peer.objects.create(
|
|
|
|
|
wireguard_instance=self.instance, name="Test Peer", public_key="peer-public", private_key="peer-private",
|
|
|
|
|
)
|
|
|
|
|
self.invite_settings = InviteSettings.objects.create(
|
|
|
|
|
name="default_settings", invite_url="https://vpn.example.com/invite/", invite_expiration=30,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def request(self, payload, api_key=None):
|
|
|
|
|
return self.client.post(
|
|
|
|
|
self.url, data=json.dumps(payload), content_type="application/json",
|
|
|
|
|
HTTP_TOKEN=str((api_key or self.api_key).token),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_creates_invite(self):
|
|
|
|
|
response = self.request({"instance": "wg0", "peer_uuid": str(self.peer.uuid)})
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
body = response.json()
|
|
|
|
|
self.assertEqual(body["message"], "Invite created.")
|
|
|
|
|
invite = PeerInvite.objects.get(peer=self.peer)
|
|
|
|
|
self.assertEqual(body["invite_data"]["uuid"], str(invite.uuid))
|
|
|
|
|
self.assertEqual(body["invite_data"]["url"], f"https://vpn.example.com/invite/?token={invite.uuid}&")
|
|
|
|
|
self.assertEqual(body["invite_data"]["password"], invite.invite_password)
|
|
|
|
|
|
|
|
|
|
def test_selects_peer_by_public_key(self):
|
|
|
|
|
response = self.request({"instance": "wg0", "peer_public_key": "peer-public"})
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2026-04-08 19:34:21 +02:00
|
|
|
self.assertTrue(PeerInvite.objects.filter(peer=self.peer).exists())
|
|
|
|
|
|
2026-11-24 11:23:47 -03:00
|
|
|
def test_existing_invite_is_renewed(self):
|
|
|
|
|
self.request({"instance": "wg0", "peer_uuid": str(self.peer.uuid)})
|
|
|
|
|
invite = PeerInvite.objects.get(peer=self.peer)
|
|
|
|
|
PeerInvite.objects.filter(pk=invite.pk).update(
|
|
|
|
|
invite_expiration=invite.invite_expiration - timedelta(minutes=20)
|
2026-04-08 19:34:21 +02:00
|
|
|
)
|
2026-11-24 11:23:47 -03:00
|
|
|
response = self.request({"instance": "wg0", "peer_uuid": str(self.peer.uuid)})
|
2026-04-08 19:34:21 +02:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2026-11-24 11:23:47 -03:00
|
|
|
self.assertEqual(response.json()["message"], "Existing invite renewed.")
|
|
|
|
|
renewed = PeerInvite.objects.get(peer=self.peer)
|
|
|
|
|
self.assertEqual(renewed.uuid, invite.uuid)
|
|
|
|
|
self.assertGreater(renewed.invite_expiration, timezone.now() + timedelta(minutes=25))
|
2026-04-08 19:34:21 +02:00
|
|
|
|
2026-11-24 11:23:47 -03:00
|
|
|
def test_missing_peer_selector(self):
|
|
|
|
|
response = self.request({"instance": "wg0"})
|
2026-04-08 19:34:21 +02:00
|
|
|
self.assertEqual(response.status_code, 400)
|
2026-11-24 11:23:47 -03:00
|
|
|
self.assertEqual(response.json()["error_message"], "Missing peer selector (peer_uuid or peer_public_key).")
|
2026-04-08 19:34:21 +02:00
|
|
|
|
2026-11-24 11:23:47 -03:00
|
|
|
def test_unknown_peer(self):
|
|
|
|
|
for peer_uuid in ("00000000-0000-0000-0000-000000000000", "not-a-uuid"):
|
|
|
|
|
response = self.request({"instance": "wg0", "peer_uuid": peer_uuid})
|
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
self.assertEqual(response.json()["error_message"], "Peer not found.")
|
2026-04-08 19:34:21 +02:00
|
|
|
|
2026-11-24 11:23:47 -03:00
|
|
|
def test_invalid_instance(self):
|
|
|
|
|
response = self.request({"instance": "wg999", "peer_uuid": str(self.peer.uuid)})
|
2026-04-08 19:34:21 +02:00
|
|
|
self.assertEqual(response.status_code, 400)
|
2026-11-24 11:23:47 -03:00
|
|
|
self.assertEqual(response.json()["error_message"], "Invalid or missing WireGuard instance.")
|
|
|
|
|
|
|
|
|
|
def test_peer_of_another_instance_is_not_found(self):
|
|
|
|
|
unrestricted_key = ApiKey.objects.create(name="unrestricted-key", enabled=True)
|
|
|
|
|
response = self.request({"instance": "wg1", "peer_uuid": str(self.peer.uuid)}, api_key=unrestricted_key)
|
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
self.assertFalse(PeerInvite.objects.exists())
|
|
|
|
|
|
|
|
|
|
def test_api_key_without_access_to_the_instance(self):
|
|
|
|
|
other_peer = Peer.objects.create(
|
|
|
|
|
wireguard_instance=self.other_instance, name="Other Peer", public_key="other-public", private_key="other-private",
|
|
|
|
|
)
|
|
|
|
|
response = self.request({"instance": "wg1", "peer_uuid": str(other_peer.uuid)})
|
|
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
self.assertFalse(PeerInvite.objects.exists())
|
|
|
|
|
|
|
|
|
|
def test_peer_with_endpoint_is_refused(self):
|
|
|
|
|
Peer.objects.filter(pk=self.peer.pk).update(endpoint="remote.example.com:51820")
|
|
|
|
|
response = self.request({"instance": "wg0", "peer_uuid": str(self.peer.uuid)})
|
|
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
|
|
self.assertEqual(response.json()["error_message"], "Peers with an endpoint have no client configuration to share.")
|
|
|
|
|
self.assertFalse(PeerInvite.objects.exists())
|
|
|
|
|
|
|
|
|
|
@patch("api_v2.views_api.send_email")
|
|
|
|
|
def test_email_disabled_in_invite_settings(self, mock_send_email):
|
|
|
|
|
InviteSettings.objects.filter(pk=self.invite_settings.pk).update(invite_email_enabled=False)
|
|
|
|
|
response = self.request({"instance": "wg0", "peer_uuid": str(self.peer.uuid), "email": "user@example.com"})
|
|
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
|
|
self.assertEqual(response.json()["error_message"], "Email invites are disabled in the VPN invite settings.")
|
|
|
|
|
self.assertFalse(PeerInvite.objects.exists())
|
|
|
|
|
mock_send_email.assert_not_called()
|
|
|
|
|
|
|
|
|
|
@patch("api_v2.views_api.send_email")
|
|
|
|
|
def test_sends_invite_by_email(self, mock_send_email):
|
|
|
|
|
mock_send_email.return_value = ("success", "Email sent successfully.")
|
|
|
|
|
response = self.request({"instance": "wg0", "peer_uuid": str(self.peer.uuid), "email": "user@example.com"})
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
self.assertEqual(response.json()["message"], "Invite created. Email sent to user@example.com.")
|
|
|
|
|
invite_data = response.json()["invite_data"]
|
|
|
|
|
mock_send_email.assert_called_once_with("user@example.com", invite_data["email_subject"], invite_data["email_body"])
|
|
|
|
|
|
|
|
|
|
@patch("api_v2.views_api.send_email")
|
|
|
|
|
def test_email_failure_keeps_the_invite(self, mock_send_email):
|
|
|
|
|
mock_send_email.return_value = ("error", "SMTP server not available")
|
|
|
|
|
response = self.request({"instance": "wg0", "peer_uuid": str(self.peer.uuid), "email": "user@example.com"})
|
|
|
|
|
self.assertEqual(response.status_code, 500)
|
2026-04-08 19:34:21 +02:00
|
|
|
body = response.json()
|
2026-11-24 11:23:47 -03:00
|
|
|
self.assertEqual(body["error_message"], "Invite created, but the email could not be sent: SMTP server not available")
|
|
|
|
|
self.assertEqual(body["invite_data"]["uuid"], str(PeerInvite.objects.get(peer=self.peer).uuid))
|
|
|
|
|
|
|
|
|
|
def test_other_methods_are_not_allowed(self):
|
|
|
|
|
response = self.client.get(self.url, HTTP_TOKEN=str(self.api_key.token))
|
|
|
|
|
self.assertEqual(response.status_code, 405)
|
2026-11-24 20:11:34 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_traffic_rrd(path, start, updates, download_rate=1000, upload_rate=500):
|
|
|
|
|
"""RRD with the same layout as the collector, receiving constant rates (bytes per second) every 5 minutes."""
|
|
|
|
|
subprocess.run([
|
|
|
|
|
"rrdtool", "create", path, "--start", str(start), "--step", "300",
|
|
|
|
|
"DS:tx:DERIVE:600:0:U", "DS:rx:DERIVE:600:0:U", "DS:status:GAUGE:600:0:1",
|
|
|
|
|
"RRA:AVERAGE:0.5:1:1440", "RRA:AVERAGE:0.5:6:700", "RRA:AVERAGE:0.5:24:775", "RRA:AVERAGE:0.5:288:797",
|
|
|
|
|
], check=True)
|
|
|
|
|
for index in range(1, updates + 1):
|
|
|
|
|
subprocess.run([
|
|
|
|
|
"rrdtool", "update", path,
|
|
|
|
|
f"{start + index * 300}:{index * 300 * download_rate}:{index * 300 * upload_rate}:1",
|
|
|
|
|
], check=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ApiV2PeerTrafficTests(TestCase):
|
|
|
|
|
url = "/api/v2/peer_traffic/"
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.instance = create_instance(0)
|
|
|
|
|
self.other_instance = create_instance(1)
|
|
|
|
|
self.api_key = ApiKey.objects.create(name="traffic-test-key", enabled=True)
|
|
|
|
|
self.api_key.allowed_instances.add(self.instance)
|
|
|
|
|
self.peer = Peer.objects.create(wireguard_instance=self.instance, name="Test Peer", public_key="peer-public")
|
|
|
|
|
rrd_dir = tempfile.TemporaryDirectory()
|
|
|
|
|
self.addCleanup(rrd_dir.cleanup)
|
|
|
|
|
patcher = patch("wgrrd.functions.RRD_PEERS_PATH", rrd_dir.name)
|
|
|
|
|
patcher.start()
|
|
|
|
|
self.addCleanup(patcher.stop)
|
|
|
|
|
|
|
|
|
|
def request(self, payload, api_key=None):
|
|
|
|
|
return self.client.post(
|
|
|
|
|
self.url, data=json.dumps(payload), content_type="application/json",
|
|
|
|
|
HTTP_TOKEN=str((api_key or self.api_key).token),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def create_history(self, updates=24):
|
|
|
|
|
"""Two hours of history ending about an hour ago: 1000 B/s of download and 500 B/s of upload."""
|
|
|
|
|
if not shutil.which("rrdtool"):
|
|
|
|
|
self.skipTest("rrdtool is not installed")
|
|
|
|
|
self.history_start = (int(time.time()) // 300) * 300 - 3 * 3600
|
|
|
|
|
create_traffic_rrd(peer_rrd_path(self.peer), self.history_start, updates)
|
|
|
|
|
|
|
|
|
|
def iso(self, timestamp):
|
|
|
|
|
return datetime.fromtimestamp(timestamp, tz=dt_timezone.utc).isoformat()
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def period_seconds(body):
|
|
|
|
|
return datetime.fromisoformat(body["end"]).timestamp() - datetime.fromisoformat(body["start"]).timestamp()
|
|
|
|
|
|
|
|
|
|
def test_returns_download_and_upload_totals_of_the_period(self):
|
|
|
|
|
self.create_history()
|
|
|
|
|
response = self.request({
|
|
|
|
|
"instance": "wg0", "peer_uuid": str(self.peer.uuid),
|
|
|
|
|
"start": self.iso(self.history_start + 1200), "end": self.iso(self.history_start + 4800),
|
|
|
|
|
})
|
|
|
|
|
self.assertEqual(response.status_code, 200, response.content)
|
|
|
|
|
body = response.json()
|
|
|
|
|
# The returned period covers the requested one, rounded to the 5 minute samples
|
|
|
|
|
self.assertLessEqual(datetime.fromisoformat(body["start"]).timestamp(), self.history_start + 1200)
|
|
|
|
|
self.assertGreaterEqual(datetime.fromisoformat(body["end"]).timestamp(), self.history_start + 4800)
|
|
|
|
|
self.assertEqual(body["resolution_seconds"], 300)
|
|
|
|
|
# What the server sent to the peer is its download
|
|
|
|
|
self.assertEqual(body["download_bytes"], 1000 * self.period_seconds(body))
|
|
|
|
|
self.assertEqual(body["upload_bytes"], 500 * self.period_seconds(body))
|
|
|
|
|
self.assertNotIn("warning", body)
|
|
|
|
|
|
|
|
|
|
def test_period_without_end_goes_until_now(self):
|
|
|
|
|
self.create_history()
|
|
|
|
|
response = self.request({
|
|
|
|
|
"instance": "wg0", "peer_uuid": str(self.peer.uuid), "start": self.iso(self.history_start + 1200),
|
|
|
|
|
})
|
|
|
|
|
self.assertEqual(response.status_code, 200, response.content)
|
|
|
|
|
body = response.json()
|
|
|
|
|
# The history ends at its last update, so the returned end is the last sample
|
|
|
|
|
self.assertEqual(datetime.fromisoformat(body["end"]).timestamp(), self.history_start + 24 * 300)
|
|
|
|
|
self.assertEqual(body["download_bytes"], 1000 * self.period_seconds(body))
|
|
|
|
|
self.assertNotIn("warning", body)
|
|
|
|
|
|
|
|
|
|
def test_old_period_returns_the_resolution_and_a_warning(self):
|
|
|
|
|
self.create_history()
|
|
|
|
|
response = self.request({
|
|
|
|
|
"instance": "wg0", "peer_uuid": str(self.peer.uuid),
|
|
|
|
|
"start": self.iso(time.time() - 30 * 86400),
|
|
|
|
|
})
|
|
|
|
|
self.assertEqual(response.status_code, 200, response.content)
|
|
|
|
|
body = response.json()
|
|
|
|
|
self.assertEqual(body["resolution_seconds"], 7200)
|
|
|
|
|
self.assertIn("resolution of 2 hours", body["warning"])
|
|
|
|
|
# Most of the period is before the history of the peer
|
|
|
|
|
self.assertIn("Part of the period has no traffic data", body["warning"])
|
|
|
|
|
|
|
|
|
|
def test_selects_peer_by_public_key(self):
|
|
|
|
|
self.create_history()
|
|
|
|
|
response = self.request({
|
|
|
|
|
"instance": "wg0", "peer_public_key": "peer-public", "start": self.iso(self.history_start + 1200),
|
|
|
|
|
})
|
|
|
|
|
self.assertEqual(response.status_code, 200, response.content)
|
|
|
|
|
self.assertEqual(response.json()["peer_uuid"], str(self.peer.uuid))
|
|
|
|
|
|
|
|
|
|
def test_refuses_invalid_dates(self):
|
|
|
|
|
peer = {"instance": "wg0", "peer_uuid": str(self.peer.uuid)}
|
|
|
|
|
for payload, message in (
|
|
|
|
|
({}, "Invalid or missing start date."),
|
|
|
|
|
({"start": "yesterday"}, "Invalid or missing start date."),
|
|
|
|
|
({"start": "2026-11-01", "end": "2026-13-01"}, "Invalid end date."),
|
|
|
|
|
({"start": "2026-11-02T00:00:00", "end": "2026-11-01T00:00:00"}, "The start date must be before the end date."),
|
|
|
|
|
({"start": self.iso(time.time() + 3600)}, "The start date must be before the end date."),
|
|
|
|
|
):
|
|
|
|
|
response = self.request({**peer, **payload})
|
|
|
|
|
self.assertEqual(response.status_code, 400, payload)
|
|
|
|
|
self.assertEqual(response.json()["error_message"], message)
|
|
|
|
|
|
|
|
|
|
def test_unknown_peer_and_peer_without_history_return_404(self):
|
|
|
|
|
response = self.request({"instance": "wg0", "peer_uuid": str(uuid.uuid4()), "start": "2026-11-01"})
|
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
self.assertEqual(response.json()["error_message"], "Peer not found.")
|
|
|
|
|
response = self.request({"instance": "wg0", "peer_uuid": str(self.peer.uuid), "start": "2026-11-01"})
|
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
self.assertEqual(response.json()["error_message"], "There is no traffic history for this peer.")
|
|
|
|
|
|
|
|
|
|
def test_api_key_scope_is_respected(self):
|
|
|
|
|
other_peer = Peer.objects.create(wireguard_instance=self.other_instance, name="Other", public_key="other-public")
|
|
|
|
|
response = self.request({"instance": "wg1", "peer_uuid": str(other_peer.uuid), "start": "2026-11-01"})
|
|
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
# The peer of another instance is not found through an allowed instance
|
|
|
|
|
response = self.request({"instance": "wg0", "peer_uuid": str(other_peer.uuid), "start": "2026-11-01"})
|
|
|
|
|
self.assertEqual(response.status_code, 404)
|