3f84e4116959e76c54b9b3f373783917561b2a58
[akkoma] / test / web / twitter_api / views / user_view_test.exs
1 defmodule Pleroma.Web.TwitterAPI.UserViewTest do
2 use Pleroma.DataCase
3
4 alias Pleroma.User
5 alias Pleroma.Web.TwitterAPI.UserView
6 alias Pleroma.Web.CommonAPI.Utils
7 alias Pleroma.Builders.UserBuilder
8
9 import Pleroma.Factory
10
11 setup do
12 user = insert(:user, bio: "<span>Here's some html</span>")
13 [user: user]
14 end
15
16 test "A user with an avatar object", %{user: user} do
17 image = "image"
18 user = %{ user | avatar: %{ "url" => [%{"href" => image}] }}
19 represented = UserView.render("show.json", %{user: user})
20 assert represented["profile_image_url"] == image
21 end
22
23 test "A user" do
24 note_activity = insert(:note_activity)
25 user = User.get_cached_by_ap_id(note_activity.data["actor"])
26 {:ok, user} = User.update_note_count(user)
27 follower = insert(:user)
28 second_follower = insert(:user)
29
30 User.follow(follower, user)
31 User.follow(second_follower, user)
32 User.follow(user, follower)
33 {:ok, user} = User.update_follower_count(user)
34 Cachex.set(:user_cache, "user_info:#{user.id}", User.user_info(Repo.get!(User, user.id)))
35
36 image = "http://localhost:4001/static/avi.png"
37 banner = "http://localhost:4001/static/banner.png"
38
39 represented = %{
40 "id" => user.id,
41 "name" => user.name,
42 "screen_name" => user.nickname,
43 "description" => HtmlSanitizeEx.strip_tags(user.bio),
44 "created_at" => user.inserted_at |> Utils.format_naive_asctime,
45 "favourites_count" => 0,
46 "statuses_count" => 1,
47 "friends_count" => 1,
48 "followers_count" => 2,
49 "profile_image_url" => image,
50 "profile_image_url_https" => image,
51 "profile_image_url_profile_size" => image,
52 "profile_image_url_original" => image,
53 "following" => false,
54 "follows_you" => false,
55 "statusnet_blocking" => false,
56 "rights" => %{},
57 "statusnet_profile_url" => user.ap_id,
58 "cover_photo" => banner,
59 "background_image" => nil
60 }
61
62 assert represented == UserView.render("show.json", %{user: user})
63 end
64
65 test "A user for a given other follower", %{user: user} do
66 {:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]})
67 {:ok, user} = User.update_follower_count(user)
68 image = "http://localhost:4001/static/avi.png"
69 banner = "http://localhost:4001/static/banner.png"
70
71 represented = %{
72 "id" => user.id,
73 "name" => user.name,
74 "screen_name" => user.nickname,
75 "description" => HtmlSanitizeEx.strip_tags(user.bio),
76 "created_at" => user.inserted_at |> Utils.format_naive_asctime,
77 "favourites_count" => 0,
78 "statuses_count" => 0,
79 "friends_count" => 0,
80 "followers_count" => 1,
81 "profile_image_url" => image,
82 "profile_image_url_https" => image,
83 "profile_image_url_profile_size" => image,
84 "profile_image_url_original" => image,
85 "following" => true,
86 "follows_you" => false,
87 "statusnet_blocking" => false,
88 "rights" => %{},
89 "statusnet_profile_url" => user.ap_id,
90 "cover_photo" => banner,
91 "background_image" => nil
92 }
93
94 assert represented == UserView.render("show.json", %{user: user, for: follower})
95 end
96
97 test "A user that follows you", %{user: user} do
98 follower = insert(:user)
99 {:ok, follower} = User.follow(follower, user)
100 {:ok, user} = User.update_follower_count(user)
101 image = "http://localhost:4001/static/avi.png"
102 banner = "http://localhost:4001/static/banner.png"
103
104 represented = %{
105 "id" => follower.id,
106 "name" => follower.name,
107 "screen_name" => follower.nickname,
108 "description" => HtmlSanitizeEx.strip_tags(follower.bio),
109 "created_at" => follower.inserted_at |> Utils.format_naive_asctime,
110 "favourites_count" => 0,
111 "statuses_count" => 0,
112 "friends_count" => 1,
113 "followers_count" => 0,
114 "profile_image_url" => image,
115 "profile_image_url_https" => image,
116 "profile_image_url_profile_size" => image,
117 "profile_image_url_original" => image,
118 "following" => false,
119 "follows_you" => true,
120 "statusnet_blocking" => false,
121 "rights" => %{},
122 "statusnet_profile_url" => follower.ap_id,
123 "cover_photo" => banner,
124 "background_image" => nil
125 }
126
127 assert represented == UserView.render("show.json", %{user: follower, for: user})
128 end
129
130 test "A blocked user for the blocker", %{user: user} do
131 user = insert(:user)
132 blocker = insert(:user)
133 User.block(blocker, user)
134 image = "http://localhost:4001/static/avi.png"
135 banner = "http://localhost:4001/static/banner.png"
136
137 represented = %{
138 "id" => user.id,
139 "name" => user.name,
140 "screen_name" => user.nickname,
141 "description" => HtmlSanitizeEx.strip_tags(user.bio),
142 "created_at" => user.inserted_at |> Utils.format_naive_asctime,
143 "favourites_count" => 0,
144 "statuses_count" => 0,
145 "friends_count" => 0,
146 "followers_count" => 0,
147 "profile_image_url" => image,
148 "profile_image_url_https" => image,
149 "profile_image_url_profile_size" => image,
150 "profile_image_url_original" => image,
151 "following" => false,
152 "follows_you" => false,
153 "statusnet_blocking" => true,
154 "rights" => %{},
155 "statusnet_profile_url" => user.ap_id,
156 "cover_photo" => banner,
157 "background_image" => nil
158 }
159
160 blocker = Repo.get(User, blocker.id)
161 assert represented == UserView.render("show.json", %{user: user, for: blocker})
162 end
163 end