fcf2b3d9037b9ca0b6fc6c610740c51f2d389ba8
[akkoma] / test / web / twitter_api / views / notification_view_test.exs
1 defmodule Pleroma.Web.TwitterAPI.NotificationViewTest do
2 use Pleroma.DataCase
3
4 alias Pleroma.{User, Notification}
5 alias Pleroma.Web.TwitterAPI.TwitterAPI
6 alias Pleroma.Web.TwitterAPI.NotificationView
7 alias Pleroma.Web.TwitterAPI.UserView
8 alias Pleroma.Web.TwitterAPI.ActivityView
9 alias Pleroma.Web.CommonAPI.Utils
10 alias Pleroma.Web.ActivityPub.ActivityPub
11
12 import Pleroma.Factory
13
14 setup do
15 user = insert(:user, bio: "<span>Here's some html</span>")
16 [user: user]
17 end
18
19 test "A follow notification" do
20 note_activity = insert(:note_activity)
21 user = User.get_cached_by_ap_id(note_activity.data["actor"])
22 follower = insert(:user)
23
24 {:ok, follower} = User.follow(follower, user)
25 {:ok, activity} = ActivityPub.follow(follower, user)
26 Cachex.put(:user_cache, "user_info:#{user.id}", User.user_info(Repo.get!(User, user.id)))
27 [follow_notif] = Notification.for_user(user)
28
29 represented = %{
30 "created_at" => follow_notif.inserted_at |> Utils.format_naive_asctime(),
31 "from_profile" => UserView.render("show.json", %{user: follower, for: user}),
32 "id" => follow_notif.id,
33 "is_seen" => 0,
34 "notice" => ActivityView.render("activity.json", %{activity: activity, for: user}),
35 "ntype" => "follow"
36 }
37
38 assert represented ==
39 NotificationView.render("notification.json", %{notification: follow_notif, for: user})
40 end
41
42 test "A mention notification" do
43 user = insert(:user)
44 other_user = insert(:user)
45
46 {:ok, activity} =
47 TwitterAPI.create_status(other_user, %{"status" => "Päivää, @#{user.nickname}"})
48
49 [notification] = Notification.for_user(user)
50
51 represented = %{
52 "created_at" => notification.inserted_at |> Utils.format_naive_asctime(),
53 "from_profile" => UserView.render("show.json", %{user: other_user, for: user}),
54 "id" => notification.id,
55 "is_seen" => 0,
56 "notice" => ActivityView.render("activity.json", %{activity: activity, for: user}),
57 "ntype" => "mention"
58 }
59
60 assert represented ==
61 NotificationView.render("notification.json", %{notification: notification, for: user})
62 end
63
64 test "A retweet notification" do
65 note_activity = insert(:note_activity)
66 user = User.get_cached_by_ap_id(note_activity.data["actor"])
67 repeater = insert(:user)
68
69 {:ok, _activity} = TwitterAPI.repeat(repeater, note_activity.id)
70 [notification] = Notification.for_user(user)
71
72 represented = %{
73 "created_at" => notification.inserted_at |> Utils.format_naive_asctime(),
74 "from_profile" => UserView.render("show.json", %{user: repeater, for: user}),
75 "id" => notification.id,
76 "is_seen" => 0,
77 "notice" =>
78 ActivityView.render("activity.json", %{activity: notification.activity, for: user}),
79 "ntype" => "repeat"
80 }
81
82 assert represented ==
83 NotificationView.render("notification.json", %{notification: notification, for: user})
84 end
85
86 test "A like notification" do
87 note_activity = insert(:note_activity)
88 user = User.get_cached_by_ap_id(note_activity.data["actor"])
89 liker = insert(:user)
90
91 {:ok, _activity} = TwitterAPI.fav(liker, note_activity.id)
92 [notification] = Notification.for_user(user)
93
94 represented = %{
95 "created_at" => notification.inserted_at |> Utils.format_naive_asctime(),
96 "from_profile" => UserView.render("show.json", %{user: liker, for: user}),
97 "id" => notification.id,
98 "is_seen" => 0,
99 "notice" =>
100 ActivityView.render("activity.json", %{activity: notification.activity, for: user}),
101 "ntype" => "like"
102 }
103
104 assert represented ==
105 NotificationView.render("notification.json", %{notification: notification, for: user})
106 end
107 end