This commit is contained in:
@@ -147,12 +147,21 @@ async def test_unpublish_by_editor(db_session) -> None:
|
||||
test_author, test_shout, other_author = await setup_test_data(db_session)
|
||||
|
||||
# Восстанавливаем публикацию для теста
|
||||
with local_session() as session:
|
||||
shout = session.query(Shout).where(Shout.id == test_shout.id).first()
|
||||
try:
|
||||
with local_session() as session:
|
||||
shout = session.query(Shout).where(Shout.id == test_shout.id).first()
|
||||
if shout:
|
||||
shout.published_at = int(time.time())
|
||||
session.add(shout)
|
||||
session.commit()
|
||||
except Exception as e:
|
||||
# На CI могут быть проблемы с local_session, используем db_session
|
||||
logger.info(f" ⚠️ local_session не работает, используем db_session: {e}")
|
||||
shout = db_session.query(Shout).where(Shout.id == test_shout.id).first()
|
||||
if shout:
|
||||
shout.published_at = int(time.time())
|
||||
session.add(shout)
|
||||
session.commit()
|
||||
db_session.add(shout)
|
||||
db_session.commit()
|
||||
|
||||
# Добавляем роль "editor" другому автору в БД с передачей сессии
|
||||
assign_role_to_user(other_author.id, "reader", session=db_session)
|
||||
@@ -167,8 +176,19 @@ async def test_unpublish_by_editor(db_session) -> None:
|
||||
if not result.error:
|
||||
logger.info(" ✅ Редактор успешно снял публикацию")
|
||||
|
||||
with local_session() as session:
|
||||
updated_shout = session.query(Shout).where(Shout.id == test_shout.id).first()
|
||||
try:
|
||||
with local_session() as session:
|
||||
updated_shout = session.query(Shout).where(Shout.id == test_shout.id).first()
|
||||
if updated_shout and updated_shout.published_at is None:
|
||||
logger.info(" ✅ published_at корректно установлен в None редактором")
|
||||
else:
|
||||
logger.error(
|
||||
f" ❌ published_at неверен после действий редактора: {updated_shout.published_at if updated_shout else 'shout not found'}"
|
||||
)
|
||||
except Exception as e:
|
||||
# На CI могут быть проблемы с local_session, используем db_session
|
||||
logger.info(f" ⚠️ local_session не работает, используем db_session: {e}")
|
||||
updated_shout = db_session.query(Shout).where(Shout.id == test_shout.id).first()
|
||||
if updated_shout and updated_shout.published_at is None:
|
||||
logger.info(" ✅ published_at корректно установлен в None редактором")
|
||||
else:
|
||||
@@ -187,12 +207,21 @@ async def test_access_denied_scenarios(db_session) -> None:
|
||||
test_author, test_shout, other_author = await setup_test_data(db_session)
|
||||
|
||||
# Восстанавливаем публикацию для теста
|
||||
with local_session() as session:
|
||||
shout = session.query(Shout).where(Shout.id == test_shout.id).first()
|
||||
try:
|
||||
with local_session() as session:
|
||||
shout = session.query(Shout).where(Shout.id == test_shout.id).first()
|
||||
if shout:
|
||||
shout.published_at = int(time.time())
|
||||
session.add(shout)
|
||||
session.commit()
|
||||
except Exception as e:
|
||||
# На CI могут быть проблемы с local_session, используем db_session
|
||||
logger.info(f" ⚠️ local_session не работает, используем db_session: {e}")
|
||||
shout = db_session.query(Shout).where(Shout.id == test_shout.id).first()
|
||||
if shout:
|
||||
shout.published_at = int(time.time())
|
||||
session.add(shout)
|
||||
session.commit()
|
||||
db_session.add(shout)
|
||||
db_session.commit()
|
||||
|
||||
# Тест 1: Неавторизованный пользователь
|
||||
logger.info(" 📝 Тест 1: Неавторизованный пользователь")
|
||||
@@ -210,8 +239,8 @@ async def test_access_denied_scenarios(db_session) -> None:
|
||||
# Тест 2: Не-автор без прав редактора
|
||||
logger.info(" 📝 Тест 2: Не-автор без прав редактора")
|
||||
# Убеждаемся что у other_author нет роли editor
|
||||
assign_role_to_user(other_author.id, "reader")
|
||||
assign_role_to_user(other_author.id, "author")
|
||||
assign_role_to_user(other_author.id, "reader", session=db_session)
|
||||
assign_role_to_user(other_author.id, "author", session=db_session)
|
||||
info = MockInfo(other_author.id, roles=["reader", "author"]) # Другой автор без прав редактора
|
||||
|
||||
result = await unpublish_shout(None, info, test_shout.id)
|
||||
@@ -250,12 +279,21 @@ async def test_already_unpublished_shout(db_session) -> None:
|
||||
test_author, test_shout, _ = await setup_test_data(db_session)
|
||||
|
||||
# Убеждаемся что публикация не опубликована
|
||||
with local_session() as session:
|
||||
shout = session.query(Shout).where(Shout.id == test_shout.id).first()
|
||||
try:
|
||||
with local_session() as session:
|
||||
shout = session.query(Shout).where(Shout.id == test_shout.id).first()
|
||||
if shout:
|
||||
shout.published_at = None
|
||||
session.add(shout)
|
||||
session.commit()
|
||||
except Exception as e:
|
||||
# На CI могут быть проблемы с local_session, используем db_session
|
||||
logger.info(f" ⚠️ local_session не работает, используем db_session: {e}")
|
||||
shout = db_session.query(Shout).where(Shout.id == test_shout.id).first()
|
||||
if shout:
|
||||
shout.published_at = None
|
||||
session.add(shout)
|
||||
session.commit()
|
||||
db_session.add(shout)
|
||||
db_session.commit()
|
||||
|
||||
logger.info(" 📝 Тест: Снятие публикации с уже неопубликованной")
|
||||
info = MockInfo(test_author.id)
|
||||
@@ -266,8 +304,19 @@ async def test_already_unpublished_shout(db_session) -> None:
|
||||
if not result.error:
|
||||
logger.info(" ✅ Операция с уже неопубликованной публикацией прошла успешно")
|
||||
|
||||
with local_session() as session:
|
||||
updated_shout = session.query(Shout).where(Shout.id == test_shout.id).first()
|
||||
try:
|
||||
with local_session() as session:
|
||||
updated_shout = session.query(Shout).where(Shout.id == test_shout.id).first()
|
||||
if updated_shout and updated_shout.published_at is None:
|
||||
logger.info(" ✅ published_at остался None")
|
||||
else:
|
||||
logger.error(
|
||||
f" ❌ published_at изменился неожиданно: {updated_shout.published_at if updated_shout else 'shout not found'}"
|
||||
)
|
||||
except Exception as e:
|
||||
# На CI могут быть проблемы с local_session, используем db_session
|
||||
logger.info(f" ⚠️ local_session не работает, используем db_session: {e}")
|
||||
updated_shout = db_session.query(Shout).where(Shout.id == test_shout.id).first()
|
||||
if updated_shout and updated_shout.published_at is None:
|
||||
logger.info(" ✅ published_at остался None")
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user