summaryrefslogtreecommitdiff
path: root/test/backup_test.exs
blob: f343b03614b026fe964c371a21238a216cc8d940 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.BackupTest do
  use Oban.Testing, repo: Pleroma.Repo
  use Pleroma.DataCase

  import Pleroma.Factory
  import Mock

  alias Pleroma.Backup
  alias Pleroma.Bookmark
  alias Pleroma.Web.CommonAPI
  alias Pleroma.Workers.BackupWorker

  setup do: clear_config([Pleroma.Upload, :uploader])

  test "it creates a backup record and an Oban job" do
    %{id: user_id} = user = insert(:user)
    assert {:ok, %Oban.Job{args: args}} = Backup.create(user)
    assert_enqueued(worker: BackupWorker, args: args)

    backup = Backup.get(args["backup_id"])
    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([Pleroma.Backup, :limit_days])
    assert {:ok, %Oban.Job{args: args}} = Backup.create(user)
    backup = Backup.get(args["backup_id"])
    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
    Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
    %{id: user_id} = user = insert(:user)
    assert {:ok, %Oban.Job{args: %{"backup_id" => backup_id}} = job} = Backup.create(user)
    assert {:ok, backup} = BackupWorker.perform(job)
    assert backup.file_size > 0
    assert %Backup{id: ^backup_id, processed: true, user_id: ^user_id} = backup
  end

  test "it creates a zip archive with user data" do
    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)

    assert {:ok, backup} = user |> Backup.new() |> Repo.insert()
    assert {:ok, path} = Backup.zip(backup)
    assert {:ok, zipfile} = :zip.zip_open(String.to_charlist(path), [:memory])
    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

  describe "it uploads a backup archive" do
    setup do
      clear_config(Pleroma.Uploaders.S3,
        bucket: "test_bucket",
        public_endpoint: "https://s3.amazonaws.com"
      )

      clear_config([Pleroma.Upload, :uploader])

      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)

      assert {:ok, backup} = user |> Backup.new() |> Repo.insert()
      assert {:ok, path} = Backup.zip(backup)

      [path: path, backup: backup]
    end

    test "S3", %{path: path, backup: backup} do
      Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.S3)

      with_mock ExAws, request: fn _ -> {:ok, :ok} end do
        assert {:ok, %Pleroma.Upload{}} = Backup.upload(backup, path)
      end
    end

    test "Local", %{path: path, backup: backup} do
      Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)

      assert {:ok, %Pleroma.Upload{}} = Backup.upload(backup, path)
    end
  end
end