From b9f8d3763d8dbfa5630ffcc4075c1b4aceb3de61 Mon Sep 17 00:00:00 2001 From: Maksim Pechnikov Date: Thu, 29 Oct 2020 11:04:44 +0300 Subject: [PATCH] fix tests --- .../web/admin_api/controllers/tag_controller.ex | 7 ++- .../web/api_spec/operations/admin/tag_operation.ex | 58 +++++++++++----------- .../admin_api/controllers/tag_controller_test.exs | 44 +++++++--------- 3 files changed, 54 insertions(+), 55 deletions(-) diff --git a/lib/pleroma/web/admin_api/controllers/tag_controller.ex b/lib/pleroma/web/admin_api/controllers/tag_controller.ex index ee48efbd7..2cf6ff9bb 100644 --- a/lib/pleroma/web/admin_api/controllers/tag_controller.ex +++ b/lib/pleroma/web/admin_api/controllers/tag_controller.ex @@ -35,7 +35,7 @@ defmodule Pleroma.Web.AdminAPI.TagController do json(conn, tags) end - def tag(%{assigns: %{user: admin}} = conn, %{nicknames: nicknames, tags: tags}) do + def tag(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames, tags: tags}} = conn, _) do with {:ok, _} <- User.tag(nicknames, tags) do ModerationLog.insert_log(%{ actor: admin, @@ -48,7 +48,10 @@ defmodule Pleroma.Web.AdminAPI.TagController do end end - def untag(%{assigns: %{user: admin}} = conn, %{nicknames: nicknames, tags: tags}) do + def untag( + %{assigns: %{user: admin}, body_params: %{nicknames: nicknames, tags: tags}} = conn, + _ + ) do with {:ok, _} <- User.untag(nicknames, tags) do ModerationLog.insert_log(%{ actor: admin, diff --git a/lib/pleroma/web/api_spec/operations/admin/tag_operation.ex b/lib/pleroma/web/api_spec/operations/admin/tag_operation.ex index 83a6b5c26..8bee51fe2 100644 --- a/lib/pleroma/web/api_spec/operations/admin/tag_operation.ex +++ b/lib/pleroma/web/api_spec/operations/admin/tag_operation.ex @@ -5,6 +5,7 @@ defmodule Pleroma.Web.ApiSpec.Admin.TagOperation do alias OpenApiSpex.Operation alias OpenApiSpex.Schema + alias Pleroma.Web.ApiSpec.Schemas.ApiError import Pleroma.Web.ApiSpec.Helpers @@ -18,6 +19,7 @@ defmodule Pleroma.Web.ApiSpec.Admin.TagOperation do tags: ["Admin", "Tags"], summary: "List available tags.", operationId: "AdminAPI.TagController.list", + parameters: admin_api_params(), responses: %{ 200 => Operation.response("Array of tags", "application/json", %Schema{ @@ -34,22 +36,22 @@ defmodule Pleroma.Web.ApiSpec.Admin.TagOperation do tags: ["Admin", "Tags"], summary: "Adds tags to users.", operationId: "AdminAPI.TagController.tag", - parameters: [ - Operation.parameter( - :nicknames, - :query, - %Schema{type: :array, items: %Schema{type: :string}}, - "User's nicknames" + parameters: admin_api_params(), + requestBody: + request_body( + "Parameters", + %Schema{ + type: :object, + properties: %{ + nicknames: %Schema{type: :array, items: %Schema{type: :string}}, + tags: %Schema{type: :array, items: %Schema{type: :string}} + } + }, + required: true ), - Operation.parameter( - :tags, - :query, - %Schema{type: :array, items: %Schema{type: :string}}, - "tags" - ) - ], responses: %{ - 200 => empty_object_response() + 204 => no_content_response(), + 400 => Operation.response("Bad request", "application/json", ApiError) }, security: [%{"oAuth" => ["write:accounts"]}] } @@ -60,22 +62,22 @@ defmodule Pleroma.Web.ApiSpec.Admin.TagOperation do tags: ["Admin", "Tags"], summary: "Remove tags from users.", operationId: "AdminAPI.TagController.untag", - parameters: [ - Operation.parameter( - :nicknames, - :query, - %Schema{type: :array, items: %Schema{type: :string}}, - "User's nicknames" + parameters: admin_api_params(), + requestBody: + request_body( + "Parameters", + %Schema{ + type: :object, + properties: %{ + nicknames: %Schema{type: :array, items: %Schema{type: :string}}, + tags: %Schema{type: :array, items: %Schema{type: :string}} + } + }, + required: true ), - Operation.parameter( - :tags, - :query, - %Schema{type: :array, items: %Schema{type: :string}}, - "tags" - ) - ], responses: %{ - 200 => empty_object_response() + 204 => no_content_response(), + 400 => Operation.response("Bad request", "application/json", ApiError) }, security: [%{"oAuth" => ["write:accounts"]}] } diff --git a/test/pleroma/web/admin_api/controllers/tag_controller_test.exs b/test/pleroma/web/admin_api/controllers/tag_controller_test.exs index 5d9963dcb..0c4164d27 100644 --- a/test/pleroma/web/admin_api/controllers/tag_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/tag_controller_test.exs @@ -38,7 +38,7 @@ defmodule Pleroma.Web.AdminAPI.TagControllerTest do response = conn - |> put_req_header("accept", "application/json") + |> put_req_header("content-type", "application/json") |> get("/api/pleroma/admin/users/tag") |> json_response_and_validate_schema(200) @@ -62,24 +62,22 @@ defmodule Pleroma.Web.AdminAPI.TagControllerTest do user2 = insert(:user, %{tags: [build(:tag, name: "y")]}) user3 = insert(:user, %{tags: [build(:tag, name: "unchanged")]}) - conn = - conn - |> put_req_header("accept", "application/json") - |> put( - "/api/pleroma/admin/users/tag?nicknames[]=#{user1.nickname}&nicknames[]=" <> - "#{user2.nickname}&tags[]=foo&tags[]=bar" - ) + assert conn + |> put_req_header("content-type", "application/json") + |> put("/api/pleroma/admin/users/tag", %{ + nicknames: [user1.nickname, user2.nickname], + tags: ["foo", "bar"] + }) + |> json_response_and_validate_schema(204) - %{conn: conn, user1: user1, user2: user2, user3: user3} + %{user1: user1, user2: user2, user3: user3} end test "it appends specified tags to users with specified nicknames", %{ - conn: conn, admin: admin, user1: user1, user2: user2 } do - assert empty_json_response(conn) {:ok, tags} = Repo.get_assoc(User.get_cached_by_id(user1.id), :tags) assert Enum.map(tags, & &1.name) == ["x", "foo", "bar"] {:ok, tags} = Repo.get_assoc(User.get_cached_by_id(user2.id), :tags) @@ -98,8 +96,7 @@ defmodule Pleroma.Web.AdminAPI.TagControllerTest do "@#{admin.nickname} added tags: #{tags} to users: #{users}" end - test "it does not modify tags of not specified users", %{conn: conn, user3: user3} do - assert empty_json_response(conn) + test "it does not modify tags of not specified users", %{user3: user3} do {:ok, tags} = Repo.get_assoc(User.get_cached_by_id(user3.id), :tags) assert Enum.map(tags, & &1.name) == ["unchanged"] end @@ -111,24 +108,22 @@ defmodule Pleroma.Web.AdminAPI.TagControllerTest do user2 = insert(:user, %{tags: [build(:tag, name: "y"), build(:tag, name: "z")]}) user3 = insert(:user, %{tags: [build(:tag, name: "unchanged")]}) - conn = - conn - |> put_req_header("accept", "application/json") - |> delete( - "/api/pleroma/admin/users/tag?nicknames[]=#{user1.nickname}&nicknames[]=" <> - "#{user2.nickname}&tags[]=x&tags[]=z" - ) + assert conn + |> put_req_header("content-type", "application/json") + |> delete( + "/api/pleroma/admin/users/tag", + %{nicknames: [user1.nickname, user2.nickname], tags: ["x", "z"]} + ) + |> json_response_and_validate_schema(204) - %{conn: conn, user1: user1, user2: user2, user3: user3} + %{user1: user1, user2: user2, user3: user3} end test "it removes specified tags from users with specified nicknames", %{ - conn: conn, admin: admin, user1: user1, user2: user2 } do - assert empty_json_response(conn) {:ok, tags} = Repo.get_assoc(User.get_cached_by_id(user1.id), :tags) assert tags == [] {:ok, tags} = Repo.get_assoc(User.get_cached_by_id(user2.id), :tags) @@ -147,8 +142,7 @@ defmodule Pleroma.Web.AdminAPI.TagControllerTest do "@#{admin.nickname} removed tags: #{tags} from users: #{users}" end - test "it does not modify tags of not specified users", %{conn: conn, user3: user3} do - assert empty_json_response(conn) + test "it does not modify tags of not specified users", %{user3: user3} do {:ok, tags} = Repo.get_assoc(User.get_cached_by_id(user3.id), :tags) assert Enum.map(tags, & &1.name) == ["unchanged"] end