1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Salmon.SalmonTest do
11 alias Pleroma.Web.Federator.Publisher
12 alias Pleroma.Web.Salmon
14 import Pleroma.Factory
16 @magickey "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwQhh-1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAB"
18 @wrong_magickey "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwQhh-1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAA"
20 @magickey_friendica "RSA.AMwa8FUs2fWEjX0xN7yRQgegQffhBpuKNC6fa5VNSVorFjGZhRrlPMn7TQOeihlc9lBz2OsHlIedbYn2uJ7yCs0.AQAB"
23 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
27 test "decodes a salmon" do
28 {:ok, salmon} = File.read("test/fixtures/salmon.xml")
29 {:ok, doc} = Salmon.decode_and_validate(@magickey, salmon)
30 assert Regex.match?(~r/xml/, doc)
33 test "errors on wrong magic key" do
34 {:ok, salmon} = File.read("test/fixtures/salmon.xml")
35 assert Salmon.decode_and_validate(@wrong_magickey, salmon) == :error
38 test "it encodes a magic key from a public key" do
39 key = Salmon.decode_key(@magickey)
40 magic_key = Salmon.encode_key(key)
42 assert @magickey == magic_key
45 test "it decodes a friendica public key" do
46 _key = Salmon.decode_key(@magickey_friendica)
49 test "encodes an xml payload with a private key" do
50 doc = File.read!("test/fixtures/incoming_note_activity.xml")
51 pem = File.read!("test/fixtures/private_key.pem")
52 {:ok, private, public} = Keys.keys_from_pem(pem)
54 # Let's try a roundtrip.
55 {:ok, salmon} = Salmon.encode(private, doc)
56 {:ok, decoded_doc} = Salmon.decode_and_validate(Salmon.encode_key(public), salmon)
58 assert doc == decoded_doc
61 test "it gets a magic key" do
62 salmon = File.read!("test/fixtures/salmon2.xml")
63 {:ok, key} = Salmon.fetch_magic_key(salmon)
66 "RSA.uzg6r1peZU0vXGADWxGJ0PE34WvmhjUmydbX5YYdOiXfODVLwCMi1umGoqUDm-mRu4vNEdFBVJU1CpFA7dKzWgIsqsa501i2XqElmEveXRLvNRWFB6nG03Q5OUY2as8eE54BJm0p20GkMfIJGwP6TSFb-ICp3QjzbatuSPJ6xCE=.AQAB"
69 test_with_mock "it pushes an activity to remote accounts it's addressed to",
75 salmon: "http://test-example.org/salmon"
80 mentioned_user = insert(:user, user_data)
84 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
86 "actor" => note.data["actor"],
87 "to" => note.data["to"] ++ [mentioned_user.ap_id],
88 "object" => note.data,
89 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601(),
90 "context" => note.data["context"]
93 {:ok, activity} = Repo.insert(%Activity{data: activity_data, recipients: activity_data["to"]})
94 user = User.get_cached_by_ap_id(activity.data["actor"])
95 {:ok, user} = User.ensure_keys_present(user)
97 Salmon.publish(user, activity)
99 assert called(Publisher.enqueue_one(Salmon, %{recipient: mentioned_user}))