akkoma/test/pleroma/user/backup_test.exs

276 lines
9.7 KiB
Elixir
Raw Normal View History

2020-08-25 08:51:09 -06:00
# Pleroma: A lightweight social networking server
2022-02-25 23:11:42 -07:00
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
2020-08-25 08:51:09 -06:00
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.User.BackupTest do
2020-09-02 11:45:22 -06:00
use Oban.Testing, repo: Pleroma.Repo
2020-08-25 08:51:09 -06:00
use Pleroma.DataCase
2020-09-02 11:45:22 -06:00
2020-08-31 13:07:14 -06:00
import Mock
2020-09-17 08:42:24 -06:00
import Pleroma.Factory
import Swoosh.TestAssertions
2020-08-25 08:51:09 -06:00
alias Pleroma.Bookmark
2020-09-04 12:00:26 -06:00
alias Pleroma.Tests.ObanHelpers
2020-10-20 07:47:04 -06:00
alias Pleroma.User.Backup
2020-08-25 09:16:01 -06:00
alias Pleroma.Web.CommonAPI
2020-09-02 11:45:22 -06:00
alias Pleroma.Workers.BackupWorker
2020-09-04 11:48:52 -06:00
setup do
clear_config([Pleroma.Upload, :uploader])
clear_config([Backup, :limit_days])
2020-10-19 05:30:32 -06:00
clear_config([Pleroma.Emails.Mailer, :enabled], true)
2020-09-15 08:07:28 -06:00
end
test "it does not requrie enabled email" do
clear_config([Pleroma.Emails.Mailer, :enabled], false)
2020-09-15 08:07:28 -06:00
user = insert(:user)
assert {:ok, _} = Backup.create(user)
2020-09-15 08:07:28 -06:00
end
test "it does not require user's email" do
2020-09-15 08:07:28 -06:00
user = insert(:user, %{email: nil})
assert {:ok, _} = Backup.create(user)
2020-09-04 11:48:52 -06:00
end
2020-08-25 08:51:09 -06:00
2020-09-02 11:45:22 -06:00
test "it creates a backup record and an Oban job" do
2020-09-02 10:21:33 -06:00
%{id: user_id} = user = insert(:user)
2020-09-02 11:45:22 -06:00
assert {:ok, %Oban.Job{args: args}} = Backup.create(user)
assert_enqueued(worker: BackupWorker, args: args)
2020-09-02 10:21:33 -06:00
2020-09-02 11:45:22 -06:00
backup = Backup.get(args["backup_id"])
2020-09-02 10:21:33 -06:00
assert %Backup{user_id: ^user_id, processed: false, file_size: 0} = backup
end
test "it return an error if the export limit is over" do
%{id: user_id} = user = insert(:user)
limit_days = Pleroma.Config.get([Backup, :limit_days])
2020-09-02 11:45:22 -06:00
assert {:ok, %Oban.Job{args: args}} = Backup.create(user)
backup = Backup.get(args["backup_id"])
2020-09-02 10:21:33 -06:00
assert %Backup{user_id: ^user_id, processed: false, file_size: 0} = backup
assert Backup.create(user) == {:error, "Last export was less than #{limit_days} days ago"}
end
test "it process a backup record" do
clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
2020-09-02 10:21:33 -06:00
%{id: user_id} = user = insert(:user)
2020-09-04 11:48:52 -06:00
assert {:ok, %Oban.Job{args: %{"backup_id" => backup_id} = args}} = Backup.create(user)
assert {:ok, backup} = perform_job(BackupWorker, args)
2020-09-02 10:21:33 -06:00
assert backup.file_size > 0
assert %Backup{id: ^backup_id, processed: true, user_id: ^user_id} = backup
2020-09-04 11:48:52 -06:00
delete_job_args = %{"op" => "delete", "backup_id" => backup_id}
assert_enqueued(worker: BackupWorker, args: delete_job_args)
assert {:ok, backup} = perform_job(BackupWorker, delete_job_args)
refute Backup.get(backup_id)
2020-09-17 08:42:24 -06:00
2020-09-23 10:23:11 -06:00
email = Pleroma.Emails.UserEmail.backup_is_ready_email(backup)
assert_email_sent(
to: {user.name, user.email},
html_body: email.html_body
)
2020-09-04 11:48:52 -06:00
end
test "it does not send an email if the user does not have an email" do
clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
%{id: user_id} = user = insert(:user, %{email: nil})
assert {:ok, %Oban.Job{args: %{"backup_id" => backup_id} = args}} = Backup.create(user)
assert {:ok, backup} = perform_job(BackupWorker, args)
assert backup.file_size > 0
assert %Backup{id: ^backup_id, processed: true, user_id: ^user_id} = backup
assert_no_email_sent()
end
test "it does not send an email if mailer is not on" do
clear_config([Pleroma.Emails.Mailer, :enabled], false)
clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
%{id: user_id} = user = insert(:user)
assert {:ok, %Oban.Job{args: %{"backup_id" => backup_id} = args}} = Backup.create(user)
assert {:ok, backup} = perform_job(BackupWorker, args)
assert backup.file_size > 0
assert %Backup{id: ^backup_id, processed: true, user_id: ^user_id} = backup
assert_no_email_sent()
end
test "it does not send an email if the user has an empty email" do
clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
%{id: user_id} = user = insert(:user, %{email: ""})
assert {:ok, %Oban.Job{args: %{"backup_id" => backup_id} = args}} = Backup.create(user)
assert {:ok, backup} = perform_job(BackupWorker, args)
assert backup.file_size > 0
assert %Backup{id: ^backup_id, processed: true, user_id: ^user_id} = backup
assert_no_email_sent()
end
2020-09-04 11:48:52 -06:00
test "it removes outdated backups after creating a fresh one" do
clear_config([Backup, :limit_days], -1)
clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
2020-09-04 11:48:52 -06:00
user = insert(:user)
assert {:ok, job1} = Backup.create(user)
2020-10-28 05:32:44 -06:00
assert {:ok, %Backup{}} = ObanHelpers.perform(job1)
2020-09-04 11:48:52 -06:00
assert {:ok, job2} = Backup.create(user)
assert Pleroma.Repo.aggregate(Backup, :count) == 2
assert {:ok, backup2} = ObanHelpers.perform(job2)
ObanHelpers.perform_all()
assert [^backup2] = Pleroma.Repo.all(Backup)
2020-09-02 10:21:33 -06:00
end
test "it creates a zip archive with user data" do
2020-08-25 08:51:09 -06:00
user = insert(:user, %{nickname: "cofe", name: "Cofe", ap_id: "http://cofe.io/users/cofe"})
{:ok, %{object: %{data: %{"id" => id1}}} = status1} =
CommonAPI.post(user, %{status: "status1"})
{:ok, %{object: %{data: %{"id" => id2}}} = status2} =
CommonAPI.post(user, %{status: "status2"})
{:ok, %{object: %{data: %{"id" => id3}}} = status3} =
CommonAPI.post(user, %{status: "status3"})
CommonAPI.favorite(user, status1.id)
CommonAPI.favorite(user, status2.id)
Bookmark.create(user.id, status2.id)
Bookmark.create(user.id, status3.id)
2020-09-02 10:21:33 -06:00
assert {:ok, backup} = user |> Backup.new() |> Repo.insert()
2020-09-15 08:07:28 -06:00
assert {:ok, path} = Backup.export(backup)
2020-08-31 10:31:21 -06:00
assert {:ok, zipfile} = :zip.zip_open(String.to_charlist(path), [:memory])
2020-08-25 08:51:09 -06:00
assert {:ok, {'actor.json', json}} = :zip.zip_get('actor.json', zipfile)
assert %{
"@context" => [
"https://www.w3.org/ns/activitystreams",
"http://localhost:4001/schemas/litepub-0.1.jsonld",
%{"@language" => "und"}
],
"bookmarks" => "bookmarks.json",
"followers" => "http://cofe.io/users/cofe/followers",
"following" => "http://cofe.io/users/cofe/following",
"id" => "http://cofe.io/users/cofe",
"inbox" => "http://cofe.io/users/cofe/inbox",
"likes" => "likes.json",
"name" => "Cofe",
"outbox" => "http://cofe.io/users/cofe/outbox",
"preferredUsername" => "cofe",
"publicKey" => %{
"id" => "http://cofe.io/users/cofe#main-key",
"owner" => "http://cofe.io/users/cofe"
},
"type" => "Person",
"url" => "http://cofe.io/users/cofe"
} = Jason.decode!(json)
assert {:ok, {'outbox.json', json}} = :zip.zip_get('outbox.json', zipfile)
assert %{
"@context" => "https://www.w3.org/ns/activitystreams",
"id" => "outbox.json",
"orderedItems" => [
%{
"object" => %{
"actor" => "http://cofe.io/users/cofe",
"content" => "status1",
"type" => "Note"
},
"type" => "Create"
},
%{
"object" => %{
"actor" => "http://cofe.io/users/cofe",
"content" => "status2"
}
},
%{
"actor" => "http://cofe.io/users/cofe",
"object" => %{
"content" => "status3"
}
}
],
"totalItems" => 3,
"type" => "OrderedCollection"
} = Jason.decode!(json)
assert {:ok, {'likes.json', json}} = :zip.zip_get('likes.json', zipfile)
assert %{
"@context" => "https://www.w3.org/ns/activitystreams",
"id" => "likes.json",
"orderedItems" => [^id1, ^id2],
"totalItems" => 2,
"type" => "OrderedCollection"
} = Jason.decode!(json)
assert {:ok, {'bookmarks.json', json}} = :zip.zip_get('bookmarks.json', zipfile)
assert %{
"@context" => "https://www.w3.org/ns/activitystreams",
"id" => "bookmarks.json",
"orderedItems" => [^id2, ^id3],
"totalItems" => 2,
"type" => "OrderedCollection"
} = Jason.decode!(json)
:zip.zip_close(zipfile)
File.rm!(path)
end
2020-08-31 10:31:21 -06:00
2020-09-04 11:48:52 -06:00
describe "it uploads and deletes a backup archive" do
2020-08-31 13:07:14 -06:00
setup do
2021-01-13 11:00:48 -07:00
clear_config([Pleroma.Upload, :base_url], "https://s3.amazonaws.com")
clear_config([Pleroma.Uploaders.S3, :bucket], "test_bucket")
2020-08-31 10:31:21 -06:00
2020-08-31 13:07:14 -06:00
user = insert(:user, %{nickname: "cofe", name: "Cofe", ap_id: "http://cofe.io/users/cofe"})
{:ok, status1} = CommonAPI.post(user, %{status: "status1"})
{:ok, status2} = CommonAPI.post(user, %{status: "status2"})
{:ok, status3} = CommonAPI.post(user, %{status: "status3"})
CommonAPI.favorite(user, status1.id)
CommonAPI.favorite(user, status2.id)
Bookmark.create(user.id, status2.id)
Bookmark.create(user.id, status3.id)
2020-09-02 10:21:33 -06:00
assert {:ok, backup} = user |> Backup.new() |> Repo.insert()
2020-09-15 08:07:28 -06:00
assert {:ok, path} = Backup.export(backup)
2020-08-31 13:07:14 -06:00
2020-09-02 10:21:33 -06:00
[path: path, backup: backup]
2020-08-31 13:07:14 -06:00
end
2020-09-02 10:21:33 -06:00
test "S3", %{path: path, backup: backup} do
2021-01-14 12:18:45 -07:00
clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.S3)
clear_config([Pleroma.Uploaders.S3, :streaming_enabled], false)
2020-08-31 13:07:14 -06:00
2020-09-04 11:48:52 -06:00
with_mock ExAws,
request: fn
%{http_method: :put} -> {:ok, :ok}
%{http_method: :delete} -> {:ok, %{status_code: 204}}
end do
2020-09-02 10:21:33 -06:00
assert {:ok, %Pleroma.Upload{}} = Backup.upload(backup, path)
2020-09-04 11:48:52 -06:00
assert {:ok, _backup} = Backup.delete(backup)
end
2020-08-31 13:07:14 -06:00
end
2020-09-02 10:21:33 -06:00
test "Local", %{path: path, backup: backup} do
2021-01-14 12:18:45 -07:00
clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
2020-08-31 13:07:14 -06:00
2020-09-02 10:21:33 -06:00
assert {:ok, %Pleroma.Upload{}} = Backup.upload(backup, path)
2020-09-04 11:48:52 -06:00
assert {:ok, _backup} = Backup.delete(backup)
2020-08-31 13:07:14 -06:00
end
2020-08-31 10:31:21 -06:00
end
2020-08-25 08:51:09 -06:00
end