108 lines
3.6 KiB
Python
108 lines
3.6 KiB
Python
from app import db
|
|
from app.models import Agent, User
|
|
|
|
|
|
def test_wecom_provision_create_bind_and_set_permissions(client, app, admin_headers):
|
|
with app.app_context():
|
|
a1 = Agent(name='A1', type='api', dify_app_id='app-a1', dify_api_key='k-a1', is_active=True)
|
|
a2 = Agent(name='A2', type='api', dify_app_id='app-a2', dify_api_key='k-a2', is_active=True)
|
|
db.session.add_all([a1, a2])
|
|
db.session.commit()
|
|
a1_id, a2_id = a1.id, a2.id
|
|
|
|
payload = {
|
|
'create_user': True,
|
|
'user': {
|
|
'username': 'emp2001',
|
|
'real_name': 'Emp 2001',
|
|
'employee_id': 'EMP2001',
|
|
'phone_number': '13800111222',
|
|
'email': 'emp2001@example.com',
|
|
'department': 'IT',
|
|
'role': 'user',
|
|
'status': 'active',
|
|
},
|
|
'bind': {
|
|
'wecom_userid': 'EMP2001',
|
|
'wecom_unionid': 'UNION-2001',
|
|
},
|
|
'agent_ids': [a1_id, a2_id],
|
|
}
|
|
res = client.post('/api/admin/wecom/provision', json=payload, headers=admin_headers)
|
|
assert res.status_code == 201
|
|
body = res.get_json()
|
|
assert body['status'] == 'ok'
|
|
assert body['permission_updated'] is True
|
|
|
|
with app.app_context():
|
|
user = db.session.get(User, body['user_id'])
|
|
assert user is not None
|
|
assert user.wecom_userid == 'EMP2001'
|
|
assert user.wecom_unionid == 'UNION-2001'
|
|
assert sorted([a.id for a in user.agents]) == sorted([a1_id, a2_id])
|
|
|
|
|
|
def test_wecom_provision_conflict_should_not_create_new_user(client, app, admin_headers):
|
|
with app.app_context():
|
|
existing = User(username='bound_user', email='bound@example.com', role='user', status='active', wecom_userid='WX-DUP')
|
|
existing.set_password('pwd')
|
|
db.session.add(existing)
|
|
db.session.commit()
|
|
|
|
payload = {
|
|
'create_user': True,
|
|
'user': {
|
|
'username': 'new_to_rollback',
|
|
'email': 'new_to_rollback@example.com',
|
|
},
|
|
'bind': {
|
|
'wecom_userid': 'WX-DUP',
|
|
},
|
|
'agent_ids': [],
|
|
}
|
|
res = client.post('/api/admin/wecom/provision', json=payload, headers=admin_headers)
|
|
assert res.status_code == 400
|
|
assert 'already bound to user' in res.get_json()['error']
|
|
|
|
with app.app_context():
|
|
should_not_exist = db.session.query(User).filter_by(username='new_to_rollback').first()
|
|
assert should_not_exist is None
|
|
|
|
|
|
def test_wecom_provision_bind_existing_user(client, app, admin_headers):
|
|
with app.app_context():
|
|
user = User(
|
|
username='existing_user',
|
|
email='existing@example.com',
|
|
employee_id='EMP3001',
|
|
phone_number='13800990011',
|
|
role='user',
|
|
status='active',
|
|
)
|
|
user.set_password('pwd')
|
|
agent = Agent(name='A3', type='api', dify_app_id='app-a3', dify_api_key='k-a3', is_active=True)
|
|
db.session.add_all([user, agent])
|
|
db.session.commit()
|
|
user_id = user.id
|
|
agent_id = agent.id
|
|
|
|
payload = {
|
|
'create_user': False,
|
|
'user_id': user_id,
|
|
'bind': {
|
|
'wecom_userid': 'EMP3001',
|
|
},
|
|
'agent_ids': [agent_id],
|
|
}
|
|
res = client.post('/api/admin/wecom/provision', json=payload, headers=admin_headers)
|
|
assert res.status_code == 200
|
|
body = res.get_json()
|
|
assert body['binding'] == 'updated'
|
|
assert body['permission_updated'] is True
|
|
|
|
with app.app_context():
|
|
user = db.session.get(User, user_id)
|
|
assert user.wecom_userid == 'EMP3001'
|
|
assert [a.id for a in user.agents] == [agent_id]
|
|
|