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.User.BackupTest do
6 use Oban.Testing, repo: Pleroma.Repo
10 import Pleroma.Factory
11 import Swoosh.TestAssertions
13 alias Pleroma.Bookmark
14 alias Pleroma.Tests.ObanHelpers
15 alias Pleroma.User.Backup
16 alias Pleroma.Web.CommonAPI
17 alias Pleroma.Workers.BackupWorker
20 clear_config([Pleroma.Upload, :uploader])
21 clear_config([Backup, :limit_days])
22 clear_config([Pleroma.Emails.Mailer, :enabled], true)
25 test "it requries enabled email" do
26 Pleroma.Config.put([Pleroma.Emails.Mailer, :enabled], false)
28 assert {:error, "Backups require enabled email"} == Backup.create(user)
31 test "it requries user's email" do
32 user = insert(:user, %{email: nil})
33 assert {:error, "Email is required"} == Backup.create(user)
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)
41 backup = Backup.get(args["backup_id"])
42 assert %Backup{user_id: ^user_id, processed: false, file_size: 0} = backup
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
52 assert Backup.create(user) == {:error, "Last export was less than #{limit_days} days ago"}
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)
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
64 delete_job_args = %{"op" => "delete", "backup_id" => backup_id}
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)
70 email = Pleroma.Emails.UserEmail.backup_is_ready_email(backup)
73 to: {user.name, user.email},
74 html_body: email.html_body
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)
83 assert {:ok, job1} = Backup.create(user)
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)
90 ObanHelpers.perform_all()
92 assert [^backup2] = Pleroma.Repo.all(Backup)
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"})
98 {:ok, %{object: %{data: %{"id" => id1}}} = status1} =
99 CommonAPI.post(user, %{status: "status1"})
101 {:ok, %{object: %{data: %{"id" => id2}}} = status2} =
102 CommonAPI.post(user, %{status: "status2"})
104 {:ok, %{object: %{data: %{"id" => id3}}} = status3} =
105 CommonAPI.post(user, %{status: "status3"})
107 CommonAPI.favorite(user, status1.id)
108 CommonAPI.favorite(user, status2.id)
110 Bookmark.create(user.id, status2.id)
111 Bookmark.create(user.id, status3.id)
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)
120 "https://www.w3.org/ns/activitystreams",
121 "http://localhost:4001/schemas/litepub-0.1.jsonld",
122 %{"@language" => "und"}
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",
131 "outbox" => "http://cofe.io/users/cofe/outbox",
132 "preferredUsername" => "cofe",
134 "id" => "http://cofe.io/users/cofe#main-key",
135 "owner" => "http://cofe.io/users/cofe"
138 "url" => "http://cofe.io/users/cofe"
139 } = Jason.decode!(json)
141 assert {:ok, {'outbox.json', json}} = :zip.zip_get('outbox.json', zipfile)
144 "@context" => "https://www.w3.org/ns/activitystreams",
145 "id" => "outbox.json",
149 "actor" => "http://cofe.io/users/cofe",
150 "content" => "status1",
157 "actor" => "http://cofe.io/users/cofe",
158 "content" => "status2"
162 "actor" => "http://cofe.io/users/cofe",
164 "content" => "status3"
169 "type" => "OrderedCollection"
170 } = Jason.decode!(json)
172 assert {:ok, {'likes.json', json}} = :zip.zip_get('likes.json', zipfile)
175 "@context" => "https://www.w3.org/ns/activitystreams",
176 "id" => "likes.json",
177 "orderedItems" => [^id1, ^id2],
179 "type" => "OrderedCollection"
180 } = Jason.decode!(json)
182 assert {:ok, {'bookmarks.json', json}} = :zip.zip_get('bookmarks.json', zipfile)
185 "@context" => "https://www.w3.org/ns/activitystreams",
186 "id" => "bookmarks.json",
187 "orderedItems" => [^id2, ^id3],
189 "type" => "OrderedCollection"
190 } = Jason.decode!(json)
192 :zip.zip_close(zipfile)
196 describe "it uploads and deletes a backup archive" do
198 clear_config(Pleroma.Uploaders.S3,
199 bucket: "test_bucket",
200 public_endpoint: "https://s3.amazonaws.com"
203 clear_config([Pleroma.Upload, :uploader])
205 user = insert(:user, %{nickname: "cofe", name: "Cofe", ap_id: "http://cofe.io/users/cofe"})
207 {:ok, status1} = CommonAPI.post(user, %{status: "status1"})
208 {:ok, status2} = CommonAPI.post(user, %{status: "status2"})
209 {:ok, status3} = CommonAPI.post(user, %{status: "status3"})
210 CommonAPI.favorite(user, status1.id)
211 CommonAPI.favorite(user, status2.id)
212 Bookmark.create(user.id, status2.id)
213 Bookmark.create(user.id, status3.id)
215 assert {:ok, backup} = user |> Backup.new() |> Repo.insert()
216 assert {:ok, path} = Backup.export(backup)
218 [path: path, backup: backup]
221 test "S3", %{path: path, backup: backup} do
222 Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.S3)
226 %{http_method: :put} -> {:ok, :ok}
227 %{http_method: :delete} -> {:ok, %{status_code: 204}}
229 assert {:ok, %Pleroma.Upload{}} = Backup.upload(backup, path)
230 assert {:ok, _backup} = Backup.delete(backup)
233 with_mock ExAws, request: fn %{http_method: :delete} -> {:ok, %{status_code: 204}} end do
237 test "Local", %{path: path, backup: backup} do
238 Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
240 assert {:ok, %Pleroma.Upload{}} = Backup.upload(backup, path)
241 assert {:ok, _backup} = Backup.delete(backup)