user_view_test.exs 8.6 KB
Newer Older
dtluna's avatar
dtluna committed
1
defmodule Pleroma.Web.TwitterAPI.UserViewTest do
lain's avatar
lain committed
2
3
4
  use Pleroma.DataCase

  alias Pleroma.User
5
6
  alias Pleroma.Web.TwitterAPI.UserView
  alias Pleroma.Web.CommonAPI.Utils
lain's avatar
lain committed
7

lain's avatar
lain committed
8
9
  import Pleroma.Factory

lain's avatar
lain committed
10
  setup do
lain's avatar
lain committed
11
    user = insert(:user, bio: "<span>Here's some html</span>")
lain's avatar
lain committed
12
13
14
    [user: user]
  end

15
16
17
18
19
20
21
  test "A user with only a nickname", %{user: user} do
    user = %{user | name: nil, nickname: "scarlett@catgirl.science"}
    represented = UserView.render("show.json", %{user: user})
    assert represented["name"] == user.nickname
    assert represented["name_html"] == user.nickname
  end

22
23
  test "A user with an avatar object", %{user: user} do
    image = "image"
lain's avatar
lain committed
24
    user = %{user | avatar: %{"url" => [%{"href" => image}]}}
dtluna's avatar
dtluna committed
25
    represented = UserView.render("show.json", %{user: user})
26
27
28
    assert represented["profile_image_url"] == image
  end

Maksim's avatar
Maksim committed
29
  test "A user with emoji in username" do
eal's avatar
eal committed
30
    expected =
kaniini's avatar
kaniini committed
31
      "<img height=\"32px\" width=\"32px\" alt=\"karjalanpiirakka\" title=\"karjalanpiirakka\" src=\"/file.png\" /> man"
eal's avatar
eal committed
32

lain's avatar
lain committed
33
34
35
36
    user =
      insert(:user, %{
        info: %{
          source_data: %{
eal's avatar
eal committed
37
38
39
40
41
42
43
44
            "tag" => [
              %{
                "type" => "Emoji",
                "icon" => %{"url" => "/file.png"},
                "name" => ":karjalanpiirakka:"
              }
            ]
          }
lain's avatar
lain committed
45
46
47
        },
        name: ":karjalanpiirakka: man"
      })
eal's avatar
eal committed
48
49
50
51
52

    represented = UserView.render("show.json", %{user: user})
    assert represented["name_html"] == expected
  end

lain's avatar
lain committed
53
54
55
  test "A user" do
    note_activity = insert(:note_activity)
    user = User.get_cached_by_ap_id(note_activity.data["actor"])
56
    {:ok, user} = User.update_note_count(user)
lain's avatar
lain committed
57
58
59
60
61
62
    follower = insert(:user)
    second_follower = insert(:user)

    User.follow(follower, user)
    User.follow(second_follower, user)
    User.follow(user, follower)
eal's avatar
eal committed
63
    {:ok, user} = User.update_follower_count(user)
Thog's avatar
Thog committed
64
    Cachex.put(:user_cache, "user_info:#{user.id}", User.user_info(Repo.get!(User, user.id)))
lain's avatar
lain committed
65

66
67
    image = "http://localhost:4001/images/avi.png"
    banner = "http://localhost:4001/images/banner.png"
lain's avatar
lain committed
68

lain's avatar
lain committed
69
70
71
72
    represented = %{
      "id" => user.id,
      "name" => user.name,
      "screen_name" => user.nickname,
eal's avatar
eal committed
73
      "name_html" => user.name,
ensra's avatar
ensra committed
74
75
      "description" => HtmlSanitizeEx.strip_tags(user.bio |> String.replace("<br>", "\n")),
      "description_html" => HtmlSanitizeEx.basic_html(user.bio),
lain's avatar
lain committed
76
      "created_at" => user.inserted_at |> Utils.format_naive_asctime(),
lain's avatar
lain committed
77
      "favourites_count" => 0,
lain's avatar
lain committed
78
79
80
      "statuses_count" => 1,
      "friends_count" => 1,
      "followers_count" => 2,
lain's avatar
lain committed
81
82
83
84
85
      "profile_image_url" => image,
      "profile_image_url_https" => image,
      "profile_image_url_profile_size" => image,
      "profile_image_url_original" => image,
      "following" => false,
eal's avatar
eal committed
86
      "follows_you" => false,
eal's avatar
eal committed
87
      "statusnet_blocking" => false,
lain's avatar
lain committed
88
89
90
      "rights" => %{
        "delete_others_notice" => false
      },
lain's avatar
lain committed
91
      "statusnet_profile_url" => user.ap_id,
92
      "cover_photo" => banner,
eal's avatar
eal committed
93
      "background_image" => nil,
94
      "is_local" => true,
95
      "locked" => false,
96
      "default_scope" => "public",
97
      "no_rich_text" => false,
Ivan Tashkinov's avatar
Ivan Tashkinov committed
98
      "fields" => [],
99
100
101
102
      "pleroma" => %{
        "confirmation_pending" => false,
        "tags" => []
      }
lain's avatar
lain committed
103
104
    }

dtluna's avatar
dtluna committed
105
    assert represented == UserView.render("show.json", %{user: user})
lain's avatar
lain committed
106
  end
107
108

  test "A user for a given other follower", %{user: user} do
lain's avatar
lain committed
109
    follower = insert(:user, %{following: [User.ap_followers(user)]})
110
    {:ok, user} = User.update_follower_count(user)
111
112
    image = "http://localhost:4001/images/avi.png"
    banner = "http://localhost:4001/images/banner.png"
113

114
115
116
117
    represented = %{
      "id" => user.id,
      "name" => user.name,
      "screen_name" => user.nickname,
eal's avatar
eal committed
118
      "name_html" => user.name,
ensra's avatar
ensra committed
119
120
      "description" => HtmlSanitizeEx.strip_tags(user.bio |> String.replace("<br>", "\n")),
      "description_html" => HtmlSanitizeEx.basic_html(user.bio),
lain's avatar
lain committed
121
      "created_at" => user.inserted_at |> Utils.format_naive_asctime(),
122
123
124
      "favourites_count" => 0,
      "statuses_count" => 0,
      "friends_count" => 0,
lain's avatar
lain committed
125
      "followers_count" => 1,
126
127
128
129
130
      "profile_image_url" => image,
      "profile_image_url_https" => image,
      "profile_image_url_profile_size" => image,
      "profile_image_url_original" => image,
      "following" => true,
eal's avatar
eal committed
131
      "follows_you" => false,
eal's avatar
eal committed
132
      "statusnet_blocking" => false,
lain's avatar
lain committed
133
134
135
      "rights" => %{
        "delete_others_notice" => false
      },
lain's avatar
lain committed
136
      "statusnet_profile_url" => user.ap_id,
137
      "cover_photo" => banner,
eal's avatar
eal committed
138
      "background_image" => nil,
139
      "is_local" => true,
140
      "locked" => false,
141
      "default_scope" => "public",
142
      "no_rich_text" => false,
Ivan Tashkinov's avatar
Ivan Tashkinov committed
143
      "fields" => [],
144
145
146
147
      "pleroma" => %{
        "confirmation_pending" => false,
        "tags" => []
      }
148
149
    }

dtluna's avatar
dtluna committed
150
    assert represented == UserView.render("show.json", %{user: user, for: follower})
151
  end
eal's avatar
eal committed
152

eal's avatar
eal committed
153
  test "A user that follows you", %{user: user} do
154
155
    follower = insert(:user)
    {:ok, follower} = User.follow(follower, user)
eal's avatar
eal committed
156
    {:ok, user} = User.update_follower_count(user)
157
158
    image = "http://localhost:4001/images/avi.png"
    banner = "http://localhost:4001/images/banner.png"
159

eal's avatar
eal committed
160
161
162
163
    represented = %{
      "id" => follower.id,
      "name" => follower.name,
      "screen_name" => follower.nickname,
eal's avatar
eal committed
164
      "name_html" => follower.name,
165
166
      "description" => HtmlSanitizeEx.strip_tags(follower.bio |> String.replace("<br>", "\n")),
      "description_html" => HtmlSanitizeEx.basic_html(follower.bio),
lain's avatar
lain committed
167
      "created_at" => follower.inserted_at |> Utils.format_naive_asctime(),
eal's avatar
eal committed
168
169
170
171
172
173
174
175
176
177
178
      "favourites_count" => 0,
      "statuses_count" => 0,
      "friends_count" => 1,
      "followers_count" => 0,
      "profile_image_url" => image,
      "profile_image_url_https" => image,
      "profile_image_url_profile_size" => image,
      "profile_image_url_original" => image,
      "following" => false,
      "follows_you" => true,
      "statusnet_blocking" => false,
lain's avatar
lain committed
179
180
181
      "rights" => %{
        "delete_others_notice" => false
      },
eal's avatar
eal committed
182
      "statusnet_profile_url" => follower.ap_id,
183
      "cover_photo" => banner,
eal's avatar
eal committed
184
      "background_image" => nil,
185
      "is_local" => true,
186
      "locked" => false,
187
      "default_scope" => "public",
188
      "no_rich_text" => false,
Ivan Tashkinov's avatar
Ivan Tashkinov committed
189
      "fields" => [],
190
191
192
193
      "pleroma" => %{
        "confirmation_pending" => false,
        "tags" => []
      }
eal's avatar
eal committed
194
195
196
197
198
    }

    assert represented == UserView.render("show.json", %{user: follower, for: user})
  end

lain's avatar
lain committed
199
  test "a user that is a moderator" do
lain's avatar
lain committed
200
    user = insert(:user, %{info: %{is_moderator: true}})
lain's avatar
lain committed
201
202
203
204
205
    represented = UserView.render("show.json", %{user: user, for: user})

    assert represented["rights"]["delete_others_notice"]
  end

lain's avatar
lain committed
206
  test "A blocked user for the blocker" do
eal's avatar
eal committed
207
208
209
    user = insert(:user)
    blocker = insert(:user)
    User.block(blocker, user)
210
211
    image = "http://localhost:4001/images/avi.png"
    banner = "http://localhost:4001/images/banner.png"
212

eal's avatar
eal committed
213
214
215
216
    represented = %{
      "id" => user.id,
      "name" => user.name,
      "screen_name" => user.nickname,
eal's avatar
eal committed
217
      "name_html" => user.name,
ensra's avatar
ensra committed
218
219
      "description" => HtmlSanitizeEx.strip_tags(user.bio |> String.replace("<br>", "\n")),
      "description_html" => HtmlSanitizeEx.basic_html(user.bio),
lain's avatar
lain committed
220
      "created_at" => user.inserted_at |> Utils.format_naive_asctime(),
eal's avatar
eal committed
221
222
223
224
225
226
227
228
229
      "favourites_count" => 0,
      "statuses_count" => 0,
      "friends_count" => 0,
      "followers_count" => 0,
      "profile_image_url" => image,
      "profile_image_url_https" => image,
      "profile_image_url_profile_size" => image,
      "profile_image_url_original" => image,
      "following" => false,
eal's avatar
eal committed
230
      "follows_you" => false,
eal's avatar
eal committed
231
      "statusnet_blocking" => true,
lain's avatar
lain committed
232
233
234
      "rights" => %{
        "delete_others_notice" => false
      },
eal's avatar
eal committed
235
      "statusnet_profile_url" => user.ap_id,
236
      "cover_photo" => banner,
eal's avatar
eal committed
237
      "background_image" => nil,
238
      "is_local" => true,
239
      "locked" => false,
240
      "default_scope" => "public",
241
      "no_rich_text" => false,
Ivan Tashkinov's avatar
Ivan Tashkinov committed
242
      "fields" => [],
243
244
245
246
      "pleroma" => %{
        "confirmation_pending" => false,
        "tags" => []
      }
eal's avatar
eal committed
247
248
249
250
251
    }

    blocker = Repo.get(User, blocker.id)
    assert represented == UserView.render("show.json", %{user: user, for: blocker})
  end
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267

  test "a user with mastodon fields" do
    fields = [
      %{
        "name" => "Pronouns",
        "value" => "she/her"
      },
      %{
        "name" => "Website",
        "value" => "https://example.org/"
      }
    ]

    user =
      insert(:user, %{
        info: %{
lain's avatar
lain committed
268
          source_data: %{
269
270
271
272
273
274
275
276
277
            "attachment" =>
              Enum.map(fields, fn field -> Map.put(field, "type", "PropertyValue") end)
          }
        }
      })

    userview = UserView.render("show.json", %{user: user})
    assert userview["fields"] == fields
  end
lain's avatar
lain committed
278
end