1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4 defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
5 use Pleroma.Web.ConnCase, async: true
8 alias Pleroma.Web.CommonAPI
10 import Pleroma.Factory
12 describe "GET /api/v1/pleroma/chats/:id/messages" do
14 # - Test that statuses don't show
15 # - Test the case where it's not the user's chat
16 # - Test the returned data
17 test "it returns the messages for a given chat", %{conn: conn} do
19 other_user = insert(:user)
20 third_user = insert(:user)
22 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
23 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
24 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
25 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
27 chat = Chat.get(user.id, other_user.ap_id)
31 |> assign(:user, user)
32 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
35 assert length(result) == 3
39 describe "POST /api/v1/pleroma/chats/by-ap-id/:id" do
40 test "it creates or returns a chat", %{conn: conn} do
42 other_user = insert(:user)
46 |> assign(:user, user)
47 |> post("/api/v1/pleroma/chats/by-ap-id/#{URI.encode_www_form(other_user.ap_id)}")
54 describe "GET /api/v1/pleroma/chats" do
55 test "it return a list of chats the current user is participating in, in descending order of updates",
59 jafnhar = insert(:user)
62 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
64 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
66 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
70 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
74 |> assign(:user, user)
75 |> get("/api/v1/pleroma/chats")
78 ids = Enum.map(result, & &1["id"])
80 assert ids == [chat_2.id, chat_3.id, chat_1.id]