Merge branch 'feature/account-deletion' into 'develop'
[akkoma] / test / web / mastodon_api / mastodon_api_controller_test.exs
index 5293b9364ee375441a130f0f121e22c5c1718a87..8d79c96b1d558a329df2dfac7fb870716af7c141 100644 (file)
@@ -63,19 +63,53 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
   test "posting a status", %{conn: conn} do
     user = insert(:user)
 
-    conn =
+    idempotency_key = "Pikachu rocks!"
+
+    conn_one =
       conn
       |> assign(:user, user)
+      |> put_req_header("idempotency-key", idempotency_key)
       |> post("/api/v1/statuses", %{
         "status" => "cofe",
         "spoiler_text" => "2hu",
         "sensitive" => "false"
       })
 
+    {:ok, ttl} = Cachex.ttl(:idempotency_cache, idempotency_key)
+    # Six hours
+    assert ttl > :timer.seconds(6 * 60 * 60 - 1)
+
     assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} =
-             json_response(conn, 200)
+             json_response(conn_one, 200)
 
     assert Repo.get(Activity, id)
+
+    conn_two =
+      conn
+      |> assign(:user, user)
+      |> put_req_header("idempotency-key", idempotency_key)
+      |> post("/api/v1/statuses", %{
+        "status" => "cofe",
+        "spoiler_text" => "2hu",
+        "sensitive" => "false"
+      })
+
+    assert %{"id" => second_id} = json_response(conn_two, 200)
+
+    assert id == second_id
+
+    conn_three =
+      conn
+      |> assign(:user, user)
+      |> post("/api/v1/statuses", %{
+        "status" => "cofe",
+        "spoiler_text" => "2hu",
+        "sensitive" => "false"
+      })
+
+    assert %{"id" => third_id} = json_response(conn_three, 200)
+
+    refute id == third_id
   end
 
   test "posting a sensitive status", %{conn: conn} do
@@ -264,6 +298,24 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
     end
   end
 
+  describe "unreblogging" do
+    test "unreblogs and returns the unreblogged status", %{conn: conn} do
+      activity = insert(:note_activity)
+      user = insert(:user)
+
+      {:ok, _, _} = CommonAPI.repeat(activity.id, user)
+
+      conn =
+        conn
+        |> assign(:user, user)
+        |> post("/api/v1/statuses/#{activity.id}/unreblog")
+
+      assert %{"id" => id, "reblogged" => false, "reblogs_count" => 0} = json_response(conn, 200)
+
+      assert to_string(activity.id) == id
+    end
+  end
+
   describe "favoriting" do
     test "favs a status and returns it", %{conn: conn} do
       activity = insert(:note_activity)
@@ -557,16 +609,29 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
 
   test "account search", %{conn: conn} do
     user = insert(:user)
-    _user_two = insert(:user, %{nickname: "shp@shitposter.club"})
+    user_two = insert(:user, %{nickname: "shp@shitposter.club"})
     user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
 
-    conn =
+    results =
+      conn
+      |> assign(:user, user)
+      |> get("/api/v1/accounts/search", %{"q" => "shp"})
+      |> json_response(200)
+
+    result_ids = for result <- results, do: result["acct"]
+
+    assert user_two.nickname in result_ids
+    assert user_three.nickname in result_ids
+
+    results =
       conn
       |> assign(:user, user)
       |> get("/api/v1/accounts/search", %{"q" => "2hu"})
+      |> json_response(200)
 
-    assert [account] = json_response(conn, 200)
-    assert account["id"] == to_string(user_three.id)
+    result_ids = for result <- results, do: result["acct"]
+
+    assert user_three.nickname in result_ids
   end
 
   test "search", %{conn: conn} do
@@ -590,7 +655,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
 
     assert results = json_response(conn, 200)
 
-    [account] = results["accounts"]
+    [account | _] = results["accounts"]
     assert account["id"] == to_string(user_three.id)
 
     assert results["hashtags"] == []