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 test "decodes a salmon" do
12 {:ok, salmon} = File.read("test/fixtures/salmon.xml")
13 {:ok, doc} = Salmon.decode_and_validate(@magickey, salmon)
14 assert Regex.match?(~r/xml/, doc)
17 test "errors on wrong magic key" do
18 {:ok, salmon} = File.read("test/fixtures/salmon.xml")
19 assert Salmon.decode_and_validate(@wrong_magickey, salmon) == :error
22 test "generates an RSA private key pem" do
23 {:ok, key} = Salmon.generate_rsa_pem
25 assert Regex.match?(~r/RSA/, key)
28 test "it encodes a magic key from a public key" do
29 key = Salmon.decode_key(@magickey)
30 magic_key = Salmon.encode_key(key)
32 assert @magickey == magic_key
35 test "returns a public and private key from a pem" do
36 pem = File.read!("test/fixtures/private_key.pem")
37 {:ok, private, public} = Salmon.keys_from_pem(pem)
39 assert elem(private, 0) == :RSAPrivateKey
40 assert elem(public, 0) == :RSAPublicKey
43 test "encodes an xml payload with a private key" do
44 doc = File.read!("test/fixtures/incoming_note_activity.xml")
45 pem = File.read!("test/fixtures/private_key.pem")
46 {:ok, private, public} = Salmon.keys_from_pem(pem)
48 # Let's try a roundtrip.
49 {:ok, salmon} = Salmon.encode(private, doc)
50 {:ok, decoded_doc} = Salmon.decode_and_validate(Salmon.encode_key(public), salmon)
52 assert doc == decoded_doc
55 test "it gets a magic key" do
56 # TODO: Make test local
57 salmon = File.read!("test/fixtures/salmon2.xml")
58 key = Salmon.fetch_magic_key(salmon)
60 assert key == "RSA.uzg6r1peZU0vXGADWxGJ0PE34WvmhjUmydbX5YYdOiXfODVLwCMi1umGoqUDm-mRu4vNEdFBVJU1CpFA7dKzWgIsqsa501i2XqElmEveXRLvNRWFB6nG03Q5OUY2as8eE54BJm0p20GkMfIJGwP6TSFb-ICp3QjzbatuSPJ6xCE=.AQAB"
63 test "it pushes an activity to remote accounts it's addressed to" do
66 "salmon" => "http://example.org/salmon"
71 mentioned_user = insert(:user, user_data)
74 "id" => Pleroma.Web.ActivityPub.ActivityPub.generate_activity_id,
76 "actor" => note.data["actor"],
77 "to" => note.data["to"] ++ [mentioned_user.ap_id],
78 "object" => note.data,
79 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601,
80 "context" => note.data["context"]
83 {:ok, activity} = Repo.insert(%Activity{data: activity_data})
84 user = Repo.get_by(User, ap_id: activity.data["actor"])
85 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
87 poster = fn (url, data, headers) ->
88 assert url == "http://example.org/salmon"
90 Salmon.publish(user, activity, poster)