Merge branch 'feature/order-users-by-nickname-admin-api' into 'develop'
[akkoma] / test / web / twitter_api / util_controller_test.exs
1 defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
2 use Pleroma.Web.ConnCase
3
4 alias Pleroma.Notification
5 alias Pleroma.Repo
6 alias Pleroma.Web.CommonAPI
7 import Pleroma.Factory
8
9 describe "POST /api/pleroma/follow_import" do
10 test "it returns HTTP 200", %{conn: conn} do
11 user1 = insert(:user)
12 user2 = insert(:user)
13
14 response =
15 conn
16 |> assign(:user, user1)
17 |> post("/api/pleroma/follow_import", %{"list" => "#{user2.ap_id}"})
18 |> json_response(:ok)
19
20 assert response == "job started"
21 end
22
23 test "requires 'follow' permission", %{conn: conn} do
24 token1 = insert(:oauth_token, scopes: ["read", "write"])
25 token2 = insert(:oauth_token, scopes: ["follow"])
26 another_user = insert(:user)
27
28 for token <- [token1, token2] do
29 conn =
30 conn
31 |> put_req_header("authorization", "Bearer #{token.token}")
32 |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
33
34 if token == token1 do
35 assert %{"error" => "Insufficient permissions: follow."} == json_response(conn, 403)
36 else
37 assert json_response(conn, 200)
38 end
39 end
40 end
41 end
42
43 describe "POST /api/pleroma/blocks_import" do
44 test "it returns HTTP 200", %{conn: conn} do
45 user1 = insert(:user)
46 user2 = insert(:user)
47
48 response =
49 conn
50 |> assign(:user, user1)
51 |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
52 |> json_response(:ok)
53
54 assert response == "job started"
55 end
56 end
57
58 describe "POST /api/pleroma/notifications/read" do
59 test "it marks a single notification as read", %{conn: conn} do
60 user1 = insert(:user)
61 user2 = insert(:user)
62 {:ok, activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
63 {:ok, activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
64 {:ok, [notification1]} = Notification.create_notifications(activity1)
65 {:ok, [notification2]} = Notification.create_notifications(activity2)
66
67 conn
68 |> assign(:user, user1)
69 |> post("/api/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
70 |> json_response(:ok)
71
72 assert Repo.get(Notification, notification1.id).seen
73 refute Repo.get(Notification, notification2.id).seen
74 end
75 end
76
77 describe "GET /api/statusnet/config.json" do
78 test "it returns the managed config", %{conn: conn} do
79 Pleroma.Config.put([:instance, :managed_config], false)
80 Pleroma.Config.put([:fe], theme: "rei-ayanami-towel")
81
82 response =
83 conn
84 |> get("/api/statusnet/config.json")
85 |> json_response(:ok)
86
87 refute response["site"]["pleromafe"]
88
89 Pleroma.Config.put([:instance, :managed_config], true)
90
91 response =
92 conn
93 |> get("/api/statusnet/config.json")
94 |> json_response(:ok)
95
96 assert response["site"]["pleromafe"]
97 end
98
99 test "if :pleroma, :fe is false, it returns the new style config settings", %{conn: conn} do
100 Pleroma.Config.put([:instance, :managed_config], true)
101 Pleroma.Config.put([:fe, :theme], "rei-ayanami-towel")
102 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
103
104 response =
105 conn
106 |> get("/api/statusnet/config.json")
107 |> json_response(:ok)
108
109 assert response["site"]["pleromafe"]["theme"] == "rei-ayanami-towel"
110
111 Pleroma.Config.put([:fe], false)
112
113 response =
114 conn
115 |> get("/api/statusnet/config.json")
116 |> json_response(:ok)
117
118 assert response["site"]["pleromafe"]["theme"] == "asuka-hospital"
119 end
120 end
121
122 describe "GET /api/pleroma/frontend_configurations" do
123 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
124 config = [
125 frontend_a: %{
126 x: 1,
127 y: 2
128 },
129 frontend_b: %{
130 z: 3
131 }
132 ]
133
134 Pleroma.Config.put(:frontend_configurations, config)
135
136 response =
137 conn
138 |> get("/api/pleroma/frontend_configurations")
139 |> json_response(:ok)
140
141 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
142 end
143 end
144 end