user_view_test.exs 5.47 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
7
  alias Pleroma.Builders.UserBuilder
lain's avatar
lain committed
8

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

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

16
17
18
  test "A user with an avatar object", %{user: user} do
    image = "image"
    user = %{ user | avatar: %{ "url" => [%{"href" => image}] }}
dtluna's avatar
dtluna committed
19
    represented = UserView.render("show.json", %{user: user})
20
21
22
    assert represented["profile_image_url"] == image
  end

lain's avatar
lain committed
23
24
25
  test "A user" do
    note_activity = insert(:note_activity)
    user = User.get_cached_by_ap_id(note_activity.data["actor"])
26
    {:ok, user} = User.update_note_count(user)
lain's avatar
lain committed
27
28
29
30
31
32
    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
33
34
    {:ok, user} = User.update_follower_count(user)
    Cachex.set(:user_cache, "user_info:#{user.id}", User.user_info(Repo.get!(User, user.id)))
lain's avatar
lain committed
35

36
37
    image = "http://localhost:4001/static/avi.png"
    banner = "http://localhost:4001/static/banner.png"
lain's avatar
lain committed
38

lain's avatar
lain committed
39
40
41
42
    represented = %{
      "id" => user.id,
      "name" => user.name,
      "screen_name" => user.nickname,
lain's avatar
lain committed
43
      "description" => HtmlSanitizeEx.strip_tags(user.bio),
dtluna's avatar
dtluna committed
44
      "created_at" => user.inserted_at |> Utils.format_naive_asctime,
lain's avatar
lain committed
45
      "favourites_count" => 0,
lain's avatar
lain committed
46
47
48
      "statuses_count" => 1,
      "friends_count" => 1,
      "followers_count" => 2,
lain's avatar
lain committed
49
50
51
52
53
      "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
54
      "follows_you" => false,
eal's avatar
eal committed
55
      "statusnet_blocking" => false,
lain's avatar
lain committed
56
      "rights" => %{},
lain's avatar
lain committed
57
      "statusnet_profile_url" => user.ap_id,
58
      "cover_photo" => banner,
eal's avatar
eal committed
59
60
      "background_image" => nil,
      "is_local" => true
lain's avatar
lain committed
61
62
    }

dtluna's avatar
dtluna committed
63
    assert represented == UserView.render("show.json", %{user: user})
lain's avatar
lain committed
64
  end
65
66
67

  test "A user for a given other follower", %{user: user} do
    {:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]})
68
    {:ok, user} = User.update_follower_count(user)
69
70
71
    image = "http://localhost:4001/static/avi.png"
    banner = "http://localhost:4001/static/banner.png"

72
73
74
75
    represented = %{
      "id" => user.id,
      "name" => user.name,
      "screen_name" => user.nickname,
lain's avatar
lain committed
76
      "description" => HtmlSanitizeEx.strip_tags(user.bio),
dtluna's avatar
dtluna committed
77
      "created_at" => user.inserted_at |> Utils.format_naive_asctime,
78
79
80
      "favourites_count" => 0,
      "statuses_count" => 0,
      "friends_count" => 0,
lain's avatar
lain committed
81
      "followers_count" => 1,
82
83
84
85
86
      "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
87
      "follows_you" => false,
eal's avatar
eal committed
88
      "statusnet_blocking" => false,
lain's avatar
lain committed
89
      "rights" => %{},
lain's avatar
lain committed
90
      "statusnet_profile_url" => user.ap_id,
91
      "cover_photo" => banner,
eal's avatar
eal committed
92
93
      "background_image" => nil,
      "is_local" => true
94
95
    }

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

eal's avatar
eal committed
99
  test "A user that follows you", %{user: user} do
100
101
    follower = insert(:user)
    {:ok, follower} = User.follow(follower, user)
eal's avatar
eal committed
102
    {:ok, user} = User.update_follower_count(user)
103
104
105
    image = "http://localhost:4001/static/avi.png"
    banner = "http://localhost:4001/static/banner.png"

eal's avatar
eal committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
    represented = %{
      "id" => follower.id,
      "name" => follower.name,
      "screen_name" => follower.nickname,
      "description" => HtmlSanitizeEx.strip_tags(follower.bio),
      "created_at" => follower.inserted_at |> Utils.format_naive_asctime,
      "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,
      "rights" => %{},
      "statusnet_profile_url" => follower.ap_id,
125
      "cover_photo" => banner,
eal's avatar
eal committed
126
127
      "background_image" => nil,
      "is_local" => true
eal's avatar
eal committed
128
129
130
131
132
    }

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

eal's avatar
eal committed
133
134
135
136
  test "A blocked user for the blocker", %{user: user} do
    user = insert(:user)
    blocker = insert(:user)
    User.block(blocker, user)
137
138
139
    image = "http://localhost:4001/static/avi.png"
    banner = "http://localhost:4001/static/banner.png"

eal's avatar
eal committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
    represented = %{
      "id" => user.id,
      "name" => user.name,
      "screen_name" => user.nickname,
      "description" => HtmlSanitizeEx.strip_tags(user.bio),
      "created_at" => user.inserted_at |> Utils.format_naive_asctime,
      "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
155
      "follows_you" => false,
eal's avatar
eal committed
156
157
158
      "statusnet_blocking" => true,
      "rights" => %{},
      "statusnet_profile_url" => user.ap_id,
159
      "cover_photo" => banner,
eal's avatar
eal committed
160
161
      "background_image" => nil,
      "is_local" => true
eal's avatar
eal committed
162
163
164
165
166
    }

    blocker = Repo.get(User, blocker.id)
    assert represented == UserView.render("show.json", %{user: user, for: blocker})
  end
lain's avatar
lain committed
167
end