Make outgoing salmons work.
[akkoma] / test / web / salmon / salmon_test.exs
1 defmodule Pleroma.Web.Salmon.SalmonTest do
2 use Pleroma.DataCase
3 alias Pleroma.Web.Salmon
4 alias Pleroma.{Repo, Activity, User}
5 import Pleroma.Factory
6
7 @magickey "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwQhh-1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAB"
8
9 @wrong_magickey "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwQhh-1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAA"
10
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)
15 end
16
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
20 end
21
22 test "generates an RSA private key pem" do
23 {:ok, key} = Salmon.generate_rsa_pem
24 assert is_binary(key)
25 assert Regex.match?(~r/RSA/, key)
26 end
27
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)
31
32 assert @magickey == magic_key
33 end
34
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)
38
39 assert elem(private, 0) == :RSAPrivateKey
40 assert elem(public, 0) == :RSAPublicKey
41 end
42
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)
47
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)
51
52 assert doc == decoded_doc
53 end
54
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)
59
60 assert key == "RSA.uzg6r1peZU0vXGADWxGJ0PE34WvmhjUmydbX5YYdOiXfODVLwCMi1umGoqUDm-mRu4vNEdFBVJU1CpFA7dKzWgIsqsa501i2XqElmEveXRLvNRWFB6nG03Q5OUY2as8eE54BJm0p20GkMfIJGwP6TSFb-ICp3QjzbatuSPJ6xCE=.AQAB"
61 end
62
63 test "it pushes an activity to remote accounts it's addressed to" do
64 user_data = %{
65 info: %{
66 "salmon" => "http://example.org/salmon"
67 },
68 local: false
69 }
70
71 mentioned_user = insert(:user, user_data)
72 note = insert(:note)
73 activity_data = %{
74 "id" => Pleroma.Web.ActivityPub.ActivityPub.generate_activity_id,
75 "type" => "Create",
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"]
81 }
82
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)
86
87 poster = fn (url, data, headers) ->
88 assert url == "http://example.org/salmon"
89 end
90 Salmon.publish(user, activity, poster)
91 end
92 end