181 lines
9.5 KiB
Python
181 lines
9.5 KiB
Python
|
|
from datetime import timedelta
|
||
|
|
from django.contrib.auth import get_user_model
|
||
|
|
from django.test import TestCase
|
||
|
|
from django.utils import timezone
|
||
|
|
from control.models import ClientSession, Device, Node, NodeDeployment, Plan, Subscription
|
||
|
|
from control.services import desired_subscriptions_for_node, render_xray_client
|
||
|
|
|
||
|
|
|
||
|
|
class OfficePermissionsTests(TestCase):
|
||
|
|
def setUp(self):
|
||
|
|
self.user = get_user_model().objects.create_user(username="office", identity_type="employee", account_status="active")
|
||
|
|
plan = Plan.objects.create(name="office", slug="office")
|
||
|
|
self.sub = Subscription.objects.create(user=self.user, plan=plan, status="active", provisioning_state="active")
|
||
|
|
self.node = Node.objects.create(name="优创未来·济南办", slug="office", region="济南", server="192.168.50.111", port=11081,
|
||
|
|
protocol="vless_reality", status="online", subscription_enabled=True, office_only=True,
|
||
|
|
office_access_scope=Node.OfficeAccessScope.EXPLICIT_USERS)
|
||
|
|
|
||
|
|
def test_empty_whitelist_respects_configured_scope(self):
|
||
|
|
self.assertFalse(self.sub.office_nodes_for_authorization().exists())
|
||
|
|
|
||
|
|
def test_whitelist_is_not_local_presence_or_legacy_config_grant(self):
|
||
|
|
self.node.office_users.add(self.user)
|
||
|
|
self.assertTrue(self.sub.office_nodes_for_authorization().exists())
|
||
|
|
self.assertFalse(self.sub.nodes_for_subscription().exists())
|
||
|
|
|
||
|
|
def test_external_even_whitelisted_denied(self):
|
||
|
|
self.node.office_users.add(self.user)
|
||
|
|
self.user.identity_type = "external"
|
||
|
|
self.user.save()
|
||
|
|
self.assertFalse(self.sub.office_nodes_for_authorization().exists())
|
||
|
|
|
||
|
|
def test_departed_employee_denied(self):
|
||
|
|
self.node.office_users.add(self.user)
|
||
|
|
self.user.account_status = "departed"
|
||
|
|
self.user.save()
|
||
|
|
self.assertFalse(self.sub.office_nodes_for_authorization().exists())
|
||
|
|
|
||
|
|
def test_whitelist_removal_respects_configured_scope(self):
|
||
|
|
self.node.office_users.add(self.user)
|
||
|
|
self.node.office_users.remove(self.user)
|
||
|
|
self.assertFalse(self.sub.office_nodes_for_authorization().exists())
|
||
|
|
|
||
|
|
def test_dedicated_subscription_still_restricts_office(self):
|
||
|
|
self.node.office_users.add(self.user)
|
||
|
|
self.sub.route_mode = Subscription.RouteMode.DEDICATED
|
||
|
|
self.sub.save()
|
||
|
|
self.assertFalse(self.sub.office_nodes_for_authorization().exists())
|
||
|
|
self.sub.allowed_nodes.add(self.node)
|
||
|
|
self.assertTrue(self.sub.office_nodes_for_authorization().exists())
|
||
|
|
|
||
|
|
def test_other_employee_candidate_boundary(self):
|
||
|
|
self.node.office_users.add(self.user)
|
||
|
|
other = get_user_model().objects.create_user(username="another-office", email="other@example.invalid",
|
||
|
|
identity_type="employee", account_status="active")
|
||
|
|
other_sub = Subscription.objects.create(user=other, plan=self.sub.plan, status="active", provisioning_state="active")
|
||
|
|
self.assertFalse(other_sub.office_nodes_for_authorization().exists())
|
||
|
|
|
||
|
|
def test_account_state_checked_even_with_office_membership(self):
|
||
|
|
self.node.office_users.add(self.user)
|
||
|
|
for status in ("pending", "suspended", "departed"):
|
||
|
|
with self.subTest(status=status):
|
||
|
|
self.user.account_status = status
|
||
|
|
self.user.save()
|
||
|
|
self.assertFalse(self.sub.office_nodes_for_authorization().exists())
|
||
|
|
self.user.account_status = "active"
|
||
|
|
self.user.is_active = False
|
||
|
|
self.user.save()
|
||
|
|
self.assertFalse(self.sub.office_nodes_for_authorization().exists())
|
||
|
|
|
||
|
|
def test_offline_disabled_or_unpublished_node_cannot_be_authorized(self):
|
||
|
|
self.node.office_users.add(self.user)
|
||
|
|
for field, value in (("active", False), ("status", "offline"), ("subscription_enabled", False)):
|
||
|
|
with self.subTest(field=field):
|
||
|
|
previous = getattr(self.node, field)
|
||
|
|
setattr(self.node, field, value)
|
||
|
|
self.node.save()
|
||
|
|
self.assertFalse(self.sub.office_nodes_for_authorization().exists())
|
||
|
|
setattr(self.node, field, previous)
|
||
|
|
self.node.save()
|
||
|
|
|
||
|
|
def test_expired_or_exhausted_subscription_cannot_use_office(self):
|
||
|
|
self.node.office_users.add(self.user)
|
||
|
|
self.sub.expires_at = timezone.now() - timedelta(seconds=1)
|
||
|
|
self.sub.save()
|
||
|
|
self.assertFalse(self.sub.office_nodes_for_authorization().exists())
|
||
|
|
self.sub.expires_at = None
|
||
|
|
self.sub.traffic_limit_bytes = 100
|
||
|
|
self.sub.upload_bytes = 100
|
||
|
|
self.sub.save()
|
||
|
|
self.assertFalse(self.sub.office_nodes_for_authorization().exists())
|
||
|
|
|
||
|
|
def test_legacy_api_cannot_bypass_pending_local_node_validation(self):
|
||
|
|
self.node.office_users.add(self.user)
|
||
|
|
device = Device.objects.create(subscription=self.sub, device_id="office-api-boundary")
|
||
|
|
_, token = ClientSession.issue(self.sub, device)
|
||
|
|
headers = {"HTTP_AUTHORIZATION": f"Bearer {token}"}
|
||
|
|
profile = self.client.get("/api/v1/client/profile/", **headers)
|
||
|
|
self.assertEqual(profile.status_code, 200)
|
||
|
|
self.assertEqual(profile.json()["profile"]["nodes"], [])
|
||
|
|
for selection in (self.node.slug, "auto"):
|
||
|
|
with self.subTest(selection=selection):
|
||
|
|
response = self.client.get("/api/v1/client/config/", {"node": selection, "route": "smart"}, **headers)
|
||
|
|
self.assertEqual(response.status_code, 404)
|
||
|
|
self.assertNotIn("config", response.json())
|
||
|
|
device.active = False
|
||
|
|
device.save()
|
||
|
|
self.assertEqual(self.client.get("/api/v1/client/profile/", **headers).status_code, 403)
|
||
|
|
|
||
|
|
def test_agent_sync_does_not_provision_office_candidates(self):
|
||
|
|
self.node.office_users.add(self.user)
|
||
|
|
self.node.set_agent_token("synthetic-office-agent-token")
|
||
|
|
self.node.save(update_fields=["agent_token_digest"])
|
||
|
|
self.sub.allowed_nodes.add(self.node)
|
||
|
|
for mode in Subscription.RouteMode.values:
|
||
|
|
with self.subTest(mode=mode):
|
||
|
|
self.sub.route_mode = mode
|
||
|
|
self.sub.save(update_fields=["route_mode"])
|
||
|
|
self.assertFalse(desired_subscriptions_for_node(self.node).exists())
|
||
|
|
response = self.client.get("/api/v1/node/sync/",
|
||
|
|
HTTP_X_NODE_SLUG=self.node.slug,
|
||
|
|
HTTP_AUTHORIZATION="Bearer synthetic-office-agent-token")
|
||
|
|
self.assertEqual(response.status_code, 200)
|
||
|
|
self.assertEqual(response.json()["clients"], [])
|
||
|
|
self.assertNotContains(response, str(self.sub.credential_uuid))
|
||
|
|
self.assertFalse(NodeDeployment.objects.filter(node=self.node).exists())
|
||
|
|
|
||
|
|
def test_direct_renderer_cannot_bypass_pending_site_validation(self):
|
||
|
|
self.node.office_users.add(self.user)
|
||
|
|
for mode in ("smart", "global", "direct"):
|
||
|
|
with self.subTest(mode=mode), self.assertRaisesMessage(ValueError, "办公室"):
|
||
|
|
render_xray_client(self.sub, self.node, route_mode=mode)
|
||
|
|
|
||
|
|
def test_public_node_provisioning_and_rendering_are_unchanged(self):
|
||
|
|
self.node.office_only = False
|
||
|
|
self.node.save(update_fields=["office_only"])
|
||
|
|
self.assertIn(self.sub, desired_subscriptions_for_node(self.node))
|
||
|
|
self.assertIn(self.node, self.sub.nodes_for_subscription())
|
||
|
|
config = render_xray_client(self.sub, self.node)
|
||
|
|
self.assertEqual(config["outbounds"][0]["settings"]["vnext"][0]["users"][0]["id"],
|
||
|
|
str(self.sub.credential_uuid))
|
||
|
|
|
||
|
|
|
||
|
|
class OfficeNetworkScopeTests(OfficePermissionsTests):
|
||
|
|
"""Site presence decides availability, not the employee's home office."""
|
||
|
|
|
||
|
|
def setUp(self):
|
||
|
|
super().setUp()
|
||
|
|
self.node.office_access_scope = Node.OfficeAccessScope.COMPANY_NETWORK
|
||
|
|
self.node.save(update_fields=["office_access_scope"])
|
||
|
|
|
||
|
|
def test_empty_whitelist_respects_configured_scope(self):
|
||
|
|
# Override the legacy-mode expectation: it is a candidate, NOT a grant.
|
||
|
|
self.assertTrue(self.sub.office_nodes_for_authorization().exists())
|
||
|
|
self.assertFalse(self.sub.nodes_for_subscription().exists())
|
||
|
|
|
||
|
|
def test_whitelist_removal_respects_configured_scope(self):
|
||
|
|
# Network mode must not depend on stale per-person membership lists.
|
||
|
|
self.node.office_users.add(self.user)
|
||
|
|
self.node.office_users.remove(self.user)
|
||
|
|
self.assertTrue(self.sub.office_nodes_for_authorization().exists())
|
||
|
|
self.user.account_status = "departed"
|
||
|
|
self.user.save()
|
||
|
|
self.assertFalse(self.sub.office_nodes_for_authorization().exists())
|
||
|
|
|
||
|
|
def test_other_employee_candidate_boundary(self):
|
||
|
|
# A visiting employee needs the same site proof, not reassignment in HR.
|
||
|
|
other = get_user_model().objects.create_user(username="visiting", email="visiting@example.invalid",
|
||
|
|
identity_type="employee", account_status="active")
|
||
|
|
other_sub = Subscription.objects.create(user=other, plan=self.sub.plan, status="active", provisioning_state="active")
|
||
|
|
self.assertTrue(other_sub.office_nodes_for_authorization().exists())
|
||
|
|
self.assertFalse(other_sub.nodes_for_subscription().exists())
|
||
|
|
|
||
|
|
def test_unknown_scope_fails_closed(self):
|
||
|
|
self.node.office_access_scope = "unknown"
|
||
|
|
self.node.save()
|
||
|
|
self.node.office_users.add(self.user)
|
||
|
|
self.assertFalse(self.sub.office_nodes_for_authorization().exists())
|
||
|
|
|
||
|
|
def test_new_node_defaults_to_company_network(self):
|
||
|
|
self.assertEqual(Node().office_access_scope, Node.OfficeAccessScope.COMPANY_NETWORK)
|