2018-12-23 21:11:29 +01:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-04-23 15:21:58 +02:00
|
|
|
defmodule Pleroma.Web.Salmon.SalmonTest do
|
|
|
|
use Pleroma.DataCase
|
2019-02-10 22:57:38 +01:00
|
|
|
alias Pleroma.Activity
|
2019-05-22 05:58:15 +02:00
|
|
|
alias Pleroma.Keys
|
2019-02-10 22:57:38 +01:00
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.User
|
2019-05-12 16:35:38 +02:00
|
|
|
alias Pleroma.Web.Federator.Publisher
|
2019-03-05 03:52:23 +01:00
|
|
|
alias Pleroma.Web.Salmon
|
2019-05-12 16:35:38 +02:00
|
|
|
import Mock
|
2017-05-01 13:14:58 +02:00
|
|
|
import Pleroma.Factory
|
2017-04-23 15:21:58 +02:00
|
|
|
|
|
|
|
@magickey "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwQhh-1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAB"
|
|
|
|
|
|
|
|
@wrong_magickey "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwQhh-1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAA"
|
|
|
|
|
2017-08-25 15:09:05 +02:00
|
|
|
@magickey_friendica "RSA.AMwa8FUs2fWEjX0xN7yRQgegQffhBpuKNC6fa5VNSVorFjGZhRrlPMn7TQOeihlc9lBz2OsHlIedbYn2uJ7yCs0.AQAB"
|
|
|
|
|
2018-12-11 13:31:52 +01:00
|
|
|
setup_all do
|
|
|
|
Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
|
2018-12-02 16:17:26 +01:00
|
|
|
:ok
|
|
|
|
end
|
2018-12-02 20:05:28 +01:00
|
|
|
|
2017-04-23 15:21:58 +02:00
|
|
|
test "decodes a salmon" do
|
|
|
|
{:ok, salmon} = File.read("test/fixtures/salmon.xml")
|
|
|
|
{:ok, doc} = Salmon.decode_and_validate(@magickey, salmon)
|
|
|
|
assert Regex.match?(~r/xml/, doc)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "errors on wrong magic key" do
|
|
|
|
{:ok, salmon} = File.read("test/fixtures/salmon.xml")
|
|
|
|
assert Salmon.decode_and_validate(@wrong_magickey, salmon) == :error
|
|
|
|
end
|
2017-04-26 14:25:44 +02:00
|
|
|
|
|
|
|
test "it encodes a magic key from a public key" do
|
|
|
|
key = Salmon.decode_key(@magickey)
|
|
|
|
magic_key = Salmon.encode_key(key)
|
|
|
|
|
|
|
|
assert @magickey == magic_key
|
|
|
|
end
|
|
|
|
|
2017-08-25 15:09:05 +02:00
|
|
|
test "it decodes a friendica public key" do
|
2018-02-12 10:13:54 +01:00
|
|
|
_key = Salmon.decode_key(@magickey_friendica)
|
2017-08-25 15:09:05 +02:00
|
|
|
end
|
|
|
|
|
2017-04-26 14:25:44 +02:00
|
|
|
test "encodes an xml payload with a private key" do
|
|
|
|
doc = File.read!("test/fixtures/incoming_note_activity.xml")
|
|
|
|
pem = File.read!("test/fixtures/private_key.pem")
|
2019-05-22 05:58:15 +02:00
|
|
|
{:ok, private, public} = Keys.keys_from_pem(pem)
|
2017-04-26 14:25:44 +02:00
|
|
|
|
|
|
|
# Let's try a roundtrip.
|
|
|
|
{:ok, salmon} = Salmon.encode(private, doc)
|
|
|
|
{:ok, decoded_doc} = Salmon.decode_and_validate(Salmon.encode_key(public), salmon)
|
|
|
|
|
|
|
|
assert doc == decoded_doc
|
|
|
|
end
|
2017-04-29 19:47:56 +02:00
|
|
|
|
|
|
|
test "it gets a magic key" do
|
|
|
|
salmon = File.read!("test/fixtures/salmon2.xml")
|
2017-05-03 14:26:49 +02:00
|
|
|
{:ok, key} = Salmon.fetch_magic_key(salmon)
|
2017-04-29 19:47:56 +02:00
|
|
|
|
2018-03-30 15:01:53 +02:00
|
|
|
assert key ==
|
|
|
|
"RSA.uzg6r1peZU0vXGADWxGJ0PE34WvmhjUmydbX5YYdOiXfODVLwCMi1umGoqUDm-mRu4vNEdFBVJU1CpFA7dKzWgIsqsa501i2XqElmEveXRLvNRWFB6nG03Q5OUY2as8eE54BJm0p20GkMfIJGwP6TSFb-ICp3QjzbatuSPJ6xCE=.AQAB"
|
2017-04-29 19:47:56 +02:00
|
|
|
end
|
2017-05-01 13:14:58 +02:00
|
|
|
|
2019-05-12 16:35:38 +02:00
|
|
|
test_with_mock "it pushes an activity to remote accounts it's addressed to",
|
|
|
|
Publisher,
|
|
|
|
[:passthrough],
|
|
|
|
[] do
|
2017-05-01 13:14:58 +02:00
|
|
|
user_data = %{
|
|
|
|
info: %{
|
2018-12-11 13:31:52 +01:00
|
|
|
salmon: "http://test-example.org/salmon"
|
2017-05-01 13:14:58 +02:00
|
|
|
},
|
|
|
|
local: false
|
|
|
|
}
|
|
|
|
|
|
|
|
mentioned_user = insert(:user, user_data)
|
|
|
|
note = insert(:note)
|
2018-03-30 15:01:53 +02:00
|
|
|
|
2017-05-01 13:14:58 +02:00
|
|
|
activity_data = %{
|
2018-03-30 15:01:53 +02:00
|
|
|
"id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
|
2017-05-01 13:14:58 +02:00
|
|
|
"type" => "Create",
|
|
|
|
"actor" => note.data["actor"],
|
|
|
|
"to" => note.data["to"] ++ [mentioned_user.ap_id],
|
|
|
|
"object" => note.data,
|
2018-03-30 15:01:53 +02:00
|
|
|
"published_at" => DateTime.utc_now() |> DateTime.to_iso8601(),
|
2017-05-01 13:14:58 +02:00
|
|
|
"context" => note.data["context"]
|
|
|
|
}
|
|
|
|
|
2018-02-25 17:06:12 +01:00
|
|
|
{:ok, activity} = Repo.insert(%Activity{data: activity_data, recipients: activity_data["to"]})
|
2019-04-22 09:20:43 +02:00
|
|
|
user = User.get_cached_by_ap_id(activity.data["actor"])
|
2019-05-22 05:58:15 +02:00
|
|
|
{:ok, user} = User.ensure_keys_present(user)
|
2017-05-01 13:14:58 +02:00
|
|
|
|
2019-05-12 16:35:38 +02:00
|
|
|
Salmon.publish(user, activity)
|
2018-03-30 15:01:53 +02:00
|
|
|
|
2019-08-01 16:28:00 +02:00
|
|
|
assert called(Publisher.enqueue_one(Salmon, %{recipient_id: mentioned_user.id}))
|
2017-05-01 13:14:58 +02:00
|
|
|
end
|
2017-04-23 15:21:58 +02:00
|
|
|
end
|