account_view_test.exs 6.5 KB
Newer Older
kaniini's avatar
kaniini committed
1
2
3
4
# Pleroma: A lightweight social networking server
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

lain's avatar
lain committed
5
6
7
defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
  use Pleroma.DataCase
  import Pleroma.Factory
Roger Braun's avatar
Roger Braun committed
8
  alias Pleroma.User
9
  alias Pleroma.Web.MastodonAPI.AccountView
lain's avatar
lain committed
10
11

  test "Represent a user account" do
eal's avatar
eal committed
12
13
14
15
16
17
18
19
20
21
    source_data = %{
      "tag" => [
        %{
          "type" => "Emoji",
          "icon" => %{"url" => "/file.png"},
          "name" => ":karjalanpiirakka:"
        }
      ]
    }

lain's avatar
lain committed
22
23
    user =
      insert(:user, %{
24
        info: %{note_count: 5, follower_count: 3, source_data: source_data},
lain's avatar
lain committed
25
        nickname: "shp@shitposter.club",
eal's avatar
eal committed
26
        name: ":karjalanpiirakka: shp",
27
        bio: "<script src=\"invalid-html\"></script><span>valid html</span>",
lain's avatar
lain committed
28
29
        inserted_at: ~N[2017-08-15 15:47:06.597036]
      })
lain's avatar
lain committed
30
31

    expected = %{
lain's avatar
lain committed
32
      id: to_string(user.id),
33
      username: "shp",
lain's avatar
lain committed
34
35
36
      acct: user.nickname,
      display_name: user.name,
      locked: false,
37
      created_at: "2017-08-15T15:47:06.000Z",
lain's avatar
lain committed
38
39
40
      followers_count: 3,
      following_count: 0,
      statuses_count: 5,
41
      note: "<span>valid html</span>",
lain's avatar
lain committed
42
      url: user.ap_id,
43
44
45
46
      avatar: "http://localhost:4001/images/avi.png",
      avatar_static: "http://localhost:4001/images/avi.png",
      header: "http://localhost:4001/images/banner.png",
      header_static: "http://localhost:4001/images/banner.png",
eal's avatar
eal committed
47
48
49
50
51
52
53
54
      emojis: [
        %{
          "static_url" => "/file.png",
          "url" => "/file.png",
          "shortcode" => "karjalanpiirakka",
          "visible_in_picker" => false
        }
      ],
Haelwenn's avatar
Haelwenn committed
55
      fields: [],
56
57
58
      bot: false,
      source: %{
        note: "",
rinpatch's avatar
rinpatch committed
59
60
        sensitive: false,
        pleroma: %{}
Ivan Tashkinov's avatar
Ivan Tashkinov committed
61
      },
62
63
      pleroma: %{
        confirmation_pending: false,
64
65
        tags: [],
        is_admin: false,
66
        is_moderator: false,
67
68
69
        hide_favorites: true,
        hide_followers: false,
        hide_follows: false,
70
        relationship: %{}
71
      }
72
73
74
75
76
    }

    assert expected == AccountView.render("account.json", %{user: user})
  end

77
78
79
80
81
82
83
84
85
86
  test "Represent the user account for the account owner" do
    user = insert(:user)

    notification_settings = %{
      "remote" => true,
      "local" => true,
      "followers" => true,
      "follows" => true
    }

87
88
89
90
91
92
    privacy = user.info.default_scope

    assert %{
             pleroma: %{notification_settings: ^notification_settings},
             source: %{privacy: ^privacy}
           } = AccountView.render("account.json", %{user: user, for: user})
93
94
  end

95
96
97
  test "Represent a Service(bot) account" do
    user =
      insert(:user, %{
98
        info: %{note_count: 5, follower_count: 3, source_data: %{"type" => "Service"}},
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
        nickname: "shp@shitposter.club",
        inserted_at: ~N[2017-08-15 15:47:06.597036]
      })

    expected = %{
      id: to_string(user.id),
      username: "shp",
      acct: user.nickname,
      display_name: user.name,
      locked: false,
      created_at: "2017-08-15T15:47:06.000Z",
      followers_count: 3,
      following_count: 0,
      statuses_count: 5,
      note: user.bio,
      url: user.ap_id,
      avatar: "http://localhost:4001/images/avi.png",
      avatar_static: "http://localhost:4001/images/avi.png",
      header: "http://localhost:4001/images/banner.png",
      header_static: "http://localhost:4001/images/banner.png",
      emojis: [],
      fields: [],
      bot: true,
122
123
      source: %{
        note: "",
rinpatch's avatar
rinpatch committed
124
125
        sensitive: false,
        pleroma: %{}
Ivan Tashkinov's avatar
Ivan Tashkinov committed
126
      },
127
128
      pleroma: %{
        confirmation_pending: false,
129
130
        tags: [],
        is_admin: false,
131
        is_moderator: false,
132
133
134
        hide_favorites: true,
        hide_followers: false,
        hide_follows: false,
135
        relationship: %{}
136
      }
lain's avatar
lain committed
137
138
139
140
141
142
143
144
145
    }

    assert expected == AccountView.render("account.json", %{user: user})
  end

  test "Represent a smaller mention" do
    user = insert(:user)

    expected = %{
lain's avatar
lain committed
146
      id: to_string(user.id),
lain's avatar
lain committed
147
148
149
150
151
152
153
      acct: user.nickname,
      username: user.nickname,
      url: user.ap_id
    }

    assert expected == AccountView.render("mention.json", %{user: user})
  end
Roger Braun's avatar
Roger Braun committed
154
155
156
157
158
159

  test "represent a relationship" do
    user = insert(:user)
    other_user = insert(:user)

    {:ok, user} = User.follow(user, other_user)
lain's avatar
lain committed
160
    {:ok, user} = User.block(user, other_user)
Roger Braun's avatar
Roger Braun committed
161
162

    expected = %{
lain's avatar
lain committed
163
      id: to_string(other_user.id),
164
      following: false,
Roger Braun's avatar
Roger Braun committed
165
      followed_by: false,
lain's avatar
lain committed
166
      blocking: true,
Roger Braun's avatar
Roger Braun committed
167
      muting: false,
168
      muting_notifications: false,
169
      subscribing: false,
Roger Braun's avatar
Roger Braun committed
170
      requested: false,
171
      domain_blocking: false,
172
      showing_reblogs: true,
173
      endorsed: false
Roger Braun's avatar
Roger Braun committed
174
175
176
177
    }

    assert expected == AccountView.render("relationship.json", %{user: user, target: other_user})
  end
178
179
180
181

  test "represent an embedded relationship" do
    user =
      insert(:user, %{
minibikini's avatar
minibikini committed
182
        info: %{note_count: 5, follower_count: 0, source_data: %{"type" => "Service"}},
183
184
185
186
187
188
189
        nickname: "shp@shitposter.club",
        inserted_at: ~N[2017-08-15 15:47:06.597036]
      })

    other_user = insert(:user)
    {:ok, other_user} = User.follow(other_user, user)
    {:ok, other_user} = User.block(other_user, user)
minibikini's avatar
minibikini committed
190
    {:ok, _} = User.follow(insert(:user), user)
191
192
193
194
195
196
197
198

    expected = %{
      id: to_string(user.id),
      username: "shp",
      acct: user.nickname,
      display_name: user.name,
      locked: false,
      created_at: "2017-08-15T15:47:06.000Z",
minibikini's avatar
minibikini committed
199
      followers_count: 1,
200
201
202
203
204
205
206
207
208
209
210
211
212
      following_count: 0,
      statuses_count: 5,
      note: user.bio,
      url: user.ap_id,
      avatar: "http://localhost:4001/images/avi.png",
      avatar_static: "http://localhost:4001/images/avi.png",
      header: "http://localhost:4001/images/banner.png",
      header_static: "http://localhost:4001/images/banner.png",
      emojis: [],
      fields: [],
      bot: true,
      source: %{
        note: "",
rinpatch's avatar
rinpatch committed
213
214
        sensitive: false,
        pleroma: %{}
215
216
217
218
219
220
      },
      pleroma: %{
        confirmation_pending: false,
        tags: [],
        is_admin: false,
        is_moderator: false,
221
222
223
        hide_favorites: true,
        hide_followers: false,
        hide_follows: false,
224
225
226
227
228
        relationship: %{
          id: to_string(user.id),
          following: false,
          followed_by: false,
          blocking: true,
229
          subscribing: false,
230
231
232
233
          muting: false,
          muting_notifications: false,
          requested: false,
          domain_blocking: false,
234
          showing_reblogs: true,
235
236
237
238
239
240
241
          endorsed: false
        }
      }
    }

    assert expected == AccountView.render("account.json", %{user: user, for: other_user})
  end
lain's avatar
lain committed
242
end