microblog.pub/tests/test_ldsig.py

55 lines
1.6 KiB
Python
Raw Normal View History

2022-06-28 07:58:33 +00:00
from copy import deepcopy
2022-07-29 07:24:51 +00:00
import httpx
import pytest
from respx import MockRouter
2022-06-28 07:58:33 +00:00
from app import activitypub as ap
from app import ldsig
2022-07-29 07:24:51 +00:00
from app.database import AsyncSession
2022-06-28 07:58:33 +00:00
from app.key import Key
from tests import factories
_SAMPLE_CREATE = {
"type": "Create",
"actor": "https://microblog.pub",
"object": {
"type": "Note",
"sensitive": False,
"cc": ["https://microblog.pub/followers"],
"to": ["https://www.w3.org/ns/activitystreams#Public"],
"content": "<p>Hello world!</p>",
"tag": [],
"attributedTo": "https://microblog.pub",
"published": "2018-05-21T15:51:59Z",
"id": "https://microblog.pub/outbox/988179f13c78b3a7/activity",
"url": "https://microblog.pub/note/988179f13c78b3a7",
},
"@context": ap.AS_EXTENDED_CTX,
"published": "2018-05-21T15:51:59Z",
"to": ["https://www.w3.org/ns/activitystreams#Public"],
"cc": ["https://microblog.pub/followers"],
"id": "https://microblog.pub/outbox/988179f13c78b3a7",
}
2022-07-29 07:24:51 +00:00
@pytest.mark.asyncio
async def test_linked_data_sig(
async_db_session: AsyncSession,
respx_mock: MockRouter,
) -> None:
2022-06-28 07:58:33 +00:00
privkey, pubkey = factories.generate_key()
ra = factories.RemoteActorFactory(
base_url="https://microblog.pub",
username="dev",
public_key=pubkey,
)
k = Key(ra.ap_id, f"{ra.ap_id}#main-key")
k.load(privkey)
2022-07-29 07:24:51 +00:00
respx_mock.get(ra.ap_id).mock(return_value=httpx.Response(200, json=ra.ap_actor))
2022-06-28 07:58:33 +00:00
doc = deepcopy(_SAMPLE_CREATE)
ldsig.generate_signature(doc, k)
2022-07-29 07:24:51 +00:00
assert (await ldsig.verify_signature(async_db_session, doc)) is True