X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=test%2Fweb%2Fmastodon_api%2Fcontrollers%2Fconversation_controller_test.exs;h=3e21e6bf178ddab076fbb7f2ebbb40cefbb81be1;hb=48f8b26c92880c0898daac3d691c61be0b891d0b;hp=4bb9781a6a27ff8d203363f90305dd8140bb89d4;hpb=8b4d81609d5627d62b826bcd3e87290cb513495f;p=akkoma
diff --git a/test/web/mastodon_api/controllers/conversation_controller_test.exs b/test/web/mastodon_api/controllers/conversation_controller_test.exs
index 4bb9781a6..3e21e6bf1 100644
--- a/test/web/mastodon_api/controllers/conversation_controller_test.exs
+++ b/test/web/mastodon_api/controllers/conversation_controller_test.exs
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do
@@ -12,133 +12,127 @@ defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do
setup do: oauth_access(["read:statuses"])
- test "returns a list of conversations", %{user: user_one, conn: conn} do
- user_two = insert(:user)
- user_three = insert(:user)
-
- {:ok, user_two} = User.follow(user_two, user_one)
-
- assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
-
- {:ok, direct} =
- CommonAPI.post(user_one, %{
- "status" => "Hi @#{user_two.nickname}, @#{user_three.nickname}!",
- "visibility" => "direct"
- })
-
- assert User.get_cached_by_id(user_two.id).unread_conversation_count == 1
-
- {:ok, _follower_only} =
- CommonAPI.post(user_one, %{
- "status" => "Hi @#{user_two.nickname}!",
- "visibility" => "private"
- })
-
- res_conn = get(conn, "/api/v1/conversations")
-
- assert response = json_response(res_conn, 200)
-
- assert [
- %{
- "id" => res_id,
- "accounts" => res_accounts,
- "last_status" => res_last_status,
- "unread" => unread
- }
- ] = response
-
- account_ids = Enum.map(res_accounts, & &1["id"])
- assert length(res_accounts) == 2
- assert user_two.id in account_ids
- assert user_three.id in account_ids
- assert is_binary(res_id)
- assert unread == false
- assert res_last_status["id"] == direct.id
- assert User.get_cached_by_id(user_one.id).unread_conversation_count == 0
+ describe "returns a list of conversations" do
+ setup(%{user: user_one, conn: conn}) do
+ user_two = insert(:user)
+ user_three = insert(:user)
+
+ {:ok, user_two} = User.follow(user_two, user_one)
+
+ {:ok, %{user: user_one, user_two: user_two, user_three: user_three, conn: conn}}
+ end
+
+ test "returns correct conversations", %{
+ user: user_one,
+ user_two: user_two,
+ user_three: user_three,
+ conn: conn
+ } do
+ assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
+ {:ok, direct} = create_direct_message(user_one, [user_two, user_three])
+
+ assert User.get_cached_by_id(user_two.id).unread_conversation_count == 1
+
+ {:ok, _follower_only} =
+ CommonAPI.post(user_one, %{
+ status: "Hi @#{user_two.nickname}!",
+ visibility: "private"
+ })
+
+ res_conn = get(conn, "/api/v1/conversations")
+
+ assert response = json_response_and_validate_schema(res_conn, 200)
+
+ assert [
+ %{
+ "id" => res_id,
+ "accounts" => res_accounts,
+ "last_status" => res_last_status,
+ "unread" => unread
+ }
+ ] = response
+
+ account_ids = Enum.map(res_accounts, & &1["id"])
+ assert length(res_accounts) == 2
+ assert user_two.id in account_ids
+ assert user_three.id in account_ids
+ assert is_binary(res_id)
+ assert unread == false
+ assert res_last_status["id"] == direct.id
+ assert User.get_cached_by_id(user_one.id).unread_conversation_count == 0
+ end
+
+ test "observes limit params", %{
+ user: user_one,
+ user_two: user_two,
+ user_three: user_three,
+ conn: conn
+ } do
+ {:ok, _} = create_direct_message(user_one, [user_two, user_three])
+ {:ok, _} = create_direct_message(user_two, [user_one, user_three])
+ {:ok, _} = create_direct_message(user_three, [user_two, user_one])
+
+ res_conn = get(conn, "/api/v1/conversations?limit=1")
+
+ assert response = json_response_and_validate_schema(res_conn, 200)
+
+ assert Enum.count(response) == 1
+
+ res_conn = get(conn, "/api/v1/conversations?limit=2")
+
+ assert response = json_response_and_validate_schema(res_conn, 200)
+
+ assert Enum.count(response) == 2
+ end
end
test "filters conversations by recipients", %{user: user_one, conn: conn} do
user_two = insert(:user)
user_three = insert(:user)
+ {:ok, direct1} = create_direct_message(user_one, [user_two])
+ {:ok, _direct2} = create_direct_message(user_one, [user_three])
+ {:ok, direct3} = create_direct_message(user_one, [user_two, user_three])
+ {:ok, _direct4} = create_direct_message(user_two, [user_three])
+ {:ok, direct5} = create_direct_message(user_two, [user_one])
- {:ok, direct1} =
- CommonAPI.post(user_one, %{
- "status" => "Hi @#{user_two.nickname}!",
- "visibility" => "direct"
- })
-
- {:ok, _direct2} =
- CommonAPI.post(user_one, %{
- "status" => "Hi @#{user_three.nickname}!",
- "visibility" => "direct"
- })
-
- {:ok, direct3} =
- CommonAPI.post(user_one, %{
- "status" => "Hi @#{user_two.nickname}, @#{user_three.nickname}!",
- "visibility" => "direct"
- })
-
- {:ok, _direct4} =
- CommonAPI.post(user_two, %{
- "status" => "Hi @#{user_three.nickname}!",
- "visibility" => "direct"
- })
-
- {:ok, direct5} =
- CommonAPI.post(user_two, %{
- "status" => "Hi @#{user_one.nickname}!",
- "visibility" => "direct"
- })
-
- [conversation1, conversation2] =
- conn
- |> get("/api/v1/conversations", %{"recipients" => [user_two.id]})
- |> json_response(200)
+ assert [conversation1, conversation2] =
+ conn
+ |> get("/api/v1/conversations?recipients[]=#{user_two.id}")
+ |> json_response_and_validate_schema(200)
assert conversation1["last_status"]["id"] == direct5.id
assert conversation2["last_status"]["id"] == direct1.id
[conversation1] =
conn
- |> get("/api/v1/conversations", %{"recipients" => [user_two.id, user_three.id]})
- |> json_response(200)
+ |> get("/api/v1/conversations?recipients[]=#{user_two.id}&recipients[]=#{user_three.id}")
+ |> json_response_and_validate_schema(200)
assert conversation1["last_status"]["id"] == direct3.id
end
test "updates the last_status on reply", %{user: user_one, conn: conn} do
user_two = insert(:user)
-
- {:ok, direct} =
- CommonAPI.post(user_one, %{
- "status" => "Hi @#{user_two.nickname}",
- "visibility" => "direct"
- })
+ {:ok, direct} = create_direct_message(user_one, [user_two])
{:ok, direct_reply} =
CommonAPI.post(user_two, %{
- "status" => "reply",
- "visibility" => "direct",
- "in_reply_to_status_id" => direct.id
+ status: "reply",
+ visibility: "direct",
+ in_reply_to_status_id: direct.id
})
[%{"last_status" => res_last_status}] =
conn
|> get("/api/v1/conversations")
- |> json_response(200)
+ |> json_response_and_validate_schema(200)
assert res_last_status["id"] == direct_reply.id
end
test "the user marks a conversation as read", %{user: user_one, conn: conn} do
user_two = insert(:user)
-
- {:ok, direct} =
- CommonAPI.post(user_one, %{
- "status" => "Hi @#{user_two.nickname}",
- "visibility" => "direct"
- })
+ {:ok, direct} = create_direct_message(user_one, [user_two])
assert User.get_cached_by_id(user_one.id).unread_conversation_count == 0
assert User.get_cached_by_id(user_two.id).unread_conversation_count == 1
@@ -154,12 +148,12 @@ defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do
[%{"id" => direct_conversation_id, "unread" => true}] =
user_two_conn
|> get("/api/v1/conversations")
- |> json_response(200)
+ |> json_response_and_validate_schema(200)
%{"unread" => false} =
user_two_conn
|> post("/api/v1/conversations/#{direct_conversation_id}/read")
- |> json_response(200)
+ |> json_response_and_validate_schema(200)
assert User.get_cached_by_id(user_one.id).unread_conversation_count == 0
assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
@@ -167,15 +161,15 @@ defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do
# The conversation is marked as unread on reply
{:ok, _} =
CommonAPI.post(user_two, %{
- "status" => "reply",
- "visibility" => "direct",
- "in_reply_to_status_id" => direct.id
+ status: "reply",
+ visibility: "direct",
+ in_reply_to_status_id: direct.id
})
[%{"unread" => true}] =
conn
|> get("/api/v1/conversations")
- |> json_response(200)
+ |> json_response_and_validate_schema(200)
assert User.get_cached_by_id(user_one.id).unread_conversation_count == 1
assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
@@ -183,9 +177,9 @@ defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do
# A reply doesn't increment the user's unread_conversation_count if the conversation is unread
{:ok, _} =
CommonAPI.post(user_two, %{
- "status" => "reply",
- "visibility" => "direct",
- "in_reply_to_status_id" => direct.id
+ status: "reply",
+ visibility: "direct",
+ in_reply_to_status_id: direct.id
})
assert User.get_cached_by_id(user_one.id).unread_conversation_count == 1
@@ -194,15 +188,22 @@ defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do
test "(vanilla) Mastodon frontend behaviour", %{user: user_one, conn: conn} do
user_two = insert(:user)
-
- {:ok, direct} =
- CommonAPI.post(user_one, %{
- "status" => "Hi @#{user_two.nickname}!",
- "visibility" => "direct"
- })
+ {:ok, direct} = create_direct_message(user_one, [user_two])
res_conn = get(conn, "/api/v1/statuses/#{direct.id}/context")
assert %{"ancestors" => [], "descendants" => []} == json_response(res_conn, 200)
end
+
+ defp create_direct_message(sender, recips) do
+ hellos =
+ recips
+ |> Enum.map(fn s -> "@#{s.nickname}" end)
+ |> Enum.join(", ")
+
+ CommonAPI.post(sender, %{
+ status: "Hi #{hellos}!",
+ visibility: "direct"
+ })
+ end
end