6b2db5064307c455182c17480c26c25f549a0acb
[akkoma] / test / web / pleroma_api / controllers / chat_controller_test.exs
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
6
7 alias Pleroma.Chat
8 alias Pleroma.Web.CommonAPI
9
10 import Pleroma.Factory
11
12 describe "GET /api/v1/pleroma/chats/:id/messages" do
13 # TODO
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
18 user = insert(:user)
19 other_user = insert(:user)
20 third_user = insert(:user)
21
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?")
26
27 chat = Chat.get(user.id, other_user.ap_id)
28
29 result =
30 conn
31 |> assign(:user, user)
32 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
33 |> json_response(200)
34
35 assert length(result) == 3
36 end
37 end
38
39 describe "POST /api/v1/pleroma/chats/by-ap-id/:id" do
40 test "it creates or returns a chat", %{conn: conn} do
41 user = insert(:user)
42 other_user = insert(:user)
43
44 result =
45 conn
46 |> assign(:user, user)
47 |> post("/api/v1/pleroma/chats/by-ap-id/#{URI.encode_www_form(other_user.ap_id)}")
48 |> json_response(200)
49
50 assert result["id"]
51 end
52 end
53
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",
56 %{conn: conn} do
57 user = insert(:user)
58 har = insert(:user)
59 jafnhar = insert(:user)
60 tridi = insert(:user)
61
62 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
63 :timer.sleep(1000)
64 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
65 :timer.sleep(1000)
66 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
67 :timer.sleep(1000)
68
69 # bump the second one
70 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
71
72 result =
73 conn
74 |> assign(:user, user)
75 |> get("/api/v1/pleroma/chats")
76 |> json_response(200)
77
78 ids = Enum.map(result, & &1["id"])
79
80 assert ids == [chat_2.id, chat_3.id, chat_1.id]
81 end
82 end
83 end