Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into chat-federation...
[akkoma] / test / web / activity_pub / views / user_view_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.UserViewTest do
6 use Pleroma.DataCase
7 import Pleroma.Factory
8
9 alias Pleroma.User
10 alias Pleroma.Web.ActivityPub.UserView
11 alias Pleroma.Web.CommonAPI
12
13 test "Renders a user, including the public key" do
14 user = insert(:user)
15 {:ok, user} = User.ensure_keys_present(user)
16
17 result = UserView.render("user.json", %{user: user})
18
19 assert result["id"] == user.ap_id
20 assert result["preferredUsername"] == user.nickname
21
22 assert String.contains?(result["publicKey"]["publicKeyPem"], "BEGIN PUBLIC KEY")
23 end
24
25 test "Renders profile fields" do
26 fields = [
27 %{"name" => "foo", "value" => "bar"}
28 ]
29
30 {:ok, user} =
31 insert(:user)
32 |> User.update_changeset(%{fields: fields})
33 |> User.update_and_set_cache()
34
35 assert %{
36 "attachment" => [%{"name" => "foo", "type" => "PropertyValue", "value" => "bar"}]
37 } = UserView.render("user.json", %{user: user})
38 end
39
40 test "Renders with emoji tags" do
41 user = insert(:user, emoji: %{"bib" => "/test"})
42
43 assert %{
44 "tag" => [
45 %{
46 "icon" => %{"type" => "Image", "url" => "/test"},
47 "id" => "/test",
48 "name" => ":bib:",
49 "type" => "Emoji",
50 "updated" => "1970-01-01T00:00:00Z"
51 }
52 ]
53 } = UserView.render("user.json", %{user: user})
54 end
55
56 test "Does not add an avatar image if the user hasn't set one" do
57 user = insert(:user)
58 {:ok, user} = User.ensure_keys_present(user)
59
60 result = UserView.render("user.json", %{user: user})
61 refute result["icon"]
62 refute result["image"]
63
64 user =
65 insert(:user,
66 avatar: %{"url" => [%{"href" => "https://someurl"}]},
67 banner: %{"url" => [%{"href" => "https://somebanner"}]}
68 )
69
70 {:ok, user} = User.ensure_keys_present(user)
71
72 result = UserView.render("user.json", %{user: user})
73 assert result["icon"]["url"] == "https://someurl"
74 assert result["image"]["url"] == "https://somebanner"
75 end
76
77 test "renders an invisible user with the invisible property set to true" do
78 user = insert(:user, invisible: true)
79
80 assert %{"invisible" => true} = UserView.render("service.json", %{user: user})
81 end
82
83 describe "endpoints" do
84 test "local users have a usable endpoints structure" do
85 user = insert(:user)
86 {:ok, user} = User.ensure_keys_present(user)
87
88 result = UserView.render("user.json", %{user: user})
89
90 assert result["id"] == user.ap_id
91
92 %{
93 "sharedInbox" => _,
94 "oauthAuthorizationEndpoint" => _,
95 "oauthRegistrationEndpoint" => _,
96 "oauthTokenEndpoint" => _
97 } = result["endpoints"]
98 end
99
100 test "remote users have an empty endpoints structure" do
101 user = insert(:user, local: false)
102 {:ok, user} = User.ensure_keys_present(user)
103
104 result = UserView.render("user.json", %{user: user})
105
106 assert result["id"] == user.ap_id
107 assert result["endpoints"] == %{}
108 end
109
110 test "instance users do not expose oAuth endpoints" do
111 user = insert(:user, nickname: nil, local: true)
112 {:ok, user} = User.ensure_keys_present(user)
113
114 result = UserView.render("user.json", %{user: user})
115
116 refute result["endpoints"]["oauthAuthorizationEndpoint"]
117 refute result["endpoints"]["oauthRegistrationEndpoint"]
118 refute result["endpoints"]["oauthTokenEndpoint"]
119 end
120 end
121
122 describe "followers" do
123 test "sets totalItems to zero when followers are hidden" do
124 user = insert(:user)
125 other_user = insert(:user)
126 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
127 assert %{"totalItems" => 1} = UserView.render("followers.json", %{user: user})
128 user = Map.merge(user, %{hide_followers_count: true, hide_followers: true})
129 refute UserView.render("followers.json", %{user: user}) |> Map.has_key?("totalItems")
130 end
131
132 test "sets correct totalItems when followers are hidden but the follower counter is not" do
133 user = insert(:user)
134 other_user = insert(:user)
135 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
136 assert %{"totalItems" => 1} = UserView.render("followers.json", %{user: user})
137 user = Map.merge(user, %{hide_followers_count: false, hide_followers: true})
138 assert %{"totalItems" => 1} = UserView.render("followers.json", %{user: user})
139 end
140 end
141
142 describe "following" do
143 test "sets totalItems to zero when follows are hidden" do
144 user = insert(:user)
145 other_user = insert(:user)
146 {:ok, user, _other_user, _activity} = CommonAPI.follow(user, other_user)
147 assert %{"totalItems" => 1} = UserView.render("following.json", %{user: user})
148 user = Map.merge(user, %{hide_follows_count: true, hide_follows: true})
149 assert %{"totalItems" => 0} = UserView.render("following.json", %{user: user})
150 end
151
152 test "sets correct totalItems when follows are hidden but the follow counter is not" do
153 user = insert(:user)
154 other_user = insert(:user)
155 {:ok, user, _other_user, _activity} = CommonAPI.follow(user, other_user)
156 assert %{"totalItems" => 1} = UserView.render("following.json", %{user: user})
157 user = Map.merge(user, %{hide_follows_count: false, hide_follows: true})
158 assert %{"totalItems" => 1} = UserView.render("following.json", %{user: user})
159 end
160 end
161
162 describe "acceptsChatMessages" do
163 test "it returns this value if it is set" do
164 true_user = insert(:user, accepts_chat_messages: true)
165 false_user = insert(:user, accepts_chat_messages: false)
166 nil_user = insert(:user, accepts_chat_messages: nil)
167
168 assert %{"capabilities" => %{"acceptsChatMessages" => true}} =
169 UserView.render("user.json", user: true_user)
170
171 assert %{"capabilities" => %{"acceptsChatMessages" => false}} =
172 UserView.render("user.json", user: false_user)
173
174 refute Map.has_key?(
175 UserView.render("user.json", user: nil_user)["capabilities"],
176 "acceptsChatMessages"
177 )
178 end
179 end
180 end