salmon_test.exs 3.44 KB
Newer Older
lain's avatar
lain committed
1 2 3
defmodule Pleroma.Web.Salmon.SalmonTest do
  use Pleroma.DataCase
  alias Pleroma.Web.Salmon
lain's avatar
lain committed
4 5
  alias Pleroma.{Repo, Activity, User}
  import Pleroma.Factory
lain's avatar
lain committed
6 7 8 9 10

  @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"

lain's avatar
lain committed
11 12
  @magickey_friendica "RSA.AMwa8FUs2fWEjX0xN7yRQgegQffhBpuKNC6fa5VNSVorFjGZhRrlPMn7TQOeihlc9lBz2OsHlIedbYn2uJ7yCs0.AQAB"

lain's avatar
lain committed
13 14 15 16 17 18 19 20 21 22
  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
lain's avatar
lain committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36

  test "generates an RSA private key pem" do
    {:ok, key} = Salmon.generate_rsa_pem
    assert is_binary(key)
    assert Regex.match?(~r/RSA/, key)
  end

  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

lain's avatar
lain committed
37
  test "it decodes a friendica public key" do
lain's avatar
lain committed
38
    _key = Salmon.decode_key(@magickey_friendica)
lain's avatar
lain committed
39 40
  end

lain's avatar
lain committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  test "returns a public and private key from a pem" do
    pem = File.read!("test/fixtures/private_key.pem")
    {:ok, private, public} = Salmon.keys_from_pem(pem)

    assert elem(private, 0) == :RSAPrivateKey
    assert elem(public, 0) == :RSAPublicKey
  end

  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")
    {:ok, private, public} = Salmon.keys_from_pem(pem)

    # 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
lain's avatar
lain committed
60 61 62

  test "it gets a magic key" do
    salmon = File.read!("test/fixtures/salmon2.xml")
63
    {:ok, key} = Salmon.fetch_magic_key(salmon)
lain's avatar
lain committed
64 65 66

    assert key == "RSA.uzg6r1peZU0vXGADWxGJ0PE34WvmhjUmydbX5YYdOiXfODVLwCMi1umGoqUDm-mRu4vNEdFBVJU1CpFA7dKzWgIsqsa501i2XqElmEveXRLvNRWFB6nG03Q5OUY2as8eE54BJm0p20GkMfIJGwP6TSFb-ICp3QjzbatuSPJ6xCE=.AQAB"
  end
lain's avatar
lain committed
67 68 69 70 71 72 73 74 75 76 77 78

  test "it pushes an activity to remote accounts it's addressed to" do
    user_data = %{
      info: %{
        "salmon" => "http://example.org/salmon"
      },
      local: false
    }

    mentioned_user = insert(:user, user_data)
    note = insert(:note)
    activity_data = %{
lain's avatar
lain committed
79
      "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id,
lain's avatar
lain committed
80 81 82 83 84 85 86 87
      "type" => "Create",
      "actor" => note.data["actor"],
      "to" => note.data["to"] ++ [mentioned_user.ap_id],
      "object" => note.data,
      "published_at" => DateTime.utc_now() |> DateTime.to_iso8601,
      "context" => note.data["context"]
    }

lain's avatar
lain committed
88
    {:ok, activity} = Repo.insert(%Activity{data: activity_data, recipients: activity_data["to"]})
lain's avatar
lain committed
89 90 91
    user = Repo.get_by(User, ap_id: activity.data["actor"])
    {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)

lain's avatar
lain committed
92
    poster = fn (url, _data, _headers, _options) ->
lain's avatar
lain committed
93 94 95 96
      assert url == "http://example.org/salmon"
    end
    Salmon.publish(user, activity, poster)
  end
lain's avatar
lain committed
97
end