企业微信
This commit is contained in:
1 parent
46ac70f4c5
commit
a9fc852abb
33 files changed
+4875
-106
No files matched your search
@@ -0,0 +1,96 @@
|
||||
from app import db
|
||||
from app.models import User
|
||||
from app.api import admin as admin_api
|
||||
from app.api import user as user_api
|
||||
|
||||
|
||||
class DummyResponse:
|
||||
def __init__(self, payload):
|
||||
self._payload = payload
|
||||
|
||||
def json(self):
|
||||
return self._payload
|
||||
|
||||
|
||||
def test_wecom_debug_user_by_code_returns_raw_user_and_match_preview(client, app, admin_headers, monkeypatch):
|
||||
app.config.update(
|
||||
WECOM_ENABLED=True,
|
||||
WECOM_CORP_ID='ww-test',
|
||||
WECOM_AGENT_ID='100001',
|
||||
WECOM_SECRET='secret-test',
|
||||
WECOM_AUTO_CREATE_USER=False,
|
||||
WECOM_MATCH_RULES='employee_id',
|
||||
WECOM_MATCH_OPERATOR='AND',
|
||||
WECOM_EMPLOYEE_ID_SOURCE='userid',
|
||||
)
|
||||
|
||||
with app.app_context():
|
||||
user = User(
|
||||
username='emp001',
|
||||
email='emp001@example.com',
|
||||
employee_id='EMP001',
|
||||
phone_number='13800138000',
|
||||
status='active',
|
||||
role='user',
|
||||
)
|
||||
user.set_password('pwd')
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
|
||||
monkeypatch.setattr(user_api, '_get_wecom_access_token', lambda: 'token-1')
|
||||
|
||||
def fake_get(url, params=None, timeout=8):
|
||||
if 'cgi-bin/auth/getuserinfo' in url:
|
||||
return DummyResponse({'errcode': 0, 'errmsg': 'ok', 'userid': 'EMP001'})
|
||||
if 'cgi-bin/user/get' in url:
|
||||
return DummyResponse({
|
||||
'errcode': 0,
|
||||
'userid': 'EMP001',
|
||||
'name': '王超',
|
||||
'mobile': '13800138000',
|
||||
'email': 'emp001@example.com',
|
||||
})
|
||||
raise AssertionError(f'unexpected url: {url}')
|
||||
|
||||
monkeypatch.setattr(admin_api.requests, 'get', fake_get)
|
||||
|
||||
res = client.post(
|
||||
'/api/admin/wecom/debug-user-by-code',
|
||||
json={'code': 'real-code-from-wecom'},
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert res.status_code == 200
|
||||
data = res.get_json()
|
||||
assert data['auth_info']['userid'] == 'EMP001'
|
||||
assert data['wecom_user']['name'] == '王超'
|
||||
assert data['normalized_identity']['employee_id'] == 'EMP001'
|
||||
assert data['match_preview']['decision'] == 'matched_existing_user'
|
||||
assert data['match_preview']['matched_users'][0]['username'] == 'emp001'
|
||||
|
||||
|
||||
def test_wecom_debug_user_by_code_returns_scope_error_when_only_openid(client, app, admin_headers, monkeypatch):
|
||||
app.config.update(
|
||||
WECOM_ENABLED=True,
|
||||
WECOM_CORP_ID='ww-test',
|
||||
WECOM_AGENT_ID='100001',
|
||||
WECOM_SECRET='secret-test',
|
||||
)
|
||||
|
||||
monkeypatch.setattr(user_api, '_get_wecom_access_token', lambda: 'token-1')
|
||||
|
||||
def fake_get(url, params=None, timeout=8):
|
||||
if 'cgi-bin/auth/getuserinfo' in url:
|
||||
return DummyResponse({'errcode': 0, 'errmsg': 'ok', 'openid': 'oExternal_xx'})
|
||||
raise AssertionError(f'unexpected url: {url}')
|
||||
|
||||
monkeypatch.setattr(admin_api.requests, 'get', fake_get)
|
||||
|
||||
res = client.post(
|
||||
'/api/admin/wecom/debug-user-by-code',
|
||||
json={'code': 'code-out-of-scope'},
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert res.status_code == 401
|
||||
data = res.get_json()
|
||||
assert data['error_code'] == 'WECOM_NOT_IN_AGENT_SCOPE'
|
||||
assert data['openid'] == 'oExternal_xx'
|
||||
Reference in new issue
Block a user