Files
shared/packages/python-backend/tests/test_core.py
T
Gabriel20xx da97fd4fb1
Lint / Lint (Node/Svelte) (push) Successful in 24s
Lint / Lint (Go) (push) Successful in 28s
Lint / Lint (Python) (push) Failing after 14s
Tests / Test (Node/Svelte) (push) Successful in 18s
Tests / Test (Go) (push) Successful in 25s
Tests / Test (Python) (push) Successful in 12s
feat(auth): implement TOTP setup and management
- Added TOTP support for two-factor authentication in the auth module.
- Introduced new types and interfaces for TOTP setup and token actions in the web UI.
- Created backend logic for generating and verifying TOTP secrets and tokens.
- Implemented user management features including role assignments and user CRUD operations.
- Enhanced UI components for managing users and TOTP settings.
- Added styles for optional labels and authentication reset links.
- Implemented reset code generation and validation for admin accounts.
2026-08-05 14:57:01 +02:00

70 lines
2.4 KiB
Python

import os
import unittest
from datetime import timedelta
from shared_backend.auth import ResetManager, hash_password, verify_password
from shared_backend.config import DatabaseConfig
from shared_backend.totp import totp_provisioning_uri, verify_totp
from shared_backend.tokens import hash_token, new_session_token
class SharedBackendTests(unittest.TestCase):
def test_password_round_trip(self):
password_hash = hash_password("correct horse battery staple")
self.assertTrue(verify_password("correct horse battery staple", password_hash))
self.assertFalse(verify_password("wrong password", password_hash))
def test_session_token_hash(self):
token, token_hash = new_session_token()
self.assertNotEqual(token, token_hash)
self.assertEqual(hash_token(token), token_hash)
def test_reset_manager_round_trip_and_one_time_use(self):
messages = []
manager = ResetManager(log=messages.append)
code = manager.request()
self.assertEqual(len(code), 8)
self.assertIn(f"ADMIN RESET CODE: {code}", messages[0])
self.assertTrue(manager.confirm(f" {code.lower()} "))
self.assertFalse(manager.confirm(code))
def test_reset_manager_expires(self):
manager = ResetManager(ttl=timedelta(seconds=-1), log=lambda _message: None)
code = manager.request()
self.assertFalse(manager.confirm(code))
def test_totp_rfc6238_sha1_vectors(self):
secret = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"
vectors = [
(59, "287082"),
(1111111109, "081804"),
(1111111111, "050471"),
(1234567890, "005924"),
(2000000000, "279037"),
]
for timestamp, code in vectors:
self.assertTrue(verify_totp(secret, code, timestamp))
self.assertTrue(
totp_provisioning_uri("Example App", "admin@example.com", secret).startswith(
"otpauth://totp/"
)
)
def test_database_config_from_environment(self):
config = DatabaseConfig.from_env(
{
**os.environ,
"DATABASE_URL": "postgresql://user:password@localhost:5432/app",
"DATABASE_MAX_CONNS": "7",
"DATABASE_MIN_CONNS": "2",
}
)
self.assertEqual(config.max_size, 7)
self.assertEqual(config.min_size, 2)
if __name__ == "__main__":
unittest.main()