mirror of
https://github.com/anten-ka/gotelegram_pro.git
synced 2026-05-19 20:36:04 +00:00
v2.5.0: align key controls and add IP limits
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
@@ -13,6 +15,8 @@ SERVER_PATH = ROOT / "admin-web" / "server.py"
|
||||
def load_server(tmpdir: Path):
|
||||
os.environ["GOTELEGRAM_BACKUP_DIR"] = str(tmpdir / "backups")
|
||||
os.environ["GOTELEGRAM_DIR"] = str(tmpdir / "gotelegram")
|
||||
os.environ["TELEMT_CONFIG"] = str(tmpdir / "etc" / "telemt" / "config.toml")
|
||||
os.environ["GOTELEGRAM_DISABLED_USERS"] = str(tmpdir / "gotelegram" / "disabled_users.json")
|
||||
module_name = "gotelegram_admin_server_test"
|
||||
sys.modules.pop(module_name, None)
|
||||
spec = importlib.util.spec_from_file_location(module_name, SERVER_PATH)
|
||||
@@ -57,6 +61,84 @@ class AdminFeatureTests(unittest.TestCase):
|
||||
with self.assertRaises(ValueError):
|
||||
server.backup_schedule_calendar("hourly")
|
||||
|
||||
def test_user_records_include_active_disabled_and_ip_limits(self):
|
||||
with tempfile.TemporaryDirectory() as raw:
|
||||
tmpdir = Path(raw)
|
||||
server = load_server(tmpdir)
|
||||
server.TELEMT_CONFIG.parent.mkdir(parents=True)
|
||||
server.TELEMT_CONFIG.write_text(
|
||||
"\n".join([
|
||||
"[access.users]",
|
||||
'main = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"',
|
||||
'client = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"',
|
||||
"",
|
||||
"[access.user_max_unique_ips]",
|
||||
"main = 0",
|
||||
"client = 2",
|
||||
"disabled = 1",
|
||||
"",
|
||||
]),
|
||||
encoding="utf-8",
|
||||
)
|
||||
server.DISABLED_USERS_FILE.parent.mkdir(parents=True)
|
||||
server.DISABLED_USERS_FILE.write_text(
|
||||
json.dumps({"users": {"disabled": "cccccccccccccccccccccccccccccccc"}}),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
records = server.read_user_records()
|
||||
|
||||
self.assertTrue(records["client"]["enabled"])
|
||||
self.assertEqual(records["client"]["max_unique_ips"], 2)
|
||||
self.assertFalse(records["disabled"]["enabled"])
|
||||
self.assertEqual(records["disabled"]["max_unique_ips"], 1)
|
||||
self.assertEqual(records["main"]["max_unique_ips"], 0)
|
||||
|
||||
def test_write_user_max_unique_ips_preserves_other_toml_sections(self):
|
||||
with tempfile.TemporaryDirectory() as raw:
|
||||
server = load_server(Path(raw))
|
||||
server.TELEMT_CONFIG.parent.mkdir(parents=True)
|
||||
server.TELEMT_CONFIG.write_text(
|
||||
"\n".join([
|
||||
"[server]",
|
||||
"port = 443",
|
||||
"",
|
||||
"[access.users]",
|
||||
'main = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"',
|
||||
'client = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"',
|
||||
"",
|
||||
"[access.user_max_unique_ips]",
|
||||
"client = 3",
|
||||
"old = 4",
|
||||
"",
|
||||
"[network]",
|
||||
'dns_overrides = ["example.com:8443:127.0.0.1"]',
|
||||
"",
|
||||
]),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
server.write_user_max_unique_ips({"main": 1, "client": 0, "new": 5})
|
||||
text = server.TELEMT_CONFIG.read_text(encoding="utf-8")
|
||||
|
||||
self.assertIn("[server]\nport = 443", text)
|
||||
self.assertIn("[network]\ndns_overrides", text)
|
||||
self.assertIn("[access.user_max_unique_ips]", text)
|
||||
self.assertIn('"main" = 1', text)
|
||||
self.assertIn('"new" = 5', text)
|
||||
self.assertNotIn("client = 3", text)
|
||||
self.assertNotIn("old = 4", text)
|
||||
|
||||
def test_keys_table_keeps_actions_inside_table_cell_wrapper(self):
|
||||
app_js = (ROOT / "admin-web" / "static" / "app.js").read_text(encoding="utf-8")
|
||||
styles = (ROOT / "admin-web" / "static" / "styles.css").read_text(encoding="utf-8")
|
||||
index = (ROOT / "admin-web" / "static" / "index.html").read_text(encoding="utf-8")
|
||||
|
||||
self.assertNotIn('class="actions"', app_js)
|
||||
self.assertIn('class="action-buttons"', app_js)
|
||||
self.assertNotIn("td.actions", styles)
|
||||
self.assertRegex(index, r'<table class="keys-table">[\s\S]*?<tbody id="usersTable">')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user