43 lines
1.4 KiB
Python
43 lines
1.4 KiB
Python
import os
|
|
import shutil
|
|
import tempfile
|
|
import pytest
|
|
from main import app, CONFIG_PATH
|
|
|
|
def setup_module(module):
|
|
# Сохраняем оригинальный config.yaml
|
|
shutil.copy(CONFIG_PATH, CONFIG_PATH + '.bak')
|
|
|
|
def teardown_module(module):
|
|
# Восстанавливаем оригинальный config.yaml
|
|
shutil.move(CONFIG_PATH + '.bak', CONFIG_PATH)
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
app.config['TESTING'] = True
|
|
with app.test_client() as client:
|
|
yield client
|
|
|
|
def test_get_config(client):
|
|
resp = client.get('/api/config')
|
|
assert resp.status_code == 200
|
|
data = resp.get_json()
|
|
assert 'content' in data
|
|
assert 'applications' in data['content']
|
|
|
|
def test_post_valid_config(client):
|
|
resp = client.get('/api/config')
|
|
orig = resp.get_json()['content']
|
|
new_content = orig + '\n# test comment\n'
|
|
resp2 = client.post('/api/config', json={'content': new_content})
|
|
assert resp2.status_code == 200
|
|
# Проверяем, что файл обновился
|
|
with open(CONFIG_PATH, encoding='utf-8') as f:
|
|
assert '# test comment' in f.read()
|
|
|
|
def test_post_invalid_config(client):
|
|
bad_yaml = 'applications: [\n - name: test\n url: [bad' # некорректный yaml
|
|
resp = client.post('/api/config', json={'content': bad_yaml})
|
|
assert resp.status_code == 400
|
|
data = resp.get_json()
|
|
assert 'error' in data |