total rebase
[anni] / test / pleroma / announcement_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.AnnouncementTest do
6   alias Pleroma.Announcement
7
8   use Pleroma.DataCase, async: true
9
10   import Pleroma.Factory
11
12   describe "list_all_visible_when/1" do
13     setup do: {:ok, time: NaiveDateTime.utc_now()}
14
15     test "with no start or end time", %{time: time} do
16       _announcement = insert(:announcement)
17
18       assert [_] = Announcement.list_all_visible_when(time)
19     end
20
21     test "with start time before current", %{time: time} do
22       before_now = NaiveDateTime.add(time, -10, :second)
23
24       _announcement = insert(:announcement, %{starts_at: before_now})
25
26       assert [_] = Announcement.list_all_visible_when(time)
27     end
28
29     test "with start time after current", %{time: time} do
30       after_now = NaiveDateTime.add(time, 10, :second)
31
32       _announcement = insert(:announcement, %{starts_at: after_now})
33
34       assert [] = Announcement.list_all_visible_when(time)
35     end
36
37     test "with end time after current", %{time: time} do
38       after_now = NaiveDateTime.add(time, 10, :second)
39
40       _announcement = insert(:announcement, %{ends_at: after_now})
41
42       assert [_] = Announcement.list_all_visible_when(time)
43     end
44
45     test "with end time before current", %{time: time} do
46       before_now = NaiveDateTime.add(time, -10, :second)
47
48       _announcement = insert(:announcement, %{ends_at: before_now})
49
50       assert [] = Announcement.list_all_visible_when(time)
51     end
52
53     test "with both start and end time", %{time: time} do
54       before_now = NaiveDateTime.add(time, -10, :second)
55       after_now = NaiveDateTime.add(time, 10, :second)
56
57       _announcement = insert(:announcement, %{starts_at: before_now, ends_at: after_now})
58
59       assert [_] = Announcement.list_all_visible_when(time)
60     end
61
62     test "with both start and end time, current not in the range", %{time: time} do
63       before_now = NaiveDateTime.add(time, -10, :second)
64       after_now = NaiveDateTime.add(time, 10, :second)
65
66       _announcement = insert(:announcement, %{starts_at: after_now, ends_at: before_now})
67
68       assert [] = Announcement.list_all_visible_when(time)
69     end
70   end
71
72   describe "announcements formatting" do
73     test "it formats links" do
74       raw = "something on https://pleroma.social ."
75       announcement = insert(:announcement, %{data: %{"content" => raw}})
76
77       assert announcement.rendered["content"] =~ ~r(<a.+?https://pleroma.social)
78       assert announcement.data["content"] == raw
79     end
80
81     test "it formats mentions" do
82       user = insert(:user)
83       raw = "something on @#{user.nickname} ."
84       announcement = insert(:announcement, %{data: %{"content" => raw}})
85
86       assert announcement.rendered["content"] =~ ~r(<a.+?#{user.nickname})
87       assert announcement.data["content"] == raw
88     end
89
90     test "it formats tags" do
91       raw = "something on #mew ."
92       announcement = insert(:announcement, %{data: %{"content" => raw}})
93
94       assert announcement.rendered["content"] =~ ~r(<a.+?#mew)
95       assert announcement.data["content"] == raw
96     end
97   end
98 end