Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from jaywink/get-user-method
Split federation.controllers to inbound and outbound
- Loading branch information
Showing
6 changed files
with
86 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# -*- coding: utf-8 -*- | ||
from federation.entities.diaspora.mappers import get_outbound_entity | ||
from federation.protocols.diaspora.protocol import Protocol | ||
|
||
|
||
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. | ||
""" | ||
# Just use Diaspora protocol for now | ||
protocol = Protocol() | ||
outbound_entity = get_outbound_entity(entity) | ||
data = protocol.build_send(from_user=from_user, to_user=to_user, entity=outbound_entity) | ||
return data |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# -*- coding: utf-8 -*- | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from federation.exceptions import NoSuitableProtocolFoundError | ||
from federation.inbound import handle_receive | ||
from federation.protocols.diaspora.protocol import Protocol | ||
from federation.tests.fixtures.payloads import UNENCRYPTED_DIASPORA_PAYLOAD | ||
|
||
|
||
class TestHandleReceiveProtocolIdentification(object): | ||
def test_handle_receive_routes_to_identified_protocol(self): | ||
payload = UNENCRYPTED_DIASPORA_PAYLOAD | ||
with patch.object( | ||
Protocol, | ||
'receive', | ||
return_value=("foobar@domain.tld", "<foobar></foobar>")) as mock_receive,\ | ||
patch( | ||
"federation.entities.diaspora.mappers.message_to_objects", | ||
return_value=[]) as mock_message_to_objects: | ||
handle_receive(payload) | ||
assert mock_receive.called | ||
|
||
def test_handle_receive_raises_on_unidentified_protocol(self): | ||
payload = "foobar" | ||
with pytest.raises(NoSuitableProtocolFoundError): | ||
handle_receive(payload) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# -*- coding: utf-8 -*- | ||
from unittest.mock import Mock, patch | ||
|
||
from Crypto.PublicKey import RSA | ||
|
||
from federation.entities.diaspora.entities import DiasporaPost | ||
from federation.outbound import handle_create_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 = DiasporaPost() | ||
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 | ||
|
||
@patch("federation.outbound.get_outbound_entity") | ||
def test_handle_create_payload_calls_get_outbound_entity(self, mock_get_outbound_entity): | ||
mock_get_outbound_entity.return_value = DiasporaPost() | ||
from_user = Mock(private_key=RSA.generate(2048), handle="foobar@domain.tld") | ||
to_user = Mock(key=RSA.generate(2048).publickey()) | ||
entity = DiasporaPost() | ||
handle_create_payload(from_user, to_user, entity) | ||
assert mock_get_outbound_entity.called |