Fix some tests

This commit is contained in:
Floatingghost 2024-06-27 06:58:05 +01:00
parent 9728e2f8f7
commit 9c876cea21
6 changed files with 36 additions and 16 deletions

View file

@ -1155,7 +1155,8 @@ def update_and_set_cache(%{data: %Pleroma.User{} = user} = changeset) do
was_superuser_before_update = User.superuser?(user) was_superuser_before_update = User.superuser?(user)
with {:ok, user} <- Repo.update(changeset, stale_error_field: :id) do with {:ok, user} <- Repo.update(changeset, stale_error_field: :id) do
set_cache(user) user
|> set_cache()
end end
|> maybe_remove_report_notifications(was_superuser_before_update) |> maybe_remove_report_notifications(was_superuser_before_update)
end end

View file

@ -18,6 +18,11 @@ defmodule Pleroma.User.SigningKey do
timestamps() timestamps()
end end
def load_key(%User{} = user) do
user
|> Repo.preload(:signing_key)
end
def key_id_of_local_user(%User{local: true, signing_key: %__MODULE__{key_id: key_id}}), def key_id_of_local_user(%User{local: true, signing_key: %__MODULE__{key_id: key_id}}),
do: key_id do: key_id

View file

@ -200,7 +200,7 @@ defp deps do
## dev & test ## dev & test
{:ex_doc, "~> 0.30", only: :dev, runtime: false}, {:ex_doc, "~> 0.30", only: :dev, runtime: false},
{:ex_machina, "~> 2.7", only: :test}, {:ex_machina, "~> 2.8", only: :test},
{:credo, "~> 1.7", only: [:dev, :test], runtime: false}, {:credo, "~> 1.7", only: [:dev, :test], runtime: false},
{:mock, "~> 0.3.8", only: :test}, {:mock, "~> 0.3.8", only: :test},
{:excoveralls, "0.16.1", only: :test}, {:excoveralls, "0.16.1", only: :test},

View file

@ -584,6 +584,7 @@ test "it inserts an incoming activity into the database" <>
local: false, local: false,
last_refreshed_at: nil last_refreshed_at: nil
) )
|> with_signing_key()
data = data =
File.read!("test/fixtures/mastodon-post-activity.json") File.read!("test/fixtures/mastodon-post-activity.json")
@ -594,7 +595,7 @@ test "it inserts an incoming activity into the database" <>
conn = conn =
conn conn
|> assign(:valid_signature, true) |> assign(:valid_signature, true)
|> put_req_header("signature", "keyId=\"#{user.ap_id}/main-key\"") |> put_req_header("signature", "keyId=\"#{user.signing_key.key_id}\"")
|> put_req_header("content-type", "application/activity+json") |> put_req_header("content-type", "application/activity+json")
|> post("/inbox", data) |> post("/inbox", data)
@ -609,6 +610,7 @@ test "it clears `unreachable` federation status of the sender", %{conn: conn} do
sender_url = data["actor"] sender_url = data["actor"]
sender = insert(:user, ap_id: data["actor"]) sender = insert(:user, ap_id: data["actor"])
|> with_signing_key()
Instances.set_consistently_unreachable(sender_url) Instances.set_consistently_unreachable(sender_url)
refute Instances.reachable?(sender_url) refute Instances.reachable?(sender_url)
@ -616,7 +618,7 @@ test "it clears `unreachable` federation status of the sender", %{conn: conn} do
conn = conn =
conn conn
|> assign(:valid_signature, true) |> assign(:valid_signature, true)
|> put_req_header("signature", "keyId=\"#{sender.ap_id}/main-key\"") |> put_req_header("signature", "keyId=\"#{sender.signing_key.key_id}\"")
|> put_req_header("content-type", "application/activity+json") |> put_req_header("content-type", "application/activity+json")
|> post("/inbox", data) |> post("/inbox", data)
@ -784,6 +786,7 @@ test "mastodon pin/unpin", %{conn: conn} do
ap_id: actor, ap_id: actor,
featured_address: "https://example.com/users/lain/collections/featured" featured_address: "https://example.com/users/lain/collections/featured"
) )
|> with_signing_key()
Tesla.Mock.mock(fn Tesla.Mock.mock(fn
%{ %{
@ -839,7 +842,7 @@ test "mastodon pin/unpin", %{conn: conn} do
assert "ok" == assert "ok" ==
conn conn
|> assign(:valid_signature, true) |> assign(:valid_signature, true)
|> put_req_header("signature", "keyId=\"#{sender.ap_id}/main-key\"") |> put_req_header("signature", "keyId=\"#{sender.signing_key.key_id}\"")
|> put_req_header("content-type", "application/activity+json") |> put_req_header("content-type", "application/activity+json")
|> post("/inbox", data) |> post("/inbox", data)
|> json_response(200) |> json_response(200)
@ -974,6 +977,7 @@ test "it accepts announces with to as string instead of array", %{conn: conn} do
{:ok, post} = CommonAPI.post(user, %{status: "hey"}) {:ok, post} = CommonAPI.post(user, %{status: "hey"})
announcer = insert(:user, local: false) announcer = insert(:user, local: false)
|> with_signing_key()
data = %{ data = %{
"@context" => "https://www.w3.org/ns/activitystreams", "@context" => "https://www.w3.org/ns/activitystreams",
@ -988,7 +992,7 @@ test "it accepts announces with to as string instead of array", %{conn: conn} do
conn = conn =
conn conn
|> assign(:valid_signature, true) |> assign(:valid_signature, true)
|> put_req_header("signature", "keyId=\"#{announcer.ap_id}/main-key\"") |> put_req_header("signature", "keyId=\"#{announcer.signing_key.key_id}\"")
|> put_req_header("content-type", "application/activity+json") |> put_req_header("content-type", "application/activity+json")
|> post("/users/#{user.nickname}/inbox", data) |> post("/users/#{user.nickname}/inbox", data)
@ -1004,6 +1008,7 @@ test "it accepts messages from actors that are followed by the user", %{
} do } do
recipient = insert(:user) recipient = insert(:user)
actor = insert(:user, %{ap_id: "http://mastodon.example.org/users/actor"}) actor = insert(:user, %{ap_id: "http://mastodon.example.org/users/actor"})
|> with_signing_key()
{:ok, recipient, actor} = User.follow(recipient, actor) {:ok, recipient, actor} = User.follow(recipient, actor)
@ -1019,7 +1024,7 @@ test "it accepts messages from actors that are followed by the user", %{
conn = conn =
conn conn
|> assign(:valid_signature, true) |> assign(:valid_signature, true)
|> put_req_header("signature", "keyId=\"#{actor.ap_id}/main-key\"") |> put_req_header("signature", "keyId=\"#{actor.signing_key.key_id}\"")
|> put_req_header("content-type", "application/activity+json") |> put_req_header("content-type", "application/activity+json")
|> post("/users/#{recipient.nickname}/inbox", data) |> post("/users/#{recipient.nickname}/inbox", data)
@ -1057,6 +1062,7 @@ test "it returns a note activity in a collection", %{conn: conn} do
test "it clears `unreachable` federation status of the sender", %{conn: conn, data: data} do test "it clears `unreachable` federation status of the sender", %{conn: conn, data: data} do
user = insert(:user) user = insert(:user)
|> with_signing_key()
data = Map.put(data, "bcc", [user.ap_id]) data = Map.put(data, "bcc", [user.ap_id])
sender_host = URI.parse(data["actor"]).host sender_host = URI.parse(data["actor"]).host
@ -1066,7 +1072,7 @@ test "it clears `unreachable` federation status of the sender", %{conn: conn, da
conn = conn =
conn conn
|> assign(:valid_signature, true) |> assign(:valid_signature, true)
|> put_req_header("signature", "keyId=\"#{data["actor"]}/main-key\"") |> put_req_header("signature", "keyId=\"#{user.signing_key.key_id}\"")
|> put_req_header("content-type", "application/activity+json") |> put_req_header("content-type", "application/activity+json")
|> post("/users/#{user.nickname}/inbox", data) |> post("/users/#{user.nickname}/inbox", data)
@ -1077,6 +1083,8 @@ test "it clears `unreachable` federation status of the sender", %{conn: conn, da
@tag capture_log: true @tag capture_log: true
test "it removes all follower collections but actor's", %{conn: conn} do test "it removes all follower collections but actor's", %{conn: conn} do
[actor, recipient] = insert_pair(:user) [actor, recipient] = insert_pair(:user)
actor = with_signing_key(actor)
to = [ to = [
recipient.ap_id, recipient.ap_id,
@ -1105,7 +1113,7 @@ test "it removes all follower collections but actor's", %{conn: conn} do
conn conn
|> assign(:valid_signature, true) |> assign(:valid_signature, true)
|> put_req_header("signature", "keyId=\"#{actor.ap_id}/main-key\"") |> put_req_header("signature", "keyId=\"#{actor.signing_key.key_id}\"")
|> put_req_header("content-type", "application/activity+json") |> put_req_header("content-type", "application/activity+json")
|> post("/users/#{recipient.nickname}/inbox", data) |> post("/users/#{recipient.nickname}/inbox", data)
|> json_response(200) |> json_response(200)
@ -1142,6 +1150,7 @@ test "it requires authentication", %{conn: conn} do
test "forwarded report", %{conn: conn} do test "forwarded report", %{conn: conn} do
admin = insert(:user, is_admin: true) admin = insert(:user, is_admin: true)
actor = insert(:user, local: false) actor = insert(:user, local: false)
|> with_signing_key()
remote_domain = URI.parse(actor.ap_id).host remote_domain = URI.parse(actor.ap_id).host
reported_user = insert(:user) reported_user = insert(:user)
@ -1198,7 +1207,7 @@ test "forwarded report", %{conn: conn} do
conn conn
|> assign(:valid_signature, true) |> assign(:valid_signature, true)
|> put_req_header("signature", "keyId=\"#{actor.ap_id}/main-key\"") |> put_req_header("signature", "keyId=\"#{actor.signing_key.key_id}\"")
|> put_req_header("content-type", "application/activity+json") |> put_req_header("content-type", "application/activity+json")
|> post("/users/#{reported_user.nickname}/inbox", data) |> post("/users/#{reported_user.nickname}/inbox", data)
|> json_response(200) |> json_response(200)

View file

@ -58,6 +58,7 @@ test "it considers a mapped identity to be invalid when the associated instance
# extract domain from user.ap_id # extract domain from user.ap_id
url = URI.parse(user.ap_id) url = URI.parse(user.ap_id)
clear_config([:mrf_simple, :reject], [ clear_config([:mrf_simple, :reject], [
{url.host, "anime is banned"} {url.host, "anime is banned"}
]) ])
@ -80,6 +81,7 @@ test "allowlist federation: it considers a mapped identity to be valid when the
clear_config([:activitypub, :authorized_fetch_mode], true) clear_config([:activitypub, :authorized_fetch_mode], true)
url = URI.parse(user.ap_id) url = URI.parse(user.ap_id)
clear_config([:mrf_simple, :accept], [ clear_config([:mrf_simple, :accept], [
{url.host, "anime is allowed"} {url.host, "anime is allowed"}
]) ])

View file

@ -88,25 +88,28 @@ def user_factory(attrs \\ %{}) do
end end
attrs = Map.delete(attrs, :domain) attrs = Map.delete(attrs, :domain)
signing_key = insert(:signing_key, %{key_id: urls[:ap_id] <> "#main-key"})
user user
|> Map.put(:raw_bio, user.bio) |> Map.put(:raw_bio, user.bio)
|> Map.put(:signing_key, signing_key)
|> Map.merge(urls) |> Map.merge(urls)
|> merge_attributes(attrs) |> merge_attributes(attrs)
end end
def with_signing_key(%User{} = user) do
signing_key = build(:signing_key, %{user: user, key_id: "#{user.ap_id}#main-key"})
insert(signing_key)
%{user | signing_key: signing_key}
end
def signing_key_factory(attrs \\ %{}) do def signing_key_factory(attrs \\ %{}) do
pem = Enum.random(@rsa_keys) pem = Enum.random(@rsa_keys)
user = attrs[:user] || insert(:user)
{:ok, public_key} = Pleroma.User.SigningKey.private_pem_to_public_pem(pem) {:ok, public_key} = Pleroma.User.SigningKey.private_pem_to_public_pem(pem)
%Pleroma.User.SigningKey{ %Pleroma.User.SigningKey{
user_id: user.id,
public_key: public_key, public_key: public_key,
private_key: pem, private_key: pem,
key_id: attrs[:key_id] || "https://example.com/key" key_id: attrs[:key_id]
} }
|> merge_attributes(attrs)
end end
def user_relationship_factory(attrs \\ %{}) do def user_relationship_factory(attrs \\ %{}) do