1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.MastodonAPITest do
6 use Pleroma.Web.ConnCase, async: true
8 alias Pleroma.Notification
9 alias Pleroma.ScheduledActivity
11 alias Pleroma.Web.CommonAPI
12 alias Pleroma.Web.MastodonAPI.MastodonAPI
14 import Pleroma.Factory
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, deactivated: true)
20 assert {:error, _error} = MastodonAPI.follow(follower, user)
23 test "following for user" do
24 follower = insert(:user)
26 {:ok, follower} = MastodonAPI.follow(follower, user)
27 assert User.following?(follower, user)
30 test "returns ok if user already followed" do
31 follower = 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)
39 describe "get_followers/2" do
40 test "returns user followers" do
41 follower1_user = insert(:user)
42 follower2_user = insert(:user)
44 {:ok, _follower1_user, _user} = User.follow(follower1_user, user)
45 {:ok, follower2_user, _user} = User.follow(follower2_user, user)
47 assert MastodonAPI.get_followers(user, %{"limit" => 1}) == [follower2_user]
51 describe "get_friends/2" do
52 test "returns user friends" do
54 followed_one = insert(:user)
55 followed_two = insert(:user)
56 followed_three = insert(:user)
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)
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))
70 describe "get_notifications/2" do
71 test "returns notifications for user" do
73 subscriber = insert(:user)
75 User.subscribe(subscriber, user)
77 {:ok, status} = CommonAPI.post(user, %{status: "Akariiiin"})
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)
84 assert Enum.member?(Enum.map(res, & &1.id), notification.id)
85 assert Enum.member?(Enum.map(res, & &1.id), notification1.id)
89 describe "get_scheduled_activities/2" do
90 test "returns user scheduled activities" do
94 NaiveDateTime.utc_now()
95 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
96 |> NaiveDateTime.to_iso8601()
98 attrs = %{params: %{}, scheduled_at: today}
99 {:ok, schedule} = ScheduledActivity.create(user, attrs)
100 assert MastodonAPI.get_scheduled_activities(user) == [schedule]