9af49da123b050b75b1e06c9905e041b6fed6d9d
[akkoma] / test / web / mastodon_api / mastodon_api_controller_test.exs
1 defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
2 use Pleroma.Web.ConnCase
3
4 alias Pleroma.Web.TwitterAPI.TwitterAPI
5 alias Pleroma.{Repo, User, Activity}
6 alias Pleroma.Web.OStatus
7
8 import Pleroma.Factory
9
10 test "the home timeline", %{conn: conn} do
11 user = insert(:user)
12 following = insert(:user)
13
14 {:ok, _activity} = TwitterAPI.create_status(following, %{"status" => "test"})
15
16 conn = conn
17 |> assign(:user, user)
18 |> get("/api/v1/timelines/home")
19
20 assert length(json_response(conn, 200)) == 0
21
22 {:ok, user} = User.follow(user, following)
23
24 conn = build_conn()
25 |> assign(:user, user)
26 |> get("/api/v1/timelines/home")
27
28 assert [%{"content" => "test"}] = json_response(conn, 200)
29 end
30
31 test "the public timeline", %{conn: conn} do
32 following = insert(:user)
33
34 {:ok, _activity} = TwitterAPI.create_status(following, %{"status" => "test"})
35 {:ok, [_activity]} = OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873")
36
37 conn = conn
38 |> get("/api/v1/timelines/public")
39
40 assert length(json_response(conn, 200)) == 2
41
42 conn = build_conn()
43 |> get("/api/v1/timelines/public", %{"local" => "True"})
44
45 assert [%{"content" => "test"}] = json_response(conn, 200)
46 end
47
48 test "posting a status", %{conn: conn} do
49 user = insert(:user)
50
51 conn = conn
52 |> assign(:user, user)
53 |> post("/api/v1/statuses", %{"status" => "cofe"})
54
55 assert %{"content" => "cofe", "id" => id} = json_response(conn, 200)
56 assert Repo.get(Activity, id)
57 end
58
59 test "replying to a status", %{conn: conn} do
60 user = insert(:user)
61
62 {:ok, replied_to} = TwitterAPI.create_status(user, %{"status" => "cofe"})
63
64 conn = conn
65 |> assign(:user, user)
66 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
67
68 assert %{"content" => "xD", "id" => id} = json_response(conn, 200)
69
70 activity = Repo.get(Activity, id)
71
72 assert activity.data["context"] == replied_to.data["context"]
73 assert activity.data["object"]["inReplyToStatusId"] == replied_to.id
74 end
75
76 test "verify_credentials", %{conn: conn} do
77 user = insert(:user)
78
79 conn = conn
80 |> assign(:user, user)
81 |> get("/api/v1/accounts/verify_credentials")
82
83 assert %{"id" => id} = json_response(conn, 200)
84 assert id == user.id
85 end
86
87 test "get a status", %{conn: conn} do
88 activity = insert(:note_activity)
89
90 conn = conn
91 |> get("/api/v1/statuses/#{activity.id}")
92
93 assert %{"id" => id} = json_response(conn, 200)
94 assert id == activity.id
95 end
96
97 describe "deleting a status" do
98 test "when you created it", %{conn: conn} do
99 activity = insert(:note_activity)
100 author = User.get_by_ap_id(activity.data["actor"])
101
102 conn = conn
103 |> assign(:user, author)
104 |> delete("/api/v1/statuses/#{activity.id}")
105
106 assert %{} = json_response(conn, 200)
107
108 assert Repo.get(Activity, activity.id) == nil
109 end
110
111 test "when you didn't create it", %{conn: conn} do
112 activity = insert(:note_activity)
113 user = insert(:user)
114
115 conn = conn
116 |> assign(:user, user)
117 |> delete("/api/v1/statuses/#{activity.id}")
118
119 assert %{"error" => _} = json_response(conn, 403)
120
121 assert Repo.get(Activity, activity.id) == activity
122 end
123 end
124
125 describe "reblogging" do
126 test "reblogs and returns the reblogged status", %{conn: conn} do
127 activity = insert(:note_activity)
128 user = insert(:user)
129
130 conn = conn
131 |> assign(:user, user)
132 |> post("/api/v1/statuses/#{activity.id}/reblog")
133
134 assert %{"id" => id, "reblogged" => true, "reblogs_count" => 1} = json_response(conn, 200)
135 assert activity.id == id
136 end
137 end
138
139 describe "favoriting" do
140 test "favs a status and returns it", %{conn: conn} do
141 activity = insert(:note_activity)
142 user = insert(:user)
143
144 conn = conn
145 |> assign(:user, user)
146 |> post("/api/v1/statuses/#{activity.id}/favourite")
147
148 assert %{"id" => id, "favourites_count" => 1, "favourited" => true} = json_response(conn, 200)
149 assert activity.id == id
150 end
151 end
152 end