forked from ahmedfgad/GeneticAlgorithmPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_auth_system.py
More file actions
104 lines (80 loc) · 3.34 KB
/
test_auth_system.py
File metadata and controls
104 lines (80 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
Unit tests for encryption and user persistence (no Streamlit UI).
"""
from __future__ import annotations
import os
import tempfile
import unittest
from pathlib import Path
from unittest import mock
# Ensure repo root on path
import sys
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
class TestEncryption(unittest.TestCase):
def test_development_key_file_roundtrip(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
key_file = Path(tmp) / "test.key"
from cryptography.fernet import Fernet
key_file.write_text(Fernet.generate_key().decode(), encoding="utf-8")
env = {
"ENV": "development",
"DEV_ENCRYPTION_KEY_PATH": str(key_file),
}
with mock.patch.dict(os.environ, env, clear=False):
import importlib
import config as cfg
import utils.encryption as enc
importlib.reload(cfg)
importlib.reload(enc)
f = enc.get_master_fernet()
token = enc.encrypt_for_storage("user-secret-key", f)
plain = enc.decrypt_from_storage(token, f)
self.assertEqual(plain, "user-secret-key")
def test_production_env_key(self) -> None:
from cryptography.fernet import Fernet
key = Fernet.generate_key().decode()
env = {"ENV": "production", "ENCRYPTION_KEY": key}
with mock.patch.dict(os.environ, env, clear=False):
import importlib
import config as cfg
import utils.encryption as enc
importlib.reload(cfg)
importlib.reload(enc)
self.assertTrue(cfg.IS_PRODUCTION)
f = enc.get_master_fernet()
self.assertIsNotNone(f.encrypt(b"test"))
class TestUserDatabase(unittest.TestCase):
def test_create_and_fetch_user(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
db_path = Path(tmp) / "test.db"
key_file = Path(tmp) / "dev.key"
from cryptography.fernet import Fernet
key_file.write_text(Fernet.generate_key().decode(), encoding="utf-8")
env = {
"ENV": "development",
"DATABASE_PATH": str(db_path),
"DEV_ENCRYPTION_KEY_PATH": str(key_file),
}
with mock.patch.dict(os.environ, env, clear=False):
import importlib
import config as cfg
import database.db_manager as dbm
import utils.encryption as enc
importlib.reload(cfg)
importlib.reload(enc)
importlib.reload(dbm)
user = dbm.create_user(
username="alice",
email="alice@test.com",
name="Alice",
password_hash="$2b$12$fakehashfortestonlyxxxxxxxxxxxxxxx",
)
loaded = dbm.get_user_by_username("alice")
self.assertIsNotNone(loaded)
assert loaded is not None
self.assertEqual(loaded.user_id, user.user_id)
# Per-user key decrypts with master
uf = enc.get_user_fernet(loaded.encryption_key)
self.assertIsNotNone(uf.encrypt(b"payload"))
if __name__ == "__main__":
unittest.main()