1 defmodule Pleroma.Web.MastodonAPI.TagControllerTest do
2 use Pleroma.Web.ConnCase
10 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
14 describe "GET /api/v1/tags/:id" do
15 test "returns 200 with tag" do
16 %{user: user, conn: conn} = oauth_access(["read"])
18 tag = insert(:hashtag, name: "jubjub")
19 {:ok, _user} = User.follow_hashtag(user, tag)
23 |> get("/api/v1/tags/jubjub")
24 |> json_response_and_validate_schema(200)
28 "url" => "http://localhost:4001/tags/jubjub",
34 test "returns 404 with unknown tag" do
35 %{conn: conn} = oauth_access(["read"])
38 |> get("/api/v1/tags/jubjub")
39 |> json_response_and_validate_schema(404)
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")
50 |> post("/api/v1/tags/jubjub/follow")
51 |> json_response_and_validate_schema(200)
53 assert response["following"] == true
54 user = User.get_cached_by_ap_id(user.ap_id)
55 assert User.following_hashtag?(user, hashtag)
58 test "should 404 if hashtag doesn't exist" do
59 %{conn: conn} = oauth_access(["write:follows"])
63 |> post("/api/v1/tags/rubrub/follow")
64 |> json_response_and_validate_schema(404)
66 assert response["error"] == "Hashtag not found"
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)
78 |> post("/api/v1/tags/jubjub/unfollow")
79 |> json_response_and_validate_schema(200)
81 assert response["following"] == false
82 user = User.get_cached_by_ap_id(user.ap_id)
83 refute User.following_hashtag?(user, hashtag)
86 test "should 404 if hashtag doesn't exist" do
87 %{conn: conn} = oauth_access(["write:follows"])
91 |> post("/api/v1/tags/rubrub/unfollow")
92 |> json_response_and_validate_schema(404)
94 assert response["error"] == "Hashtag not found"
98 describe "GET /api/v1/followed_tags" do
99 test "should list followed tags" do
100 %{user: user, conn: conn} = oauth_access(["read:follows"])
104 |> get("/api/v1/followed_tags")
105 |> json_response_and_validate_schema(200)
107 assert Enum.empty?(response)
109 hashtag = insert(:hashtag, name: "jubjub")
110 {:ok, _user} = User.follow_hashtag(user, hashtag)
114 |> get("/api/v1/followed_tags")
115 |> json_response_and_validate_schema(200)
117 assert [%{"name" => "jubjub"}] = response
120 test "should include a link header to paginate" do
121 %{user: user, conn: conn} = oauth_access(["read:follows"])
124 hashtag = insert(:hashtag, name: "jubjub#{i}}")
125 {:ok, _user} = User.follow_hashtag(user, hashtag)
130 |> get("/api/v1/followed_tags")
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)
141 |> json_response_and_validate_schema(200)
143 assert Enum.count(response) == 1
146 test "should refuse access without read:follows scope" do
147 %{conn: conn} = oauth_access(["write"])
150 |> get("/api/v1/followed_tags")
151 |> json_response_and_validate_schema(403)
155 defp extract_next_link_header(header) do
156 [_, next_link] = Regex.run(~r{<(?<next_link>.*)>; rel="next"}, header)