Create controllers.handle_create_payload - but only for diaspora now

merge-requests/130/head
Jason Robinson 2015-07-06 22:50:30 +03:00
rodzic eecb3e013f
commit d2c565bc24
2 zmienionych plików z 36 dodań i 6 usunięć

Wyświetl plik

@ -1,7 +1,7 @@
import importlib
from federation.exceptions import NoSuitableProtocolFoundError
from federation.protocols.diaspora.protocol import Protocol
PROTOCOLS = (
"diaspora",
@ -35,6 +35,20 @@ def handle_receive(payload, user=None, sender_key_fetcher=None):
return sender, found_protocol.PROTOCOL_NAME, entities
def handle_send():
"""Send."""
pass
def handle_create_payload(from_user, to_user, entity):
"""Create a payload with the correct protocol.
Since we don't know the protocol, we need to first query the recipient. However, for a PoC implementation,
supporting only Diaspora, we're going to assume that for now.
Args:
from_user (obj) - User sending the object
to_user (obj) - Contact entry to send to
entity (obj) - Entity object to send
`from_user` must have `private_key` and `handle` attributes.
`to_user` must have `key` attribute.
"""
protocol = Protocol()
data = protocol.build_send(from_user=from_user, to_user=to_user, entity=entity)
return data

Wyświetl plik

@ -1,7 +1,9 @@
from unittest.mock import patch
from unittest.mock import patch, Mock
from Crypto.PublicKey import RSA
import pytest
from federation.controllers import handle_receive
from federation.controllers import handle_receive, handle_create_payload
from federation.entities.base import Post
from federation.exceptions import NoSuitableProtocolFoundError
from federation.protocols.diaspora.protocol import Protocol
from federation.tests.fixtures.payloads import UNENCRYPTED_DIASPORA_PAYLOAD
@ -25,3 +27,17 @@ class TestHandleReceiveProtocolIdentification(object):
payload = "foobar"
with pytest.raises(NoSuitableProtocolFoundError):
handle_receive(payload)
class TestHandleCreatePayloadBuildsAPayload(object):
def test_handle_create_payload_builds_an_xml(self):
from_user = Mock(private_key=RSA.generate(2048), handle="foobar@domain.tld")
to_user = Mock(key=RSA.generate(2048).publickey())
entity = Post()
data = handle_create_payload(from_user, to_user, entity)
assert len(data) > 0
parts = data.split("=")
assert len(parts) == 2
assert parts[0] == "xml"
assert len(parts[1]) > 0