Merge branch 'develop' into 'remove-avatar-header'
[akkoma] / test / object / containment_test.exs
1 defmodule Pleroma.Object.ContainmentTest do
2 use Pleroma.DataCase
3
4 alias Pleroma.Object.Containment
5 alias Pleroma.User
6
7 import Pleroma.Factory
8 import ExUnit.CaptureLog
9
10 setup_all do
11 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
12 :ok
13 end
14
15 describe "general origin containment" do
16 test "contain_origin_from_id() catches obvious spoofing attempts" do
17 data = %{
18 "id" => "http://example.com/~alyssa/activities/1234.json"
19 }
20
21 :error =
22 Containment.contain_origin_from_id(
23 "http://example.org/~alyssa/activities/1234.json",
24 data
25 )
26 end
27
28 test "contain_origin_from_id() allows alternate IDs within the same origin domain" do
29 data = %{
30 "id" => "http://example.com/~alyssa/activities/1234.json"
31 }
32
33 :ok =
34 Containment.contain_origin_from_id(
35 "http://example.com/~alyssa/activities/1234",
36 data
37 )
38 end
39
40 test "contain_origin_from_id() allows matching IDs" do
41 data = %{
42 "id" => "http://example.com/~alyssa/activities/1234.json"
43 }
44
45 :ok =
46 Containment.contain_origin_from_id(
47 "http://example.com/~alyssa/activities/1234.json",
48 data
49 )
50 end
51
52 test "users cannot be collided through fake direction spoofing attempts" do
53 _user =
54 insert(:user, %{
55 nickname: "rye@niu.moe",
56 local: false,
57 ap_id: "https://niu.moe/users/rye",
58 follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})
59 })
60
61 assert capture_log(fn ->
62 {:error, _} = User.get_or_fetch_by_ap_id("https://n1u.moe/users/rye")
63 end) =~
64 "[error] Could not decode user at fetch https://n1u.moe/users/rye, {:error, :error}"
65 end
66 end
67 end