summaryrefslogtreecommitdiff
path: root/test/web/twitter_api/views/notification_view_test.exs
blob: fcf2b3d9037b9ca0b6fc6c610740c51f2d389ba8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
defmodule Pleroma.Web.TwitterAPI.NotificationViewTest do
  use Pleroma.DataCase

  alias Pleroma.{User, Notification}
  alias Pleroma.Web.TwitterAPI.TwitterAPI
  alias Pleroma.Web.TwitterAPI.NotificationView
  alias Pleroma.Web.TwitterAPI.UserView
  alias Pleroma.Web.TwitterAPI.ActivityView
  alias Pleroma.Web.CommonAPI.Utils
  alias Pleroma.Web.ActivityPub.ActivityPub

  import Pleroma.Factory

  setup do
    user = insert(:user, bio: "<span>Here's some html</span>")
    [user: user]
  end

  test "A follow notification" do
    note_activity = insert(:note_activity)
    user = User.get_cached_by_ap_id(note_activity.data["actor"])
    follower = insert(:user)

    {:ok, follower} = User.follow(follower, user)
    {:ok, activity} = ActivityPub.follow(follower, user)
    Cachex.put(:user_cache, "user_info:#{user.id}", User.user_info(Repo.get!(User, user.id)))
    [follow_notif] = Notification.for_user(user)

    represented = %{
      "created_at" => follow_notif.inserted_at |> Utils.format_naive_asctime(),
      "from_profile" => UserView.render("show.json", %{user: follower, for: user}),
      "id" => follow_notif.id,
      "is_seen" => 0,
      "notice" => ActivityView.render("activity.json", %{activity: activity, for: user}),
      "ntype" => "follow"
    }

    assert represented ==
             NotificationView.render("notification.json", %{notification: follow_notif, for: user})
  end

  test "A mention notification" do
    user = insert(:user)
    other_user = insert(:user)

    {:ok, activity} =
      TwitterAPI.create_status(other_user, %{"status" => "Päivää, @#{user.nickname}"})

    [notification] = Notification.for_user(user)

    represented = %{
      "created_at" => notification.inserted_at |> Utils.format_naive_asctime(),
      "from_profile" => UserView.render("show.json", %{user: other_user, for: user}),
      "id" => notification.id,
      "is_seen" => 0,
      "notice" => ActivityView.render("activity.json", %{activity: activity, for: user}),
      "ntype" => "mention"
    }

    assert represented ==
             NotificationView.render("notification.json", %{notification: notification, for: user})
  end

  test "A retweet notification" do
    note_activity = insert(:note_activity)
    user = User.get_cached_by_ap_id(note_activity.data["actor"])
    repeater = insert(:user)

    {:ok, _activity} = TwitterAPI.repeat(repeater, note_activity.id)
    [notification] = Notification.for_user(user)

    represented = %{
      "created_at" => notification.inserted_at |> Utils.format_naive_asctime(),
      "from_profile" => UserView.render("show.json", %{user: repeater, for: user}),
      "id" => notification.id,
      "is_seen" => 0,
      "notice" =>
        ActivityView.render("activity.json", %{activity: notification.activity, for: user}),
      "ntype" => "repeat"
    }

    assert represented ==
             NotificationView.render("notification.json", %{notification: notification, for: user})
  end

  test "A like notification" do
    note_activity = insert(:note_activity)
    user = User.get_cached_by_ap_id(note_activity.data["actor"])
    liker = insert(:user)

    {:ok, _activity} = TwitterAPI.fav(liker, note_activity.id)
    [notification] = Notification.for_user(user)

    represented = %{
      "created_at" => notification.inserted_at |> Utils.format_naive_asctime(),
      "from_profile" => UserView.render("show.json", %{user: liker, for: user}),
      "id" => notification.id,
      "is_seen" => 0,
      "notice" =>
        ActivityView.render("activity.json", %{activity: notification.activity, for: user}),
      "ntype" => "like"
    }

    assert represented ==
             NotificationView.render("notification.json", %{notification: notification, for: user})
  end
end