diff options
Diffstat (limited to 'users')
| -rw-r--r-- | users/models/identity.py | 13 | ||||
| -rw-r--r-- | users/tests/test_activitypub.py | 7 | ||||
| -rw-r--r-- | users/tests/test_identity.py | 86 | 
3 files changed, 96 insertions, 10 deletions
| diff --git a/users/models/identity.py b/users/models/identity.py index 53b6f80..5056ce0 100644 --- a/users/models/identity.py +++ b/users/models/identity.py @@ -297,11 +297,14 @@ class Identity(StatorModel):          if self.local:              raise ValueError("Cannot fetch local identities")          async with httpx.AsyncClient() as client: -            response = await client.get( -                self.actor_uri, -                headers={"Accept": "application/json"}, -                follow_redirects=True, -            ) +            try: +                response = await client.get( +                    self.actor_uri, +                    headers={"Accept": "application/json"}, +                    follow_redirects=True, +                ) +            except (httpx.ReadTimeout, httpx.ReadError): +                return False              if response.status_code >= 400:                  return False              document = canonicalise(response.json(), include_security=True) diff --git a/users/tests/test_activitypub.py b/users/tests/test_activitypub.py index 7c5e789..72ab8c3 100644 --- a/users/tests/test_activitypub.py +++ b/users/tests/test_activitypub.py @@ -4,7 +4,6 @@ from users.models import Domain, Identity, User  @pytest.mark.django_db -@pytest.mark.xfail  def test_webfinger_actor(client):      """      Ensures the webfinger and actor URLs are working properly @@ -16,7 +15,7 @@ def test_webfinger_actor(client):      domain.users.add(user)      # Make an identity for them      identity = Identity.objects.create( -        actor_uri="https://example.com/@test@example.com/actor/", +        actor_uri="https://example.com/@test@example.com/",          username="test",          domain=domain,          name="Test User", @@ -28,5 +27,5 @@ def test_webfinger_actor(client):      assert data["subject"] == "acct:test@example.com"      assert data["aliases"][0] == "https://example.com/@test/"      # Fetch their actor -    data = client.get("/@test@example.com/actor/").json() -    assert data["id"] == "https://example.com/@test@example.com/actor/" +    data = client.get("/@test@example.com/", HTTP_ACCEPT="application/ld+json").json() +    assert data["id"] == "https://example.com/@test@example.com/" diff --git a/users/tests/test_identity.py b/users/tests/test_identity.py index f056d60..868894a 100644 --- a/users/tests/test_identity.py +++ b/users/tests/test_identity.py @@ -1,4 +1,5 @@  import pytest +from asgiref.sync import async_to_sync  from core.models import Config  from users.models import Domain, Identity, User @@ -63,7 +64,7 @@ def test_create_identity_form(client):  @pytest.mark.django_db  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      user = User.objects.create(email="test@example.com") @@ -92,3 +93,86 @@ def test_identity_max_per_user(client):      user.admin = True      form = CreateIdentity.form_class(user=user, data=data)      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 | 
