Add backup upload
[akkoma] / test / export_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.ExportTest do
6 use Pleroma.DataCase
7 import Pleroma.Factory
8
9 alias Pleroma.Bookmark
10 alias Pleroma.Web.CommonAPI
11
12 test "it exports user data" do
13 user = insert(:user, %{nickname: "cofe", name: "Cofe", ap_id: "http://cofe.io/users/cofe"})
14
15 {:ok, %{object: %{data: %{"id" => id1}}} = status1} =
16 CommonAPI.post(user, %{status: "status1"})
17
18 {:ok, %{object: %{data: %{"id" => id2}}} = status2} =
19 CommonAPI.post(user, %{status: "status2"})
20
21 {:ok, %{object: %{data: %{"id" => id3}}} = status3} =
22 CommonAPI.post(user, %{status: "status3"})
23
24 CommonAPI.favorite(user, status1.id)
25 CommonAPI.favorite(user, status2.id)
26
27 Bookmark.create(user.id, status2.id)
28 Bookmark.create(user.id, status3.id)
29
30 assert {:ok, path} = Pleroma.Export.run(user)
31 assert {:ok, zipfile} = :zip.zip_open(String.to_charlist(path), [:memory])
32 assert {:ok, {'actor.json', json}} = :zip.zip_get('actor.json', zipfile)
33
34 assert %{
35 "@context" => [
36 "https://www.w3.org/ns/activitystreams",
37 "http://localhost:4001/schemas/litepub-0.1.jsonld",
38 %{"@language" => "und"}
39 ],
40 "bookmarks" => "bookmarks.json",
41 "followers" => "http://cofe.io/users/cofe/followers",
42 "following" => "http://cofe.io/users/cofe/following",
43 "id" => "http://cofe.io/users/cofe",
44 "inbox" => "http://cofe.io/users/cofe/inbox",
45 "likes" => "likes.json",
46 "name" => "Cofe",
47 "outbox" => "http://cofe.io/users/cofe/outbox",
48 "preferredUsername" => "cofe",
49 "publicKey" => %{
50 "id" => "http://cofe.io/users/cofe#main-key",
51 "owner" => "http://cofe.io/users/cofe"
52 },
53 "type" => "Person",
54 "url" => "http://cofe.io/users/cofe"
55 } = Jason.decode!(json)
56
57 assert {:ok, {'outbox.json', json}} = :zip.zip_get('outbox.json', zipfile)
58
59 assert %{
60 "@context" => "https://www.w3.org/ns/activitystreams",
61 "id" => "outbox.json",
62 "orderedItems" => [
63 %{
64 "object" => %{
65 "actor" => "http://cofe.io/users/cofe",
66 "content" => "status1",
67 "type" => "Note"
68 },
69 "type" => "Create"
70 },
71 %{
72 "object" => %{
73 "actor" => "http://cofe.io/users/cofe",
74 "content" => "status2"
75 }
76 },
77 %{
78 "actor" => "http://cofe.io/users/cofe",
79 "object" => %{
80 "content" => "status3"
81 }
82 }
83 ],
84 "totalItems" => 3,
85 "type" => "OrderedCollection"
86 } = Jason.decode!(json)
87
88 assert {:ok, {'likes.json', json}} = :zip.zip_get('likes.json', zipfile)
89
90 assert %{
91 "@context" => "https://www.w3.org/ns/activitystreams",
92 "id" => "likes.json",
93 "orderedItems" => [^id1, ^id2],
94 "totalItems" => 2,
95 "type" => "OrderedCollection"
96 } = Jason.decode!(json)
97
98 assert {:ok, {'bookmarks.json', json}} = :zip.zip_get('bookmarks.json', zipfile)
99
100 assert %{
101 "@context" => "https://www.w3.org/ns/activitystreams",
102 "id" => "bookmarks.json",
103 "orderedItems" => [^id2, ^id3],
104 "totalItems" => 2,
105 "type" => "OrderedCollection"
106 } = Jason.decode!(json)
107
108 :zip.zip_close(zipfile)
109 File.rm!(path)
110 end
111
112 test "it uploads an exported backup archive" do
113 user = insert(:user, %{nickname: "cofe", name: "Cofe", ap_id: "http://cofe.io/users/cofe"})
114
115 {:ok, status1} = CommonAPI.post(user, %{status: "status1"})
116 {:ok, status2} = CommonAPI.post(user, %{status: "status2"})
117 {:ok, status3} = CommonAPI.post(user, %{status: "status3"})
118 CommonAPI.favorite(user, status1.id)
119 CommonAPI.favorite(user, status2.id)
120 Bookmark.create(user.id, status2.id)
121 Bookmark.create(user.id, status3.id)
122
123 assert {:ok, path} = Pleroma.Export.run(user)
124 assert {:ok, %Pleroma.Upload{}} = Pleroma.Export.upload(path)
125 end
126 end