1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.ChatTest do
6 use Pleroma.DataCase, async: true
10 import Pleroma.Factory
12 describe "creation and getting" do
13 test "it only works if the recipient is a valid user (for now)" do
16 assert {:error, _chat} = Chat.bump_or_create(user.id, "http://some/nonexisting/account")
17 assert {:error, _chat} = Chat.get_or_create(user.id, "http://some/nonexisting/account")
20 test "it creates a chat for a user and recipient" do
22 other_user = insert(:user)
24 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
29 test "deleting the user deletes the chat" do
31 other_user = insert(:user)
33 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
37 refute Chat.get_by_id(chat.id)
40 test "deleting the recipient deletes the chat" do
42 other_user = insert(:user)
44 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
46 Repo.delete(other_user)
48 refute Chat.get_by_id(chat.id)
51 test "it returns and bumps a chat for a user and recipient if it already exists" do
53 other_user = insert(:user)
55 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
56 {:ok, chat_two} = Chat.bump_or_create(user.id, other_user.ap_id)
58 assert chat.id == chat_two.id
61 test "it returns a chat for a user and recipient if it already exists" do
63 other_user = insert(:user)
65 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
66 {:ok, chat_two} = Chat.get_or_create(user.id, other_user.ap_id)
68 assert chat.id == chat_two.id
71 test "a returning chat will have an updated `update_at` field" do
73 other_user = insert(:user)
75 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
76 {:ok, chat} = time_travel(chat, -2)
78 {:ok, chat_two} = Chat.bump_or_create(user.id, other_user.ap_id)
80 assert chat.id == chat_two.id
81 assert chat.updated_at != chat_two.updated_at