Merge remote-tracking branch 'pleroma/develop' into feature/disable-account
[akkoma] / test / activity_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.ActivityTest do
6 use Pleroma.DataCase
7 alias Pleroma.Activity
8 alias Pleroma.Bookmark
9 import Pleroma.Factory
10
11 test "returns an activity by it's AP id" do
12 activity = insert(:note_activity)
13 found_activity = Activity.get_by_ap_id(activity.data["id"])
14
15 assert activity == found_activity
16 end
17
18 test "returns activities by it's objects AP ids" do
19 activity = insert(:note_activity)
20 [found_activity] = Activity.get_all_create_by_object_ap_id(activity.data["object"]["id"])
21
22 assert activity == found_activity
23 end
24
25 test "returns the activity that created an object" do
26 activity = insert(:note_activity)
27
28 found_activity = Activity.get_create_by_object_ap_id(activity.data["object"]["id"])
29
30 assert activity == found_activity
31 end
32
33 test "preloading a bookmark" do
34 user = insert(:user)
35 user2 = insert(:user)
36 user3 = insert(:user)
37 activity = insert(:note_activity)
38 {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
39 {:ok, _bookmark2} = Bookmark.create(user2.id, activity.id)
40 {:ok, bookmark3} = Bookmark.create(user3.id, activity.id)
41
42 queried_activity =
43 Ecto.Query.from(Pleroma.Activity)
44 |> Activity.with_preloaded_bookmark(user3)
45 |> Repo.one()
46
47 assert queried_activity.bookmark == bookmark3
48 end
49
50 describe "getting a bookmark" do
51 test "when association is loaded" do
52 user = insert(:user)
53 activity = insert(:note_activity)
54 {:ok, bookmark} = Bookmark.create(user.id, activity.id)
55
56 queried_activity =
57 Ecto.Query.from(Pleroma.Activity)
58 |> Activity.with_preloaded_bookmark(user)
59 |> Repo.one()
60
61 assert Activity.get_bookmark(queried_activity, user) == bookmark
62 end
63
64 test "when association is not loaded" do
65 user = insert(:user)
66 activity = insert(:note_activity)
67 {:ok, bookmark} = Bookmark.create(user.id, activity.id)
68
69 queried_activity =
70 Ecto.Query.from(Pleroma.Activity)
71 |> Repo.one()
72
73 assert Activity.get_bookmark(queried_activity, user) == bookmark
74 end
75 end
76 end