ChatController: Add creation and return of chats.
[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
9 import Pleroma.Factory
10
11 describe "POST /api/v1/pleroma/chats/by-ap-id/:id" do
12 test "it creates or returns a chat", %{conn: conn} do
13 user = insert(:user)
14 other_user = insert(:user)
15
16 result =
17 conn
18 |> assign(:user, user)
19 |> post("/api/v1/pleroma/chats/by-ap-id/#{URI.encode_www_form(other_user.ap_id)}")
20 |> json_response(200)
21
22 assert result["id"]
23 end
24 end
25
26 describe "GET /api/v1/pleroma/chats" do
27 test "it return a list of chats the current user is participating in, in descending order of updates",
28 %{conn: conn} do
29 user = insert(:user)
30 har = insert(:user)
31 jafnhar = insert(:user)
32 tridi = insert(:user)
33
34 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
35 :timer.sleep(1000)
36 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
37 :timer.sleep(1000)
38 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
39 :timer.sleep(1000)
40
41 # bump the second one
42 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
43
44 result =
45 conn
46 |> assign(:user, user)
47 |> get("/api/v1/pleroma/chats")
48 |> json_response(200)
49
50 ids = Enum.map(result, & &1["id"])
51
52 assert ids == [chat_2.id, chat_3.id, chat_1.id]
53 end
54 end
55 end