2019-07-10 01:13:23 -04:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-12-11 12:19:46 -05:00
|
|
|
defmodule Pleroma.Web.ActivityPub.UserViewTest do
|
|
|
|
use Pleroma.DataCase
|
|
|
|
import Pleroma.Factory
|
|
|
|
|
2019-05-21 23:58:15 -04:00
|
|
|
alias Pleroma.User
|
2017-12-11 12:19:46 -05:00
|
|
|
alias Pleroma.Web.ActivityPub.UserView
|
2019-07-12 11:36:07 -04:00
|
|
|
alias Pleroma.Web.CommonAPI
|
2017-12-11 12:19:46 -05:00
|
|
|
|
|
|
|
test "Renders a user, including the public key" do
|
|
|
|
user = insert(:user)
|
2019-05-21 23:58:15 -04:00
|
|
|
{:ok, user} = User.ensure_keys_present(user)
|
2017-12-11 12:19:46 -05:00
|
|
|
|
|
|
|
result = UserView.render("user.json", %{user: user})
|
|
|
|
|
|
|
|
assert result["id"] == user.ap_id
|
|
|
|
assert result["preferredUsername"] == user.nickname
|
|
|
|
|
2018-07-11 22:45:48 -04:00
|
|
|
assert String.contains?(result["publicKey"]["publicKeyPem"], "BEGIN PUBLIC KEY")
|
2017-12-11 12:19:46 -05:00
|
|
|
end
|
2019-02-13 14:48:24 -05:00
|
|
|
|
2019-07-25 08:35:34 -04:00
|
|
|
test "Renders profile fields" do
|
|
|
|
fields = [
|
2019-08-06 07:21:25 -04:00
|
|
|
%{"name" => "foo", "value" => "bar"}
|
2019-07-25 08:35:34 -04:00
|
|
|
]
|
|
|
|
|
2019-08-06 07:21:25 -04:00
|
|
|
{:ok, user} =
|
|
|
|
insert(:user)
|
|
|
|
|> User.upgrade_changeset(%{info: %{fields: fields}})
|
|
|
|
|> User.update_and_set_cache()
|
2019-07-25 08:35:34 -04:00
|
|
|
|
|
|
|
assert %{
|
2019-08-06 07:21:25 -04:00
|
|
|
"attachment" => [%{"name" => "foo", "type" => "PropertyValue", "value" => "bar"}]
|
2019-07-25 08:35:34 -04:00
|
|
|
} = UserView.render("user.json", %{user: user})
|
|
|
|
end
|
|
|
|
|
2019-03-26 11:40:09 -04:00
|
|
|
test "Does not add an avatar image if the user hasn't set one" do
|
|
|
|
user = insert(:user)
|
2019-05-21 23:58:15 -04:00
|
|
|
{:ok, user} = User.ensure_keys_present(user)
|
2019-03-26 11:40:09 -04:00
|
|
|
|
|
|
|
result = UserView.render("user.json", %{user: user})
|
|
|
|
refute result["icon"]
|
|
|
|
refute result["image"]
|
|
|
|
|
|
|
|
user =
|
|
|
|
insert(:user,
|
|
|
|
avatar: %{"url" => [%{"href" => "https://someurl"}]},
|
|
|
|
info: %{
|
|
|
|
banner: %{"url" => [%{"href" => "https://somebanner"}]}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-05-21 23:58:15 -04:00
|
|
|
{:ok, user} = User.ensure_keys_present(user)
|
2019-03-26 11:40:09 -04:00
|
|
|
|
|
|
|
result = UserView.render("user.json", %{user: user})
|
|
|
|
assert result["icon"]["url"] == "https://someurl"
|
|
|
|
assert result["image"]["url"] == "https://somebanner"
|
|
|
|
end
|
|
|
|
|
2019-02-13 14:48:24 -05:00
|
|
|
describe "endpoints" do
|
|
|
|
test "local users have a usable endpoints structure" do
|
|
|
|
user = insert(:user)
|
2019-05-21 23:58:15 -04:00
|
|
|
{:ok, user} = User.ensure_keys_present(user)
|
2019-02-13 14:48:24 -05:00
|
|
|
|
|
|
|
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)
|
2019-05-21 23:58:15 -04:00
|
|
|
{:ok, user} = User.ensure_keys_present(user)
|
2019-02-13 14:48:24 -05:00
|
|
|
|
|
|
|
result = UserView.render("user.json", %{user: user})
|
|
|
|
|
|
|
|
assert result["id"] == user.ap_id
|
|
|
|
assert result["endpoints"] == %{}
|
|
|
|
end
|
2019-02-13 21:41:21 -05:00
|
|
|
|
|
|
|
test "instance users do not expose oAuth endpoints" do
|
|
|
|
user = insert(:user, nickname: nil, local: true)
|
2019-05-21 23:58:15 -04:00
|
|
|
{:ok, user} = User.ensure_keys_present(user)
|
2019-02-13 21:41:21 -05:00
|
|
|
|
|
|
|
result = UserView.render("user.json", %{user: user})
|
|
|
|
|
|
|
|
refute result["endpoints"]["oauthAuthorizationEndpoint"]
|
|
|
|
refute result["endpoints"]["oauthRegistrationEndpoint"]
|
|
|
|
refute result["endpoints"]["oauthTokenEndpoint"]
|
|
|
|
end
|
2019-02-13 14:48:24 -05:00
|
|
|
end
|
2019-07-12 11:36:07 -04: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-09-13 10:37:30 -04:00
|
|
|
info = Map.merge(user.info, %{hide_followers_count: true, hide_followers: true})
|
2019-07-12 11:36:07 -04:00
|
|
|
user = Map.put(user, :info, info)
|
|
|
|
assert %{"totalItems" => 0} = UserView.render("followers.json", %{user: user})
|
|
|
|
end
|
2019-09-13 10:37:30 -04: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})
|
|
|
|
info = Map.merge(user.info, %{hide_followers_count: false, hide_followers: true})
|
|
|
|
user = Map.put(user, :info, info)
|
|
|
|
assert %{"totalItems" => 1} = UserView.render("followers.json", %{user: user})
|
|
|
|
end
|
2019-07-12 11:36:07 -04: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-09-13 10:37:30 -04:00
|
|
|
info = Map.merge(user.info, %{hide_follows_count: true, hide_follows: true})
|
2019-07-12 11:36:07 -04:00
|
|
|
user = Map.put(user, :info, info)
|
|
|
|
assert %{"totalItems" => 0} = UserView.render("following.json", %{user: user})
|
|
|
|
end
|
2019-09-13 10:37:30 -04: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})
|
|
|
|
info = Map.merge(user.info, %{hide_follows_count: false, hide_follows: true})
|
|
|
|
user = Map.put(user, :info, info)
|
|
|
|
assert %{"totalItems" => 1} = UserView.render("following.json", %{user: user})
|
|
|
|
end
|
2019-07-12 11:36:07 -04:00
|
|
|
end
|
2019-09-19 10:09:24 -04:00
|
|
|
|
|
|
|
test "outbox paginates correctly" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
posts =
|
|
|
|
for i <- 0..25 do
|
|
|
|
{:ok, activity} = CommonAPI.post(user, %{"status" => "post #{i}"})
|
|
|
|
activity
|
|
|
|
end
|
|
|
|
|
|
|
|
# outbox sorts chronologically, newest first, with ten per page
|
|
|
|
posts = Enum.reverse(posts)
|
|
|
|
|
|
|
|
%{"first" => %{"next" => next_url}} =
|
|
|
|
UserView.render("outbox.json", %{user: user, max_id: nil})
|
|
|
|
|
|
|
|
next_id = Enum.at(posts, 9).id
|
|
|
|
assert next_url =~ next_id
|
|
|
|
|
|
|
|
%{"next" => next_url} = UserView.render("outbox.json", %{user: user, max_id: next_id})
|
|
|
|
next_id = Enum.at(posts, 19).id
|
|
|
|
assert next_url =~ next_id
|
|
|
|
end
|
2017-12-11 12:19:46 -05:00
|
|
|
end
|