Remerge of hashtag following (#341)
[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 end