2019-07-10 05:13:23 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-07-10 05:13:23 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-12-11 17:19:46 +00:00
|
|
|
defmodule Pleroma.Web.ActivityPub.UserViewTest do
|
2020-12-21 11:21:40 +00:00
|
|
|
use Pleroma.DataCase, async: true
|
2017-12-11 17:19:46 +00:00
|
|
|
import Pleroma.Factory
|
|
|
|
|
2019-05-22 03:58:15 +00:00
|
|
|
alias Pleroma.User
|
2017-12-11 17:19:46 +00:00
|
|
|
alias Pleroma.Web.ActivityPub.UserView
|
2019-07-12 15:36:07 +00:00
|
|
|
alias Pleroma.Web.CommonAPI
|
2017-12-11 17:19:46 +00:00
|
|
|
|
|
|
|
test "Renders a user, including the public key" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
result = UserView.render("user.json", %{user: user})
|
|
|
|
|
|
|
|
assert result["id"] == user.ap_id
|
|
|
|
assert result["preferredUsername"] == user.nickname
|
|
|
|
|
2018-07-12 02:45:48 +00:00
|
|
|
assert String.contains?(result["publicKey"]["publicKeyPem"], "BEGIN PUBLIC KEY")
|
2017-12-11 17:19:46 +00:00
|
|
|
end
|
2019-02-13 19:48:24 +00:00
|
|
|
|
2019-07-25 12:35:34 +00:00
|
|
|
test "Renders profile fields" do
|
|
|
|
fields = [
|
2019-08-06 11:21:25 +00:00
|
|
|
%{"name" => "foo", "value" => "bar"}
|
2019-07-25 12:35:34 +00:00
|
|
|
]
|
|
|
|
|
2019-08-06 11:21:25 +00:00
|
|
|
{:ok, user} =
|
|
|
|
insert(:user)
|
2020-04-11 18:44:52 +00:00
|
|
|
|> User.update_changeset(%{fields: fields})
|
2019-08-06 11:21:25 +00:00
|
|
|
|> User.update_and_set_cache()
|
2019-07-25 12:35:34 +00:00
|
|
|
|
|
|
|
assert %{
|
2019-08-06 11:21:25 +00:00
|
|
|
"attachment" => [%{"name" => "foo", "type" => "PropertyValue", "value" => "bar"}]
|
2019-07-25 12:35:34 +00:00
|
|
|
} = UserView.render("user.json", %{user: user})
|
|
|
|
end
|
|
|
|
|
2019-09-11 20:19:06 +00:00
|
|
|
test "Renders with emoji tags" do
|
2020-04-03 11:03:32 +00:00
|
|
|
user = insert(:user, emoji: %{"bib" => "/test"})
|
2019-09-11 20:19:06 +00:00
|
|
|
|
|
|
|
assert %{
|
|
|
|
"tag" => [
|
|
|
|
%{
|
|
|
|
"icon" => %{"type" => "Image", "url" => "/test"},
|
|
|
|
"id" => "/test",
|
|
|
|
"name" => ":bib:",
|
|
|
|
"type" => "Emoji",
|
|
|
|
"updated" => "1970-01-01T00:00:00Z"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
} = UserView.render("user.json", %{user: user})
|
|
|
|
end
|
|
|
|
|
2019-03-26 15:40:09 +00:00
|
|
|
test "Does not add an avatar image if the user hasn't set one" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
result = UserView.render("user.json", %{user: user})
|
|
|
|
refute result["icon"]
|
|
|
|
refute result["image"]
|
|
|
|
|
|
|
|
user =
|
|
|
|
insert(:user,
|
|
|
|
avatar: %{"url" => [%{"href" => "https://someurl"}]},
|
2019-10-16 18:59:21 +00:00
|
|
|
banner: %{"url" => [%{"href" => "https://somebanner"}]}
|
2019-03-26 15:40:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
result = UserView.render("user.json", %{user: user})
|
|
|
|
assert result["icon"]["url"] == "https://someurl"
|
|
|
|
assert result["image"]["url"] == "https://somebanner"
|
|
|
|
end
|
|
|
|
|
2019-10-19 17:53:52 +00:00
|
|
|
test "renders an invisible user with the invisible property set to true" do
|
2019-10-21 08:58:22 +00:00
|
|
|
user = insert(:user, invisible: true)
|
2019-10-19 17:53:52 +00:00
|
|
|
|
|
|
|
assert %{"invisible" => true} = UserView.render("service.json", %{user: user})
|
|
|
|
end
|
|
|
|
|
2023-12-15 06:01:31 +00:00
|
|
|
test "service has a few essential fields" do
|
|
|
|
user = insert(:user)
|
|
|
|
result = UserView.render("service.json", %{user: user})
|
|
|
|
assert result["id"]
|
|
|
|
assert result["type"] == "Application"
|
|
|
|
assert result["inbox"]
|
|
|
|
assert result["outbox"]
|
|
|
|
end
|
|
|
|
|
2020-12-31 00:53:27 +00:00
|
|
|
test "renders AKAs" do
|
|
|
|
akas = ["https://i.tusooa.xyz/users/test-pleroma"]
|
|
|
|
user = insert(:user, also_known_as: akas)
|
|
|
|
assert %{"alsoKnownAs" => ^akas} = UserView.render("user.json", %{user: user})
|
|
|
|
end
|
|
|
|
|
2019-02-13 19:48:24 +00:00
|
|
|
describe "endpoints" do
|
|
|
|
test "local users have a usable endpoints structure" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
result = UserView.render("user.json", %{user: user})
|
|
|
|
|
|
|
|
assert result["id"] == user.ap_id
|
|
|
|
|
|
|
|
%{
|
|
|
|
"sharedInbox" => _,
|
|
|
|
"oauthAuthorizationEndpoint" => _,
|
|
|
|
"oauthRegistrationEndpoint" => _,
|
|
|
|
"oauthTokenEndpoint" => _
|
|
|
|
} = result["endpoints"]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "remote users have an empty endpoints structure" do
|
|
|
|
user = insert(:user, local: false)
|
|
|
|
|
|
|
|
result = UserView.render("user.json", %{user: user})
|
|
|
|
|
|
|
|
assert result["id"] == user.ap_id
|
|
|
|
assert result["endpoints"] == %{}
|
|
|
|
end
|
2019-02-14 02:41:21 +00:00
|
|
|
|
|
|
|
test "instance users do not expose oAuth endpoints" do
|
|
|
|
user = insert(:user, nickname: nil, local: true)
|
|
|
|
|
|
|
|
result = UserView.render("user.json", %{user: user})
|
|
|
|
|
|
|
|
refute result["endpoints"]["oauthAuthorizationEndpoint"]
|
|
|
|
refute result["endpoints"]["oauthRegistrationEndpoint"]
|
|
|
|
refute result["endpoints"]["oauthTokenEndpoint"]
|
|
|
|
end
|
2019-02-13 19:48:24 +00:00
|
|
|
end
|
2019-07-12 15:36:07 +00:00
|
|
|
|
|
|
|
describe "followers" do
|
|
|
|
test "sets totalItems to zero when followers are hidden" do
|
|
|
|
user = insert(:user)
|
|
|
|
other_user = insert(:user)
|
|
|
|
{:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
|
|
|
|
assert %{"totalItems" => 1} = UserView.render("followers.json", %{user: user})
|
2019-10-16 18:59:21 +00:00
|
|
|
user = Map.merge(user, %{hide_followers_count: true, hide_followers: true})
|
2019-12-17 06:34:07 +00:00
|
|
|
refute UserView.render("followers.json", %{user: user}) |> Map.has_key?("totalItems")
|
2019-07-12 15:36:07 +00:00
|
|
|
end
|
2019-09-13 14:37:30 +00:00
|
|
|
|
|
|
|
test "sets correct totalItems when followers are hidden but the follower counter is not" do
|
|
|
|
user = insert(:user)
|
|
|
|
other_user = insert(:user)
|
|
|
|
{:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
|
|
|
|
assert %{"totalItems" => 1} = UserView.render("followers.json", %{user: user})
|
2019-10-16 18:59:21 +00:00
|
|
|
user = Map.merge(user, %{hide_followers_count: false, hide_followers: true})
|
2019-09-13 14:37:30 +00:00
|
|
|
assert %{"totalItems" => 1} = UserView.render("followers.json", %{user: user})
|
|
|
|
end
|
2019-07-12 15:36:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "following" do
|
|
|
|
test "sets totalItems to zero when follows are hidden" do
|
|
|
|
user = insert(:user)
|
|
|
|
other_user = insert(:user)
|
|
|
|
{:ok, user, _other_user, _activity} = CommonAPI.follow(user, other_user)
|
|
|
|
assert %{"totalItems" => 1} = UserView.render("following.json", %{user: user})
|
2019-10-16 18:59:21 +00:00
|
|
|
user = Map.merge(user, %{hide_follows_count: true, hide_follows: true})
|
2019-07-12 15:36:07 +00:00
|
|
|
assert %{"totalItems" => 0} = UserView.render("following.json", %{user: user})
|
|
|
|
end
|
2019-09-13 14:37:30 +00:00
|
|
|
|
|
|
|
test "sets correct totalItems when follows are hidden but the follow counter is not" do
|
|
|
|
user = insert(:user)
|
|
|
|
other_user = insert(:user)
|
|
|
|
{:ok, user, _other_user, _activity} = CommonAPI.follow(user, other_user)
|
|
|
|
assert %{"totalItems" => 1} = UserView.render("following.json", %{user: user})
|
2019-10-16 18:59:21 +00:00
|
|
|
user = Map.merge(user, %{hide_follows_count: false, hide_follows: true})
|
2019-09-13 14:37:30 +00:00
|
|
|
assert %{"totalItems" => 1} = UserView.render("following.json", %{user: user})
|
|
|
|
end
|
2019-07-12 15:36:07 +00:00
|
|
|
end
|
2017-12-11 17:19:46 +00:00
|
|
|
end
|