Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into develop
[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 alias Pleroma.Builders.UserBuilder
12
13 import Pleroma.Factory
14
15 setup do
16 user = insert(:user, bio: "<span>Here's some html</span>")
17 [user: user]
18 end
19
20 test "A follow notification" do
21 note_activity = insert(:note_activity)
22 user = User.get_cached_by_ap_id(note_activity.data["actor"])
23 follower = insert(:user)
24
25 {:ok, follower} = User.follow(follower, user)
26 {:ok, activity} = ActivityPub.follow(follower, user)
27 Cachex.set(:user_cache, "user_info:#{user.id}", User.user_info(Repo.get!(User, user.id)))
28 [follow_notif] = Notification.for_user(user)
29
30 represented = %{
31 "created_at" => follow_notif.inserted_at |> Utils.format_naive_asctime(),
32 "from_profile" => UserView.render("show.json", %{user: follower, for: user}),
33 "id" => follow_notif.id,
34 "is_seen" => 0,
35 "notice" => ActivityView.render("activity.json", %{activity: activity, for: user}),
36 "ntype" => "follow"
37 }
38
39 assert represented == 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} = TwitterAPI.create_status(other_user, %{"status" => "Päivää, @#{user.nickname}"})
47 [notification] = Notification.for_user(user)
48
49 represented = %{
50 "created_at" => notification.inserted_at |> Utils.format_naive_asctime(),
51 "from_profile" => UserView.render("show.json", %{user: other_user, for: user}),
52 "id" => notification.id,
53 "is_seen" => 0,
54 "notice" => ActivityView.render("activity.json", %{activity: activity, for: user}),
55 "ntype" => "mention"
56 }
57
58 assert represented == NotificationView.render("notification.json", %{notification: notification, for: user})
59 end
60
61 test "A retweet notification" do
62 note_activity = insert(:note_activity)
63 user = User.get_cached_by_ap_id(note_activity.data["actor"])
64 repeater = insert(:user)
65
66 {:ok, activity} = TwitterAPI.repeat(repeater, note_activity.id)
67 [notification] = Notification.for_user(user)
68
69 represented = %{
70 "created_at" => notification.inserted_at |> Utils.format_naive_asctime(),
71 "from_profile" => UserView.render("show.json", %{user: repeater, for: user}),
72 "id" => notification.id,
73 "is_seen" => 0,
74 "notice" => ActivityView.render("activity.json", %{activity: notification.activity, for: user}),
75 "ntype" => "repeat"
76 }
77
78 assert represented == NotificationView.render("notification.json", %{notification: notification, for: user})
79 end
80
81 test "A like notification" do
82 note_activity = insert(:note_activity)
83 user = User.get_cached_by_ap_id(note_activity.data["actor"])
84 liker = insert(:user)
85
86 {:ok, activity} = TwitterAPI.fav(liker, note_activity.id)
87 [notification] = Notification.for_user(user)
88
89 represented = %{
90 "created_at" => notification.inserted_at |> Utils.format_naive_asctime(),
91 "from_profile" => UserView.render("show.json", %{user: liker, for: user}),
92 "id" => notification.id,
93 "is_seen" => 0,
94 "notice" => ActivityView.render("activity.json", %{activity: notification.activity, for: user}),
95 "ntype" => "like"
96 }
97
98 assert represented == NotificationView.render("notification.json", %{notification: notification, for: user})
99 end
100 end