Merge branch 'spc-fix-3' into 'develop'
[akkoma] / test / spc_fixes_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.SpcFixesTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.SpcFixes
9 alias Pleroma.Web.CommonAPI
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 alias Pleroma.User
12 alias Pleroma.Activity
13 alias Pleroma.Repo
14 alias Pleroma.Object
15
16 import Pleroma.Factory
17
18 test "resets the ap_id and follower_address of old spc users" do
19 Tesla.Mock.mock(fn
20 %{url: "https://shitposter.club/users/zep"} ->
21 %Tesla.Env{status: 200, body: File.read!("test/fixtures/zep.json")}
22
23 %{url: nil} ->
24 nil
25 end)
26
27 user =
28 insert(:user, %{
29 local: false,
30 ap_id: "https://shitposter.club/user/4962",
31 follower_address: User.ap_followers(%User{nickname: "zep@shitposter.club"}),
32 info: %{topic: "ignore"},
33 nickname: "zep@shitposter.club"
34 })
35
36 other_user = insert(:user)
37 {:ok, other_user} = User.follow(other_user, user)
38 {:ok, activity} = CommonAPI.post(user, %{"status" => "blabla"})
39 {:ok, _other_activity} = CommonAPI.post(other_user, %{"status" => "blabla"})
40
41 assert User.following?(other_user, user)
42 assert [activity] == ActivityPub.fetch_activities(other_user.following)
43
44 SpcFixes.upgrade_users()
45
46 user = Pleroma.Repo.get(User, user.id)
47 other_user = Pleroma.Repo.get(User, other_user.id)
48
49 assert user.ap_id == "https://shitposter.club/users/zep"
50 assert user.follower_address == "https://shitposter.club/users/zep/followers"
51
52 aid = activity.id
53 # Activites and following are correctly stitched.
54 assert User.following?(other_user, user)
55 assert [%{id: ^aid}] = ActivityPub.fetch_activities(other_user.following)
56
57 third_user = insert(:user)
58 {:ok, third_user} = User.follow(third_user, user)
59 assert [%{id: ^aid}] = ActivityPub.fetch_activities(third_user.following)
60
61 activity = Repo.get(Activity, aid)
62
63 assert activity.data["actor"] == user.ap_id
64 assert user.follower_address in activity.recipients
65 assert user.follower_address in activity.data["to"]
66
67 object = Object.get_by_ap_id(activity.data["object"]["id"])
68
69 assert object.data["actor"] == user.ap_id
70 assert user.follower_address in object.data["to"]
71 end
72 end