2018-11-17 16:11:18 -05:00
|
|
|
defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
|
|
|
|
use Pleroma.Web.ConnCase
|
|
|
|
|
|
|
|
alias Pleroma.{Repo, User}
|
|
|
|
|
|
|
|
import Pleroma.Factory
|
|
|
|
import ExUnit.CaptureLog
|
|
|
|
|
|
|
|
describe "/api/pleroma/admin/user" do
|
|
|
|
test "Delete" do
|
2018-12-01 03:03:16 -05:00
|
|
|
admin = insert(:user, info: %{is_admin: true})
|
2018-11-17 16:11:18 -05:00
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
conn =
|
|
|
|
build_conn()
|
|
|
|
|> assign(:user, admin)
|
|
|
|
|> put_req_header("accept", "application/json")
|
|
|
|
|> delete("/api/pleroma/admin/user?nickname=#{user.nickname}")
|
|
|
|
|
|
|
|
assert json_response(conn, 200) == user.nickname
|
|
|
|
end
|
|
|
|
|
|
|
|
test "Create" do
|
2018-12-01 03:03:16 -05:00
|
|
|
admin = insert(:user, info: %{is_admin: true})
|
2018-11-17 16:11:18 -05:00
|
|
|
|
|
|
|
conn =
|
|
|
|
build_conn()
|
|
|
|
|> assign(:user, admin)
|
|
|
|
|> put_req_header("accept", "application/json")
|
|
|
|
|> post("/api/pleroma/admin/user", %{
|
|
|
|
"nickname" => "lain",
|
|
|
|
"email" => "lain@example.org",
|
|
|
|
"password" => "test"
|
|
|
|
})
|
|
|
|
|
|
|
|
assert json_response(conn, 200) == "lain"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-07 03:04:39 -05:00
|
|
|
describe "PUT /api/pleroma/admin/users/tag" do
|
2018-12-06 12:06:50 -05:00
|
|
|
setup do
|
|
|
|
admin = insert(:user, info: %{is_admin: true})
|
|
|
|
user1 = insert(:user, %{tags: ["x"]})
|
|
|
|
user2 = insert(:user, %{tags: ["y"]})
|
|
|
|
user3 = insert(:user, %{tags: ["unchanged"]})
|
|
|
|
|
|
|
|
conn =
|
|
|
|
build_conn()
|
|
|
|
|> assign(:user, admin)
|
|
|
|
|> put_req_header("accept", "application/json")
|
2018-12-06 12:13:07 -05:00
|
|
|
|> put(
|
|
|
|
"/api/pleroma/admin/users/tag?nicknames[]=#{user1.nickname}&nicknames[]=#{
|
|
|
|
user2.nickname
|
|
|
|
}&tags[]=foo&tags[]=bar"
|
|
|
|
)
|
2018-12-06 12:06:50 -05:00
|
|
|
|
|
|
|
%{conn: conn, user1: user1, user2: user2, user3: user3}
|
|
|
|
end
|
|
|
|
|
2018-12-06 12:13:07 -05:00
|
|
|
test "it appends specified tags to users with specified nicknames", %{
|
|
|
|
conn: conn,
|
|
|
|
user1: user1,
|
|
|
|
user2: user2
|
|
|
|
} do
|
2018-12-06 12:06:50 -05:00
|
|
|
assert json_response(conn, :no_content)
|
|
|
|
assert Repo.get(User, user1.id).tags == ["x", "foo", "bar"]
|
|
|
|
assert Repo.get(User, user2.id).tags == ["y", "foo", "bar"]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it does not modify tags of not specified users", %{conn: conn, user3: user3} do
|
|
|
|
assert json_response(conn, :no_content)
|
|
|
|
assert Repo.get(User, user3.id).tags == ["unchanged"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-07 03:04:39 -05:00
|
|
|
describe "DELETE /api/pleroma/admin/users/tag" do
|
2018-12-06 12:06:50 -05:00
|
|
|
setup do
|
|
|
|
admin = insert(:user, info: %{is_admin: true})
|
|
|
|
user1 = insert(:user, %{tags: ["x"]})
|
|
|
|
user2 = insert(:user, %{tags: ["y", "z"]})
|
|
|
|
user3 = insert(:user, %{tags: ["unchanged"]})
|
|
|
|
|
|
|
|
conn =
|
|
|
|
build_conn()
|
|
|
|
|> assign(:user, admin)
|
|
|
|
|> put_req_header("accept", "application/json")
|
2018-12-07 03:04:39 -05:00
|
|
|
|> delete(
|
|
|
|
"/api/pleroma/admin/users/tag?nicknames[]=#{user1.nickname}&nicknames[]=#{
|
2018-12-06 12:13:07 -05:00
|
|
|
user2.nickname
|
|
|
|
}&tags[]=x&tags[]=z"
|
|
|
|
)
|
2018-12-06 12:06:50 -05:00
|
|
|
|
|
|
|
%{conn: conn, user1: user1, user2: user2, user3: user3}
|
|
|
|
end
|
|
|
|
|
2018-12-06 12:13:07 -05:00
|
|
|
test "it removes specified tags from users with specified nicknames", %{
|
|
|
|
conn: conn,
|
|
|
|
user1: user1,
|
|
|
|
user2: user2
|
|
|
|
} do
|
2018-12-06 12:06:50 -05:00
|
|
|
assert json_response(conn, :no_content)
|
|
|
|
assert Repo.get(User, user1.id).tags == []
|
|
|
|
assert Repo.get(User, user2.id).tags == ["y"]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it does not modify tags of not specified users", %{conn: conn, user3: user3} do
|
|
|
|
assert json_response(conn, :no_content)
|
|
|
|
assert Repo.get(User, user3.id).tags == ["unchanged"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-17 16:11:18 -05:00
|
|
|
describe "/api/pleroma/admin/permission_group" do
|
|
|
|
test "GET is giving user_info" do
|
2018-12-01 03:03:16 -05:00
|
|
|
admin = insert(:user, info: %{is_admin: true})
|
2018-11-17 16:11:18 -05:00
|
|
|
|
|
|
|
conn =
|
|
|
|
build_conn()
|
|
|
|
|> assign(:user, admin)
|
|
|
|
|> put_req_header("accept", "application/json")
|
|
|
|
|> get("/api/pleroma/admin/permission_group/#{admin.nickname}")
|
|
|
|
|
2018-12-01 03:03:16 -05:00
|
|
|
assert json_response(conn, 200) == %{
|
|
|
|
"is_admin" => true,
|
|
|
|
"is_moderator" => false
|
|
|
|
}
|
2018-11-17 16:11:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "/:right POST, can add to a permission group" do
|
2018-12-01 03:03:16 -05:00
|
|
|
admin = insert(:user, info: %{is_admin: true})
|
2018-11-17 16:11:18 -05:00
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
conn =
|
|
|
|
build_conn()
|
|
|
|
|> assign(:user, admin)
|
|
|
|
|> put_req_header("accept", "application/json")
|
|
|
|
|> post("/api/pleroma/admin/permission_group/#{user.nickname}/admin")
|
|
|
|
|
2018-12-01 03:03:16 -05:00
|
|
|
assert json_response(conn, 200) == %{
|
|
|
|
"is_admin" => true
|
|
|
|
}
|
2018-11-17 16:11:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "/:right DELETE, can remove from a permission group" do
|
2018-12-01 03:03:16 -05:00
|
|
|
admin = insert(:user, info: %{is_admin: true})
|
|
|
|
user = insert(:user, info: %{is_admin: true})
|
2018-11-17 16:11:18 -05:00
|
|
|
|
|
|
|
conn =
|
|
|
|
build_conn()
|
|
|
|
|> assign(:user, admin)
|
|
|
|
|> put_req_header("accept", "application/json")
|
|
|
|
|> delete("/api/pleroma/admin/permission_group/#{user.nickname}/admin")
|
|
|
|
|
2018-12-01 03:03:16 -05:00
|
|
|
assert json_response(conn, 200) == %{
|
|
|
|
"is_admin" => false
|
|
|
|
}
|
2018-11-17 16:11:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "/api/pleroma/admin/invite_token" do
|
2018-12-01 03:03:16 -05:00
|
|
|
admin = insert(:user, info: %{is_admin: true})
|
2018-11-17 16:11:18 -05:00
|
|
|
|
|
|
|
conn =
|
|
|
|
build_conn()
|
|
|
|
|> assign(:user, admin)
|
|
|
|
|> put_req_header("accept", "application/json")
|
|
|
|
|> get("/api/pleroma/admin/invite_token")
|
|
|
|
|
|
|
|
assert conn.status == 200
|
|
|
|
end
|
|
|
|
|
|
|
|
test "/api/pleroma/admin/password_reset" do
|
2018-12-01 03:03:16 -05:00
|
|
|
admin = insert(:user, info: %{is_admin: true})
|
|
|
|
user = insert(:user)
|
2018-11-17 16:11:18 -05:00
|
|
|
|
|
|
|
conn =
|
|
|
|
build_conn()
|
|
|
|
|> assign(:user, admin)
|
|
|
|
|> put_req_header("accept", "application/json")
|
|
|
|
|> get("/api/pleroma/admin/password_reset?nickname=#{user.nickname}")
|
|
|
|
|
|
|
|
assert conn.status == 200
|
|
|
|
end
|
|
|
|
end
|