1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.ChatTest do
6 use Pleroma.DataCase, async: true
9 alias Pleroma.Web.CommonAPI
11 import Pleroma.Factory
13 describe "creation and getting" do
14 test "it only works if the recipient is a valid user (for now)" do
17 assert {:error, _chat} = Chat.bump_or_create(user.id, "http://some/nonexisting/account")
18 assert {:error, _chat} = Chat.get_or_create(user.id, "http://some/nonexisting/account")
21 test "it creates a chat for a user and recipient" do
23 other_user = insert(:user)
25 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
30 test "it returns and bumps a chat for a user and recipient if it already exists" do
32 other_user = insert(:user)
34 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
35 {:ok, chat_two} = Chat.bump_or_create(user.id, other_user.ap_id)
37 assert chat.id == chat_two.id
38 assert chat_two.unread == 2
41 test "it returns a chat for a user and recipient if it already exists" do
43 other_user = insert(:user)
45 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
46 {:ok, chat_two} = Chat.get_or_create(user.id, other_user.ap_id)
48 assert chat.id == chat_two.id
51 test "a returning chat will have an updated `update_at` field and an incremented unread count" do
53 other_user = insert(:user)
55 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
56 assert chat.unread == 1
58 {:ok, chat_two} = Chat.bump_or_create(user.id, other_user.ap_id)
59 assert chat_two.unread == 2
61 assert chat.id == chat_two.id
62 assert chat.updated_at != chat_two.updated_at