move to 2.5.5
[anni] / test / pleroma / web / activity_pub / transmogrifier / user_update_handling_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.Transmogrifier.UserUpdateHandlingTest do
6   use Pleroma.DataCase
7
8   alias Pleroma.Activity
9   alias Pleroma.User
10   alias Pleroma.Web.ActivityPub.Transmogrifier
11
12   import Pleroma.Factory
13
14   test "it works for incoming update activities" do
15     user = insert(:user, local: false)
16
17     update_data = File.read!("test/fixtures/mastodon-update.json") |> Jason.decode!()
18
19     object =
20       update_data["object"]
21       |> Map.put("actor", user.ap_id)
22       |> Map.put("id", user.ap_id)
23
24     update_data =
25       update_data
26       |> Map.put("actor", user.ap_id)
27       |> Map.put("object", object)
28
29     {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data)
30
31     assert data["id"] == update_data["id"]
32
33     user = User.get_cached_by_ap_id(data["actor"])
34     assert user.name == "gargle"
35
36     assert user.avatar["url"] == [
37              %{
38                "href" =>
39                  "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"
40              }
41            ]
42
43     assert user.banner["url"] == [
44              %{
45                "href" =>
46                  "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"
47              }
48            ]
49
50     assert user.bio == "<p>Some bio</p>"
51   end
52
53   test "it works with alsoKnownAs" do
54     %{ap_id: actor} = insert(:user, local: false)
55
56     assert User.get_cached_by_ap_id(actor).also_known_as == []
57
58     {:ok, _activity} =
59       "test/fixtures/mastodon-update.json"
60       |> File.read!()
61       |> Jason.decode!()
62       |> Map.put("actor", actor)
63       |> Map.update!("object", fn object ->
64         object
65         |> Map.put("actor", actor)
66         |> Map.put("id", actor)
67         |> Map.put("alsoKnownAs", [
68           "http://mastodon.example.org/users/foo",
69           "http://example.org/users/bar"
70         ])
71       end)
72       |> Transmogrifier.handle_incoming()
73
74     assert User.get_cached_by_ap_id(actor).also_known_as == [
75              "http://mastodon.example.org/users/foo",
76              "http://example.org/users/bar"
77            ]
78   end
79
80   test "it works with custom profile fields" do
81     user = insert(:user, local: false)
82
83     assert user.fields == []
84
85     update_data = File.read!("test/fixtures/mastodon-update.json") |> Jason.decode!()
86
87     object =
88       update_data["object"]
89       |> Map.put("actor", user.ap_id)
90       |> Map.put("id", user.ap_id)
91
92     update_data =
93       update_data
94       |> Map.put("actor", user.ap_id)
95       |> Map.put("object", object)
96
97     {:ok, _update_activity} = Transmogrifier.handle_incoming(update_data)
98
99     user = User.get_cached_by_ap_id(user.ap_id)
100
101     assert user.fields == [
102              %{"name" => "foo", "value" => "updated"},
103              %{"name" => "foo1", "value" => "updated"}
104            ]
105
106     clear_config([:instance, :max_remote_account_fields], 2)
107
108     update_data =
109       update_data
110       |> put_in(["object", "attachment"], [
111         %{"name" => "foo", "type" => "PropertyValue", "value" => "bar"},
112         %{"name" => "foo11", "type" => "PropertyValue", "value" => "bar11"},
113         %{"name" => "foo22", "type" => "PropertyValue", "value" => "bar22"}
114       ])
115       |> Map.put("id", update_data["id"] <> ".")
116
117     {:ok, _} = Transmogrifier.handle_incoming(update_data)
118
119     user = User.get_cached_by_ap_id(user.ap_id)
120
121     assert user.fields == [
122              %{"name" => "foo", "value" => "updated"},
123              %{"name" => "foo1", "value" => "updated"}
124            ]
125
126     update_data =
127       update_data
128       |> put_in(["object", "attachment"], [])
129       |> Map.put("id", update_data["id"] <> ".")
130
131     {:ok, _} = Transmogrifier.handle_incoming(update_data)
132
133     user = User.get_cached_by_ap_id(user.ap_id)
134
135     assert user.fields == []
136   end
137
138   test "it works for incoming update activities which lock the account" do
139     user = insert(:user, local: false)
140
141     update_data = File.read!("test/fixtures/mastodon-update.json") |> Jason.decode!()
142
143     object =
144       update_data["object"]
145       |> Map.put("actor", user.ap_id)
146       |> Map.put("id", user.ap_id)
147       |> Map.put("manuallyApprovesFollowers", true)
148
149     update_data =
150       update_data
151       |> Map.put("actor", user.ap_id)
152       |> Map.put("object", object)
153
154     {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(update_data)
155
156     user = User.get_cached_by_ap_id(user.ap_id)
157     assert user.is_locked == true
158   end
159 end