X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;ds=sidebyside;f=test%2Fpleroma%2Fweb%2Fadmin_api%2Fcontrollers%2Fadmin_api_controller_test.exs;h=8cd9f939b20312ca64553e47a1b39c78b9ba63c3;hb=c45b3bde949bbbd6b8d6de30022c1d1e5b99dc44;hp=c06ae55cae2c7580c3850ab5cd8f12060b52bf57;hpb=9546c1444c2c8c4abc9bcb35b6a8ff360ddc83af;p=akkoma
diff --git a/test/pleroma/web/admin_api/controllers/admin_api_controller_test.exs b/test/pleroma/web/admin_api/controllers/admin_api_controller_test.exs
index c06ae55ca..8cd9f939b 100644
--- a/test/pleroma/web/admin_api/controllers/admin_api_controller_test.exs
+++ b/test/pleroma/web/admin_api/controllers/admin_api_controller_test.exs
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors
+# Copyright © 2017-2021 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
@@ -46,104 +46,47 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
assert json_response(conn, 200)
end
- describe "with [:auth, :enforce_oauth_admin_scope_usage]," do
- setup do: clear_config([:auth, :enforce_oauth_admin_scope_usage], true)
+ test "GET /api/pleroma/admin/users/:nickname requires admin:read:accounts or broader scope",
+ %{admin: admin} do
+ user = insert(:user)
+ url = "/api/pleroma/admin/users/#{user.nickname}"
- test "GET /api/pleroma/admin/users/:nickname requires admin:read:accounts or broader scope",
- %{admin: admin} do
- user = insert(:user)
- url = "/api/pleroma/admin/users/#{user.nickname}"
-
- good_token1 = insert(:oauth_token, user: admin, scopes: ["admin"])
- good_token2 = insert(:oauth_token, user: admin, scopes: ["admin:read"])
- good_token3 = insert(:oauth_token, user: admin, scopes: ["admin:read:accounts"])
-
- bad_token1 = insert(:oauth_token, user: admin, scopes: ["read:accounts"])
- bad_token2 = insert(:oauth_token, user: admin, scopes: ["admin:read:accounts:partial"])
- bad_token3 = nil
-
- for good_token <- [good_token1, good_token2, good_token3] do
- conn =
- build_conn()
- |> assign(:user, admin)
- |> assign(:token, good_token)
- |> get(url)
-
- assert json_response(conn, 200)
- end
-
- for good_token <- [good_token1, good_token2, good_token3] do
- conn =
- build_conn()
- |> assign(:user, nil)
- |> assign(:token, good_token)
- |> get(url)
-
- assert json_response(conn, :forbidden)
- end
-
- for bad_token <- [bad_token1, bad_token2, bad_token3] do
- conn =
- build_conn()
- |> assign(:user, admin)
- |> assign(:token, bad_token)
- |> get(url)
-
- assert json_response(conn, :forbidden)
- end
+ good_token1 = insert(:oauth_token, user: admin, scopes: ["admin"])
+ good_token2 = insert(:oauth_token, user: admin, scopes: ["admin:read"])
+ good_token3 = insert(:oauth_token, user: admin, scopes: ["admin:read:accounts"])
+
+ bad_token1 = insert(:oauth_token, user: admin, scopes: ["read:accounts"])
+ bad_token2 = insert(:oauth_token, user: admin, scopes: ["admin:read:accounts:partial"])
+ bad_token3 = nil
+
+ for good_token <- [good_token1, good_token2, good_token3] do
+ conn =
+ build_conn()
+ |> assign(:user, admin)
+ |> assign(:token, good_token)
+ |> get(url)
+
+ assert json_response(conn, 200)
end
- end
- describe "unless [:auth, :enforce_oauth_admin_scope_usage]," do
- setup do: clear_config([:auth, :enforce_oauth_admin_scope_usage], false)
+ for good_token <- [good_token1, good_token2, good_token3] do
+ conn =
+ build_conn()
+ |> assign(:user, nil)
+ |> assign(:token, good_token)
+ |> get(url)
- test "GET /api/pleroma/admin/users/:nickname requires " <>
- "read:accounts or admin:read:accounts or broader scope",
- %{admin: admin} do
- user = insert(:user)
- url = "/api/pleroma/admin/users/#{user.nickname}"
-
- good_token1 = insert(:oauth_token, user: admin, scopes: ["admin"])
- good_token2 = insert(:oauth_token, user: admin, scopes: ["admin:read"])
- good_token3 = insert(:oauth_token, user: admin, scopes: ["admin:read:accounts"])
- good_token4 = insert(:oauth_token, user: admin, scopes: ["read:accounts"])
- good_token5 = insert(:oauth_token, user: admin, scopes: ["read"])
-
- good_tokens = [good_token1, good_token2, good_token3, good_token4, good_token5]
-
- bad_token1 = insert(:oauth_token, user: admin, scopes: ["read:accounts:partial"])
- bad_token2 = insert(:oauth_token, user: admin, scopes: ["admin:read:accounts:partial"])
- bad_token3 = nil
-
- for good_token <- good_tokens do
- conn =
- build_conn()
- |> assign(:user, admin)
- |> assign(:token, good_token)
- |> get(url)
-
- assert json_response(conn, 200)
- end
-
- for good_token <- good_tokens do
- conn =
- build_conn()
- |> assign(:user, nil)
- |> assign(:token, good_token)
- |> get(url)
-
- assert json_response(conn, :forbidden)
- end
-
- for bad_token <- [bad_token1, bad_token2, bad_token3] do
- conn =
- build_conn()
- |> assign(:user, admin)
- |> assign(:token, bad_token)
- |> get(url)
-
- assert json_response(conn, :forbidden)
- end
+ assert json_response(conn, :forbidden)
+ end
+
+ for bad_token <- [bad_token1, bad_token2, bad_token3] do
+ conn =
+ build_conn()
+ |> assign(:user, admin)
+ |> assign(:token, bad_token)
+ |> get(url)
+
+ assert json_response(conn, :forbidden)
end
end
@@ -405,13 +348,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
setup do
user = insert(:user)
- date1 = (DateTime.to_unix(DateTime.utc_now()) + 2000) |> DateTime.from_unix!()
- date2 = (DateTime.to_unix(DateTime.utc_now()) + 1000) |> DateTime.from_unix!()
- date3 = (DateTime.to_unix(DateTime.utc_now()) + 3000) |> DateTime.from_unix!()
-
- insert(:note_activity, user: user, published: date1)
- insert(:note_activity, user: user, published: date2)
- insert(:note_activity, user: user, published: date3)
+ insert(:note_activity, user: user)
+ insert(:note_activity, user: user)
+ insert(:note_activity, user: user)
%{user: user}
end
@@ -419,13 +358,22 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
test "renders user's statuses", %{conn: conn, user: user} do
conn = get(conn, "/api/pleroma/admin/users/#{user.nickname}/statuses")
- assert json_response(conn, 200) |> length() == 3
+ assert %{"total" => 3, "activities" => activities} = json_response(conn, 200)
+ assert length(activities) == 3
end
- test "renders user's statuses with a limit", %{conn: conn, user: user} do
- conn = get(conn, "/api/pleroma/admin/users/#{user.nickname}/statuses?page_size=2")
+ test "renders user's statuses with pagination", %{conn: conn, user: user} do
+ %{"total" => 3, "activities" => [activity1]} =
+ conn
+ |> get("/api/pleroma/admin/users/#{user.nickname}/statuses?page_size=1&page=1")
+ |> json_response(200)
- assert json_response(conn, 200) |> length() == 2
+ %{"total" => 3, "activities" => [activity2]} =
+ conn
+ |> get("/api/pleroma/admin/users/#{user.nickname}/statuses?page_size=1&page=2")
+ |> json_response(200)
+
+ refute activity1 == activity2
end
test "doesn't return private statuses by default", %{conn: conn, user: user} do
@@ -433,9 +381,12 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
{:ok, _public_status} = CommonAPI.post(user, %{status: "public", visibility: "public"})
- conn = get(conn, "/api/pleroma/admin/users/#{user.nickname}/statuses")
+ %{"total" => 4, "activities" => activities} =
+ conn
+ |> get("/api/pleroma/admin/users/#{user.nickname}/statuses")
+ |> json_response(200)
- assert json_response(conn, 200) |> length() == 4
+ assert length(activities) == 4
end
test "returns private statuses with godmode on", %{conn: conn, user: user} do
@@ -443,9 +394,12 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
{:ok, _public_status} = CommonAPI.post(user, %{status: "public", visibility: "public"})
- conn = get(conn, "/api/pleroma/admin/users/#{user.nickname}/statuses?godmode=true")
+ %{"total" => 5, "activities" => activities} =
+ conn
+ |> get("/api/pleroma/admin/users/#{user.nickname}/statuses?godmode=true")
+ |> json_response(200)
- assert json_response(conn, 200) |> length() == 5
+ assert length(activities) == 5
end
test "excludes reblogs by default", %{conn: conn, user: user} do
@@ -453,13 +407,17 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
{:ok, activity} = CommonAPI.post(user, %{status: "."})
{:ok, %Activity{}} = CommonAPI.repeat(activity.id, other_user)
- conn_res = get(conn, "/api/pleroma/admin/users/#{other_user.nickname}/statuses")
- assert json_response(conn_res, 200) |> length() == 0
-
- conn_res =
- get(conn, "/api/pleroma/admin/users/#{other_user.nickname}/statuses?with_reblogs=true")
+ assert %{"total" => 0, "activities" => []} ==
+ conn
+ |> get("/api/pleroma/admin/users/#{other_user.nickname}/statuses")
+ |> json_response(200)
- assert json_response(conn_res, 200) |> length() == 1
+ assert %{"total" => 1, "activities" => [_]} =
+ conn
+ |> get(
+ "/api/pleroma/admin/users/#{other_user.nickname}/statuses?with_reblogs=true"
+ )
+ |> json_response(200)
end
end
@@ -849,42 +807,39 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
insert_pair(:note_activity, user: user)
activity = insert(:note_activity, user: user2)
- ret_conn = get(conn, "/api/pleroma/admin/instances/archae.me/statuses")
+ %{"total" => 2, "activities" => activities} =
+ conn |> get("/api/pleroma/admin/instances/archae.me/statuses") |> json_response(200)
- response = json_response(ret_conn, 200)
+ assert length(activities) == 2
- assert length(response) == 2
+ %{"total" => 1, "activities" => [_]} =
+ conn |> get("/api/pleroma/admin/instances/test.com/statuses") |> json_response(200)
- ret_conn = get(conn, "/api/pleroma/admin/instances/test.com/statuses")
+ %{"total" => 0, "activities" => []} =
+ conn |> get("/api/pleroma/admin/instances/nonexistent.com/statuses") |> json_response(200)
- response = json_response(ret_conn, 200)
-
- assert length(response) == 1
-
- ret_conn = get(conn, "/api/pleroma/admin/instances/nonexistent.com/statuses")
-
- response = json_response(ret_conn, 200)
+ CommonAPI.repeat(activity.id, user)
- assert Enum.empty?(response)
+ %{"total" => 2, "activities" => activities} =
+ conn |> get("/api/pleroma/admin/instances/archae.me/statuses") |> json_response(200)
- CommonAPI.repeat(activity.id, user)
+ assert length(activities) == 2
- ret_conn = get(conn, "/api/pleroma/admin/instances/archae.me/statuses")
- response = json_response(ret_conn, 200)
- assert length(response) == 2
+ %{"total" => 3, "activities" => activities} =
+ conn
+ |> get("/api/pleroma/admin/instances/archae.me/statuses?with_reblogs=true")
+ |> json_response(200)
- ret_conn = get(conn, "/api/pleroma/admin/instances/archae.me/statuses?with_reblogs=true")
- response = json_response(ret_conn, 200)
- assert length(response) == 3
+ assert length(activities) == 3
end
end
describe "PATCH /confirm_email" do
test "it confirms emails of two users", %{conn: conn, admin: admin} do
- [first_user, second_user] = insert_pair(:user, confirmation_pending: true)
+ [first_user, second_user] = insert_pair(:user, is_confirmed: false)
- assert first_user.confirmation_pending == true
- assert second_user.confirmation_pending == true
+ refute first_user.is_confirmed
+ refute second_user.is_confirmed
ret_conn =
patch(conn, "/api/pleroma/admin/users/confirm_email", %{
@@ -896,8 +851,11 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
assert ret_conn.status == 200
- assert first_user.confirmation_pending == true
- assert second_user.confirmation_pending == true
+ first_user = User.get_by_id(first_user.id)
+ second_user = User.get_by_id(second_user.id)
+
+ assert first_user.is_confirmed
+ assert second_user.is_confirmed
log_entry = Repo.one(ModerationLog)
@@ -910,7 +868,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
describe "PATCH /resend_confirmation_email" do
test "it resend emails for two users", %{conn: conn, admin: admin} do
- [first_user, second_user] = insert_pair(:user, confirmation_pending: true)
+ [first_user, second_user] = insert_pair(:user, is_confirmed: false)
ret_conn =
patch(conn, "/api/pleroma/admin/users/resend_confirmation_email", %{
@@ -941,7 +899,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
describe "/api/pleroma/admin/stats" do
test "status visibility count", %{conn: conn} do
- admin = insert(:user, is_admin: true)
user = insert(:user)
CommonAPI.post(user, %{visibility: "public", status: "hey"})
CommonAPI.post(user, %{visibility: "unlisted", status: "hey"})
@@ -949,7 +906,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
response =
conn
- |> assign(:user, admin)
|> get("/api/pleroma/admin/stats")
|> json_response(200)
@@ -958,7 +914,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
end
test "by instance", %{conn: conn} do
- admin = insert(:user, is_admin: true)
user1 = insert(:user)
instance2 = "instance2.tld"
user2 = insert(:user, %{ap_id: "https://#{instance2}/@actor"})
@@ -969,7 +924,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
response =
conn
- |> assign(:user, admin)
|> get("/api/pleroma/admin/stats", instance: instance2)
|> json_response(200)