a758925b7eb5071d82f9ab539501d0d8158a5174
[anni] / test / pleroma / web / pleroma_api / controllers / backup_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.BackupControllerTest do
6   use Pleroma.Web.ConnCase
7
8   alias Pleroma.User.Backup
9   alias Pleroma.Web.PleromaAPI.BackupView
10
11   setup do
12     clear_config([Pleroma.Upload, :uploader])
13     clear_config([Backup, :limit_days])
14     oauth_access(["read:backups"])
15   end
16
17   test "GET /api/v1/pleroma/backups", %{user: user, conn: conn} do
18     assert {:ok, %Oban.Job{args: %{"backup_id" => backup_id}}} = Backup.create(user)
19
20     backup = Backup.get(backup_id)
21
22     response =
23       conn
24       |> get("/api/v1/pleroma/backups")
25       |> json_response_and_validate_schema(:ok)
26
27     assert [
28              %{
29                "content_type" => "application/zip",
30                "url" => url,
31                "file_size" => 0,
32                "processed" => false,
33                "inserted_at" => _
34              }
35            ] = response
36
37     assert url == BackupView.download_url(backup)
38
39     Pleroma.Tests.ObanHelpers.perform_all()
40
41     assert [
42              %{
43                "url" => ^url,
44                "processed" => true
45              }
46            ] =
47              conn
48              |> get("/api/v1/pleroma/backups")
49              |> json_response_and_validate_schema(:ok)
50   end
51
52   test "POST /api/v1/pleroma/backups", %{user: _user, conn: conn} do
53     assert [
54              %{
55                "content_type" => "application/zip",
56                "url" => url,
57                "file_size" => 0,
58                "processed" => false,
59                "inserted_at" => _
60              }
61            ] =
62              conn
63              |> post("/api/v1/pleroma/backups")
64              |> json_response_and_validate_schema(:ok)
65
66     Pleroma.Tests.ObanHelpers.perform_all()
67
68     assert [
69              %{
70                "url" => ^url,
71                "processed" => true
72              }
73            ] =
74              conn
75              |> get("/api/v1/pleroma/backups")
76              |> json_response_and_validate_schema(:ok)
77
78     days = Pleroma.Config.get([Backup, :limit_days])
79
80     assert %{"error" => "Last export was less than #{days} days ago"} ==
81              conn
82              |> post("/api/v1/pleroma/backups")
83              |> json_response_and_validate_schema(400)
84   end
85
86   test "Backup without email address" do
87     user = Pleroma.Factory.insert(:user, email: nil)
88     %{conn: conn} = oauth_access(["read:backups"], user: user)
89
90     assert is_nil(user.email)
91
92     assert [
93              %{
94                "content_type" => "application/zip",
95                "url" => _url,
96                "file_size" => 0,
97                "processed" => false,
98                "inserted_at" => _
99              }
100            ] =
101              conn
102              |> post("/api/v1/pleroma/backups")
103              |> json_response_and_validate_schema(:ok)
104   end
105 end