Merge remote-tracking branch 'remotes/upstream/develop' into 1260-rate-limited-auth...
[akkoma] / test / web / activity_pub / views / user_view_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.upgrade_changeset(%{info: %{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 "Does not add an avatar image if the user hasn't set one" do
41 user = insert(:user)
42 {:ok, user} = User.ensure_keys_present(user)
43
44 result = UserView.render("user.json", %{user: user})
45 refute result["icon"]
46 refute result["image"]
47
48 user =
49 insert(:user,
50 avatar: %{"url" => [%{"href" => "https://someurl"}]},
51 info: %{
52 banner: %{"url" => [%{"href" => "https://somebanner"}]}
53 }
54 )
55
56 {:ok, user} = User.ensure_keys_present(user)
57
58 result = UserView.render("user.json", %{user: user})
59 assert result["icon"]["url"] == "https://someurl"
60 assert result["image"]["url"] == "https://somebanner"
61 end
62
63 describe "endpoints" do
64 test "local users have a usable endpoints structure" do
65 user = insert(:user)
66 {:ok, user} = User.ensure_keys_present(user)
67
68 result = UserView.render("user.json", %{user: user})
69
70 assert result["id"] == user.ap_id
71
72 %{
73 "sharedInbox" => _,
74 "oauthAuthorizationEndpoint" => _,
75 "oauthRegistrationEndpoint" => _,
76 "oauthTokenEndpoint" => _
77 } = result["endpoints"]
78 end
79
80 test "remote users have an empty endpoints structure" do
81 user = insert(:user, local: false)
82 {:ok, user} = User.ensure_keys_present(user)
83
84 result = UserView.render("user.json", %{user: user})
85
86 assert result["id"] == user.ap_id
87 assert result["endpoints"] == %{}
88 end
89
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)
93
94 result = UserView.render("user.json", %{user: user})
95
96 refute result["endpoints"]["oauthAuthorizationEndpoint"]
97 refute result["endpoints"]["oauthRegistrationEndpoint"]
98 refute result["endpoints"]["oauthTokenEndpoint"]
99 end
100 end
101
102 describe "followers" do
103 test "sets totalItems to zero when followers are hidden" do
104 user = insert(:user)
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})
111 end
112 end
113
114 describe "following" do
115 test "sets totalItems to zero when follows are hidden" do
116 user = insert(:user)
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})
123 end
124 end
125 end