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.UserRelationshipTest do
6 alias Pleroma.UserRelationship
8 use Pleroma.DataCase, async: false
11 import Pleroma.Factory
13 describe "*_exists?/2" do
15 {:ok, users: insert_list(2, :user)}
18 test "returns false if record doesn't exist", %{users: [user1, user2]} do
19 refute UserRelationship.block_exists?(user1, user2)
20 refute UserRelationship.mute_exists?(user1, user2)
21 refute UserRelationship.notification_mute_exists?(user1, user2)
22 refute UserRelationship.reblog_mute_exists?(user1, user2)
23 refute UserRelationship.inverse_subscription_exists?(user1, user2)
26 test "returns true if record exists", %{users: [user1, user2]} do
27 for relationship_type <- [
34 insert(:user_relationship,
37 relationship_type: relationship_type
41 assert UserRelationship.block_exists?(user1, user2)
42 assert UserRelationship.mute_exists?(user1, user2)
43 assert UserRelationship.notification_mute_exists?(user1, user2)
44 assert UserRelationship.reblog_mute_exists?(user1, user2)
45 assert UserRelationship.inverse_subscription_exists?(user1, user2)
49 describe "create_*/2" do
51 {:ok, users: insert_list(2, :user)}
54 test "creates user relationship record if it doesn't exist", %{users: [user1, user2]} do
55 for relationship_type <- [
62 insert(:user_relationship,
65 relationship_type: relationship_type
69 UserRelationship.create_block(user1, user2)
70 UserRelationship.create_mute(user1, user2)
71 UserRelationship.create_notification_mute(user1, user2)
72 UserRelationship.create_reblog_mute(user1, user2)
73 UserRelationship.create_inverse_subscription(user1, user2)
75 assert UserRelationship.block_exists?(user1, user2)
76 assert UserRelationship.mute_exists?(user1, user2)
77 assert UserRelationship.notification_mute_exists?(user1, user2)
78 assert UserRelationship.reblog_mute_exists?(user1, user2)
79 assert UserRelationship.inverse_subscription_exists?(user1, user2)
82 test "if record already exists, returns it", %{users: [user1, user2]} do
84 with_mock NaiveDateTime, [:passthrough], utc_now: fn -> ~N[2017-03-17 17:09:58] end do
85 {:ok, %{inserted_at: ~N[2017-03-17 17:09:58]}} =
86 UserRelationship.create_block(user1, user2)
89 assert user_block == UserRelationship.create_block(user1, user2)
93 describe "delete_*/2" do
95 {:ok, users: insert_list(2, :user)}
98 test "deletes user relationship record if it exists", %{users: [user1, user2]} do
99 for relationship_type <- [
104 :inverse_subscription
106 insert(:user_relationship,
109 relationship_type: relationship_type
113 assert {:ok, %UserRelationship{}} = UserRelationship.delete_block(user1, user2)
114 assert {:ok, %UserRelationship{}} = UserRelationship.delete_mute(user1, user2)
115 assert {:ok, %UserRelationship{}} = UserRelationship.delete_notification_mute(user1, user2)
116 assert {:ok, %UserRelationship{}} = UserRelationship.delete_reblog_mute(user1, user2)
118 assert {:ok, %UserRelationship{}} =
119 UserRelationship.delete_inverse_subscription(user1, user2)
121 refute UserRelationship.block_exists?(user1, user2)
122 refute UserRelationship.mute_exists?(user1, user2)
123 refute UserRelationship.notification_mute_exists?(user1, user2)
124 refute UserRelationship.reblog_mute_exists?(user1, user2)
125 refute UserRelationship.inverse_subscription_exists?(user1, user2)
128 test "if record does not exist, returns {:ok, nil}", %{users: [user1, user2]} do
129 assert {:ok, nil} = UserRelationship.delete_block(user1, user2)
130 assert {:ok, nil} = UserRelationship.delete_mute(user1, user2)
131 assert {:ok, nil} = UserRelationship.delete_notification_mute(user1, user2)
132 assert {:ok, nil} = UserRelationship.delete_reblog_mute(user1, user2)
133 assert {:ok, nil} = UserRelationship.delete_inverse_subscription(user1, user2)