Add /api/v1/followed_tags
[akkoma] / test / pleroma / web / mastodon_api / controllers / tag_controller_test.exs
1 defmodule Pleroma.Web.MastodonAPI.TagControllerTest do
2 use Pleroma.Web.ConnCase
3
4 import Pleroma.Factory
5 import Tesla.Mock
6
7 alias Pleroma.User
8
9 setup do
10 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
11 :ok
12 end
13
14 describe "GET /api/v1/tags/:id" do
15 test "returns 200 with tag" do
16 %{user: user, conn: conn} = oauth_access(["read"])
17
18 tag = insert(:hashtag, name: "jubjub")
19 {:ok, _user} = User.follow_hashtag(user, tag)
20
21 response =
22 conn
23 |> get("/api/v1/tags/jubjub")
24 |> json_response_and_validate_schema(200)
25
26 assert %{
27 "name" => "jubjub",
28 "url" => "http://localhost:4001/tags/jubjub",
29 "history" => [],
30 "following" => true
31 } = response
32 end
33
34 test "returns 404 with unknown tag" do
35 %{conn: conn} = oauth_access(["read"])
36
37 conn
38 |> get("/api/v1/tags/jubjub")
39 |> json_response_and_validate_schema(404)
40 end
41 end
42
43 describe "POST /api/v1/tags/:id/follow" do
44 test "should follow a hashtag" do
45 %{user: user, conn: conn} = oauth_access(["write:follows"])
46 hashtag = insert(:hashtag, name: "jubjub")
47
48 response =
49 conn
50 |> post("/api/v1/tags/jubjub/follow")
51 |> json_response_and_validate_schema(200)
52
53 assert response["following"] == true
54 user = User.get_cached_by_ap_id(user.ap_id)
55 assert User.following_hashtag?(user, hashtag)
56 end
57
58 test "should 404 if hashtag doesn't exist" do
59 %{conn: conn} = oauth_access(["write:follows"])
60
61 response =
62 conn
63 |> post("/api/v1/tags/rubrub/follow")
64 |> json_response_and_validate_schema(404)
65
66 assert response["error"] == "Hashtag not found"
67 end
68 end
69
70 describe "POST /api/v1/tags/:id/unfollow" do
71 test "should unfollow a hashtag" do
72 %{user: user, conn: conn} = oauth_access(["write:follows"])
73 hashtag = insert(:hashtag, name: "jubjub")
74 {:ok, user} = User.follow_hashtag(user, hashtag)
75
76 response =
77 conn
78 |> post("/api/v1/tags/jubjub/unfollow")
79 |> json_response_and_validate_schema(200)
80
81 assert response["following"] == false
82 user = User.get_cached_by_ap_id(user.ap_id)
83 refute User.following_hashtag?(user, hashtag)
84 end
85
86 test "should 404 if hashtag doesn't exist" do
87 %{conn: conn} = oauth_access(["write:follows"])
88
89 response =
90 conn
91 |> post("/api/v1/tags/rubrub/unfollow")
92 |> json_response_and_validate_schema(404)
93
94 assert response["error"] == "Hashtag not found"
95 end
96 end
97
98 describe "GET /api/v1/followed_tags" do
99 test "should list followed tags" do
100 %{user: user, conn: conn} = oauth_access(["read:follows"])
101
102 response =
103 conn
104 |> get("/api/v1/followed_tags")
105 |> json_response_and_validate_schema(200)
106
107 assert Enum.empty?(response)
108
109 hashtag = insert(:hashtag, name: "jubjub")
110 {:ok, _user} = User.follow_hashtag(user, hashtag)
111
112 response =
113 conn
114 |> get("/api/v1/followed_tags")
115 |> json_response_and_validate_schema(200)
116
117 assert [%{"name" => "jubjub"}] = response
118 end
119
120 test "should include a link header to paginate" do
121 %{user: user, conn: conn} = oauth_access(["read:follows"])
122
123 for i <- 1..21 do
124 hashtag = insert(:hashtag, name: "jubjub#{i}}")
125 {:ok, _user} = User.follow_hashtag(user, hashtag)
126 end
127
128 response =
129 conn
130 |> get("/api/v1/followed_tags")
131
132 json = json_response_and_validate_schema(response, 200)
133 assert Enum.count(json) == 20
134 assert [link_header] = get_resp_header(response, "link")
135 assert link_header =~ "rel=\"next\""
136 next_link = extract_next_link_header(link_header)
137
138 response =
139 conn
140 |> get(next_link)
141 |> json_response_and_validate_schema(200)
142
143 assert Enum.count(response) == 1
144 end
145
146 test "should refuse access without read:follows scope" do
147 %{conn: conn} = oauth_access(["write"])
148
149 conn
150 |> get("/api/v1/followed_tags")
151 |> json_response_and_validate_schema(403)
152 end
153 end
154
155 defp extract_next_link_header(header) do
156 [_, next_link] = Regex.run(~r{<(?<next_link>.*)>; rel="next"}, header)
157 next_link
158 end
159 end