Qualitätsprüfungen und Fehlerpfade härten
Some checks failed
Tests / test (push) Has been cancelled

This commit is contained in:
2026-10-15 10:05:48 +02:00
parent c0498b6935
commit 9dc37d1ece
12 changed files with 2383 additions and 517 deletions

View File

@@ -1,4 +1,5 @@
import importlib.util
import contextlib
import json
from pathlib import Path
import sqlite3
@@ -31,7 +32,7 @@ class Tests(unittest.TestCase):
for data in [{}, {'system_uuid':'00000000-0000-0000-0000-000000000000'}, {'serial_number':'To Be Filled By O.E.M.','manufacturer':'HP'}]:
with self.assertRaises(ValueError): bridge.hardware_id(data)
def test_missing_hwid_never_uses_manual_number(self):
with tempfile.TemporaryDirectory() as tmp, sqlite3.connect(':memory:') as db:
with tempfile.TemporaryDirectory() as tmp, contextlib.closing(sqlite3.connect(':memory:')) as db:
bridge.init_db(db)
path=Path(tmp)/'report.json'
path.write_text(json.dumps({'asset_id':'45','model':'Laptop'}))
@@ -48,7 +49,7 @@ class Tests(unittest.TestCase):
if method=='POST':
return {'number':501,'revision':3,'model':'HP EliteBook 745 G6','cpu':'old','ram_gb':8,'ssd_gb':128}
return {'revision':4}
with sqlite3.connect(':memory:') as db:
with contextlib.closing(sqlite3.connect(':memory:')) as db:
bridge.init_db(db)
with patch.object(bridge,'api_request',side_effect=fake), patch.dict('os.environ',{'CSR_API_KEY':'fake'}):
res=bridge.sync_one(path,{'url':'https://example.org'},db)
@@ -63,7 +64,7 @@ class Tests(unittest.TestCase):
def test_mismatch_not_patched(self):
with tempfile.TemporaryDirectory() as tmp:
path=Path(tmp)/'report.json';path.write_text(json.dumps(self.report))
with sqlite3.connect(':memory:') as db:
with contextlib.closing(sqlite3.connect(':memory:')) as db:
bridge.init_db(db)
with patch.object(bridge,'api_request',return_value={'number':501,'revision':1,'model':'Different machine'}),patch.dict('os.environ',{'CSR_API_KEY':'fake'}):
with self.assertRaisesRegex(ValueError,'MODEL_MISMATCH'):
@@ -172,3 +173,19 @@ class ConnectionTests(unittest.TestCase):
self.assertIn('API key', bridge.connection_error(error))
self.assertNotIn('secret', bridge.connection_error(error))
error.close()
def test_inventory_url_must_be_an_https_origin(self):
with patch.dict('os.environ', {'CSR_API_KEY': 'test-key'}):
self.assertEqual(
bridge.credentials({'url': 'https://inventory.test/'}),
('https://inventory.test', 'test-key'),
)
for url in (
'http://inventory.test',
'https://user:secret@inventory.test',
'https://inventory.test/api',
'https://inventory.test?token=secret',
'https://inventory.test#fragment',
):
with self.subTest(url=url), self.assertRaisesRegex(ValueError, 'HTTPS origin'):
bridge.credentials({'url': url})