1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
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 {:error, error} = MastodonAPI.follow(follower, user)
21 assert error == :rejected
24 test "following for user" do
25 follower = insert(:user)
27 {:ok, follower} = MastodonAPI.follow(follower, user)
28 assert User.following?(follower, user)
31 test "returns ok if user already followed" do
32 follower = insert(:user)
34 {:ok, follower} = User.follow(follower, user)
35 {:ok, follower} = MastodonAPI.follow(follower, refresh_record(user))
36 assert User.following?(follower, user)
40 describe "get_followers/2" do
41 test "returns user followers" do
42 follower1_user = insert(:user)
43 follower2_user = insert(:user)
45 {:ok, _follower1_user} = User.follow(follower1_user, user)
46 {:ok, follower2_user} = User.follow(follower2_user, user)
48 assert MastodonAPI.get_followers(user, %{"limit" => 1}) == [follower2_user]
52 describe "get_friends/2" do
53 test "returns user friends" do
55 followed_one = insert(:user)
56 followed_two = insert(:user)
57 followed_three = insert(:user)
59 {:ok, user} = User.follow(user, followed_one)
60 {:ok, user} = User.follow(user, followed_two)
61 {:ok, user} = User.follow(user, followed_three)
62 res = MastodonAPI.get_friends(user)
64 assert length(res) == 3
65 assert Enum.member?(res, refresh_record(followed_three))
66 assert Enum.member?(res, refresh_record(followed_two))
67 assert Enum.member?(res, refresh_record(followed_one))
71 describe "get_notifications/2" do
72 test "returns notifications for user" do
74 subscriber = insert(:user)
76 User.subscribe(subscriber, user)
78 {:ok, status} = CommonAPI.post(user, %{status: "Akariiiin"})
80 {:ok, status1} = CommonAPI.post(user, %{status: "Magi"})
81 {:ok, [notification]} = Notification.create_notifications(status)
82 {:ok, [notification1]} = Notification.create_notifications(status1)
83 res = MastodonAPI.get_notifications(subscriber)
85 assert Enum.member?(Enum.map(res, & &1.id), notification.id)
86 assert Enum.member?(Enum.map(res, & &1.id), notification1.id)
90 describe "get_scheduled_activities/2" do
91 test "returns user scheduled activities" do
95 NaiveDateTime.utc_now()
96 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
97 |> NaiveDateTime.to_iso8601()
99 attrs = %{params: %{}, scheduled_at: today}
100 {:ok, schedule} = ScheduledActivity.create(user, attrs)
101 assert MastodonAPI.get_scheduled_activities(user) == [schedule]