1 defmodule Pleroma.Web.Salmon.SalmonTest do
3 alias Pleroma.Web.Salmon
4 alias Pleroma.{Repo, Activity, User}
7 @magickey "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwQhh-1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAB"
9 @wrong_magickey "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwQhh-1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAA"
11 @magickey_friendica "RSA.AMwa8FUs2fWEjX0xN7yRQgegQffhBpuKNC6fa5VNSVorFjGZhRrlPMn7TQOeihlc9lBz2OsHlIedbYn2uJ7yCs0.AQAB"
13 test "decodes a salmon" do
14 {:ok, salmon} = File.read("test/fixtures/salmon.xml")
15 {:ok, doc} = Salmon.decode_and_validate(@magickey, salmon)
16 assert Regex.match?(~r/xml/, doc)
19 test "errors on wrong magic key" do
20 {:ok, salmon} = File.read("test/fixtures/salmon.xml")
21 assert Salmon.decode_and_validate(@wrong_magickey, salmon) == :error
24 test "generates an RSA private key pem" do
25 {:ok, key} = Salmon.generate_rsa_pem
27 assert Regex.match?(~r/RSA/, key)
30 test "it encodes a magic key from a public key" do
31 key = Salmon.decode_key(@magickey)
32 magic_key = Salmon.encode_key(key)
34 assert @magickey == magic_key
37 test "it decodes a friendica public key" do
38 _key = Salmon.decode_key(@magickey_friendica)
41 test "returns a public and private key from a pem" do
42 pem = File.read!("test/fixtures/private_key.pem")
43 {:ok, private, public} = Salmon.keys_from_pem(pem)
45 assert elem(private, 0) == :RSAPrivateKey
46 assert elem(public, 0) == :RSAPublicKey
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} = Salmon.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 # TODO: Make test local
63 salmon = File.read!("test/fixtures/salmon2.xml")
64 {:ok, key} = Salmon.fetch_magic_key(salmon)
66 assert key == "RSA.uzg6r1peZU0vXGADWxGJ0PE34WvmhjUmydbX5YYdOiXfODVLwCMi1umGoqUDm-mRu4vNEdFBVJU1CpFA7dKzWgIsqsa501i2XqElmEveXRLvNRWFB6nG03Q5OUY2as8eE54BJm0p20GkMfIJGwP6TSFb-ICp3QjzbatuSPJ6xCE=.AQAB"
69 test "it pushes an activity to remote accounts it's addressed to" do
72 "salmon" => "http://example.org/salmon"
77 mentioned_user = insert(:user, user_data)
80 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id,
82 "actor" => note.data["actor"],
83 "to" => note.data["to"] ++ [mentioned_user.ap_id],
84 "object" => note.data,
85 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601,
86 "context" => note.data["context"]
89 {:ok, activity} = Repo.insert(%Activity{data: activity_data})
90 user = Repo.get_by(User, ap_id: activity.data["actor"])
91 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
93 poster = fn (url, _data, _headers, _options) ->
94 assert url == "http://example.org/salmon"
96 Salmon.publish(user, activity, poster)