Files
aza/AzA march 2026/_test_chat_device_capacity.py
2026-06-13 22:47:31 +02:00

184 lines
6.9 KiB
Python

# -*- coding: utf-8 -*-
"""Tests für Chat-Gerätekapazität (+5 pro Office-Lizenz)."""
from __future__ import annotations
import sqlite3
import tempfile
import unittest
from pathlib import Path
def _lic(status="active", practice_id="prac_a", lookup_key="aza_office"):
return {
"status": status,
"practice_id": practice_id,
"lookup_key": lookup_key,
}
class TestChatDeviceCapacity(unittest.TestCase):
def test_one_license_gives_five_chat_devices(self):
from aza_chat_device_capacity import chat_device_limit_for_practice
rows = [_lic()]
self.assertEqual(chat_device_limit_for_practice(rows, "prac_a"), 5)
def test_two_licenses_same_practice_gives_ten(self):
from aza_chat_device_capacity import chat_device_limit_for_practice
rows = [_lic(), _lic(lookup_key="aza_pro")]
self.assertEqual(chat_device_limit_for_practice(rows, "prac_a"), 10)
def test_inactive_license_not_counted(self):
from aza_chat_device_capacity import chat_device_limit_for_practice
rows = [_lic(status="canceled"), _lic()]
self.assertEqual(chat_device_limit_for_practice(rows, "prac_a"), 5)
def test_chat_only_license_not_counted(self):
from aza_chat_device_capacity import chat_device_limit_for_practice
rows = [_lic(lookup_key="aza_chat_only"), _lic()]
self.assertEqual(chat_device_limit_for_practice(rows, "prac_a"), 5)
def test_joined_license_counts_for_target_practice(self):
from aza_chat_device_capacity import chat_device_limit_for_practice
rows = [
_lic(practice_id="prac_target"),
_lic(practice_id="prac_other"),
]
self.assertEqual(chat_device_limit_for_practice(rows, "prac_target"), 5)
self.assertEqual(chat_device_limit_for_practice(rows, "prac_other"), 5)
def test_office_and_chat_devices_counted_separately(self):
from aza_chat_device_capacity import (
DEVICE_SCOPE_CHAT,
DEVICE_SCOPE_OFFICE,
count_devices_by_scope,
)
devices = {
"d1": {"practice_id": "p1", "device_name": "AZA Desktop PC1"},
"d2": {"practice_id": "p1", "device_name": "AZA_EmpfangShell Tablet"},
"d3": {"practice_id": "p1", "device_name": "Browser Chat"},
}
self.assertEqual(count_devices_by_scope(devices, practice_id="p1", scope=DEVICE_SCOPE_OFFICE), 1)
self.assertEqual(count_devices_by_scope(devices, practice_id="p1", scope=DEVICE_SCOPE_CHAT), 2)
def test_infer_device_scope_markers(self):
from aza_chat_device_capacity import DEVICE_SCOPE_CHAT, DEVICE_SCOPE_OFFICE, infer_device_scope
self.assertEqual(infer_device_scope("AZA Desktop"), DEVICE_SCOPE_OFFICE)
self.assertEqual(infer_device_scope("AZA_EmpfangShell"), DEVICE_SCOPE_CHAT)
self.assertEqual(infer_device_scope("", "browser"), DEVICE_SCOPE_CHAT)
def test_enforce_chat_device_limit(self):
from aza_device_enforcement import enforce_and_touch_chat_device, ensure_device_table
with tempfile.TemporaryDirectory() as td:
db = str(Path(td) / "test.sqlite")
conn = sqlite3.connect(db)
try:
ensure_device_table(conn)
conn.execute(
"""
CREATE TABLE licenses (
subscription_id TEXT PRIMARY KEY,
status TEXT,
lookup_key TEXT,
practice_id TEXT,
updated_at INTEGER NOT NULL
)
"""
)
conn.execute(
"""
INSERT INTO licenses (subscription_id, status, lookup_key, practice_id, updated_at)
VALUES ('sub1', 'active', 'aza_office', 'prac_x', 1)
"""
)
conn.commit()
finally:
conn.close()
d1 = enforce_and_touch_chat_device(
"prac_x", "device-chat-1", db_path=db, device_name="EmpfangShell",
)
self.assertTrue(d1.allowed)
self.assertEqual(d1.devices_allowed, 5)
self.assertEqual(d1.devices_used, 1)
d2 = enforce_and_touch_chat_device(
"prac_x", "device-chat-1", db_path=db, device_name="EmpfangShell",
)
self.assertTrue(d2.allowed)
self.assertEqual(d2.devices_used, 1)
def test_office_enforcement_unchanged_scope(self):
from aza_device_enforcement import enforce_and_touch_device, ensure_device_table
with tempfile.TemporaryDirectory() as td:
db = str(Path(td) / "test.sqlite")
conn = sqlite3.connect(db)
try:
ensure_device_table(conn)
conn.execute(
"""
CREATE TABLE licenses (
subscription_id TEXT PRIMARY KEY,
status TEXT,
lookup_key TEXT,
practice_id TEXT,
customer_email TEXT,
devices_per_user INTEGER,
updated_at INTEGER NOT NULL
)
"""
)
conn.execute(
"""
INSERT INTO licenses (
subscription_id, status, lookup_key, practice_id,
customer_email, devices_per_user, updated_at
)
VALUES ('sub1', 'active', 'aza_office', 'prac_y', 'user@test.ch', 2, 1)
"""
)
conn.commit()
finally:
conn.close()
office = enforce_and_touch_device(
"user@test.ch", "prac_y", "office-dev-1", db_path=db, device_name="AZA Desktop",
)
self.assertTrue(office.allowed)
self.assertEqual(office.devices_allowed, 2)
def test_admin_enrichment_shows_chat_capacity(self):
from aza_admin_control_shell import _enrich_license_rows_with_users
licenses = [
{
"license_key_plain": "AZA-TEST",
"practice_id": "prac_z",
"status": "active",
"lookup_key": "aza_office",
"devices_per_user": "2",
}
]
devices = {
"c1": {"practice_id": "prac_z", "device_name": "AZA Desktop"},
"c2": {"practice_id": "prac_z", "device_name": "EmpfangShell"},
}
out = _enrich_license_rows_with_users(licenses, {}, devices)
self.assertEqual(out[0]["chat_device_limit"], "5")
self.assertEqual(out[0]["contributing_office_licenses"], "1")
self.assertEqual(out[0]["office_devices_used"], "1")
self.assertEqual(out[0]["chat_devices_used"], "1")
if __name__ == "__main__":
unittest.main()