126 lines
4.8 KiB
Python
126 lines
4.8 KiB
Python
from dotenv import dotenv_values
|
|
|
|
from app import db, env_persistence
|
|
from app.models import AppSetting
|
|
|
|
|
|
def test_wecom_system_settings_persist_to_env_and_reload(client, app, admin_headers, monkeypatch, tmp_path):
|
|
env_file = tmp_path / '.env'
|
|
monkeypatch.setattr(env_persistence, 'BACKEND_ENV_PATH', env_file)
|
|
|
|
payload = {
|
|
'enabled': True,
|
|
'corp_id': 'ww-persisted',
|
|
'agent_id': '1000999',
|
|
'secret': 'persist-secret',
|
|
'redirect_uri': 'https://example.com/workspace',
|
|
'auto_create_user': False,
|
|
'match_rules': 'employee_id,email',
|
|
'match_operator': 'OR',
|
|
'employee_id_source': 'extattr:job_no',
|
|
'mobile_normalize': False,
|
|
'callback_token': 'cb-token',
|
|
'encoding_aes_key': 'enc-key',
|
|
'callback_url': 'https://example.com/api/user/wecom/callback',
|
|
'homepage_url': 'https://example.com/workspace',
|
|
'trusted_domain': 'example.com',
|
|
'public_base_url': 'https://example.com',
|
|
}
|
|
|
|
res = client.put('/api/admin/wecom/system-settings', json=payload, headers=admin_headers)
|
|
assert res.status_code == 200
|
|
|
|
saved = dotenv_values(env_file)
|
|
assert saved['WECOM_ENABLED'] == 'true'
|
|
assert saved['WECOM_CORP_ID'] == 'ww-persisted'
|
|
assert saved['WECOM_MATCH_OPERATOR'] == 'OR'
|
|
assert saved['WECOM_PUBLIC_BASE_URL'] == 'https://example.com'
|
|
|
|
with app.app_context():
|
|
rows = {row.key: row.value for row in db.session.query(AppSetting).all()}
|
|
assert rows['WECOM_ENABLED'] == 'true'
|
|
assert rows['WECOM_CORP_ID'] == 'ww-persisted'
|
|
assert rows['WECOM_MATCH_OPERATOR'] == 'OR'
|
|
assert rows['WECOM_PUBLIC_BASE_URL'] == 'https://example.com'
|
|
|
|
with app.app_context():
|
|
app.config['WECOM_ENABLED'] = False
|
|
app.config['WECOM_CORP_ID'] = ''
|
|
app.config['WECOM_MATCH_OPERATOR'] = 'AND'
|
|
app.config['WECOM_PUBLIC_BASE_URL'] = ''
|
|
|
|
res = client.get('/api/admin/wecom/system-settings', headers=admin_headers)
|
|
assert res.status_code == 200
|
|
data = res.get_json()
|
|
assert data['enabled'] is True
|
|
assert data['corp_id'] == 'ww-persisted'
|
|
assert data['match_operator'] == 'OR'
|
|
assert data['public_base_url'] == 'https://example.com'
|
|
|
|
|
|
def test_wecom_notify_settings_persist_to_env_and_reload(client, app, admin_headers, monkeypatch, tmp_path):
|
|
env_file = tmp_path / '.env'
|
|
monkeypatch.setattr(env_persistence, 'BACKEND_ENV_PATH', env_file)
|
|
|
|
payload = {
|
|
'enabled': False,
|
|
'base_url': 'https://notify.example.com',
|
|
'events': ['task_completed', 'permission_batch_updated'],
|
|
}
|
|
|
|
res = client.put('/api/admin/wecom/notify-settings', json=payload, headers=admin_headers)
|
|
assert res.status_code == 200
|
|
|
|
saved = dotenv_values(env_file)
|
|
assert saved['WECOM_NOTIFY_ENABLED'] == 'false'
|
|
assert saved['WECOM_NOTIFY_BASE_URL'] == 'https://notify.example.com'
|
|
assert saved['WECOM_NOTIFY_EVENTS'] == 'task_completed,permission_batch_updated'
|
|
|
|
with app.app_context():
|
|
rows = {row.key: row.value for row in db.session.query(AppSetting).all()}
|
|
assert rows['WECOM_NOTIFY_ENABLED'] == 'false'
|
|
assert rows['WECOM_NOTIFY_BASE_URL'] == 'https://notify.example.com'
|
|
assert rows['WECOM_NOTIFY_EVENTS'] == 'task_completed,permission_batch_updated'
|
|
|
|
with app.app_context():
|
|
app.config['WECOM_NOTIFY_ENABLED'] = True
|
|
app.config['WECOM_NOTIFY_BASE_URL'] = ''
|
|
app.config['WECOM_NOTIFY_EVENTS'] = ''
|
|
|
|
res = client.get('/api/admin/wecom/notify-settings', headers=admin_headers)
|
|
assert res.status_code == 200
|
|
data = res.get_json()
|
|
assert data['enabled'] is False
|
|
assert data['base_url'] == 'https://notify.example.com'
|
|
assert data['events'] == ['task_completed', 'permission_batch_updated']
|
|
|
|
|
|
def test_wecom_system_settings_survive_when_env_file_is_wiped(client, app, admin_headers, monkeypatch, tmp_path):
|
|
"""Simulate a container rebuild that wipes backend/.env. The DB row must
|
|
still drive the runtime configuration."""
|
|
env_file = tmp_path / '.env'
|
|
monkeypatch.setattr(env_persistence, 'BACKEND_ENV_PATH', env_file)
|
|
|
|
res = client.put(
|
|
'/api/admin/wecom/system-settings',
|
|
json={'corp_id': 'ww-from-db-only', 'enabled': True},
|
|
headers=admin_headers,
|
|
)
|
|
assert res.status_code == 200
|
|
|
|
# Wipe the env file the way a fresh container build would.
|
|
if env_file.exists():
|
|
env_file.unlink()
|
|
|
|
# Reset in-memory config to defaults to prove the next request reloads
|
|
# values from the database rather than the (now empty) env file.
|
|
with app.app_context():
|
|
app.config['WECOM_CORP_ID'] = ''
|
|
app.config['WECOM_ENABLED'] = False
|
|
|
|
res = client.get('/api/admin/wecom/system-settings', headers=admin_headers)
|
|
assert res.status_code == 200
|
|
data = res.get_json()
|
|
assert data['corp_id'] == 'ww-from-db-only'
|
|
assert data['enabled'] is True
|