From 826fc446d56b48b67e97144c74bbf74109fb8168 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Tue, 4 Dec 2018 18:35:57 +0300 Subject: [#210] TwitterAPI: implemented /api/media/metadata/create to allow uploads description (alt text) setting. --- test/web/twitter_api/twitter_api_controller_test.exs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'test/web/twitter_api/twitter_api_controller_test.exs') diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index a6495ffc1..8faa4b58e 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -1253,4 +1253,24 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do assert [user.id, user_two.id, user_three.id] == Enum.map(resp, fn %{"id" => id} -> id end) end end + + describe "POST /api/media/metadata/create" do + test "it updates `data[name]` of referenced Object with provided value", %{conn: conn} do + user = insert(:user) + object = insert(:note) + description = "Informative description of the image. Initial: #{object.data["name"]}}" + + _conn = + conn + |> assign(:user, user) + |> post("/api/media/metadata/create.json", %{ + "media_id" => object.id, + "alt_text" => %{"text" => description} + }) + |> json_response(:no_content) + + object = Repo.get!(Object, object.id) + assert object.data["name"] == description + end + end end -- cgit v1.2.3 From 44ab3dbe2c3d25a1772b99679653eaf96d4fbd8b Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Tue, 4 Dec 2018 19:45:09 +0300 Subject: [#210] Refactoring. --- test/web/twitter_api/twitter_api_controller_test.exs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'test/web/twitter_api/twitter_api_controller_test.exs') diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 8faa4b58e..21e844124 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -1260,14 +1260,13 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do object = insert(:note) description = "Informative description of the image. Initial: #{object.data["name"]}}" - _conn = - conn - |> assign(:user, user) - |> post("/api/media/metadata/create.json", %{ - "media_id" => object.id, - "alt_text" => %{"text" => description} - }) - |> json_response(:no_content) + conn + |> assign(:user, user) + |> post("/api/media/metadata/create.json", %{ + "media_id" => object.id, + "alt_text" => %{"text" => description} + }) + |> json_response(:no_content) object = Repo.get!(Object, object.id) assert object.data["name"] == description -- cgit v1.2.3 From 53797d19c5e8463388862eaa20931c8cb78d66a6 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Wed, 5 Dec 2018 11:56:31 +0300 Subject: [#210] Test update (replaced bang- with non-bang method). --- test/web/twitter_api/twitter_api_controller_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/web/twitter_api/twitter_api_controller_test.exs') diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 21e844124..478763de7 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -1268,7 +1268,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do }) |> json_response(:no_content) - object = Repo.get!(Object, object.id) + object = Repo.get(Object, object.id) assert object.data["name"] == description end end -- cgit v1.2.3 From 848151f7cbf372d008c178d13c9a74942164c955 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Wed, 5 Dec 2018 13:37:06 +0300 Subject: [#210] [TwitterAPI] Made actor be stored for uploads. Added ownership check to `update_media` action. Added controller tests for `upload` and `update_media` actions. Refactoring. --- .../twitter_api/twitter_api_controller_test.exs | 67 ++++++++++++++++++++-- 1 file changed, 63 insertions(+), 4 deletions(-) (limited to 'test/web/twitter_api/twitter_api_controller_test.exs') diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 478763de7..c07dc6912 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -1254,15 +1254,74 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do end end - describe "POST /api/media/metadata/create" do - test "it updates `data[name]` of referenced Object with provided value", %{conn: conn} do + describe "POST /api/media/upload" do + setup context do + Pleroma.DataCase.ensure_local_uploader(context) + end + + test "it performs the upload and sets `data[actor]` with AP id of uploader user", %{ + conn: conn + } do user = insert(:user) + + upload_filename = "test/fixtures/image_tmp.jpg" + File.cp!("test/fixtures/image.jpg", upload_filename) + + file = %Plug.Upload{ + content_type: "image/jpg", + path: Path.absname(upload_filename), + filename: "image.jpg" + } + + response = + conn + |> assign(:user, user) + |> put_req_header("content-type", "application/octet-stream") + |> post("/api/media/upload", %{ + "media" => file + }) + |> json_response(:ok) + + assert response["media_id"] + object = Repo.get(Object, response["media_id"]) + assert object + assert object.data["actor"] == User.ap_id(user) + end + end + + describe "POST /api/media/metadata/create" do + setup do object = insert(:note) - description = "Informative description of the image. Initial: #{object.data["name"]}}" + user = User.get_by_ap_id(object.data["actor"]) + %{object: object, user: user} + end + + test "it returns :forbidden status on attempt to modify someone else's upload", %{ + conn: conn, + object: object + } do + initial_description = object.data["name"] + another_user = insert(:user) + + conn + |> assign(:user, another_user) + |> post("/api/media/metadata/create", %{"media_id" => object.id}) + |> json_response(:forbidden) + + object = Repo.get(Object, object.id) + assert object.data["name"] == initial_description + end + + test "it updates `data[name]` of referenced Object with provided value", %{ + conn: conn, + object: object, + user: user + } do + description = "Informative description of the image. Initial value: #{object.data["name"]}}" conn |> assign(:user, user) - |> post("/api/media/metadata/create.json", %{ + |> post("/api/media/metadata/create", %{ "media_id" => object.id, "alt_text" => %{"text" => description} }) -- cgit v1.2.3