Merge branch 'ecto-rollback-in-test-env' into 'develop'
[akkoma] / test / support / factory.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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(attrs \\ %{}) 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: Pleroma.Password.Pbkdf2.hash_pwd_salt("test"),
33 bio: sequence(:bio, &"Tester Number #{&1}"),
34 is_discoverable: true,
35 last_digest_emailed_at: NaiveDateTime.utc_now(),
36 last_refreshed_at: NaiveDateTime.utc_now(),
37 notification_settings: %Pleroma.User.NotificationSetting{},
38 multi_factor_authentication_settings: %Pleroma.MFA.Settings{},
39 ap_enabled: true
40 }
41
42 urls =
43 if attrs[:local] == false do
44 base_domain = Enum.random(["domain1.com", "domain2.com", "domain3.com"])
45
46 ap_id = "https://#{base_domain}/users/#{user.nickname}"
47
48 %{
49 ap_id: ap_id,
50 follower_address: ap_id <> "/followers",
51 following_address: ap_id <> "/following"
52 }
53 else
54 %{
55 ap_id: User.ap_id(user),
56 follower_address: User.ap_followers(user),
57 following_address: User.ap_following(user)
58 }
59 end
60
61 user
62 |> Map.put(:raw_bio, user.bio)
63 |> Map.merge(urls)
64 |> merge_attributes(attrs)
65 end
66
67 def user_relationship_factory(attrs \\ %{}) do
68 source = attrs[:source] || insert(:user)
69 target = attrs[:target] || insert(:user)
70 relationship_type = attrs[:relationship_type] || :block
71
72 %Pleroma.UserRelationship{
73 source_id: source.id,
74 target_id: target.id,
75 relationship_type: relationship_type
76 }
77 end
78
79 def note_factory(attrs \\ %{}) do
80 text = sequence(:text, &"This is :moominmamma: note #{&1}")
81
82 user = attrs[:user] || insert(:user)
83
84 data = %{
85 "type" => "Note",
86 "content" => text,
87 "source" => text,
88 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
89 "actor" => user.ap_id,
90 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
91 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
92 "likes" => [],
93 "like_count" => 0,
94 "context" => "2hu",
95 "summary" => "2hu",
96 "tag" => ["2hu"],
97 "emoji" => %{
98 "2hu" => "corndog.png"
99 }
100 }
101
102 %Pleroma.Object{
103 data: merge_attributes(data, Map.get(attrs, :data, %{}))
104 }
105 end
106
107 def attachment_note_factory(attrs \\ %{}) do
108 user = attrs[:user] || insert(:user)
109 {length, attrs} = Map.pop(attrs, :length, 1)
110
111 data = %{
112 "attachment" =>
113 Stream.repeatedly(fn -> attachment_data(user.ap_id, attrs[:href]) end)
114 |> Enum.take(length)
115 }
116
117 build(:note, Map.put(attrs, :data, data))
118 end
119
120 defp attachment_data(ap_id, href) do
121 href = href || sequence(:href, &"#{Pleroma.Web.Endpoint.url()}/media/#{&1}.jpg")
122
123 %{
124 "url" => [
125 %{
126 "href" => href,
127 "type" => "Link",
128 "mediaType" => "image/jpeg"
129 }
130 ],
131 "name" => "some name",
132 "type" => "Document",
133 "actor" => ap_id,
134 "mediaType" => "image/jpeg"
135 }
136 end
137
138 def audio_factory(attrs \\ %{}) do
139 text = sequence(:text, &"lain radio episode #{&1}")
140
141 user = attrs[:user] || insert(:user)
142
143 data = %{
144 "type" => "Audio",
145 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
146 "artist" => "lain",
147 "title" => text,
148 "album" => "lain radio",
149 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
150 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
151 "actor" => user.ap_id,
152 "length" => 180_000
153 }
154
155 %Pleroma.Object{
156 data: merge_attributes(data, Map.get(attrs, :data, %{}))
157 }
158 end
159
160 def listen_factory do
161 audio = insert(:audio)
162
163 data = %{
164 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
165 "type" => "Listen",
166 "actor" => audio.data["actor"],
167 "to" => audio.data["to"],
168 "object" => audio.data,
169 "published" => audio.data["published"]
170 }
171
172 %Pleroma.Activity{
173 data: data,
174 actor: data["actor"],
175 recipients: data["to"]
176 }
177 end
178
179 def direct_note_factory do
180 user2 = insert(:user)
181
182 %Pleroma.Object{data: data} = note_factory()
183 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
184 end
185
186 def article_factory do
187 note_factory()
188 |> Map.put("type", "Article")
189 end
190
191 def tombstone_factory do
192 data = %{
193 "type" => "Tombstone",
194 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
195 "formerType" => "Note",
196 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
197 }
198
199 %Pleroma.Object{
200 data: data
201 }
202 end
203
204 def direct_note_activity_factory do
205 dm = insert(:direct_note)
206
207 data = %{
208 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
209 "type" => "Create",
210 "actor" => dm.data["actor"],
211 "to" => dm.data["to"],
212 "object" => dm.data,
213 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
214 "context" => dm.data["context"]
215 }
216
217 %Pleroma.Activity{
218 data: data,
219 actor: data["actor"],
220 recipients: data["to"]
221 }
222 end
223
224 def note_activity_factory(attrs \\ %{}) do
225 user = attrs[:user] || insert(:user)
226 note = attrs[:note] || insert(:note, user: user)
227
228 data_attrs = attrs[:data_attrs] || %{}
229 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
230
231 data =
232 %{
233 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
234 "type" => "Create",
235 "actor" => note.data["actor"],
236 "to" => note.data["to"],
237 "object" => note.data["id"],
238 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
239 "context" => note.data["context"]
240 }
241 |> Map.merge(data_attrs)
242
243 %Pleroma.Activity{
244 data: data,
245 actor: data["actor"],
246 recipients: data["to"]
247 }
248 |> Map.merge(attrs)
249 end
250
251 def article_activity_factory do
252 article = insert(:article)
253
254 data = %{
255 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
256 "type" => "Create",
257 "actor" => article.data["actor"],
258 "to" => article.data["to"],
259 "object" => article.data,
260 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
261 "context" => article.data["context"]
262 }
263
264 %Pleroma.Activity{
265 data: data,
266 actor: data["actor"],
267 recipients: data["to"]
268 }
269 end
270
271 def announce_activity_factory(attrs \\ %{}) do
272 note_activity = attrs[:note_activity] || insert(:note_activity)
273 user = attrs[:user] || insert(:user)
274
275 data = %{
276 "type" => "Announce",
277 "actor" => note_activity.actor,
278 "object" => note_activity.data["id"],
279 "to" => [user.follower_address, note_activity.data["actor"]],
280 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
281 "context" => note_activity.data["context"]
282 }
283
284 %Pleroma.Activity{
285 data: data,
286 actor: user.ap_id,
287 recipients: data["to"]
288 }
289 end
290
291 def like_activity_factory(attrs \\ %{}) do
292 note_activity = attrs[:note_activity] || insert(:note_activity)
293 object = Object.normalize(note_activity, fetch: false)
294 user = insert(:user)
295
296 data =
297 %{
298 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
299 "actor" => user.ap_id,
300 "type" => "Like",
301 "object" => object.data["id"],
302 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
303 }
304 |> Map.merge(attrs[:data_attrs] || %{})
305
306 %Pleroma.Activity{
307 data: data
308 }
309 end
310
311 def follow_activity_factory do
312 follower = insert(:user)
313 followed = insert(:user)
314
315 data = %{
316 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
317 "actor" => follower.ap_id,
318 "type" => "Follow",
319 "object" => followed.ap_id,
320 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
321 }
322
323 %Pleroma.Activity{
324 data: data,
325 actor: follower.ap_id
326 }
327 end
328
329 def report_activity_factory(attrs \\ %{}) do
330 user = attrs[:user] || insert(:user)
331 activity = attrs[:activity] || insert(:note_activity)
332 state = attrs[:state] || "open"
333
334 data = %{
335 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
336 "actor" => user.ap_id,
337 "type" => "Flag",
338 "object" => [activity.actor, activity.data["id"]],
339 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
340 "to" => [],
341 "cc" => [activity.actor],
342 "context" => activity.data["context"],
343 "state" => state
344 }
345
346 %Pleroma.Activity{
347 data: data,
348 actor: data["actor"],
349 recipients: data["to"] ++ data["cc"]
350 }
351 end
352
353 def oauth_app_factory do
354 %Pleroma.Web.OAuth.App{
355 client_name: sequence(:client_name, &"Some client #{&1}"),
356 redirect_uris: "https://example.com/callback",
357 scopes: ["read", "write", "follow", "push", "admin"],
358 website: "https://example.com",
359 client_id: Ecto.UUID.generate(),
360 client_secret: "aaa;/&bbb"
361 }
362 end
363
364 def instance_factory do
365 %Pleroma.Instances.Instance{
366 host: "domain.com",
367 unreachable_since: nil
368 }
369 end
370
371 def oauth_token_factory(attrs \\ %{}) do
372 scopes = Map.get(attrs, :scopes, ["read"])
373 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
374 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
375
376 valid_until =
377 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
378
379 %Pleroma.Web.OAuth.Token{
380 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
381 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
382 scopes: scopes,
383 user: user,
384 app: oauth_app,
385 valid_until: valid_until
386 }
387 end
388
389 def oauth_admin_token_factory(attrs \\ %{}) do
390 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
391
392 scopes =
393 attrs
394 |> Map.get(:scopes, ["admin"])
395 |> Kernel.++(["admin"])
396 |> Enum.uniq()
397
398 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
399 oauth_token_factory(attrs)
400 end
401
402 def oauth_authorization_factory do
403 %Pleroma.Web.OAuth.Authorization{
404 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
405 scopes: ["read", "write", "follow", "push"],
406 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
407 user: build(:user),
408 app: build(:oauth_app)
409 }
410 end
411
412 def push_subscription_factory do
413 %Pleroma.Web.Push.Subscription{
414 user: build(:user),
415 token: build(:oauth_token),
416 endpoint: "https://example.com/example/1234",
417 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
418 key_p256dh:
419 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
420 data: %{}
421 }
422 end
423
424 def notification_factory do
425 %Pleroma.Notification{
426 user: build(:user)
427 }
428 end
429
430 def scheduled_activity_factory do
431 %Pleroma.ScheduledActivity{
432 user: build(:user),
433 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
434 params: build(:note) |> Map.from_struct() |> Map.get(:data)
435 }
436 end
437
438 def registration_factory do
439 user = insert(:user)
440
441 %Pleroma.Registration{
442 user: user,
443 provider: "twitter",
444 uid: "171799000",
445 info: %{
446 "name" => "John Doe",
447 "email" => "john@doe.com",
448 "nickname" => "johndoe",
449 "description" => "My bio"
450 }
451 }
452 end
453
454 def config_factory(attrs \\ %{}) do
455 %Pleroma.ConfigDB{
456 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
457 group: :pleroma,
458 value:
459 sequence(
460 :value,
461 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
462 )
463 }
464 |> merge_attributes(attrs)
465 end
466
467 def marker_factory do
468 %Pleroma.Marker{
469 user: build(:user),
470 timeline: "notifications",
471 lock_version: 0,
472 last_read_id: "1"
473 }
474 end
475
476 def mfa_token_factory do
477 %Pleroma.MFA.Token{
478 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
479 authorization: build(:oauth_authorization),
480 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
481 user: build(:user)
482 }
483 end
484
485 def filter_factory do
486 %Pleroma.Filter{
487 user: build(:user),
488 filter_id: sequence(:filter_id, & &1),
489 phrase: "cofe",
490 context: ["home"]
491 }
492 end
493 end