transmogrifier: Drop incoming create early if it already exists
[akkoma] / test / chat_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.ChatTest do
6 use Pleroma.DataCase, async: true
7
8 alias Pleroma.Chat
9
10 import Pleroma.Factory
11
12 describe "creation and getting" do
13 test "it only works if the recipient is a valid user (for now)" do
14 user = insert(:user)
15
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")
18 end
19
20 test "it creates a chat for a user and recipient" do
21 user = insert(:user)
22 other_user = insert(:user)
23
24 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
25
26 assert chat.id
27 end
28
29 test "deleting the user deletes the chat" do
30 user = insert(:user)
31 other_user = insert(:user)
32
33 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
34
35 Repo.delete(user)
36
37 refute Chat.get_by_id(chat.id)
38 end
39
40 test "deleting the recipient deletes the chat" do
41 user = insert(:user)
42 other_user = insert(:user)
43
44 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
45
46 Repo.delete(other_user)
47
48 refute Chat.get_by_id(chat.id)
49 end
50
51 test "it returns and bumps a chat for a user and recipient if it already exists" do
52 user = insert(:user)
53 other_user = insert(:user)
54
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)
57
58 assert chat.id == chat_two.id
59 end
60
61 test "it returns a chat for a user and recipient if it already exists" do
62 user = insert(:user)
63 other_user = insert(:user)
64
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)
67
68 assert chat.id == chat_two.id
69 end
70
71 test "a returning chat will have an updated `update_at` field" do
72 user = insert(:user)
73 other_user = insert(:user)
74
75 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
76 :timer.sleep(1500)
77 {:ok, chat_two} = Chat.bump_or_create(user.id, other_user.ap_id)
78
79 assert chat.id == chat_two.id
80 assert chat.updated_at != chat_two.updated_at
81 end
82 end
83 end