logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

salmon_test.exs (3535B)


      1 # Pleroma: A lightweight social networking server
      2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
      3 # SPDX-License-Identifier: AGPL-3.0-only
      4 
      5 defmodule Pleroma.Web.Salmon.SalmonTest do
      6   use Pleroma.DataCase
      7   alias Pleroma.Activity
      8   alias Pleroma.Keys
      9   alias Pleroma.Repo
     10   alias Pleroma.User
     11   alias Pleroma.Web.Federator.Publisher
     12   alias Pleroma.Web.Salmon
     13   import Mock
     14   import Pleroma.Factory
     15 
     16   @magickey "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwQhh-1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAB"
     17 
     18   @wrong_magickey "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwQhh-1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAA"
     19 
     20   @magickey_friendica "RSA.AMwa8FUs2fWEjX0xN7yRQgegQffhBpuKNC6fa5VNSVorFjGZhRrlPMn7TQOeihlc9lBz2OsHlIedbYn2uJ7yCs0.AQAB"
     21 
     22   setup_all do
     23     Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
     24     :ok
     25   end
     26 
     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)
     31   end
     32 
     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
     36   end
     37 
     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)
     41 
     42     assert @magickey == magic_key
     43   end
     44 
     45   test "it decodes a friendica public key" do
     46     _key = Salmon.decode_key(@magickey_friendica)
     47   end
     48 
     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)
     53 
     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)
     57 
     58     assert doc == decoded_doc
     59   end
     60 
     61   test "it gets a magic key" do
     62     salmon = File.read!("test/fixtures/salmon2.xml")
     63     {:ok, key} = Salmon.fetch_magic_key(salmon)
     64 
     65     assert key ==
     66              "RSA.uzg6r1peZU0vXGADWxGJ0PE34WvmhjUmydbX5YYdOiXfODVLwCMi1umGoqUDm-mRu4vNEdFBVJU1CpFA7dKzWgIsqsa501i2XqElmEveXRLvNRWFB6nG03Q5OUY2as8eE54BJm0p20GkMfIJGwP6TSFb-ICp3QjzbatuSPJ6xCE=.AQAB"
     67   end
     68 
     69   test_with_mock "it pushes an activity to remote accounts it's addressed to",
     70                  Publisher,
     71                  [:passthrough],
     72                  [] do
     73     user_data = %{
     74       info: %{
     75         salmon: "http://test-example.org/salmon"
     76       },
     77       local: false
     78     }
     79 
     80     mentioned_user = insert(:user, user_data)
     81     note = insert(:note)
     82 
     83     activity_data = %{
     84       "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
     85       "type" => "Create",
     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"]
     91     }
     92 
     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)
     96 
     97     Salmon.publish(user, activity)
     98 
     99     assert called(Publisher.enqueue_one(Salmon, %{recipient_id: mentioned_user.id}))
    100   end
    101 end