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(%{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 "Renders with emoji tags" do
41 user = insert(:user, emoji: [%{"bib" => "/test"}])
46 "icon" => %{"type" => "Image", "url" => "/test"},
50 "updated" => "1970-01-01T00:00:00Z"
53 } = UserView.render("user.json", %{user: user})
56 test "Does not add an avatar image if the user hasn't set one" do
58 {:ok, user} = User.ensure_keys_present(user)
60 result = UserView.render("user.json", %{user: user})
62 refute result["image"]
66 avatar: %{"url" => [%{"href" => "https://someurl"}]},
67 banner: %{"url" => [%{"href" => "https://somebanner"}]}
70 {:ok, user} = User.ensure_keys_present(user)
72 result = UserView.render("user.json", %{user: user})
73 assert result["icon"]["url"] == "https://someurl"
74 assert result["image"]["url"] == "https://somebanner"
77 test "renders an invisible user with the invisible property set to true" do
78 user = insert(:user, %{info: %{invisible: true}})
80 assert %{"invisible" => true} = UserView.render("service.json", %{user: user})
83 describe "endpoints" do
84 test "local users have a usable endpoints structure" do
86 {:ok, user} = User.ensure_keys_present(user)
88 result = UserView.render("user.json", %{user: user})
90 assert result["id"] == user.ap_id
94 "oauthAuthorizationEndpoint" => _,
95 "oauthRegistrationEndpoint" => _,
96 "oauthTokenEndpoint" => _
97 } = result["endpoints"]
100 test "remote users have an empty endpoints structure" do
101 user = insert(:user, local: false)
102 {:ok, user} = User.ensure_keys_present(user)
104 result = UserView.render("user.json", %{user: user})
106 assert result["id"] == user.ap_id
107 assert result["endpoints"] == %{}
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)
114 result = UserView.render("user.json", %{user: user})
116 refute result["endpoints"]["oauthAuthorizationEndpoint"]
117 refute result["endpoints"]["oauthRegistrationEndpoint"]
118 refute result["endpoints"]["oauthTokenEndpoint"]
122 describe "followers" do
123 test "sets totalItems to zero when followers are hidden" do
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 assert %{"totalItems" => 0} = UserView.render("followers.json", %{user: user})
132 test "sets correct totalItems when followers are hidden but the follower counter is not" do
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})
142 describe "following" do
143 test "sets totalItems to zero when follows are hidden" do
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})
152 test "sets correct totalItems when follows are hidden but the follow counter is not" do
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})
162 test "activity collection page aginates correctly" do
167 {:ok, activity} = CommonAPI.post(user, %{"status" => "post #{i}"})
171 # outbox sorts chronologically, newest first, with ten per page
172 posts = Enum.reverse(posts)
174 %{"next" => next_url} =
175 UserView.render("activity_collection_page.json", %{
176 iri: "#{user.ap_id}/outbox",
177 activities: Enum.take(posts, 10)
180 next_id = Enum.at(posts, 9).id
181 assert next_url =~ next_id
183 %{"next" => next_url} =
184 UserView.render("activity_collection_page.json", %{
185 iri: "#{user.ap_id}/outbox",
186 activities: Enum.take(Enum.drop(posts, 10), 10)
189 next_id = Enum.at(posts, 19).id
190 assert next_url =~ next_id