Merge remote-tracking branch 'upstream/develop' into earmark
[akkoma] / test / pleroma / web / admin_api / controllers / o_auth_app_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.AdminAPI.OAuthAppControllerTest do
6 use Pleroma.Web.ConnCase, async: true
7 use Oban.Testing, repo: Pleroma.Repo
8
9 import Pleroma.Factory
10
11 alias Pleroma.Web
12
13 setup do
14 admin = insert(:user, is_admin: true)
15 token = insert(:oauth_admin_token, user: admin)
16
17 conn =
18 build_conn()
19 |> assign(:user, admin)
20 |> assign(:token, token)
21
22 {:ok, %{admin: admin, token: token, conn: conn}}
23 end
24
25 describe "POST /api/pleroma/admin/oauth_app" do
26 test "errors", %{conn: conn} do
27 response =
28 conn
29 |> put_req_header("content-type", "application/json")
30 |> post("/api/pleroma/admin/oauth_app", %{})
31 |> json_response_and_validate_schema(400)
32
33 assert %{
34 "error" => "Missing field: name. Missing field: redirect_uris."
35 } = response
36 end
37
38 test "success", %{conn: conn} do
39 base_url = Web.base_url()
40 app_name = "Trusted app"
41
42 response =
43 conn
44 |> put_req_header("content-type", "application/json")
45 |> post("/api/pleroma/admin/oauth_app", %{
46 name: app_name,
47 redirect_uris: base_url
48 })
49 |> json_response_and_validate_schema(200)
50
51 assert %{
52 "client_id" => _,
53 "client_secret" => _,
54 "name" => ^app_name,
55 "redirect_uri" => ^base_url,
56 "trusted" => false
57 } = response
58 end
59
60 test "with trusted", %{conn: conn} do
61 base_url = Web.base_url()
62 app_name = "Trusted app"
63
64 response =
65 conn
66 |> put_req_header("content-type", "application/json")
67 |> post("/api/pleroma/admin/oauth_app", %{
68 name: app_name,
69 redirect_uris: base_url,
70 trusted: true
71 })
72 |> json_response_and_validate_schema(200)
73
74 assert %{
75 "client_id" => _,
76 "client_secret" => _,
77 "name" => ^app_name,
78 "redirect_uri" => ^base_url,
79 "trusted" => true
80 } = response
81 end
82 end
83
84 describe "GET /api/pleroma/admin/oauth_app" do
85 setup do
86 app = insert(:oauth_app)
87 {:ok, app: app}
88 end
89
90 test "list", %{conn: conn} do
91 response =
92 conn
93 |> get("/api/pleroma/admin/oauth_app")
94 |> json_response_and_validate_schema(200)
95
96 assert %{"apps" => apps, "count" => count, "page_size" => _} = response
97
98 assert length(apps) == count
99 end
100
101 test "with page size", %{conn: conn} do
102 insert(:oauth_app)
103 page_size = 1
104
105 response =
106 conn
107 |> get("/api/pleroma/admin/oauth_app?page_size=#{page_size}")
108 |> json_response_and_validate_schema(200)
109
110 assert %{"apps" => apps, "count" => _, "page_size" => ^page_size} = response
111
112 assert length(apps) == page_size
113 end
114
115 test "search by client name", %{conn: conn, app: app} do
116 response =
117 conn
118 |> get("/api/pleroma/admin/oauth_app?name=#{app.client_name}")
119 |> json_response_and_validate_schema(200)
120
121 assert %{"apps" => [returned], "count" => _, "page_size" => _} = response
122
123 assert returned["client_id"] == app.client_id
124 assert returned["name"] == app.client_name
125 end
126
127 test "search by client id", %{conn: conn, app: app} do
128 response =
129 conn
130 |> get("/api/pleroma/admin/oauth_app?client_id=#{app.client_id}")
131 |> json_response_and_validate_schema(200)
132
133 assert %{"apps" => [returned], "count" => _, "page_size" => _} = response
134
135 assert returned["client_id"] == app.client_id
136 assert returned["name"] == app.client_name
137 end
138
139 test "only trusted", %{conn: conn} do
140 app = insert(:oauth_app, trusted: true)
141
142 response =
143 conn
144 |> get("/api/pleroma/admin/oauth_app?trusted=true")
145 |> json_response_and_validate_schema(200)
146
147 assert %{"apps" => [returned], "count" => _, "page_size" => _} = response
148
149 assert returned["client_id"] == app.client_id
150 assert returned["name"] == app.client_name
151 end
152 end
153
154 describe "DELETE /api/pleroma/admin/oauth_app/:id" do
155 test "with id", %{conn: conn} do
156 app = insert(:oauth_app)
157
158 response =
159 conn
160 |> delete("/api/pleroma/admin/oauth_app/" <> to_string(app.id))
161 |> json_response_and_validate_schema(:no_content)
162
163 assert response == ""
164 end
165
166 test "with non existance id", %{conn: conn} do
167 response =
168 conn
169 |> delete("/api/pleroma/admin/oauth_app/0")
170 |> json_response_and_validate_schema(:bad_request)
171
172 assert response == ""
173 end
174 end
175
176 describe "PATCH /api/pleroma/admin/oauth_app/:id" do
177 test "with id", %{conn: conn} do
178 app = insert(:oauth_app)
179
180 name = "another name"
181 url = "https://example.com"
182 scopes = ["admin"]
183 id = app.id
184 website = "http://website.com"
185
186 response =
187 conn
188 |> put_req_header("content-type", "application/json")
189 |> patch("/api/pleroma/admin/oauth_app/#{id}", %{
190 name: name,
191 trusted: true,
192 redirect_uris: url,
193 scopes: scopes,
194 website: website
195 })
196 |> json_response_and_validate_schema(200)
197
198 assert %{
199 "client_id" => _,
200 "client_secret" => _,
201 "id" => ^id,
202 "name" => ^name,
203 "redirect_uri" => ^url,
204 "trusted" => true,
205 "website" => ^website
206 } = response
207 end
208
209 test "without id", %{conn: conn} do
210 response =
211 conn
212 |> put_req_header("content-type", "application/json")
213 |> patch("/api/pleroma/admin/oauth_app/0")
214 |> json_response_and_validate_schema(:bad_request)
215
216 assert response == ""
217 end
218 end
219 end