First
[anni] / test / pleroma / resilience_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.ResilienceTest do
6   use Pleroma.Web.ConnCase, async: true
7
8   import Pleroma.Factory
9
10   alias Pleroma.Activity
11   alias Pleroma.Repo
12   alias Pleroma.Web.CommonAPI
13   alias Pleroma.Web.MastodonAPI.StatusView
14
15   setup do
16     # user = insert(:user)
17     %{user: user, conn: conn} = oauth_access(["write", "read"])
18     other_user = insert(:user)
19
20     {:ok, post_one} = CommonAPI.post(user, %{status: "Here is a post"})
21     {:ok, like} = CommonAPI.favorite(other_user, post_one.id)
22
23     %{
24       user: user,
25       other_user: other_user,
26       post_one: post_one,
27       like: like,
28       conn: conn
29     }
30   end
31
32   test "after destruction of like activities, things still work", %{
33     user: user,
34     post_one: post,
35     other_user: other_user,
36     conn: conn,
37     like: like
38   } do
39     post = Repo.get(Activity, post.id)
40
41     # Rendering the liked status
42     rendered_for_user = StatusView.render("show.json", %{activity: post, for: user})
43     assert rendered_for_user.favourites_count == 1
44
45     rendered_for_other_user = StatusView.render("show.json", %{activity: post, for: other_user})
46     assert rendered_for_other_user.favourites_count == 1
47     assert rendered_for_other_user.favourited
48
49     # Getting the favourited by
50     [liking_user] =
51       conn
52       |> get("/api/v1/statuses/#{post.id}/favourited_by")
53       |> json_response(200)
54
55     assert liking_user["id"] == other_user.id
56
57     # We have one notification
58     [notification] =
59       conn
60       |> get("/api/v1/notifications")
61       |> json_response(200)
62
63     assert notification["type"] == "favourite"
64
65     # Destroying the like
66     Repo.delete(like)
67     post = Repo.get(Activity, post.id)
68
69     # Rendering the liked status
70     rendered_for_user = StatusView.render("show.json", %{activity: post, for: user})
71     assert rendered_for_user.favourites_count == 1
72
73     rendered_for_other_user = StatusView.render("show.json", %{activity: post, for: other_user})
74     assert rendered_for_other_user.favourites_count == 1
75     assert rendered_for_other_user.favourited
76
77     # Getting the favourited by
78     [liking_user] =
79       conn
80       |> get("/api/v1/statuses/#{post.id}/favourited_by")
81       |> json_response(200)
82
83     assert liking_user["id"] == other_user.id
84
85     # Notification is removed
86
87     assert [] ==
88              conn
89              |> get("/api/v1/notifications")
90              |> json_response(200)
91
92     # Favoriting again doesn't hurt
93     {:ok, _like_two} = CommonAPI.favorite(other_user, post.id)
94
95     post = Repo.get(Activity, post.id)
96
97     # Rendering the liked status
98     rendered_for_user = StatusView.render("show.json", %{activity: post, for: user})
99     assert rendered_for_user.favourites_count == 1
100
101     # General fallout: Can't unfavorite stuff anymore. Acceptable for remote users.
102   end
103 end