| 1 |
import pytest |
| 2 |
from unittest.mock import patch, MagicMock |
| 3 |
from a2d.app import app |
| 4 |
from a2d.routes.run import add_cronjob |
| 5 |
|
| 6 |
@pytest.fixture |
| 7 |
def client(): |
| 8 |
app.config['TESTING'] = True |
| 9 |
with app.test_client() as client: |
| 10 |
with app.app_context(): |
| 11 |
yield client |
| 12 |
|
| 13 |
def test_add_and_check_cronjob(monkeypatch): |
| 14 |
# Mocking filesystem operations for add_cronjob and check_cronjob functions |
| 15 |
mock_file = MagicMock() |
| 16 |
mock_open = MagicMock(return_value=mock_file) |
| 17 |
monkeypatch.setattr('builtins.open', mock_open) |
| 18 |
|
| 19 |
# Mocking ConfigParser to return a mock instance |
| 20 |
with patch('a2d.routes.run.configparser.ConfigParser') as MockConfigParser: |
| 21 |
mock_config = MockConfigParser.return_value |
| 22 |
mock_config.read.return_value = [] # Simulate an empty configuration file |
| 23 |
|
| 24 |
# Test adding cron job |
| 25 |
add_cronjob() |
| 26 |
|
| 27 |
# Assert that the file was opened correctly |
| 28 |
mock_open.assert_called_once_with('/etc/cron.d/a2d', 'a') |
| 29 |
|
| 30 |
def test_service_routes(client): |
| 31 |
# Test start-service route |
| 32 |
rv = client.get('/start-service') |
| 33 |
assert rv.status_code == 302 # Redirects to '/' on success |
| 34 |
|
| 35 |
# Test stop-service route |
| 36 |
rv = client.get('/stop-service') |
| 37 |
assert rv.status_code == 302 # Redirects to '/' on success |