Post editing (#202)
[akkoma] / test / pleroma / web / mastodon_api / mastodon_api_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.MastodonAPITest do
6 use Pleroma.Web.ConnCase, async: true
7
8 alias Pleroma.Notification
9 alias Pleroma.ScheduledActivity
10 alias Pleroma.User
11 alias Pleroma.Web.CommonAPI
12 alias Pleroma.Web.MastodonAPI.MastodonAPI
13
14 import Pleroma.Factory
15
16 describe "follow/3" do
17 test "returns error when followed user is deactivated" do
18 follower = insert(:user)
19 user = insert(:user, local: true, is_active: false)
20 assert {:error, _error} = MastodonAPI.follow(follower, user)
21 end
22
23 test "following for user" do
24 follower = insert(:user)
25 user = insert(:user)
26 {:ok, follower} = MastodonAPI.follow(follower, user)
27 assert User.following?(follower, user)
28 end
29
30 test "returns ok if user already followed" do
31 follower = insert(:user)
32 user = insert(:user)
33 {:ok, follower, user} = User.follow(follower, user)
34 {:ok, follower} = MastodonAPI.follow(follower, refresh_record(user))
35 assert User.following?(follower, user)
36 end
37 end
38
39 describe "get_followers/2" do
40 test "returns user followers" do
41 follower1_user = insert(:user)
42 follower2_user = insert(:user)
43 user = insert(:user)
44 {:ok, _follower1_user, _user} = User.follow(follower1_user, user)
45 {:ok, follower2_user, _user} = User.follow(follower2_user, user)
46
47 assert MastodonAPI.get_followers(user, %{"limit" => 1}) == [follower2_user]
48 end
49 end
50
51 describe "get_friends/2" do
52 test "returns user friends" do
53 user = insert(:user)
54 followed_one = insert(:user)
55 followed_two = insert(:user)
56 followed_three = insert(:user)
57
58 {:ok, user, followed_one} = User.follow(user, followed_one)
59 {:ok, user, followed_two} = User.follow(user, followed_two)
60 {:ok, user, followed_three} = User.follow(user, followed_three)
61 res = MastodonAPI.get_friends(user)
62
63 assert length(res) == 3
64 assert Enum.member?(res, refresh_record(followed_three))
65 assert Enum.member?(res, refresh_record(followed_two))
66 assert Enum.member?(res, refresh_record(followed_one))
67 end
68 end
69
70 describe "get_notifications/2" do
71 test "returns notifications for user" do
72 user = insert(:user)
73 subscriber = insert(:user)
74
75 User.subscribe(subscriber, user)
76
77 {:ok, status} = CommonAPI.post(user, %{status: "Akariiiin"})
78
79 {:ok, status1} = CommonAPI.post(user, %{status: "Magi"})
80 {:ok, [notification]} = Notification.create_notifications(status)
81 {:ok, [notification1]} = Notification.create_notifications(status1)
82 res = MastodonAPI.get_notifications(subscriber)
83
84 assert Enum.member?(Enum.map(res, & &1.id), notification.id)
85 assert Enum.member?(Enum.map(res, & &1.id), notification1.id)
86 end
87 end
88
89 describe "get_scheduled_activities/2" do
90 test "returns user scheduled activities" do
91 user = insert(:user)
92
93 today =
94 NaiveDateTime.utc_now()
95 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
96 |> NaiveDateTime.to_iso8601()
97
98 attrs = %{params: %{}, scheduled_at: today}
99 {:ok, schedule} = ScheduledActivity.create(user, attrs)
100 assert MastodonAPI.get_scheduled_activities(user) == [schedule]
101 end
102 end
103 end