Merge branch 'develop' into refactor/deactivated_user_field
[akkoma] / test / pleroma / user / backup_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.User.BackupTest do
6 use Oban.Testing, repo: Pleroma.Repo
7 use Pleroma.DataCase
8
9 import Mock
10 import Pleroma.Factory
11 import Swoosh.TestAssertions
12
13 alias Pleroma.Bookmark
14 alias Pleroma.Tests.ObanHelpers
15 alias Pleroma.User.Backup
16 alias Pleroma.Web.CommonAPI
17 alias Pleroma.Workers.BackupWorker
18
19 setup do
20 clear_config([Pleroma.Upload, :uploader])
21 clear_config([Backup, :limit_days])
22 clear_config([Pleroma.Emails.Mailer, :enabled], true)
23 end
24
25 test "it requries enabled email" do
26 Pleroma.Config.put([Pleroma.Emails.Mailer, :enabled], false)
27 user = insert(:user)
28 assert {:error, "Backups require enabled email"} == Backup.create(user)
29 end
30
31 test "it requries user's email" do
32 user = insert(:user, %{email: nil})
33 assert {:error, "Email is required"} == Backup.create(user)
34 end
35
36 test "it creates a backup record and an Oban job" do
37 %{id: user_id} = user = insert(:user)
38 assert {:ok, %Oban.Job{args: args}} = Backup.create(user)
39 assert_enqueued(worker: BackupWorker, args: args)
40
41 backup = Backup.get(args["backup_id"])
42 assert %Backup{user_id: ^user_id, processed: false, file_size: 0} = backup
43 end
44
45 test "it return an error if the export limit is over" do
46 %{id: user_id} = user = insert(:user)
47 limit_days = Pleroma.Config.get([Backup, :limit_days])
48 assert {:ok, %Oban.Job{args: args}} = Backup.create(user)
49 backup = Backup.get(args["backup_id"])
50 assert %Backup{user_id: ^user_id, processed: false, file_size: 0} = backup
51
52 assert Backup.create(user) == {:error, "Last export was less than #{limit_days} days ago"}
53 end
54
55 test "it process a backup record" do
56 Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
57 %{id: user_id} = user = insert(:user)
58
59 assert {:ok, %Oban.Job{args: %{"backup_id" => backup_id} = args}} = Backup.create(user)
60 assert {:ok, backup} = perform_job(BackupWorker, args)
61 assert backup.file_size > 0
62 assert %Backup{id: ^backup_id, processed: true, user_id: ^user_id} = backup
63
64 delete_job_args = %{"op" => "delete", "backup_id" => backup_id}
65
66 assert_enqueued(worker: BackupWorker, args: delete_job_args)
67 assert {:ok, backup} = perform_job(BackupWorker, delete_job_args)
68 refute Backup.get(backup_id)
69
70 email = Pleroma.Emails.UserEmail.backup_is_ready_email(backup)
71
72 assert_email_sent(
73 to: {user.name, user.email},
74 html_body: email.html_body
75 )
76 end
77
78 test "it removes outdated backups after creating a fresh one" do
79 Pleroma.Config.put([Backup, :limit_days], -1)
80 Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
81 user = insert(:user)
82
83 assert {:ok, job1} = Backup.create(user)
84
85 assert {:ok, %Backup{}} = ObanHelpers.perform(job1)
86 assert {:ok, job2} = Backup.create(user)
87 assert Pleroma.Repo.aggregate(Backup, :count) == 2
88 assert {:ok, backup2} = ObanHelpers.perform(job2)
89
90 ObanHelpers.perform_all()
91
92 assert [^backup2] = Pleroma.Repo.all(Backup)
93 end
94
95 test "it creates a zip archive with user data" do
96 user = insert(:user, %{nickname: "cofe", name: "Cofe", ap_id: "http://cofe.io/users/cofe"})
97
98 {:ok, %{object: %{data: %{"id" => id1}}} = status1} =
99 CommonAPI.post(user, %{status: "status1"})
100
101 {:ok, %{object: %{data: %{"id" => id2}}} = status2} =
102 CommonAPI.post(user, %{status: "status2"})
103
104 {:ok, %{object: %{data: %{"id" => id3}}} = status3} =
105 CommonAPI.post(user, %{status: "status3"})
106
107 CommonAPI.favorite(user, status1.id)
108 CommonAPI.favorite(user, status2.id)
109
110 Bookmark.create(user.id, status2.id)
111 Bookmark.create(user.id, status3.id)
112
113 assert {:ok, backup} = user |> Backup.new() |> Repo.insert()
114 assert {:ok, path} = Backup.export(backup)
115 assert {:ok, zipfile} = :zip.zip_open(String.to_charlist(path), [:memory])
116 assert {:ok, {'actor.json', json}} = :zip.zip_get('actor.json', zipfile)
117
118 assert %{
119 "@context" => [
120 "https://www.w3.org/ns/activitystreams",
121 "http://localhost:4001/schemas/litepub-0.1.jsonld",
122 %{"@language" => "und"}
123 ],
124 "bookmarks" => "bookmarks.json",
125 "followers" => "http://cofe.io/users/cofe/followers",
126 "following" => "http://cofe.io/users/cofe/following",
127 "id" => "http://cofe.io/users/cofe",
128 "inbox" => "http://cofe.io/users/cofe/inbox",
129 "likes" => "likes.json",
130 "name" => "Cofe",
131 "outbox" => "http://cofe.io/users/cofe/outbox",
132 "preferredUsername" => "cofe",
133 "publicKey" => %{
134 "id" => "http://cofe.io/users/cofe#main-key",
135 "owner" => "http://cofe.io/users/cofe"
136 },
137 "type" => "Person",
138 "url" => "http://cofe.io/users/cofe"
139 } = Jason.decode!(json)
140
141 assert {:ok, {'outbox.json', json}} = :zip.zip_get('outbox.json', zipfile)
142
143 assert %{
144 "@context" => "https://www.w3.org/ns/activitystreams",
145 "id" => "outbox.json",
146 "orderedItems" => [
147 %{
148 "object" => %{
149 "actor" => "http://cofe.io/users/cofe",
150 "content" => "status1",
151 "type" => "Note"
152 },
153 "type" => "Create"
154 },
155 %{
156 "object" => %{
157 "actor" => "http://cofe.io/users/cofe",
158 "content" => "status2"
159 }
160 },
161 %{
162 "actor" => "http://cofe.io/users/cofe",
163 "object" => %{
164 "content" => "status3"
165 }
166 }
167 ],
168 "totalItems" => 3,
169 "type" => "OrderedCollection"
170 } = Jason.decode!(json)
171
172 assert {:ok, {'likes.json', json}} = :zip.zip_get('likes.json', zipfile)
173
174 assert %{
175 "@context" => "https://www.w3.org/ns/activitystreams",
176 "id" => "likes.json",
177 "orderedItems" => [^id1, ^id2],
178 "totalItems" => 2,
179 "type" => "OrderedCollection"
180 } = Jason.decode!(json)
181
182 assert {:ok, {'bookmarks.json', json}} = :zip.zip_get('bookmarks.json', zipfile)
183
184 assert %{
185 "@context" => "https://www.w3.org/ns/activitystreams",
186 "id" => "bookmarks.json",
187 "orderedItems" => [^id2, ^id3],
188 "totalItems" => 2,
189 "type" => "OrderedCollection"
190 } = Jason.decode!(json)
191
192 :zip.zip_close(zipfile)
193 File.rm!(path)
194 end
195
196 describe "it uploads and deletes a backup archive" do
197 setup do
198 clear_config([Pleroma.Upload, :base_url], "https://s3.amazonaws.com")
199 clear_config([Pleroma.Uploaders.S3, :bucket], "test_bucket")
200
201 user = insert(:user, %{nickname: "cofe", name: "Cofe", ap_id: "http://cofe.io/users/cofe"})
202
203 {:ok, status1} = CommonAPI.post(user, %{status: "status1"})
204 {:ok, status2} = CommonAPI.post(user, %{status: "status2"})
205 {:ok, status3} = CommonAPI.post(user, %{status: "status3"})
206 CommonAPI.favorite(user, status1.id)
207 CommonAPI.favorite(user, status2.id)
208 Bookmark.create(user.id, status2.id)
209 Bookmark.create(user.id, status3.id)
210
211 assert {:ok, backup} = user |> Backup.new() |> Repo.insert()
212 assert {:ok, path} = Backup.export(backup)
213
214 [path: path, backup: backup]
215 end
216
217 test "S3", %{path: path, backup: backup} do
218 clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.S3)
219 clear_config([Pleroma.Uploaders.S3, :streaming_enabled], false)
220
221 with_mock ExAws,
222 request: fn
223 %{http_method: :put} -> {:ok, :ok}
224 %{http_method: :delete} -> {:ok, %{status_code: 204}}
225 end do
226 assert {:ok, %Pleroma.Upload{}} = Backup.upload(backup, path)
227 assert {:ok, _backup} = Backup.delete(backup)
228 end
229 end
230
231 test "Local", %{path: path, backup: backup} do
232 clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
233
234 assert {:ok, %Pleroma.Upload{}} = Backup.upload(backup, path)
235 assert {:ok, _backup} = Backup.delete(backup)
236 end
237 end
238 end