From 6cbe63726d298ae85a75efa7591a54394469525e Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Wed, 9 Jan 2019 19:54:37 +0700 Subject: [PATCH] improve tests --- test/web/common_api/common_api_test.exs | 32 ++++------ .../mastodon_api_controller_test.exs | 59 +++++++------------ 2 files changed, 34 insertions(+), 57 deletions(-) diff --git a/test/web/common_api/common_api_test.exs b/test/web/common_api/common_api_test.exs index 84b264439..eb69ea4b2 100644 --- a/test/web/common_api/common_api_test.exs +++ b/test/web/common_api/common_api_test.exs @@ -1,5 +1,5 @@ # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors +# Copyright © 2017-2019 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.CommonAPI.Test do @@ -98,30 +98,26 @@ defmodule Pleroma.Web.CommonAPI.Test do end describe "pinned statuses" do - test "pin status" do + setup do Pleroma.Config.put([:instance, :max_pinned_statuses], 1) - user = insert(:user) + user = insert(:user) {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) - assert {:ok, ^activity} = CommonAPI.pin(activity.id, user) + [user: user, activity: activity] end - test "only self-authored can be pinned" do - Pleroma.Config.put([:instance, :max_pinned_statuses], 1) - user_one = insert(:user) - user_two = insert(:user) - - {:ok, activity} = CommonAPI.post(user_one, %{"status" => "HI!!!"}) - - assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user_two) + test "pin status", %{user: user, activity: activity} do + assert {:ok, ^activity} = CommonAPI.pin(activity.id, user) end - test "max pinned statuses" do - Pleroma.Config.put([:instance, :max_pinned_statuses], 1) + test "only self-authored can be pinned", %{activity: activity} do user = insert(:user) - {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"}) + assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user) + end + + test "max pinned statuses", %{user: user, activity: activity_one} do {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"}) assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user) @@ -132,11 +128,7 @@ defmodule Pleroma.Web.CommonAPI.Test do CommonAPI.pin(activity_two.id, user) end - test "unpin status" do - Pleroma.Config.put([:instance, :max_pinned_statuses], 1) - user = insert(:user) - - {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) + test "unpin status", %{user: user, activity: activity} do {:ok, activity} = CommonAPI.pin(activity.id, user) assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user) diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index a3c58379e..b448d13f5 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -1,5 +1,5 @@ # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors +# Copyright © 2017-2019 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do @@ -1473,88 +1473,74 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do end describe "pinned statuses" do - test "returns pinned statuses", %{conn: conn} do + setup do Pleroma.Config.put([:instance, :max_pinned_statuses], 1) - user = insert(:user) + user = insert(:user) {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) + + [user: user, activity: activity] + end + + test "returns pinned statuses", %{conn: conn, user: user, activity: activity} do {:ok, _} = CommonAPI.pin(activity.id, user) result = conn |> assign(:user, user) |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true") - |> Map.get(:resp_body) - |> Jason.decode!() + |> json_response(200) - id_str = Integer.to_string(activity.id) + id_str = to_string(activity.id) assert [%{"id" => ^id_str, "pinned" => true}] = result end - test "pin status", %{conn: conn} do - Pleroma.Config.put([:instance, :max_pinned_statuses], 1) - user = insert(:user) - - {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) - id_str = Integer.to_string(activity.id) + test "pin status", %{conn: conn, user: user, activity: activity} do + id_str = to_string(activity.id) assert %{"id" => ^id_str, "pinned" => true} = conn |> assign(:user, user) |> post("/api/v1/statuses/#{activity.id}/pin") - |> Map.get(:resp_body) - |> Jason.decode!() + |> json_response(200) assert [%{"id" => ^id_str, "pinned" => true}] = conn |> assign(:user, user) |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true") - |> Map.get(:resp_body) - |> Jason.decode!() + |> json_response(200) end - test "unpin status", %{conn: conn} do - Pleroma.Config.put([:instance, :max_pinned_statuses], 1) - user = insert(:user) - - {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) + test "unpin status", %{conn: conn, user: user, activity: activity} do {:ok, _} = CommonAPI.pin(activity.id, user) - id_str = Integer.to_string(activity.id) + id_str = to_string(activity.id) user = refresh_record(user) assert %{"id" => ^id_str, "pinned" => false} = conn |> assign(:user, user) |> post("/api/v1/statuses/#{activity.id}/unpin") - |> Map.get(:resp_body) - |> Jason.decode!() + |> json_response(200) assert [] = conn |> assign(:user, user) |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true") - |> Map.get(:resp_body) - |> Jason.decode!() + |> json_response(200) end - test "max pinned statuses", %{conn: conn} do - Pleroma.Config.put([:instance, :max_pinned_statuses], 1) - - user = insert(:user) - - {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"}) + test "max pinned statuses", %{conn: conn, user: user, activity: activity_one} do {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"}) - id_str_one = Integer.to_string(activity_one.id) + id_str_one = to_string(activity_one.id) assert %{"id" => ^id_str_one, "pinned" => true} = conn |> assign(:user, user) |> post("/api/v1/statuses/#{id_str_one}/pin") - |> Map.get(:resp_body) - |> Jason.decode!() + |> json_response(200) user = refresh_record(user) @@ -1562,8 +1548,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do conn |> assign(:user, user) |> post("/api/v1/statuses/#{activity_two.id}/pin") - |> Map.get(:resp_body) - |> Jason.decode!() + |> json_response(400) end end end -- 2.45.2