total rebase
[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.UnstubbedConfigMock, as: ConfigMock
9   alias Pleroma.User.Backup
10   alias Pleroma.Web.PleromaAPI.BackupView
11
12   setup do
13     clear_config([Pleroma.Upload, :uploader])
14     clear_config([Backup, :limit_days])
15
16     ConfigMock
17     |> Mox.stub_with(Pleroma.Config)
18
19     oauth_access(["read:backups"])
20   end
21
22   test "GET /api/v1/pleroma/backups", %{user: user, conn: conn} do
23     assert {:ok, %Oban.Job{args: %{"backup_id" => backup_id}}} = Backup.create(user)
24
25     backup = Backup.get(backup_id)
26
27     response =
28       conn
29       |> get("/api/v1/pleroma/backups")
30       |> json_response_and_validate_schema(:ok)
31
32     assert [
33              %{
34                "content_type" => "application/zip",
35                "url" => url,
36                "file_size" => 0,
37                "processed" => false,
38                "inserted_at" => _
39              }
40            ] = response
41
42     assert url == BackupView.download_url(backup)
43
44     Pleroma.Tests.ObanHelpers.perform_all()
45
46     assert [
47              %{
48                "url" => ^url,
49                "processed" => true
50              }
51            ] =
52              conn
53              |> get("/api/v1/pleroma/backups")
54              |> json_response_and_validate_schema(:ok)
55   end
56
57   test "POST /api/v1/pleroma/backups", %{user: _user, conn: conn} do
58     assert [
59              %{
60                "content_type" => "application/zip",
61                "url" => url,
62                "file_size" => 0,
63                "processed" => false,
64                "inserted_at" => _
65              }
66            ] =
67              conn
68              |> post("/api/v1/pleroma/backups")
69              |> json_response_and_validate_schema(:ok)
70
71     Pleroma.Tests.ObanHelpers.perform_all()
72
73     assert [
74              %{
75                "url" => ^url,
76                "processed" => true
77              }
78            ] =
79              conn
80              |> get("/api/v1/pleroma/backups")
81              |> json_response_and_validate_schema(:ok)
82
83     days = Pleroma.Config.get([Backup, :limit_days])
84
85     assert %{"error" => "Last export was less than #{days} days ago"} ==
86              conn
87              |> post("/api/v1/pleroma/backups")
88              |> json_response_and_validate_schema(400)
89   end
90
91   test "Backup without email address" do
92     user = Pleroma.Factory.insert(:user, email: nil)
93     %{conn: conn} = oauth_access(["read:backups"], user: user)
94
95     assert is_nil(user.email)
96
97     assert [
98              %{
99                "content_type" => "application/zip",
100                "url" => _url,
101                "file_size" => 0,
102                "processed" => false,
103                "inserted_at" => _
104              }
105            ] =
106              conn
107              |> post("/api/v1/pleroma/backups")
108              |> json_response_and_validate_schema(:ok)
109   end
110 end