Add `backups` table
[akkoma] / test / backup_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.BackupTest do
6 use Pleroma.DataCase
7 import Pleroma.Factory
8 import Mock
9
10 alias Pleroma.Backup
11 alias Pleroma.Bookmark
12 alias Pleroma.Web.CommonAPI
13
14 test "it creates a backup record" do
15 %{id: user_id} = user = insert(:user)
16 assert {:ok, backup} = Backup.create(user)
17
18 assert %Backup{user_id: ^user_id, processed: false, file_size: 0} = backup
19 end
20
21 test "it return an error if the export limit is over" do
22 %{id: user_id} = user = insert(:user)
23 limit_days = 7
24
25 assert {:ok, backup} = Backup.create(user)
26 assert %Backup{user_id: ^user_id, processed: false, file_size: 0} = backup
27
28 assert Backup.create(user) == {:error, "Last export was less than #{limit_days} days ago"}
29 end
30
31 test "it process a backup record" do
32 %{id: user_id} = user = insert(:user)
33 assert {:ok, %{id: backup_id} = backup} = Backup.create(user)
34 assert {:ok, %Backup{} = backup} = Backup.process(backup)
35 assert backup.file_size > 0
36 assert %Backup{id: ^backup_id, processed: true, user_id: ^user_id} = backup
37 end
38
39 test "it creates a zip archive with user data" do
40 user = insert(:user, %{nickname: "cofe", name: "Cofe", ap_id: "http://cofe.io/users/cofe"})
41
42 {:ok, %{object: %{data: %{"id" => id1}}} = status1} =
43 CommonAPI.post(user, %{status: "status1"})
44
45 {:ok, %{object: %{data: %{"id" => id2}}} = status2} =
46 CommonAPI.post(user, %{status: "status2"})
47
48 {:ok, %{object: %{data: %{"id" => id3}}} = status3} =
49 CommonAPI.post(user, %{status: "status3"})
50
51 CommonAPI.favorite(user, status1.id)
52 CommonAPI.favorite(user, status2.id)
53
54 Bookmark.create(user.id, status2.id)
55 Bookmark.create(user.id, status3.id)
56
57 assert {:ok, backup} = user |> Backup.new() |> Repo.insert()
58 assert {:ok, path} = Backup.zip(backup)
59 assert {:ok, zipfile} = :zip.zip_open(String.to_charlist(path), [:memory])
60 assert {:ok, {'actor.json', json}} = :zip.zip_get('actor.json', zipfile)
61
62 assert %{
63 "@context" => [
64 "https://www.w3.org/ns/activitystreams",
65 "http://localhost:4001/schemas/litepub-0.1.jsonld",
66 %{"@language" => "und"}
67 ],
68 "bookmarks" => "bookmarks.json",
69 "followers" => "http://cofe.io/users/cofe/followers",
70 "following" => "http://cofe.io/users/cofe/following",
71 "id" => "http://cofe.io/users/cofe",
72 "inbox" => "http://cofe.io/users/cofe/inbox",
73 "likes" => "likes.json",
74 "name" => "Cofe",
75 "outbox" => "http://cofe.io/users/cofe/outbox",
76 "preferredUsername" => "cofe",
77 "publicKey" => %{
78 "id" => "http://cofe.io/users/cofe#main-key",
79 "owner" => "http://cofe.io/users/cofe"
80 },
81 "type" => "Person",
82 "url" => "http://cofe.io/users/cofe"
83 } = Jason.decode!(json)
84
85 assert {:ok, {'outbox.json', json}} = :zip.zip_get('outbox.json', zipfile)
86
87 assert %{
88 "@context" => "https://www.w3.org/ns/activitystreams",
89 "id" => "outbox.json",
90 "orderedItems" => [
91 %{
92 "object" => %{
93 "actor" => "http://cofe.io/users/cofe",
94 "content" => "status1",
95 "type" => "Note"
96 },
97 "type" => "Create"
98 },
99 %{
100 "object" => %{
101 "actor" => "http://cofe.io/users/cofe",
102 "content" => "status2"
103 }
104 },
105 %{
106 "actor" => "http://cofe.io/users/cofe",
107 "object" => %{
108 "content" => "status3"
109 }
110 }
111 ],
112 "totalItems" => 3,
113 "type" => "OrderedCollection"
114 } = Jason.decode!(json)
115
116 assert {:ok, {'likes.json', json}} = :zip.zip_get('likes.json', zipfile)
117
118 assert %{
119 "@context" => "https://www.w3.org/ns/activitystreams",
120 "id" => "likes.json",
121 "orderedItems" => [^id1, ^id2],
122 "totalItems" => 2,
123 "type" => "OrderedCollection"
124 } = Jason.decode!(json)
125
126 assert {:ok, {'bookmarks.json', json}} = :zip.zip_get('bookmarks.json', zipfile)
127
128 assert %{
129 "@context" => "https://www.w3.org/ns/activitystreams",
130 "id" => "bookmarks.json",
131 "orderedItems" => [^id2, ^id3],
132 "totalItems" => 2,
133 "type" => "OrderedCollection"
134 } = Jason.decode!(json)
135
136 :zip.zip_close(zipfile)
137 File.rm!(path)
138 end
139
140 describe "it uploads a backup archive" do
141 setup do
142 clear_config(Pleroma.Uploaders.S3,
143 bucket: "test_bucket",
144 public_endpoint: "https://s3.amazonaws.com"
145 )
146
147 clear_config([Pleroma.Upload, :uploader])
148
149 user = insert(:user, %{nickname: "cofe", name: "Cofe", ap_id: "http://cofe.io/users/cofe"})
150
151 {:ok, status1} = CommonAPI.post(user, %{status: "status1"})
152 {:ok, status2} = CommonAPI.post(user, %{status: "status2"})
153 {:ok, status3} = CommonAPI.post(user, %{status: "status3"})
154 CommonAPI.favorite(user, status1.id)
155 CommonAPI.favorite(user, status2.id)
156 Bookmark.create(user.id, status2.id)
157 Bookmark.create(user.id, status3.id)
158
159 assert {:ok, backup} = user |> Backup.new() |> Repo.insert()
160 assert {:ok, path} = Backup.zip(backup)
161
162 [path: path, backup: backup]
163 end
164
165 test "S3", %{path: path, backup: backup} do
166 Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.S3)
167
168 with_mock ExAws, request: fn _ -> {:ok, :ok} end do
169 assert {:ok, %Pleroma.Upload{}} = Backup.upload(backup, path)
170 end
171 end
172
173 test "Local", %{path: path, backup: backup} do
174 Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
175
176 assert {:ok, %Pleroma.Upload{}} = Backup.upload(backup, path)
177 end
178 end
179 end