Files
tap/tests/feishu.browser.py
T

123 lines
9.1 KiB
Python
Raw Normal View History

"""Run through feishu.integration.mjs with FEISHU_BROWSER_PYTHON; uses its disposable instance."""
import os
from urllib.parse import urlsplit, parse_qs, urlencode
from pathlib import Path
from playwright.sync_api import sync_playwright, expect
origin = os.environ['TEST_ORIGIN']
artifacts = Path('/tmp/demand-feishu-browser-qa')
artifacts.mkdir(exist_ok=True)
errors = []
with sync_playwright() as p:
browser = p.chromium.launch(headless=True, **({'executable_path': os.environ['PLAYWRIGHT_CHROMIUM_EXECUTABLE']} if os.environ.get('PLAYWRIGHT_CHROMIUM_EXECUTABLE') else {}))
page = browser.new_page(viewport={'width': 1440, 'height': 1000}, device_scale_factor=1)
page.set_extra_http_headers({'x-test-client-ip': '198.51.100.30'})
page.on('pageerror', lambda e: errors.append(str(e)))
authorize_urls = []
def intercept_start(route):
response = route.fetch(max_redirects=0)
authorize_urls.append(response.headers['location'])
route.fulfill(status=200, content_type='text/html; charset=utf-8', body='<h1>模拟飞书授权页</h1>')
page.route('**/api/auth/feishu/start', intercept_start)
page.goto(origin + '/login', wait_until='networkidle')
expect(page.get_by_role('button', name='飞书登录', exact=True)).to_be_visible()
page.screenshot(path=str(artifacts / 'login-desktop.png'), full_page=True)
page.get_by_role('button', name='飞书登录', exact=True).click()
expect(page.get_by_role('heading', name='模拟飞书授权页')).to_be_visible()
assert authorize_urls[0].startswith('https://accounts.feishu.cn/')
assert 'code_challenge_method=S256' in authorize_urls[0]
page.goto(origin + '/?demand=' + os.environ['TEST_DEMAND_ID'], wait_until='networkidle')
assert '/login?next=' in page.url
page.get_by_label('账号', exact=True).fill(os.environ['TEST_USERNAME'])
page.get_by_label('密码', exact=True).fill(os.environ['TEST_PASSWORD'])
page.get_by_role('button', name='登录工作台', exact=True).click()
page.wait_for_url(lambda url: '?demand=' in url and '/login' not in url)
page.wait_for_load_state('networkidle')
expect(page.get_by_role('dialog')).to_be_visible()
expect(page.get_by_role('dialog')).to_contain_text('飞书通知联调需求')
page.screenshot(path=str(artifacts / 'notification-deep-link.png'), full_page=True)
page.goto(origin + '/account/feishu', wait_until='networkidle')
expect(page.get_by_role('heading', name='飞书绑定', exact=True)).to_be_visible()
expect(page.get_by_text('飞书联调用户', exact=True)).to_be_visible()
page.screenshot(path=str(artifacts / 'binding-desktop.png'), full_page=True)
page.goto(origin + '/admin/feishu', wait_until='networkidle')
expect(page.get_by_role('heading', name='飞书集成', exact=True)).to_be_visible()
page.get_by_role('button', name='刷新状态', exact=True).click()
expect(page.get_by_role('status')).to_contain_text('状态已刷新')
expect(page.get_by_role('cell', name='已发送', exact=True).first).to_be_visible()
page.screenshot(path=str(artifacts / 'admin-desktop.png'), full_page=False)
page.get_by_label('通知筛选', exact=True).select_option('failed')
expect(page.get_by_role('cell', name='发送失败', exact=True)).to_have_count(50)
page.get_by_role('button', name='下一页', exact=True).click()
expect(page.get_by_role('cell', name='发送失败', exact=True)).to_have_count(1)
expect(page.get_by_text('第 2 页', exact=False)).to_be_visible()
page.get_by_role('button', name='重试发送', exact=True).click()
expect(page.get_by_role('status')).to_contain_text('已加入发送队列')
expect(page.get_by_text('当前页暂无失败通知。', exact=False)).to_be_visible()
page.get_by_role('button', name='上一页', exact=True).click()
expect(page.get_by_role('cell', name='发送失败', exact=True)).to_have_count(50)
page.get_by_role('button', name='刷新状态', exact=True).click()
expect(page.get_by_label('通知筛选', exact=True)).to_have_value('failed')
page.screenshot(path=str(artifacts / 'admin-failed-filter.png'), full_page=False)
for path, name in [('/admin/feishu', 'admin-mobile'), ('/account/feishu', 'binding-mobile')]:
page.set_viewport_size({'width': 375, 'height': 812})
page.goto(origin + path, wait_until='networkidle')
assert page.evaluate('document.documentElement.scrollWidth <= innerWidth'), name + ' overflow'
page.screenshot(path=str(artifacts / (name + '.png')), full_page=False)
if name == 'admin-mobile':
page.get_by_role('button', name='下一页', exact=True).scroll_into_view_if_needed()
page.screenshot(path=str(artifacts / 'admin-mobile-pagination.png'), full_page=False)
anonymous = browser.new_context(viewport={'width': 375, 'height': 812})
mobile = anonymous.new_page()
mobile.goto(origin + '/login?feishuError=expired', wait_until='networkidle')
expect(mobile.get_by_role('alert').filter(has_text='授权已过期')).to_be_visible()
assert mobile.evaluate('document.documentElement.scrollWidth <= innerWidth')
mobile.screenshot(path=str(artifacts / 'login-error-mobile.png'), full_page=True)
newcomer = browser.new_context(viewport={'width': 390, 'height': 844}, extra_http_headers={'x-test-client-ip': '198.51.100.40'})
registration = newcomer.new_page()
registration.on('pageerror', lambda e: errors.append(str(e)))
def finish_mock_authorization(route):
response = route.fetch(max_redirects=0)
params = parse_qs(urlsplit(response.headers['location']).query)
callback = origin + '/api/auth/feishu/callback?' + urlencode({'state': params['state'][0], 'code': 'browser-new.' + params['code_challenge'][0]})
route.fulfill(status=303, headers={'location': callback, 'set-cookie': response.headers['set-cookie'], 'cache-control': 'no-store'}, body='')
registration.route('**/api/auth/feishu/start', finish_mock_authorization)
registration.goto(origin + '/login', wait_until='networkidle')
expect(registration.get_by_text('首次飞书登录将自动注册普通成员账号。', exact=False)).to_be_visible()
registration.get_by_role('button', name='飞书登录', exact=True).click()
registration.wait_for_url(lambda url: url.rstrip('/') == origin)
registration.wait_for_load_state('networkidle')
auto_user = newcomer.request.get(origin + '/api/auth/me').json()['user']
assert auto_user['role'] == 'user' and not auto_user['mustChangePassword']
assert newcomer.request.get(origin + '/api/admin/users').status == 403
registration.get_by_role('button', name='账号菜单', exact=True).click()
expect(registration.get_by_role('menuitem', name='设置密码', exact=True)).to_be_visible()
expect(registration.get_by_role('menuitem', name='账号管理', exact=True)).to_have_count(0)
registration.goto(origin + '/account/feishu', wait_until='networkidle')
expect(registration.get_by_role('button', name='验证密码并解除绑定', exact=True)).to_have_count(0)
registration.get_by_role('link', name='设置系统密码', exact=True).click()
expect(registration.get_by_role('heading', name='设置系统密码', exact=True)).to_be_visible()
expect(registration.get_by_label('当前密码', exact=True)).to_have_count(0)
assert registration.evaluate('document.documentElement.scrollWidth <= innerWidth')
registration.screenshot(path=str(artifacts / 'first-password-mobile.png'), full_page=True)
registration.get_by_label('新密码', exact=True).fill(os.environ['TEST_PASSWORD'])
registration.get_by_label('确认新密码', exact=True).fill(os.environ['TEST_PASSWORD'])
registration.get_by_role('button', name='保存密码并进入工作台', exact=True).click()
registration.wait_for_url(lambda url: url.rstrip('/') == origin)
registration.goto(origin + '/account/feishu', wait_until='networkidle')
registration.get_by_label('当前系统密码', exact=True).fill(os.environ['TEST_PASSWORD'])
registration.get_by_role('button', name='验证密码并解除绑定', exact=True).click()
expect(registration.get_by_text('已解除飞书绑定', exact=False)).to_be_visible()
expect(registration.get_by_role('button', name='绑定我的飞书账号', exact=True)).to_be_visible()
registration.goto(origin + '/', wait_until='networkidle')
registration.get_by_role('button', name='账号菜单', exact=True).click()
registration.get_by_role('menuitem', name='退出登录', exact=True).click()
registration.wait_for_url('**/login')
registration.get_by_label('账号', exact=True).fill(auto_user['username'])
registration.get_by_label('密码', exact=True).fill(os.environ['TEST_PASSWORD'])
registration.get_by_role('button', name='登录工作台', exact=True).click()
registration.wait_for_url(lambda url: url.rstrip('/') == origin)
assert newcomer.request.get(origin + '/api/auth/me').json()['user']['userId'] == auto_user['userId']
assert not errors, errors
browser.close()
print('Feishu browser QA passed: desktop/mobile, first-login registration, first password setup, unlink/password fallback, permissions, notification history, no page errors.')