AP UserView: Refactor banner / avatar display code, add test.
[akkoma] / test / web / activity_pub / views / user_view_test.exs
1 defmodule Pleroma.Web.ActivityPub.UserViewTest do
2 use Pleroma.DataCase
3 import Pleroma.Factory
4
5 alias Pleroma.Web.ActivityPub.UserView
6
7 test "Renders a user, including the public key" do
8 user = insert(:user)
9 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
10
11 result = UserView.render("user.json", %{user: user})
12
13 assert result["id"] == user.ap_id
14 assert result["preferredUsername"] == user.nickname
15
16 assert String.contains?(result["publicKey"]["publicKeyPem"], "BEGIN PUBLIC KEY")
17 end
18
19 test "Does not add an avatar image if the user hasn't set one" do
20 user = insert(:user)
21 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
22
23 result = UserView.render("user.json", %{user: user})
24 refute result["icon"]
25 refute result["image"]
26
27 user =
28 insert(:user,
29 avatar: %{"url" => [%{"href" => "https://someurl"}]},
30 info: %{
31 banner: %{"url" => [%{"href" => "https://somebanner"}]}
32 }
33 )
34
35 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
36
37 result = UserView.render("user.json", %{user: user})
38 assert result["icon"]["url"] == "https://someurl"
39 assert result["image"]["url"] == "https://somebanner"
40 end
41
42 describe "endpoints" do
43 test "local users have a usable endpoints structure" do
44 user = insert(:user)
45 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
46
47 result = UserView.render("user.json", %{user: user})
48
49 assert result["id"] == user.ap_id
50
51 %{
52 "sharedInbox" => _,
53 "oauthAuthorizationEndpoint" => _,
54 "oauthRegistrationEndpoint" => _,
55 "oauthTokenEndpoint" => _
56 } = result["endpoints"]
57 end
58
59 test "remote users have an empty endpoints structure" do
60 user = insert(:user, local: false)
61 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
62
63 result = UserView.render("user.json", %{user: user})
64
65 assert result["id"] == user.ap_id
66 assert result["endpoints"] == %{}
67 end
68
69 test "instance users do not expose oAuth endpoints" do
70 user = insert(:user, nickname: nil, local: true)
71 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
72
73 result = UserView.render("user.json", %{user: user})
74
75 refute result["endpoints"]["oauthAuthorizationEndpoint"]
76 refute result["endpoints"]["oauthRegistrationEndpoint"]
77 refute result["endpoints"]["oauthTokenEndpoint"]
78 end
79 end
80 end