feat: add council user role and restrict crawl runs

This commit is contained in:
codex committed 2026-11-12 09:35:26 +00:00
1 parent 59dcacbaff
commit 508aae4f8a
28 files changed
+217 -65

No files matched your search

+38 -6
View File
@@ -126,13 +126,19 @@ def test_regular_user_cannot_start_scraping(accounts, path):
assert client.get('/api/stats').status_code == 200
def test_regular_user_edit_audit_and_personal_history_isolation(accounts):
def test_council_user_edit_audit_and_personal_history_isolation(accounts):
client, db, settings, admin, user, employee = accounts
act_as(client, user, settings)
candidate = council.council_candidates(db)['candidates'][0]
article = candidate['articles'][0]
data = {'employee_id': employee.id, 'edit_key': article['edit_key'], 'revision': 0,
'title': 'Изменённая статья', 'journal': 'Новый журнал', 'quartile': 'Q2', 'specialty_codes': ['1.2.2']}
act_as(client, user, settings)
assert client.post('/admin/council/publications', json=data, headers={'X-Council-Edit': '1'}).status_code == 403
assert client.post(f'/admin/users/{user.id}', data={'role': 'council', 'is_active': 'true'},
headers=act_as(client, admin, settings), follow_redirects=False).status_code == 303
db.refresh(user)
assert user.role == 'council' and user.session_version == 1
act_as(client, user, settings)
response = client.post('/admin/council/publications', json=data, headers={'X-Council-Edit': '1'})
assert response.status_code == 200
action = db.scalar(select(UserAction).where(UserAction.action == 'card_edit'))
@@ -150,6 +156,29 @@ def test_regular_user_edit_audit_and_personal_history_isolation(accounts):
assert db.scalars(select(UserAction).where(UserAction.user_id == user.id)).all() == [action]
@pytest.mark.parametrize('role', ['user', 'council', 'admin'])
def test_navigation_and_run_access_match_role(accounts, role):
client, db, settings, _, user, _ = accounts
user.role = role
db.commit()
act_as(client, user, settings)
dashboard = BeautifulSoup(client.get('/admin').text, 'html.parser')
links = {link['href'] for link in dashboard.select('nav a[href]')}
assert {'/admin', '/admin/directory', '/admin/account', '/admin/guide'} <= links
assert ('/admin/council' in links) == (role != 'user')
assert ('/admin/runs' in links) == (role == 'admin')
assert ('/admin/users' in links) == (role == 'admin')
assert bool(dashboard.select('[data-progress-panel]')) == (role == 'admin')
assert ('latest_run' in client.get('/api/stats').json()) == (role == 'admin')
for path in ('/admin/runs', '/api/crawl-runs', '/api/crawl-runs/latest', '/api/crawl-runs/1'):
assert client.get(path).status_code == (200 if role == 'admin' else 403)
assert client.get('/admin/runs/1', follow_redirects=False).status_code == (303 if role == 'admin' else 403)
client.cookies.clear()
assert client.get('/admin/directory', follow_redirects=False).headers['location'] == '/admin/login'
assert client.get('/admin/council').status_code == 200
assert client.get('/docs').status_code == 404
def test_admin_reset_invalidates_sessions_and_preserves_password_change_date(accounts):
client, db, settings, admin, user, _ = accounts
user.password_changed_at = utcnow()
@@ -248,6 +277,8 @@ def test_cross_origin_and_session_expiry_fail_closed(accounts, monkeypatch):
def test_password_errors_and_noop_card_edit_are_not_audited(accounts):
client, db, settings, _, user, employee = accounts
user.role = 'council'
db.commit()
headers = act_as(client, user, settings)
before = user.password_hash
for current, new, confirmation in [('wrong', 'New valid personal password', 'New valid personal password'),
@@ -290,7 +321,7 @@ def test_password_change_and_audit_rollback_atomically(accounts, monkeypatch):
assert db.scalar(select(UserAction).where(UserAction.action == 'guide_start')) is None
@pytest.mark.parametrize('role', ['user', 'admin'])
@pytest.mark.parametrize('role', ['user', 'council', 'admin'])
def test_guide_first_password_dismissal_and_repeat_change(accounts, role):
client, db, settings, _, user, _ = accounts
user.role = role
@@ -306,11 +337,12 @@ def test_guide_first_password_dismissal_and_repeat_change(accounts, role):
assert page.headers['Cache-Control'] == 'no-store'
guide = client.get('/admin/guide')
parsed = BeautifulSoup(guide.text, 'html.parser')
assert len(parsed.select('[data-guide-step]')) == 6
assert len(parsed.select('[data-guide-step]')) == (2 if role == 'user' else 6)
assert not parsed.select('[data-guide]')
assert 'Прочитайте показатели сотрудника' in guide.text and 'Проверьте строку публикации' in guide.text
assert ('Прочитайте показатели сотрудника' in guide.text) == (role != 'user')
assert ('Проверьте строку публикации' in guide.text) == (role != 'user')
assert ('управлять доступом' in guide.text) == (role == 'admin')
assert 'не ограничивает «ВСЕГО»' in guide.text
assert ('не ограничивает «ВСЕГО»' in guide.text) == (role != 'user')
csrf = BeautifulSoup(page.text, 'html.parser').select_one('[data-guide]')['data-guide-csrf']
assert client.post('/admin/guide/dismiss', headers={'X-CSRF-Token': 'wrong'}, follow_redirects=False).status_code == 403
assert 'data-guide-pending="true"' in client.get('/admin/directory').text