Add error catching on actor fetch

This commit is contained in:
Andrew Godwin 2022-11-20 11:37:26 -07:00
parent ef910c53b7
commit 7a4f9cf293
4 changed files with 102 additions and 12 deletions

8
.gitignore vendored
View File

@ -1,9 +1,13 @@
*.egg
*.egg-info
*.psql *.psql
*.pyc
*.sqlite3 *.sqlite3
.venv .venv
/*.env /*.env
/build
/docs/_build /docs/_build
/media/ /media/
notes.md /static-collected
__pycache__/ __pycache__/
*.pyc notes.md

View File

@ -297,11 +297,14 @@ class Identity(StatorModel):
if self.local: if self.local:
raise ValueError("Cannot fetch local identities") raise ValueError("Cannot fetch local identities")
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
try:
response = await client.get( response = await client.get(
self.actor_uri, self.actor_uri,
headers={"Accept": "application/json"}, headers={"Accept": "application/json"},
follow_redirects=True, follow_redirects=True,
) )
except (httpx.ReadTimeout, httpx.ReadError):
return False
if response.status_code >= 400: if response.status_code >= 400:
return False return False
document = canonicalise(response.json(), include_security=True) document = canonicalise(response.json(), include_security=True)

View File

@ -4,7 +4,6 @@ from users.models import Domain, Identity, User
@pytest.mark.django_db @pytest.mark.django_db
@pytest.mark.xfail
def test_webfinger_actor(client): def test_webfinger_actor(client):
""" """
Ensures the webfinger and actor URLs are working properly Ensures the webfinger and actor URLs are working properly
@ -16,7 +15,7 @@ def test_webfinger_actor(client):
domain.users.add(user) domain.users.add(user)
# Make an identity for them # Make an identity for them
identity = Identity.objects.create( identity = Identity.objects.create(
actor_uri="https://example.com/@test@example.com/actor/", actor_uri="https://example.com/@test@example.com/",
username="test", username="test",
domain=domain, domain=domain,
name="Test User", name="Test User",
@ -28,5 +27,5 @@ def test_webfinger_actor(client):
assert data["subject"] == "acct:test@example.com" assert data["subject"] == "acct:test@example.com"
assert data["aliases"][0] == "https://example.com/@test/" assert data["aliases"][0] == "https://example.com/@test/"
# Fetch their actor # Fetch their actor
data = client.get("/@test@example.com/actor/").json() data = client.get("/@test@example.com/", HTTP_ACCEPT="application/ld+json").json()
assert data["id"] == "https://example.com/@test@example.com/actor/" assert data["id"] == "https://example.com/@test@example.com/"

View File

@ -1,4 +1,5 @@
import pytest import pytest
from asgiref.sync import async_to_sync
from core.models import Config from core.models import Config
from users.models import Domain, Identity, User from users.models import Domain, Identity, User
@ -63,7 +64,7 @@ def test_create_identity_form(client):
@pytest.mark.django_db @pytest.mark.django_db
def test_identity_max_per_user(client): def test_identity_max_per_user(client):
""" """
Ensures the webfinger and actor URLs are working properly Ensures that the identity limit is functioning
""" """
# Make a user # Make a user
user = User.objects.create(email="test@example.com") user = User.objects.create(email="test@example.com")
@ -92,3 +93,86 @@ def test_identity_max_per_user(client):
user.admin = True user.admin = True
form = CreateIdentity.form_class(user=user, data=data) form = CreateIdentity.form_class(user=user, data=data)
assert form.is_valid() assert form.is_valid()
@pytest.mark.django_db
def test_fetch_actor(httpx_mock):
"""
Ensures that making identities via actor fetching works
"""
# Make a shell remote identity
identity = Identity.objects.create(
actor_uri="https://example.com/test-actor/",
local=False,
)
# Trigger actor fetch
httpx_mock.add_response(
url="https://example.com/.well-known/webfinger?resource=acct:test@example.com",
json={
"subject": "acct:test@example.com",
"aliases": [
"https://example.com/test-actor/",
],
"links": [
{
"rel": "http://webfinger.net/rel/profile-page",
"type": "text/html",
"href": "https://example.com/test-actor/",
},
{
"rel": "self",
"type": "application/activity+json",
"href": "https://example.com/test-actor/",
},
],
},
)
httpx_mock.add_response(
url="https://example.com/test-actor/",
json={
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
],
"id": "https://example.com/test-actor/",
"type": "Person",
"inbox": "https://example.com/test-actor/inbox/",
"publicKey": {
"id": "https://example.com/test-actor/#main-key",
"owner": "https://example.com/test-actor/",
"publicKeyPem": "-----BEGIN PUBLIC KEY-----\nits-a-faaaake\n-----END PUBLIC KEY-----\n",
},
"followers": "https://example.com/test-actor/followers/",
"following": "https://example.com/test-actor/following/",
"icon": {
"type": "Image",
"mediaType": "image/jpeg",
"url": "https://example.com/icon.jpg",
},
"image": {
"type": "Image",
"mediaType": "image/jpeg",
"url": "https://example.com/image.jpg",
},
"as:manuallyApprovesFollowers": False,
"name": "Test User",
"preferredUsername": "test",
"published": "2022-11-02T00:00:00Z",
"summary": "<p>A test user</p>",
"url": "https://example.com/test-actor/view/",
},
)
async_to_sync(identity.fetch_actor)()
# Verify the data arrived
identity = Identity.objects.get(pk=identity.pk)
assert identity.name == "Test User"
assert identity.username == "test"
assert identity.domain_id == "example.com"
assert identity.profile_uri == "https://example.com/test-actor/view/"
assert identity.inbox_uri == "https://example.com/test-actor/inbox/"
assert identity.icon_uri == "https://example.com/icon.jpg"
assert identity.image_uri == "https://example.com/image.jpg"
assert identity.summary == "<p>A test user</p>"
assert "ts-a-faaaake" in identity.public_key