Improve workers for incoming/outgoing activities
This commit is contained in:
parent
9d6b1e5c17
commit
8633696da0
10 changed files with 209 additions and 135 deletions
|
@ -12,6 +12,8 @@ from app import activitypub as ap
|
|||
from app import models
|
||||
from app.actor import LOCAL_ACTOR
|
||||
from app.ap_object import RemoteObject
|
||||
from app.database import AsyncSession
|
||||
from app.incoming_activities import fetch_next_incoming_activity
|
||||
from app.incoming_activities import process_next_incoming_activity
|
||||
from tests import factories
|
||||
from tests.utils import mock_httpsig_checker
|
||||
|
@ -21,6 +23,12 @@ from tests.utils import setup_remote_actor
|
|||
from tests.utils import setup_remote_actor_as_follower
|
||||
|
||||
|
||||
async def _process_next_incoming_activity(db_session: AsyncSession) -> None:
|
||||
next_activity = await fetch_next_incoming_activity(db_session, set())
|
||||
assert next_activity
|
||||
await process_next_incoming_activity(db_session, next_activity)
|
||||
|
||||
|
||||
def test_inbox_requires_httpsig(
|
||||
client: TestClient,
|
||||
):
|
||||
|
@ -64,7 +72,7 @@ def test_inbox_incoming_follow_request(
|
|||
# Then the server returns a 204
|
||||
assert response.status_code == 202
|
||||
|
||||
run_async(process_next_incoming_activity)
|
||||
run_async(_process_next_incoming_activity)
|
||||
|
||||
# And the actor was saved in DB
|
||||
saved_actor = db.execute(select(models.Actor)).scalar_one()
|
||||
|
@ -122,7 +130,7 @@ def test_inbox_incoming_follow_request__manually_approves_followers(
|
|||
assert response.status_code == 202
|
||||
|
||||
with mock.patch("app.boxes.MANUALLY_APPROVES_FOLLOWERS", True):
|
||||
run_async(process_next_incoming_activity)
|
||||
run_async(_process_next_incoming_activity)
|
||||
|
||||
# And the actor was saved in DB
|
||||
saved_actor = db.execute(select(models.Actor)).scalar_one()
|
||||
|
@ -177,7 +185,7 @@ def test_inbox_accept_follow_request(
|
|||
# Then the server returns a 204
|
||||
assert response.status_code == 202
|
||||
|
||||
run_async(process_next_incoming_activity)
|
||||
run_async(_process_next_incoming_activity)
|
||||
|
||||
# And the Accept activity was saved in the inbox
|
||||
inbox_activity = db.execute(select(models.InboxObject)).scalar_one()
|
||||
|
@ -224,7 +232,7 @@ def test_inbox__create_from_follower(
|
|||
assert response.status_code == 202
|
||||
|
||||
# And when processing the incoming activity
|
||||
run_async(process_next_incoming_activity)
|
||||
run_async(_process_next_incoming_activity)
|
||||
|
||||
# Then the Create activity was saved
|
||||
create_activity_from_inbox: models.InboxObject | None = db.execute(
|
||||
|
@ -278,7 +286,7 @@ def test_inbox__create_already_deleted_object(
|
|||
assert response.status_code == 202
|
||||
|
||||
# And when processing the incoming activity
|
||||
run_async(process_next_incoming_activity)
|
||||
run_async(_process_next_incoming_activity)
|
||||
|
||||
# Then the Create activity was saved
|
||||
create_activity_from_inbox: models.InboxObject | None = db.execute(
|
||||
|
@ -334,7 +342,7 @@ def test_inbox__actor_is_blocked(
|
|||
assert response.status_code == 202
|
||||
|
||||
# And when processing the incoming activity from a blocked actor
|
||||
run_async(process_next_incoming_activity)
|
||||
run_async(_process_next_incoming_activity)
|
||||
|
||||
# Then the Create activity was discarded
|
||||
assert (
|
||||
|
|
|
@ -5,13 +5,13 @@ import pytest
|
|||
import respx
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app import models
|
||||
from app.actor import LOCAL_ACTOR
|
||||
from app.ap_object import RemoteObject
|
||||
from app.database import AsyncSession
|
||||
from app.outgoing_activities import _MAX_RETRIES
|
||||
from app.outgoing_activities import fetch_next_outgoing_activity
|
||||
from app.outgoing_activities import new_outgoing_activity
|
||||
from app.outgoing_activities import process_next_outgoing_activity
|
||||
from tests import factories
|
||||
|
@ -65,15 +65,18 @@ async def test_new_outgoing_activity(
|
|||
assert outgoing_activity.recipient == inbox_url
|
||||
|
||||
|
||||
def test_process_next_outgoing_activity__no_next_activity(
|
||||
db: Session,
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_next_outgoing_activity__no_next_activity(
|
||||
respx_mock: respx.MockRouter,
|
||||
async_db_session: AsyncSession,
|
||||
) -> None:
|
||||
assert process_next_outgoing_activity(db) is False
|
||||
next_activity = await fetch_next_outgoing_activity(async_db_session, set())
|
||||
assert next_activity is None
|
||||
|
||||
|
||||
def test_process_next_outgoing_activity__server_200(
|
||||
db: Session,
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_next_outgoing_activity__server_200(
|
||||
async_db_session: AsyncSession,
|
||||
respx_mock: respx.MockRouter,
|
||||
) -> None:
|
||||
# And an outgoing activity
|
||||
|
@ -91,19 +94,24 @@ def test_process_next_outgoing_activity__server_200(
|
|||
|
||||
# When processing the next outgoing activity
|
||||
# Then it is processed
|
||||
assert process_next_outgoing_activity(db) is True
|
||||
next_activity = await fetch_next_outgoing_activity(async_db_session, set())
|
||||
assert next_activity
|
||||
await process_next_outgoing_activity(async_db_session, next_activity)
|
||||
|
||||
assert respx_mock.calls.call_count == 1
|
||||
|
||||
outgoing_activity = db.query(models.OutgoingActivity).one()
|
||||
outgoing_activity = (
|
||||
await async_db_session.execute(select(models.OutgoingActivity))
|
||||
).scalar_one()
|
||||
assert outgoing_activity.is_sent is True
|
||||
assert outgoing_activity.last_status_code == 204
|
||||
assert outgoing_activity.error is None
|
||||
assert outgoing_activity.is_errored is False
|
||||
|
||||
|
||||
def test_process_next_outgoing_activity__webmention(
|
||||
db: Session,
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_next_outgoing_activity__webmention(
|
||||
async_db_session: AsyncSession,
|
||||
respx_mock: respx.MockRouter,
|
||||
) -> None:
|
||||
# And an outgoing activity
|
||||
|
@ -121,19 +129,24 @@ def test_process_next_outgoing_activity__webmention(
|
|||
|
||||
# When processing the next outgoing activity
|
||||
# Then it is processed
|
||||
assert process_next_outgoing_activity(db) is True
|
||||
next_activity = await fetch_next_outgoing_activity(async_db_session, set())
|
||||
assert next_activity
|
||||
await process_next_outgoing_activity(async_db_session, next_activity)
|
||||
|
||||
assert respx_mock.calls.call_count == 1
|
||||
|
||||
outgoing_activity = db.query(models.OutgoingActivity).one()
|
||||
outgoing_activity = (
|
||||
await async_db_session.execute(select(models.OutgoingActivity))
|
||||
).scalar_one()
|
||||
assert outgoing_activity.is_sent is True
|
||||
assert outgoing_activity.last_status_code == 204
|
||||
assert outgoing_activity.error is None
|
||||
assert outgoing_activity.is_errored is False
|
||||
|
||||
|
||||
def test_process_next_outgoing_activity__error_500(
|
||||
db: Session,
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_next_outgoing_activity__error_500(
|
||||
async_db_session: AsyncSession,
|
||||
respx_mock: respx.MockRouter,
|
||||
) -> None:
|
||||
outbox_object = _setup_outbox_object()
|
||||
|
@ -152,11 +165,15 @@ def test_process_next_outgoing_activity__error_500(
|
|||
|
||||
# When processing the next outgoing activity
|
||||
# Then it is processed
|
||||
assert process_next_outgoing_activity(db) is True
|
||||
next_activity = await fetch_next_outgoing_activity(async_db_session, set())
|
||||
assert next_activity
|
||||
await process_next_outgoing_activity(async_db_session, next_activity)
|
||||
|
||||
assert respx_mock.calls.call_count == 1
|
||||
|
||||
outgoing_activity = db.query(models.OutgoingActivity).one()
|
||||
outgoing_activity = (
|
||||
await async_db_session.execute(select(models.OutgoingActivity))
|
||||
).scalar_one()
|
||||
assert outgoing_activity.is_sent is False
|
||||
assert outgoing_activity.last_status_code == 500
|
||||
assert outgoing_activity.last_response == "oops"
|
||||
|
@ -164,8 +181,9 @@ def test_process_next_outgoing_activity__error_500(
|
|||
assert outgoing_activity.tries == 1
|
||||
|
||||
|
||||
def test_process_next_outgoing_activity__errored(
|
||||
db: Session,
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_next_outgoing_activity__errored(
|
||||
async_db_session: AsyncSession,
|
||||
respx_mock: respx.MockRouter,
|
||||
) -> None:
|
||||
outbox_object = _setup_outbox_object()
|
||||
|
@ -185,22 +203,28 @@ def test_process_next_outgoing_activity__errored(
|
|||
|
||||
# When processing the next outgoing activity
|
||||
# Then it is processed
|
||||
assert process_next_outgoing_activity(db) is True
|
||||
next_activity = await fetch_next_outgoing_activity(async_db_session, set())
|
||||
assert next_activity
|
||||
await process_next_outgoing_activity(async_db_session, next_activity)
|
||||
|
||||
assert respx_mock.calls.call_count == 1
|
||||
|
||||
outgoing_activity = db.query(models.OutgoingActivity).one()
|
||||
outgoing_activity = (
|
||||
await async_db_session.execute(select(models.OutgoingActivity))
|
||||
).scalar_one()
|
||||
assert outgoing_activity.is_sent is False
|
||||
assert outgoing_activity.last_status_code == 500
|
||||
assert outgoing_activity.last_response == "oops"
|
||||
assert outgoing_activity.is_errored is True
|
||||
|
||||
# And it is skipped from processing
|
||||
assert process_next_outgoing_activity(db) is False
|
||||
next_activity = await fetch_next_outgoing_activity(async_db_session, set())
|
||||
assert next_activity is None
|
||||
|
||||
|
||||
def test_process_next_outgoing_activity__connect_error(
|
||||
db: Session,
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_next_outgoing_activity__connect_error(
|
||||
async_db_session: AsyncSession,
|
||||
respx_mock: respx.MockRouter,
|
||||
) -> None:
|
||||
outbox_object = _setup_outbox_object()
|
||||
|
@ -217,11 +241,15 @@ def test_process_next_outgoing_activity__connect_error(
|
|||
|
||||
# When processing the next outgoing activity
|
||||
# Then it is processed
|
||||
assert process_next_outgoing_activity(db) is True
|
||||
next_activity = await fetch_next_outgoing_activity(async_db_session, set())
|
||||
assert next_activity
|
||||
await process_next_outgoing_activity(async_db_session, next_activity)
|
||||
|
||||
assert respx_mock.calls.call_count == 1
|
||||
|
||||
outgoing_activity = db.query(models.OutgoingActivity).one()
|
||||
outgoing_activity = (
|
||||
await async_db_session.execute(select(models.OutgoingActivity))
|
||||
).scalar_one()
|
||||
assert outgoing_activity.is_sent is False
|
||||
assert outgoing_activity.error is not None
|
||||
assert outgoing_activity.tries == 1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue