Update majic & call plug before OpenApiSpex
[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.Chat.MessageReference
10 alias Pleroma.Object
11 alias Pleroma.Web.CommonAPI
12 alias Pleroma.Web.CommonAPI.Utils
13 alias Pleroma.Web.MastodonAPI.AccountView
14 alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
15 alias Pleroma.Web.PleromaAPI.ChatView
16
17 import Pleroma.Factory
18
19 test "it represents a chat" do
20 user = insert(:user)
21 recipient = insert(:user)
22
23 {:ok, chat} = Chat.get_or_create(user.id, recipient.ap_id)
24
25 represented_chat = ChatView.render("show.json", chat: chat)
26
27 assert represented_chat == %{
28 id: "#{chat.id}",
29 account: AccountView.render("show.json", user: recipient),
30 unread: 0,
31 last_message: nil,
32 updated_at: Utils.to_masto_date(chat.updated_at)
33 }
34
35 {:ok, chat_message_creation} = CommonAPI.post_chat_message(user, recipient, "hello")
36
37 chat_message = Object.normalize(chat_message_creation, false)
38
39 {:ok, chat} = Chat.get_or_create(user.id, recipient.ap_id)
40
41 represented_chat = ChatView.render("show.json", chat: chat)
42
43 cm_ref = MessageReference.for_chat_and_object(chat, chat_message)
44
45 assert represented_chat[:last_message] ==
46 MessageReferenceView.render("show.json", chat_message_reference: cm_ref)
47 end
48 end