Merge branch 'fix/mrf-delete' into 'develop'
[akkoma] / test / user_relationship_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.UserRelationshipTest do
6 alias Pleroma.UserRelationship
7
8 use Pleroma.DataCase
9
10 import Pleroma.Factory
11
12 describe "*_exists?/2" do
13 setup do
14 {:ok, users: insert_list(2, :user)}
15 end
16
17 test "returns false if record doesn't exist", %{users: [user1, user2]} do
18 refute UserRelationship.block_exists?(user1, user2)
19 refute UserRelationship.mute_exists?(user1, user2)
20 refute UserRelationship.notification_mute_exists?(user1, user2)
21 refute UserRelationship.reblog_mute_exists?(user1, user2)
22 refute UserRelationship.inverse_subscription_exists?(user1, user2)
23 end
24
25 test "returns true if record exists", %{users: [user1, user2]} do
26 for relationship_type <- [
27 :block,
28 :mute,
29 :notification_mute,
30 :reblog_mute,
31 :inverse_subscription
32 ] do
33 insert(:user_relationship,
34 source: user1,
35 target: user2,
36 relationship_type: relationship_type
37 )
38 end
39
40 assert UserRelationship.block_exists?(user1, user2)
41 assert UserRelationship.mute_exists?(user1, user2)
42 assert UserRelationship.notification_mute_exists?(user1, user2)
43 assert UserRelationship.reblog_mute_exists?(user1, user2)
44 assert UserRelationship.inverse_subscription_exists?(user1, user2)
45 end
46 end
47
48 describe "create_*/2" do
49 setup do
50 {:ok, users: insert_list(2, :user)}
51 end
52
53 test "creates user relationship record if it doesn't exist", %{users: [user1, user2]} do
54 for relationship_type <- [
55 :block,
56 :mute,
57 :notification_mute,
58 :reblog_mute,
59 :inverse_subscription
60 ] do
61 insert(:user_relationship,
62 source: user1,
63 target: user2,
64 relationship_type: relationship_type
65 )
66 end
67
68 UserRelationship.create_block(user1, user2)
69 UserRelationship.create_mute(user1, user2)
70 UserRelationship.create_notification_mute(user1, user2)
71 UserRelationship.create_reblog_mute(user1, user2)
72 UserRelationship.create_inverse_subscription(user1, user2)
73
74 assert UserRelationship.block_exists?(user1, user2)
75 assert UserRelationship.mute_exists?(user1, user2)
76 assert UserRelationship.notification_mute_exists?(user1, user2)
77 assert UserRelationship.reblog_mute_exists?(user1, user2)
78 assert UserRelationship.inverse_subscription_exists?(user1, user2)
79 end
80
81 test "if record already exists, returns it", %{users: [user1, user2]} do
82 user_block = UserRelationship.create_block(user1, user2)
83 assert user_block == UserRelationship.create_block(user1, user2)
84 end
85 end
86
87 describe "delete_*/2" do
88 setup do
89 {:ok, users: insert_list(2, :user)}
90 end
91
92 test "deletes user relationship record if it exists", %{users: [user1, user2]} do
93 for relationship_type <- [
94 :block,
95 :mute,
96 :notification_mute,
97 :reblog_mute,
98 :inverse_subscription
99 ] do
100 insert(:user_relationship,
101 source: user1,
102 target: user2,
103 relationship_type: relationship_type
104 )
105 end
106
107 assert {:ok, %UserRelationship{}} = UserRelationship.delete_block(user1, user2)
108 assert {:ok, %UserRelationship{}} = UserRelationship.delete_mute(user1, user2)
109 assert {:ok, %UserRelationship{}} = UserRelationship.delete_notification_mute(user1, user2)
110 assert {:ok, %UserRelationship{}} = UserRelationship.delete_reblog_mute(user1, user2)
111
112 assert {:ok, %UserRelationship{}} =
113 UserRelationship.delete_inverse_subscription(user1, user2)
114
115 refute UserRelationship.block_exists?(user1, user2)
116 refute UserRelationship.mute_exists?(user1, user2)
117 refute UserRelationship.notification_mute_exists?(user1, user2)
118 refute UserRelationship.reblog_mute_exists?(user1, user2)
119 refute UserRelationship.inverse_subscription_exists?(user1, user2)
120 end
121
122 test "if record does not exist, returns {:ok, nil}", %{users: [user1, user2]} do
123 assert {:ok, nil} = UserRelationship.delete_block(user1, user2)
124 assert {:ok, nil} = UserRelationship.delete_mute(user1, user2)
125 assert {:ok, nil} = UserRelationship.delete_notification_mute(user1, user2)
126 assert {:ok, nil} = UserRelationship.delete_reblog_mute(user1, user2)
127 assert {:ok, nil} = UserRelationship.delete_inverse_subscription(user1, user2)
128 end
129 end
130 end