146 lines
5.7 KiB
Python
146 lines
5.7 KiB
Python
from dotenv import dotenv_values
|
|
|
|
from app import db
|
|
from app.core.auth.jwt import issue_local_access_token
|
|
from app.core.config import env_persistence
|
|
from app.models import AppSetting, User
|
|
|
|
|
|
def _admin_auth_headers(app) -> dict[str, str]:
|
|
"""使用当前 JWT 鉴权合同构造企微管理端请求身份。"""
|
|
with app.app_context():
|
|
admin = db.session.query(User).filter_by(username='wecom-settings-admin').first()
|
|
if admin is None:
|
|
admin = User(
|
|
username='wecom-settings-admin',
|
|
email='wecom-settings-admin@example.com',
|
|
role='admin',
|
|
status='active',
|
|
)
|
|
admin.set_password('test-password')
|
|
db.session.add(admin)
|
|
db.session.commit()
|
|
token = issue_local_access_token(admin)
|
|
return {'Authorization': f'Bearer {token}', 'Content-Type': 'application/json'}
|
|
|
|
|
|
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, 'ROOT_ENV_LOCAL_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_auth_headers(app))
|
|
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_auth_headers(app))
|
|
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, 'ROOT_ENV_LOCAL_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_auth_headers(app))
|
|
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_auth_headers(app))
|
|
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, 'ROOT_ENV_LOCAL_PATH', env_file)
|
|
|
|
res = client.put(
|
|
'/api/admin/wecom/system-settings',
|
|
json={'corp_id': 'ww-from-db-only', 'enabled': True},
|
|
headers=_admin_auth_headers(app),
|
|
)
|
|
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_auth_headers(app))
|
|
assert res.status_code == 200
|
|
data = res.get_json()
|
|
assert data['corp_id'] == 'ww-from-db-only'
|
|
assert data['enabled'] is True
|