Fix tests
[akkoma] / test / conversation / participation_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Conversation.ParticipationTest do
6 use Pleroma.DataCase
7 import Pleroma.Factory
8 alias Pleroma.Conversation.Participation
9 alias Pleroma.Web.CommonAPI
10
11 test "it creates a participation for a conversation and a user" do
12 user = insert(:user)
13 conversation = insert(:conversation)
14
15 {:ok, %Participation{} = participation} =
16 Participation.create_for_user_and_conversation(user, conversation)
17
18 assert participation.user_id == user.id
19 assert participation.conversation_id == conversation.id
20
21 :timer.sleep(1000)
22 # Creating again returns the same participation
23 {:ok, %Participation{} = participation_two} =
24 Participation.create_for_user_and_conversation(user, conversation)
25
26 assert participation.id == participation_two.id
27 refute participation.updated_at == participation_two.updated_at
28 end
29
30 test "recreating an existing participations sets it to unread" do
31 participation = insert(:participation, %{read: true})
32
33 {:ok, participation} =
34 Participation.create_for_user_and_conversation(
35 participation.user,
36 participation.conversation
37 )
38
39 refute participation.read
40 end
41
42 test "it marks a participation as read" do
43 participation = insert(:participation, %{read: false})
44 {:ok, participation} = Participation.mark_as_read(participation)
45
46 assert participation.read
47 end
48
49 test "it marks a participation as unread" do
50 participation = insert(:participation, %{read: true})
51 {:ok, participation} = Participation.mark_as_unread(participation)
52
53 refute participation.read
54 end
55
56 test "gets all the participations for a user, ordered by updated at descending" do
57 user = insert(:user)
58 {:ok, activity_one} = CommonAPI.post(user, %{"status" => "x", "visibility" => "direct"})
59 :timer.sleep(1000)
60 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "x", "visibility" => "direct"})
61 :timer.sleep(1000)
62
63 {:ok, activity_three} =
64 CommonAPI.post(user, %{
65 "status" => "x",
66 "visibility" => "direct",
67 "in_reply_to_status_id" => activity_one.id
68 })
69
70 assert [participation_one, participation_two] = Participation.for_user(user)
71
72 object2 = Pleroma.Object.normalize(activity_two)
73 object3 = Pleroma.Object.normalize(activity_three)
74
75 assert participation_one.conversation.ap_id == object3.data["context"]
76 assert participation_two.conversation.ap_id == object2.data["context"]
77
78 # Pagination
79 assert [participation_one] = Participation.for_user(user, %{"limit" => 1})
80
81 assert participation_one.conversation.ap_id == object3.data["context"]
82
83 # With last_activity_id
84 assert [participation_one] =
85 Participation.for_user_with_last_activity_id(user, %{"limit" => 1})
86
87 assert participation_one.last_activity_id == activity_three.id
88 end
89 end