X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=test%2Fpleroma%2Fweb%2Fpleroma_api%2Fcontrollers%2Fchat_controller_test.exs;h=1114da242aad153c53d708a102a82b6fdabfaae6;hb=07ea4d73e12c65cf3fd5c194a8eb1f27900ed17f;hp=99b0d43a7e5d2f60b7dab3804653e9724389c334;hpb=52fc59f125c10ad73b9fd1a0639b6dc5681776ca;p=akkoma diff --git a/test/pleroma/web/pleroma_api/controllers/chat_controller_test.exs b/test/pleroma/web/pleroma_api/controllers/chat_controller_test.exs index 99b0d43a7..1114da242 100644 --- a/test/pleroma/web/pleroma_api/controllers/chat_controller_test.exs +++ b/test/pleroma/web/pleroma_api/controllers/chat_controller_test.exs @@ -106,6 +106,9 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do end test "it works with an attachment", %{conn: conn, user: user} do + clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local) + clear_config([Pleroma.Uploaders.Local, :uploads], "uploads") + file = %Plug.Upload{ content_type: "image/jpeg", path: Path.absname("test/fixtures/image.jpg"), @@ -304,165 +307,147 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do end end - for tested_endpoint <- ["/api/v1/pleroma/chats", "/api/v2/pleroma/chats"] do - describe "GET #{tested_endpoint}" do - setup do: oauth_access(["read:chats"]) + describe "GET /api/v2/pleroma/chats" do + setup do: oauth_access(["read:chats"]) + + test "it does not return chats with deleted users", %{conn: conn, user: user} do + recipient = insert(:user) + {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id) + + Pleroma.Repo.delete(recipient) + User.invalidate_cache(recipient) + + result = + conn + |> get("/api/v2/pleroma/chats") + |> json_response_and_validate_schema(200) + + assert length(result) == 0 + end + + test "it does not return chats with users you blocked", %{conn: conn, user: user} do + recipient = insert(:user) + + {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id) + + result = + conn + |> get("/api/v2/pleroma/chats") + |> json_response_and_validate_schema(200) + + assert length(result) == 1 - test "it does not return chats with deleted users", %{conn: conn, user: user} do + User.block(user, recipient) + + result = + conn + |> get("/api/v2/pleroma/chats") + |> json_response_and_validate_schema(200) + + assert length(result) == 0 + end + + test "it does not return chats with users you muted", %{conn: conn, user: user} do + recipient = insert(:user) + + {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id) + + result = + conn + |> get("/api/v2/pleroma/chats") + |> json_response_and_validate_schema(200) + + assert length(result) == 1 + + User.mute(user, recipient) + + result = + conn + |> get("/api/v2/pleroma/chats") + |> json_response_and_validate_schema(200) + + assert length(result) == 0 + + result = + conn + |> get("/api/v2/pleroma/chats?with_muted=true") + |> json_response_and_validate_schema(200) + + assert length(result) == 1 + end + + test "it paginates chats", %{conn: conn, user: user} do + Enum.each(1..30, fn _ -> recipient = insert(:user) {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id) + end) - Pleroma.Repo.delete(recipient) - User.invalidate_cache(recipient) + result = + conn + |> get("/api/v2/pleroma/chats") + |> json_response_and_validate_schema(200) + + assert length(result) == 20 + last_id = List.last(result)["id"] + + result = + conn + |> get("/api/v2/pleroma/chats?max_id=#{last_id}") + |> json_response_and_validate_schema(200) - result = - conn - |> get(unquote(tested_endpoint)) - |> json_response_and_validate_schema(200) + assert length(result) == 10 + end - assert length(result) == 0 - end + test "it return a list of chats the current user is participating in, in descending order of updates", + %{conn: conn, user: user} do + har = insert(:user) + jafnhar = insert(:user) + tridi = insert(:user) - test "it does not return chats with users you blocked", %{conn: conn, user: user} do - recipient = insert(:user) + {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id) + {:ok, chat_1} = time_travel(chat_1, -3) + {:ok, chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id) + {:ok, _chat_2} = time_travel(chat_2, -2) + {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id) + {:ok, chat_3} = time_travel(chat_3, -1) - {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id) + # bump the second one + {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id) - result = - conn - |> get(unquote(tested_endpoint)) - |> json_response_and_validate_schema(200) + result = + conn + |> get("/api/v2/pleroma/chats") + |> json_response_and_validate_schema(200) - assert length(result) == 1 + ids = Enum.map(result, & &1["id"]) - User.block(user, recipient) + assert ids == [ + chat_2.id |> to_string(), + chat_3.id |> to_string(), + chat_1.id |> to_string() + ] + end - result = - conn - |> get(unquote(tested_endpoint)) - |> json_response_and_validate_schema(200) + test "it is not affected by :restrict_unauthenticated setting (issue #1973)", %{ + conn: conn, + user: user + } do + clear_config([:restrict_unauthenticated, :profiles, :local], true) + clear_config([:restrict_unauthenticated, :profiles, :remote], true) - assert length(result) == 0 - end + user2 = insert(:user) + user3 = insert(:user, local: false) - test "it does not return chats with users you muted", %{conn: conn, user: user} do - recipient = insert(:user) + {:ok, _chat_12} = Chat.get_or_create(user.id, user2.ap_id) + {:ok, _chat_13} = Chat.get_or_create(user.id, user3.ap_id) - {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id) + result = + conn + |> get("/api/v2/pleroma/chats") + |> json_response_and_validate_schema(200) - result = - conn - |> get(unquote(tested_endpoint)) - |> json_response_and_validate_schema(200) - - assert length(result) == 1 - - User.mute(user, recipient) - - result = - conn - |> get(unquote(tested_endpoint)) - |> json_response_and_validate_schema(200) - - assert length(result) == 0 - - result = - conn - |> get("#{unquote(tested_endpoint)}?with_muted=true") - |> json_response_and_validate_schema(200) - - assert length(result) == 1 - end - - if tested_endpoint == "/api/v1/pleroma/chats" do - test "it returns all chats", %{conn: conn, user: user} do - Enum.each(1..30, fn _ -> - recipient = insert(:user) - {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id) - end) - - result = - conn - |> get(unquote(tested_endpoint)) - |> json_response_and_validate_schema(200) - - assert length(result) == 30 - end - else - test "it paginates chats", %{conn: conn, user: user} do - Enum.each(1..30, fn _ -> - recipient = insert(:user) - {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id) - end) - - result = - conn - |> get(unquote(tested_endpoint)) - |> json_response_and_validate_schema(200) - - assert length(result) == 20 - last_id = List.last(result)["id"] - - result = - conn - |> get(unquote(tested_endpoint) <> "?max_id=#{last_id}") - |> json_response_and_validate_schema(200) - - assert length(result) == 10 - end - end - - test "it return a list of chats the current user is participating in, in descending order of updates", - %{conn: conn, user: user} do - har = insert(:user) - jafnhar = insert(:user) - tridi = insert(:user) - - {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id) - {:ok, chat_1} = time_travel(chat_1, -3) - {:ok, chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id) - {:ok, _chat_2} = time_travel(chat_2, -2) - {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id) - {:ok, chat_3} = time_travel(chat_3, -1) - - # bump the second one - {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id) - - result = - conn - |> get(unquote(tested_endpoint)) - |> json_response_and_validate_schema(200) - - ids = Enum.map(result, & &1["id"]) - - assert ids == [ - chat_2.id |> to_string(), - chat_3.id |> to_string(), - chat_1.id |> to_string() - ] - end - - test "it is not affected by :restrict_unauthenticated setting (issue #1973)", %{ - conn: conn, - user: user - } do - clear_config([:restrict_unauthenticated, :profiles, :local], true) - clear_config([:restrict_unauthenticated, :profiles, :remote], true) - - user2 = insert(:user) - user3 = insert(:user, local: false) - - {:ok, _chat_12} = Chat.get_or_create(user.id, user2.ap_id) - {:ok, _chat_13} = Chat.get_or_create(user.id, user3.ap_id) - - result = - conn - |> get(unquote(tested_endpoint)) - |> json_response_and_validate_schema(200) - - account_ids = Enum.map(result, &get_in(&1, ["account", "id"])) - assert Enum.sort(account_ids) == Enum.sort([user2.id, user3.id]) - end + account_ids = Enum.map(result, &get_in(&1, ["account", "id"])) + assert Enum.sort(account_ids) == Enum.sort([user2.id, user3.id]) end end end