0e5f78f04d9bb5da617264104ec62c40b931a6d5
[akkoma] / test / web / mastodon_api / mastodon_api_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.Notification
9 alias Pleroma.Repo
10 alias Pleroma.User
11 alias Pleroma.Web.CommonAPI
12
13 import Pleroma.Factory
14 import Tesla.Mock
15
16 setup do
17 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
18 :ok
19 end
20
21 clear_config([:rich_media, :enabled])
22
23 test "unimplemented follow_requests, blocks, domain blocks" do
24 user = insert(:user)
25
26 ["blocks", "domain_blocks", "follow_requests"]
27 |> Enum.each(fn endpoint ->
28 conn =
29 build_conn()
30 |> assign(:user, user)
31 |> get("/api/v1/#{endpoint}")
32
33 assert [] = json_response(conn, 200)
34 end)
35 end
36
37 test "returns the favorites of a user", %{conn: conn} do
38 user = insert(:user)
39 other_user = insert(:user)
40
41 {:ok, _} = CommonAPI.post(other_user, %{"status" => "bla"})
42 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "traps are happy"})
43
44 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
45
46 first_conn =
47 conn
48 |> assign(:user, user)
49 |> get("/api/v1/favourites")
50
51 assert [status] = json_response(first_conn, 200)
52 assert status["id"] == to_string(activity.id)
53
54 assert [{"link", _link_header}] =
55 Enum.filter(first_conn.resp_headers, fn element -> match?({"link", _}, element) end)
56
57 # Honours query params
58 {:ok, second_activity} =
59 CommonAPI.post(other_user, %{
60 "status" =>
61 "Trees Are Never Sad Look At Them Every Once In Awhile They're Quite Beautiful."
62 })
63
64 {:ok, _, _} = CommonAPI.favorite(second_activity.id, user)
65
66 last_like = status["id"]
67
68 second_conn =
69 conn
70 |> assign(:user, user)
71 |> get("/api/v1/favourites?since_id=#{last_like}")
72
73 assert [second_status] = json_response(second_conn, 200)
74 assert second_status["id"] == to_string(second_activity.id)
75
76 third_conn =
77 conn
78 |> assign(:user, user)
79 |> get("/api/v1/favourites?limit=0")
80
81 assert [] = json_response(third_conn, 200)
82 end
83
84 describe "link headers" do
85 test "preserves parameters in link headers", %{conn: conn} do
86 user = insert(:user)
87 other_user = insert(:user)
88
89 {:ok, activity1} =
90 CommonAPI.post(other_user, %{
91 "status" => "hi @#{user.nickname}",
92 "visibility" => "public"
93 })
94
95 {:ok, activity2} =
96 CommonAPI.post(other_user, %{
97 "status" => "hi @#{user.nickname}",
98 "visibility" => "public"
99 })
100
101 notification1 = Repo.get_by(Notification, activity_id: activity1.id)
102 notification2 = Repo.get_by(Notification, activity_id: activity2.id)
103
104 conn =
105 conn
106 |> assign(:user, user)
107 |> get("/api/v1/notifications", %{media_only: true})
108
109 assert [link_header] = get_resp_header(conn, "link")
110 assert link_header =~ ~r/media_only=true/
111 assert link_header =~ ~r/min_id=#{notification2.id}/
112 assert link_header =~ ~r/max_id=#{notification1.id}/
113 end
114 end
115
116 describe "empty_array, stubs for mastodon api" do
117 test "GET /api/v1/accounts/:id/identity_proofs", %{conn: conn} do
118 user = insert(:user)
119
120 res =
121 conn
122 |> assign(:user, user)
123 |> get("/api/v1/accounts/#{user.id}/identity_proofs")
124 |> json_response(200)
125
126 assert res == []
127 end
128
129 test "GET /api/v1/endorsements", %{conn: conn} do
130 user = insert(:user)
131
132 res =
133 conn
134 |> assign(:user, user)
135 |> get("/api/v1/endorsements")
136 |> json_response(200)
137
138 assert res == []
139 end
140
141 test "GET /api/v1/trends", %{conn: conn} do
142 user = insert(:user)
143
144 res =
145 conn
146 |> assign(:user, user)
147 |> get("/api/v1/trends")
148 |> json_response(200)
149
150 assert res == []
151 end
152 end
153 end