def test_handle_create_payload_calls_sign_with_parent( self, mock_get_outbound_entity): comment = DiasporaComment() mock_get_outbound_entity.return_value = comment author_user = Mock(private_key=RSA.generate(2048), handle="*****@*****.**") parent_user = Mock(private_key=RSA.generate(2048), handle="*****@*****.**") entity = DiasporaComment() with patch.object(comment, "sign_with_parent") as mock_sign: handle_create_payload(entity, author_user, parent_user=parent_user) mock_sign.assert_called_once_with(parent_user.private_key)
def test_comment_to_xml(self): entity = DiasporaComment(raw_content="raw_content", guid="guid", target_guid="target_guid", handle="handle") result = entity.to_xml() assert result.tag == "comment" converted = b"<comment><guid>guid</guid><parent_guid>target_guid</parent_guid>" \ b"<author_signature></author_signature><text>raw_content</text>" \ b"<diaspora_handle>handle</diaspora_handle></comment>" assert etree.tostring(result) == converted
def test_calls_verify_signature(self, mock_verify): entity = DiasporaComment() entity._sender_key = "key" entity._source_object = "<obj></obj>" entity.signature = "sig" mock_verify.return_value = False with pytest.raises(SignatureVerificationError): entity._validate_signatures() mock_verify.reset_mock() mock_verify.return_value = True entity._validate_signatures()
def test_signing_comment_works(self, mock_format_dt): entity = DiasporaComment( raw_content="raw_content", guid="guid", target_guid="target_guid", handle="handle", created_at="created_at", ) entity.sign(get_dummy_private_key()) assert entity.signature == "OWvW/Yxw4uCnx0WDn0n5/B4uhyZ8Pr6h3FZaw8J7PCXyPluOfYXFoHO21bykP8c2aVnuJNHe+lmeAkUC" \ "/kHnl4yxk/jqe3uroW842OWvsyDRQ11vHxhIqNMjiepFPkZmXX3vqrYYh5FrC/tUsZrEc8hHoOIHXFR2" \ "kGD0gPV+4EEG6pbMNNZ+SBVun0hvruX8iKQVnBdc/+zUI9+T/MZmLyqTq/CvuPxDyHzQPSHi68N9rJyr" \ "4Xa1K+R33Xq8eHHxs8LVNRqzaHGeD3DX8yBu/vP9TYmZsiWlymbuGwLCa4Yfv/VS1hQZovhg6YTxV4CR" \ "v4ToGL+CAJ7UHEugRRBwDw=="
def test_already_fine_entities_are_returned_as_is(self): entity = DiasporaPost() assert get_outbound_entity(entity) == entity entity = DiasporaLike() assert get_outbound_entity(entity) == entity entity = DiasporaComment() assert get_outbound_entity(entity) == entity entity = DiasporaRequest() assert get_outbound_entity(entity) == entity entity = DiasporaProfile() assert get_outbound_entity(entity) == entity
def diasporacomment_activitypub_id(): return DiasporaComment( raw_content="raw_content", signature="signature", id="https://domain.tld/id", guid="guid", actor_id="*****@*****.**", handle="*****@*****.**", target_id="target_guid", target_guid="target_guid", )
def diasporacomment(): return DiasporaComment( raw_content="raw_content", signature="signature", id="guid", guid="guid", actor_id="*****@*****.**", handle="*****@*****.**", target_id="target_guid", target_guid="target_guid", )
def test_already_fine_entities_are_returned_as_is(self, private_key): entity = DiasporaPost() assert get_outbound_entity(entity, private_key) == entity entity = DiasporaLike() assert get_outbound_entity(entity, private_key) == entity entity = DiasporaComment() assert get_outbound_entity(entity, private_key) == entity entity = DiasporaProfile(handle="*****@*****.**", guid="1234") assert get_outbound_entity(entity, private_key) == entity entity = DiasporaContact() assert get_outbound_entity(entity, private_key) == entity entity = DiasporaReshare() assert get_outbound_entity(entity, private_key) == entity
def test_comment_to_xml(self): entity = DiasporaComment( raw_content="raw_content", guid="guid", target_guid="target_guid", handle="handle", signature="signature" ) result = entity.to_xml() assert result.tag == "comment" assert len(result.find("created_at").text) > 0 result.find("created_at").text = "" # timestamp makes testing painful converted = b"<comment><guid>guid</guid><parent_guid>target_guid</parent_guid>" \ b"<author_signature>signature</author_signature><parent_author_signature>" \ b"</parent_author_signature><text>raw_content</text><author>handle</author>" \ b"<created_at></created_at></comment>" assert etree.tostring(result) == converted
def test_signing_comment_works(self, mock_format_dt): entity = DiasporaComment( raw_content="raw_content", created_at="created_at", actor_id="handle", handle="handle", id="guid", guid="guid", target_id="target_guid", target_guid="target_guid", root_target_id="target_guid", root_target_guid="target_guid", ) entity.sign(get_dummy_private_key()) assert entity.signature == "XZYggFdQHOicguZ0ReVJkYiK5othHgBgAtwnSmm4NR31qeLa76Ur/i2B5Xi9dtopDlNS8EbFy+MLJ1ds" \ "ovDjPsVC1nLZrL57y0v+HtwJas6hQqNbvmEyr1q6X+0p1i93eINzt/7bxcP5uEGxy8J4ItsJzbDVLlC5" \ "3ZtIg7pmhR0ltqNqBHrgL8WDokfGKFlXqANchbD+Xeyv2COGbI78LwplVdYjHW1+jefjpYhMCxayIvMv" \ "WS8TV1hMTqUz+zSqoCHU04RgjjGW8e8vINDblQwMfEMeJ5T6OP5RiU3zCqDc3uL2zxHHh9IGC+clVuhP" \ "HTv8tHUHNLgc2vIzRtGh6w=="
def test_raises_if_no_sender_key(self): entity = DiasporaComment() with pytest.raises(SignatureVerificationError): entity._validate_signatures()
def test_sign_with_parent__calls_to_xml(self, mock_validate): entity = DiasporaComment() with patch.object(entity, "to_xml") as mock_to_xml: entity.sign_with_parent(get_dummy_private_key()) mock_to_xml.assert_called_once_with()
def test_signs_relayable_if_no_signature(self, private_key): entity = DiasporaComment() outbound = get_outbound_entity(entity, private_key) assert outbound.signature != ""
def diasporacomment(): return DiasporaComment( raw_content="raw_content", guid="guid", target_guid="target_guid", handle="handle", signature="signature" )