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 "POST /api/v1/pleroma/chats/:id/messages" do
13 test "it posts a message to the chat", %{conn: conn} do
15 other_user = insert(:user)
17 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
21 |> assign(:user, user)
22 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
25 assert result["content"] == "Hallo!!"
29 describe "GET /api/v1/pleroma/chats/:id/messages" do
31 # - Test that statuses don't show
32 # - Test the case where it's not the user's chat
33 # - Test the returned data
34 test "it returns the messages for a given chat", %{conn: conn} do
36 other_user = insert(:user)
37 third_user = insert(:user)
39 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
40 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
41 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
42 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
44 chat = Chat.get(user.id, other_user.ap_id)
48 |> assign(:user, user)
49 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
52 assert length(result) == 3
56 describe "POST /api/v1/pleroma/chats/by-ap-id/:id" do
57 test "it creates or returns a chat", %{conn: conn} do
59 other_user = insert(:user)
63 |> assign(:user, user)
64 |> post("/api/v1/pleroma/chats/by-ap-id/#{URI.encode_www_form(other_user.ap_id)}")
71 describe "GET /api/v1/pleroma/chats" do
72 test "it return a list of chats the current user is participating in, in descending order of updates",
76 jafnhar = insert(:user)
79 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
81 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
83 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
87 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
91 |> assign(:user, user)
92 |> get("/api/v1/pleroma/chats")
95 ids = Enum.map(result, & &1["id"])
97 assert ids == [chat_2.id, chat_3.id, chat_1.id]