"""Tests for OA userInfo_view → wecom_userid sync.""" from unittest.mock import patch import pytest from app.models import User from app.services.integrations.oa.wecom_sync import sync_wecom_userids_from_oa @pytest.fixture def oa_rows_for_local_user(): return [ {'workcode': '26030025', 'wecom_userid': '21784', 'name': 'sophie'}, ] def test_sync_wecom_userids_from_oa_dry_run(app, oa_rows_for_local_user): with app.app_context(): user = User( username='26030025', email='26030025@test.local', employee_id='26030025', real_name='sophie', role='user', status='active', ) user.set_password('test123') from app import db db.session.add(user) db.session.commit() with patch( 'app.services.integrations.oa.wecom_sync.fetch_oa_wecom_bindings_for_workcodes', return_value=oa_rows_for_local_user, ) as fetch_mock: summary = sync_wecom_userids_from_oa(apply=False) fetch_mock.assert_called_once_with(['26030025']) assert summary['local_candidates'] == 1 assert summary['wecom_users'] == 1 assert summary['local_matched'] == 1 assert summary['would_update'] == 1 assert summary['local_missing'] == 0 assert summary['pending_updates'][0]['wecom_userid'] == '21784' db.session.refresh(user) assert user.wecom_userid is None def test_sync_wecom_userids_from_oa_apply(app, oa_rows_for_local_user): with app.app_context(): user = User( username='26030025', email='26030025@test.local', employee_id='26030025', real_name='sophie', role='user', status='active', ) user.set_password('test123') from app import db db.session.add(user) db.session.commit() with patch( 'app.services.integrations.oa.wecom_sync.fetch_oa_wecom_bindings_for_workcodes', return_value=oa_rows_for_local_user, ): summary = sync_wecom_userids_from_oa(apply=True) assert summary['updated'] == 1 db.session.refresh(user) assert user.wecom_userid == '21784' def test_sync_wecom_userids_from_oa_local_not_in_oa(app): with app.app_context(): user = User( username='missing_oa', email='missing_oa@test.local', employee_id='99999999', real_name='missing', role='user', status='active', ) user.set_password('test123') from app import db db.session.add(user) db.session.commit() with patch( 'app.services.integrations.oa.wecom_sync.fetch_oa_wecom_bindings_for_workcodes', return_value=[], ) as fetch_mock: summary = sync_wecom_userids_from_oa(apply=False) fetch_mock.assert_called_once_with(['99999999']) assert summary['local_candidates'] == 1 assert summary['wecom_users'] == 0 assert summary['local_matched'] == 0 assert summary['local_missing'] == 1