ChatView: Add the last message to the view.
[akkoma] / test / web / pleroma_api / views / chat_view_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
5 defmodule Pleroma.Web.PleromaAPI.ChatViewTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Chat
9 alias Pleroma.Object
10 alias Pleroma.Web.CommonAPI
11 alias Pleroma.Web.MastodonAPI.AccountView
12 alias Pleroma.Web.PleromaAPI.ChatView
13 alias Pleroma.Web.PleromaAPI.ChatMessageView
14
15 import Pleroma.Factory
16
17 test "it represents a chat" do
18 user = insert(:user)
19 recipient = insert(:user)
20
21 {:ok, chat} = Chat.get_or_create(user.id, recipient.ap_id)
22
23 represented_chat = ChatView.render("show.json", chat: chat)
24
25 assert represented_chat == %{
26 id: "#{chat.id}",
27 account: AccountView.render("show.json", user: recipient),
28 unread: 0,
29 last_message: nil
30 }
31
32 {:ok, chat_message_creation} = CommonAPI.post_chat_message(user, recipient, "hello")
33
34 chat_message = Object.normalize(chat_message_creation, false)
35
36 {:ok, chat} = Chat.get_or_create(user.id, recipient.ap_id)
37
38 represented_chat = ChatView.render("show.json", chat: chat)
39
40 assert represented_chat[:last_message] ==
41 ChatMessageView.render("show.json", chat: chat, object: chat_message)
42 end
43 end