tests for Pleroma.Web.ActivityPub.Publisher
[akkoma] / test / support / factory.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Factory do
6 use ExMachina.Ecto, repo: Pleroma.Repo
7 alias Pleroma.Object
8 alias Pleroma.User
9
10 def participation_factory do
11 conversation = insert(:conversation)
12 user = insert(:user)
13
14 %Pleroma.Conversation.Participation{
15 conversation: conversation,
16 user: user,
17 read: false
18 }
19 end
20
21 def conversation_factory do
22 %Pleroma.Conversation{
23 ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}")
24 }
25 end
26
27 def user_factory do
28 user = %User{
29 name: sequence(:name, &"Test テスト User #{&1}"),
30 email: sequence(:email, &"user#{&1}@example.com"),
31 nickname: sequence(:nickname, &"nick#{&1}"),
32 password_hash: Comeonin.Pbkdf2.hashpwsalt("test"),
33 bio: sequence(:bio, &"Tester Number #{&1}"),
34 info: %{}
35 }
36
37 %{
38 user
39 | ap_id: User.ap_id(user),
40 follower_address: User.ap_followers(user),
41 following_address: User.ap_following(user),
42 following: [User.ap_id(user)]
43 }
44 end
45
46 def note_factory(attrs \\ %{}) do
47 text = sequence(:text, &"This is :moominmamma: note #{&1}")
48
49 user = attrs[:user] || insert(:user)
50
51 data = %{
52 "type" => "Note",
53 "content" => text,
54 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
55 "actor" => user.ap_id,
56 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
57 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
58 "likes" => [],
59 "like_count" => 0,
60 "context" => "2hu",
61 "summary" => "2hu",
62 "tag" => ["2hu"],
63 "emoji" => %{
64 "2hu" => "corndog.png"
65 }
66 }
67
68 %Pleroma.Object{
69 data: merge_attributes(data, Map.get(attrs, :data, %{}))
70 }
71 end
72
73 def direct_note_factory do
74 user2 = insert(:user)
75
76 %Pleroma.Object{data: data} = note_factory()
77 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
78 end
79
80 def article_factory do
81 note_factory()
82 |> Map.put("type", "Article")
83 end
84
85 def tombstone_factory do
86 data = %{
87 "type" => "Tombstone",
88 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
89 "formerType" => "Note",
90 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
91 }
92
93 %Pleroma.Object{
94 data: data
95 }
96 end
97
98 def direct_note_activity_factory do
99 dm = insert(:direct_note)
100
101 data = %{
102 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
103 "type" => "Create",
104 "actor" => dm.data["actor"],
105 "to" => dm.data["to"],
106 "object" => dm.data,
107 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
108 "context" => dm.data["context"]
109 }
110
111 %Pleroma.Activity{
112 data: data,
113 actor: data["actor"],
114 recipients: data["to"]
115 }
116 end
117
118 def note_activity_factory(attrs \\ %{}) do
119 user = attrs[:user] || insert(:user)
120 note = attrs[:note] || insert(:note, user: user)
121 data_attrs = attrs[:data_attrs] || %{}
122 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
123
124 data =
125 %{
126 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
127 "type" => "Create",
128 "actor" => note.data["actor"],
129 "to" => note.data["to"],
130 "object" => note.data["id"],
131 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
132 "context" => note.data["context"]
133 }
134 |> Map.merge(data_attrs)
135
136 %Pleroma.Activity{
137 data: data,
138 actor: data["actor"],
139 recipients: data["to"]
140 }
141 |> Map.merge(attrs)
142 end
143
144 def article_activity_factory do
145 article = insert(:article)
146
147 data = %{
148 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
149 "type" => "Create",
150 "actor" => article.data["actor"],
151 "to" => article.data["to"],
152 "object" => article.data,
153 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
154 "context" => article.data["context"]
155 }
156
157 %Pleroma.Activity{
158 data: data,
159 actor: data["actor"],
160 recipients: data["to"]
161 }
162 end
163
164 def announce_activity_factory(attrs \\ %{}) do
165 note_activity = attrs[:note_activity] || insert(:note_activity)
166 user = attrs[:user] || insert(:user)
167
168 data = %{
169 "type" => "Announce",
170 "actor" => note_activity.actor,
171 "object" => note_activity.data["id"],
172 "to" => [user.follower_address, note_activity.data["actor"]],
173 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
174 "context" => note_activity.data["context"]
175 }
176
177 %Pleroma.Activity{
178 data: data,
179 actor: user.ap_id,
180 recipients: data["to"]
181 }
182 end
183
184 def like_activity_factory do
185 note_activity = insert(:note_activity)
186 object = Object.normalize(note_activity)
187 user = insert(:user)
188
189 data = %{
190 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
191 "actor" => user.ap_id,
192 "type" => "Like",
193 "object" => object.data["id"],
194 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
195 }
196
197 %Pleroma.Activity{
198 data: data
199 }
200 end
201
202 def follow_activity_factory do
203 follower = insert(:user)
204 followed = insert(:user)
205
206 data = %{
207 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
208 "actor" => follower.ap_id,
209 "type" => "Follow",
210 "object" => followed.ap_id,
211 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
212 }
213
214 %Pleroma.Activity{
215 data: data,
216 actor: follower.ap_id
217 }
218 end
219
220 def websub_subscription_factory do
221 %Pleroma.Web.Websub.WebsubServerSubscription{
222 topic: "http://example.org",
223 callback: "http://example.org/callback",
224 secret: "here's a secret",
225 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 100),
226 state: "requested"
227 }
228 end
229
230 def websub_client_subscription_factory do
231 %Pleroma.Web.Websub.WebsubClientSubscription{
232 topic: "http://example.org",
233 secret: "here's a secret",
234 valid_until: nil,
235 state: "requested",
236 subscribers: []
237 }
238 end
239
240 def oauth_app_factory do
241 %Pleroma.Web.OAuth.App{
242 client_name: "Some client",
243 redirect_uris: "https://example.com/callback",
244 scopes: ["read", "write", "follow", "push"],
245 website: "https://example.com",
246 client_id: Ecto.UUID.generate(),
247 client_secret: "aaa;/&bbb"
248 }
249 end
250
251 def instance_factory do
252 %Pleroma.Instances.Instance{
253 host: "domain.com",
254 unreachable_since: nil
255 }
256 end
257
258 def oauth_token_factory do
259 oauth_app = insert(:oauth_app)
260
261 %Pleroma.Web.OAuth.Token{
262 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
263 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
264 user: build(:user),
265 app_id: oauth_app.id,
266 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
267 }
268 end
269
270 def oauth_authorization_factory do
271 %Pleroma.Web.OAuth.Authorization{
272 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
273 scopes: ["read", "write", "follow", "push"],
274 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
275 user: build(:user),
276 app: build(:oauth_app)
277 }
278 end
279
280 def push_subscription_factory do
281 %Pleroma.Web.Push.Subscription{
282 user: build(:user),
283 token: build(:oauth_token),
284 endpoint: "https://example.com/example/1234",
285 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
286 key_p256dh:
287 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
288 data: %{}
289 }
290 end
291
292 def notification_factory do
293 %Pleroma.Notification{
294 user: build(:user)
295 }
296 end
297
298 def scheduled_activity_factory do
299 %Pleroma.ScheduledActivity{
300 user: build(:user),
301 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
302 params: build(:note) |> Map.from_struct() |> Map.get(:data)
303 }
304 end
305
306 def registration_factory do
307 user = insert(:user)
308
309 %Pleroma.Registration{
310 user: user,
311 provider: "twitter",
312 uid: "171799000",
313 info: %{
314 "name" => "John Doe",
315 "email" => "john@doe.com",
316 "nickname" => "johndoe",
317 "description" => "My bio"
318 }
319 }
320 end
321
322 def config_factory do
323 %Pleroma.Web.AdminAPI.Config{
324 key: sequence(:key, &"some_key_#{&1}"),
325 group: "pleroma",
326 value:
327 sequence(
328 :value,
329 fn key ->
330 :erlang.term_to_binary(%{another_key: "#{key}somevalue", another: "#{key}somevalue"})
331 end
332 )
333 }
334 end
335 end