1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.UserViewTest do
10 alias Pleroma.Web.ActivityPub.UserView
11 alias Pleroma.Web.CommonAPI
13 test "Renders a user, including the public key" do
15 {:ok, user} = User.ensure_keys_present(user)
17 result = UserView.render("user.json", %{user: user})
19 assert result["id"] == user.ap_id
20 assert result["preferredUsername"] == user.nickname
22 assert String.contains?(result["publicKey"]["publicKeyPem"], "BEGIN PUBLIC KEY")
25 test "Renders profile fields" do
27 %{"name" => "foo", "value" => "bar"}
32 |> User.upgrade_changeset(%{info: %{fields: fields}})
33 |> User.update_and_set_cache()
36 "attachment" => [%{"name" => "foo", "type" => "PropertyValue", "value" => "bar"}]
37 } = UserView.render("user.json", %{user: user})
40 test "Does not add an avatar image if the user hasn't set one" do
42 {:ok, user} = User.ensure_keys_present(user)
44 result = UserView.render("user.json", %{user: user})
46 refute result["image"]
50 avatar: %{"url" => [%{"href" => "https://someurl"}]},
52 banner: %{"url" => [%{"href" => "https://somebanner"}]}
56 {:ok, user} = User.ensure_keys_present(user)
58 result = UserView.render("user.json", %{user: user})
59 assert result["icon"]["url"] == "https://someurl"
60 assert result["image"]["url"] == "https://somebanner"
63 describe "endpoints" do
64 test "local users have a usable endpoints structure" do
66 {:ok, user} = User.ensure_keys_present(user)
68 result = UserView.render("user.json", %{user: user})
70 assert result["id"] == user.ap_id
74 "oauthAuthorizationEndpoint" => _,
75 "oauthRegistrationEndpoint" => _,
76 "oauthTokenEndpoint" => _
77 } = result["endpoints"]
80 test "remote users have an empty endpoints structure" do
81 user = insert(:user, local: false)
82 {:ok, user} = User.ensure_keys_present(user)
84 result = UserView.render("user.json", %{user: user})
86 assert result["id"] == user.ap_id
87 assert result["endpoints"] == %{}
90 test "instance users do not expose oAuth endpoints" do
91 user = insert(:user, nickname: nil, local: true)
92 {:ok, user} = User.ensure_keys_present(user)
94 result = UserView.render("user.json", %{user: user})
96 refute result["endpoints"]["oauthAuthorizationEndpoint"]
97 refute result["endpoints"]["oauthRegistrationEndpoint"]
98 refute result["endpoints"]["oauthTokenEndpoint"]
102 describe "followers" do
103 test "sets totalItems to zero when followers are hidden" do
105 other_user = insert(:user)
106 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
107 assert %{"totalItems" => 1} = UserView.render("followers.json", %{user: user})
108 info = Map.put(user.info, :hide_followers, true)
109 user = Map.put(user, :info, info)
110 assert %{"totalItems" => 0} = UserView.render("followers.json", %{user: user})
114 describe "following" do
115 test "sets totalItems to zero when follows are hidden" do
117 other_user = insert(:user)
118 {:ok, user, _other_user, _activity} = CommonAPI.follow(user, other_user)
119 assert %{"totalItems" => 1} = UserView.render("following.json", %{user: user})
120 info = Map.put(user.info, :hide_follows, true)
121 user = Map.put(user, :info, info)
122 assert %{"totalItems" => 0} = UserView.render("following.json", %{user: user})