| 1 |
import os |
| 2 |
import tempfile |
| 3 |
import pytest |
| 4 |
from a2d.app import app |
| 5 |
|
| 6 |
@pytest.fixture |
| 7 |
def client(): |
| 8 |
db_fd, app.config['DATABASE'] = tempfile.mkstemp() |
| 9 |
app.config['TESTING'] = True |
| 10 |
with app.test_client() as client: |
| 11 |
with app.app_context(): |
| 12 |
pass |
| 13 |
yield client |
| 14 |
os.close(db_fd) |
| 15 |
os.unlink(app.config['DATABASE']) |
| 16 |
|
| 17 |
def test_home_route(client): |
| 18 |
"""Test the home route.""" |
| 19 |
rv = client.get('/') |
| 20 |
assert rv.status_code == 302 # Should redirect to /login if no session |
| 21 |
|
| 22 |
def test_get_version(client): |
| 23 |
"""Test the version route.""" |
| 24 |
rv = client.get('/version') |
| 25 |
assert rv.status_code == 200 |
| 26 |
assert rv.json == {'version': '2.0.5'} |
| 27 |
|
| 28 |
def test_session_key_generation(monkeypatch): |
| 29 |
"""Test session key generation and loading.""" |
| 30 |
import tempfile |
| 31 |
import a2d.app |
| 32 |
|
| 33 |
with tempfile.TemporaryDirectory() as tempdir: |
| 34 |
SESSION_KEY_FILE = os.path.join(tempdir, "session_key.bin") |
| 35 |
|
| 36 |
def mock_generate_and_store_session_key(): |
| 37 |
session_key = os.urandom(32) |
| 38 |
with open(SESSION_KEY_FILE, "wb") as key_file: |
| 39 |
key_file.write(session_key) |
| 40 |
|
| 41 |
def mock_load_session_key(): |
| 42 |
try: |
| 43 |
with open(SESSION_KEY_FILE, "rb") as key_file: |
| 44 |
return key_file.read() |
| 45 |
except FileNotFoundError: |
| 46 |
return None |
| 47 |
|
| 48 |
monkeypatch.setattr(a2d.app, "generate_and_store_session_key", mock_generate_and_store_session_key) |
| 49 |
monkeypatch.setattr(a2d.app, "load_session_key", mock_load_session_key) |
| 50 |
|
| 51 |
a2d.app.generate_and_store_session_key() |
| 52 |
session_key = a2d.app.load_session_key() |
| 53 |
|
| 54 |
assert session_key is not None |
| 55 |
assert len(session_key) == 32 |
| 56 |
|
| 57 |
def test_blueprints_registered(): |
| 58 |
"""Test if all blueprints are registered.""" |
| 59 |
expected_blueprints = [ |
| 60 |
'auth', |
| 61 |
'run', |
| 62 |
'dns', |
| 63 |
'system', |
| 64 |
'network', |
| 65 |
'config', |
| 66 |
'reset', |
| 67 |
'data' |
| 68 |
] |
| 69 |
registered_blueprints = list(app.blueprints.keys()) |
| 70 |
|
| 71 |
for blueprint in expected_blueprints: |
| 72 |
assert blueprint in registered_blueprints |