1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.AdminAPI.OAuthAppControllerTest do
6 use Pleroma.Web.ConnCase, async: true
7 use Oban.Testing, repo: Pleroma.Repo
14 admin = insert(:user, is_admin: true)
15 token = insert(:oauth_admin_token, user: admin)
19 |> assign(:user, admin)
20 |> assign(:token, token)
22 {:ok, %{admin: admin, token: token, conn: conn}}
25 describe "POST /api/pleroma/admin/oauth_app" do
26 test "errors", %{conn: conn} do
29 |> put_req_header("content-type", "application/json")
30 |> post("/api/pleroma/admin/oauth_app", %{})
31 |> json_response_and_validate_schema(400)
34 "error" => "Missing field: name. Missing field: redirect_uris."
38 test "success", %{conn: conn} do
39 base_url = Web.base_url()
40 app_name = "Trusted app"
44 |> put_req_header("content-type", "application/json")
45 |> post("/api/pleroma/admin/oauth_app", %{
47 redirect_uris: base_url
49 |> json_response_and_validate_schema(200)
55 "redirect_uri" => ^base_url,
60 test "with trusted", %{conn: conn} do
61 base_url = Web.base_url()
62 app_name = "Trusted app"
66 |> put_req_header("content-type", "application/json")
67 |> post("/api/pleroma/admin/oauth_app", %{
69 redirect_uris: base_url,
72 |> json_response_and_validate_schema(200)
78 "redirect_uri" => ^base_url,
84 describe "GET /api/pleroma/admin/oauth_app" do
86 app = insert(:oauth_app)
90 test "list", %{conn: conn} do
93 |> get("/api/pleroma/admin/oauth_app")
94 |> json_response_and_validate_schema(200)
96 assert %{"apps" => apps, "count" => count, "page_size" => _} = response
98 assert length(apps) == count
101 test "with page size", %{conn: conn} do
107 |> get("/api/pleroma/admin/oauth_app?page_size=#{page_size}")
108 |> json_response_and_validate_schema(200)
110 assert %{"apps" => apps, "count" => _, "page_size" => ^page_size} = response
112 assert length(apps) == page_size
115 test "search by client name", %{conn: conn, app: app} do
118 |> get("/api/pleroma/admin/oauth_app?name=#{app.client_name}")
119 |> json_response_and_validate_schema(200)
121 assert %{"apps" => [returned], "count" => _, "page_size" => _} = response
123 assert returned["client_id"] == app.client_id
124 assert returned["name"] == app.client_name
127 test "search by client id", %{conn: conn, app: app} do
130 |> get("/api/pleroma/admin/oauth_app?client_id=#{app.client_id}")
131 |> json_response_and_validate_schema(200)
133 assert %{"apps" => [returned], "count" => _, "page_size" => _} = response
135 assert returned["client_id"] == app.client_id
136 assert returned["name"] == app.client_name
139 test "only trusted", %{conn: conn} do
140 app = insert(:oauth_app, trusted: true)
144 |> get("/api/pleroma/admin/oauth_app?trusted=true")
145 |> json_response_and_validate_schema(200)
147 assert %{"apps" => [returned], "count" => _, "page_size" => _} = response
149 assert returned["client_id"] == app.client_id
150 assert returned["name"] == app.client_name
154 describe "DELETE /api/pleroma/admin/oauth_app/:id" do
155 test "with id", %{conn: conn} do
156 app = insert(:oauth_app)
160 |> delete("/api/pleroma/admin/oauth_app/" <> to_string(app.id))
161 |> json_response_and_validate_schema(:no_content)
163 assert response == ""
166 test "with non existance id", %{conn: conn} do
169 |> delete("/api/pleroma/admin/oauth_app/0")
170 |> json_response_and_validate_schema(:bad_request)
172 assert response == ""
176 describe "PATCH /api/pleroma/admin/oauth_app/:id" do
177 test "with id", %{conn: conn} do
178 app = insert(:oauth_app)
180 name = "another name"
181 url = "https://example.com"
184 website = "http://website.com"
188 |> put_req_header("content-type", "application/json")
189 |> patch("/api/pleroma/admin/oauth_app/#{id}", %{
196 |> json_response_and_validate_schema(200)
200 "client_secret" => _,
203 "redirect_uri" => ^url,
205 "website" => ^website
209 test "without id", %{conn: conn} do
212 |> put_req_header("content-type", "application/json")
213 |> patch("/api/pleroma/admin/oauth_app/0")
214 |> json_response_and_validate_schema(:bad_request)
216 assert response == ""