improve tests
authorEgor Kislitsyn <egor@kislitsyn.com>
Wed, 9 Jan 2019 12:54:37 +0000 (19:54 +0700)
committerEgor Kislitsyn <egor@kislitsyn.com>
Wed, 9 Jan 2019 12:54:37 +0000 (19:54 +0700)
test/web/common_api/common_api_test.exs
test/web/mastodon_api/mastodon_api_controller_test.exs

index 84b264439fe02c1f94ae00202267803b16f16567..eb69ea4b2682ec88d48ca87b8d490d4e0cf6e8cf 100644 (file)
@@ -1,5 +1,5 @@
 # Pleroma: A lightweight social networking server
-# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
 # 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)
index a3c58379e95e4c24062fc030b0e86f1f7871e27f..b448d13f5323ef069d94b15b68c92a7fafcfe491 100644 (file)
@@ -1,5 +1,5 @@
 # Pleroma: A lightweight social networking server
-# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
 # 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