6ce4decbcc144212797e8569c90b2b0c152f67d7
[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
8 require Pleroma.Constants
9
10 alias Pleroma.Object
11 alias Pleroma.User
12
13 @rsa_keys [
14 "test/fixtures/rsa_keys/key_1.pem",
15 "test/fixtures/rsa_keys/key_2.pem",
16 "test/fixtures/rsa_keys/key_3.pem",
17 "test/fixtures/rsa_keys/key_4.pem",
18 "test/fixtures/rsa_keys/key_5.pem"
19 ]
20 |> Enum.map(&File.read!/1)
21
22 def participation_factory do
23 conversation = insert(:conversation)
24 user = insert(:user)
25
26 %Pleroma.Conversation.Participation{
27 conversation: conversation,
28 user: user,
29 read: false
30 }
31 end
32
33 def conversation_factory do
34 %Pleroma.Conversation{
35 ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}")
36 }
37 end
38
39 def instance_factory(attrs \\ %{}) do
40 %Pleroma.Instances.Instance{
41 host: attrs[:domain] || "example.com",
42 nodeinfo: %{version: "2.0", openRegistrations: true},
43 unreachable_since: nil
44 }
45 |> Map.merge(attrs)
46 end
47
48 def user_factory(attrs \\ %{}) do
49 pem = Enum.random(@rsa_keys)
50
51 user = %User{
52 name: sequence(:name, &"Test テスト User #{&1}"),
53 email: sequence(:email, &"user#{&1}@example.com"),
54 nickname: sequence(:nickname, &"nick#{&1}"),
55 password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt("test"),
56 bio: sequence(:bio, &"Tester Number #{&1}"),
57 is_discoverable: true,
58 last_digest_emailed_at: NaiveDateTime.utc_now(),
59 last_refreshed_at: NaiveDateTime.utc_now(),
60 notification_settings: %Pleroma.User.NotificationSetting{},
61 multi_factor_authentication_settings: %Pleroma.MFA.Settings{},
62 ap_enabled: true,
63 keys: pem
64 }
65
66 urls =
67 if attrs[:local] == false do
68 base_domain = attrs[:domain] || Enum.random(["domain1.com", "domain2.com", "domain3.com"])
69
70 ap_id = "https://#{base_domain}/users/#{user.nickname}"
71
72 %{
73 ap_id: ap_id,
74 follower_address: ap_id <> "/followers",
75 following_address: ap_id <> "/following",
76 featured_address: ap_id <> "/collections/featured"
77 }
78 else
79 %{
80 ap_id: User.ap_id(user),
81 follower_address: User.ap_followers(user),
82 following_address: User.ap_following(user),
83 featured_address: User.ap_featured_collection(user)
84 }
85 end
86
87 attrs = Map.delete(attrs, :domain)
88
89 user
90 |> Map.put(:raw_bio, user.bio)
91 |> Map.merge(urls)
92 |> merge_attributes(attrs)
93 end
94
95 def user_relationship_factory(attrs \\ %{}) do
96 source = attrs[:source] || insert(:user)
97 target = attrs[:target] || insert(:user)
98 relationship_type = attrs[:relationship_type] || :block
99
100 %Pleroma.UserRelationship{
101 source_id: source.id,
102 target_id: target.id,
103 relationship_type: relationship_type
104 }
105 end
106
107 def note_factory(attrs \\ %{}) do
108 text = sequence(:text, &"This is :moominmamma: note #{&1}")
109
110 user = attrs[:user] || insert(:user)
111
112 data = %{
113 "type" => "Note",
114 "content" => text,
115 "source" => text,
116 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
117 "actor" => user.ap_id,
118 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
119 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
120 "likes" => [],
121 "like_count" => 0,
122 "context" => "2hu",
123 "summary" => "2hu",
124 "tag" => ["2hu"],
125 "emoji" => %{
126 "2hu" => "corndog.png"
127 }
128 }
129
130 %Pleroma.Object{
131 data: merge_attributes(data, Map.get(attrs, :data, %{}))
132 }
133 end
134
135 def attachment_factory(attrs \\ %{}) do
136 user = attrs[:user] || insert(:user)
137
138 data =
139 attachment_data(user.ap_id, nil)
140 |> Map.put("id", Pleroma.Web.ActivityPub.Utils.generate_object_id())
141
142 %Pleroma.Object{
143 data: merge_attributes(data, Map.get(attrs, :data, %{}))
144 }
145 end
146
147 def attachment_note_factory(attrs \\ %{}) do
148 user = attrs[:user] || insert(:user)
149 {length, attrs} = Map.pop(attrs, :length, 1)
150
151 data = %{
152 "attachment" =>
153 Stream.repeatedly(fn -> attachment_data(user.ap_id, attrs[:href]) end)
154 |> Enum.take(length)
155 }
156
157 build(:note, Map.put(attrs, :data, data))
158 end
159
160 defp attachment_data(ap_id, href) do
161 href = href || sequence(:href, &"#{Pleroma.Web.Endpoint.url()}/media/#{&1}.jpg")
162
163 %{
164 "url" => [
165 %{
166 "href" => href,
167 "type" => "Link",
168 "mediaType" => "image/jpeg"
169 }
170 ],
171 "name" => "some name",
172 "type" => "Document",
173 "actor" => ap_id,
174 "mediaType" => "image/jpeg"
175 }
176 end
177
178 def followers_only_note_factory(attrs \\ %{}) do
179 %Pleroma.Object{data: data} = note_factory(attrs)
180 %Pleroma.Object{data: Map.merge(data, %{"to" => [data["actor"] <> "/followers"]})}
181 end
182
183 def audio_factory(attrs \\ %{}) do
184 text = sequence(:text, &"lain radio episode #{&1}")
185
186 user = attrs[:user] || insert(:user)
187
188 data = %{
189 "type" => "Audio",
190 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
191 "artist" => "lain",
192 "title" => text,
193 "album" => "lain radio",
194 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
195 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
196 "actor" => user.ap_id,
197 "length" => 180_000
198 }
199
200 %Pleroma.Object{
201 data: merge_attributes(data, Map.get(attrs, :data, %{}))
202 }
203 end
204
205 def listen_factory do
206 audio = insert(:audio)
207
208 data = %{
209 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
210 "type" => "Listen",
211 "actor" => audio.data["actor"],
212 "to" => audio.data["to"],
213 "object" => audio.data,
214 "published" => audio.data["published"]
215 }
216
217 %Pleroma.Activity{
218 data: data,
219 actor: data["actor"],
220 recipients: data["to"]
221 }
222 end
223
224 def direct_note_factory do
225 user2 = insert(:user)
226
227 %Pleroma.Object{data: data} = note_factory()
228 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
229 end
230
231 def article_factory do
232 %Pleroma.Object{data: data} = note_factory()
233 %Pleroma.Object{data: Map.merge(data, %{"type" => "Article"})}
234 end
235
236 def tombstone_factory(attrs) do
237 data = %{
238 "type" => "Tombstone",
239 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
240 "formerType" => "Note",
241 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
242 }
243
244 %Pleroma.Object{
245 data: data
246 }
247 |> merge_attributes(attrs)
248 end
249
250 def question_factory(attrs \\ %{}) do
251 user = attrs[:user] || insert(:user)
252
253 data = %{
254 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
255 "type" => "Question",
256 "actor" => user.ap_id,
257 "attributedTo" => user.ap_id,
258 "attachment" => [],
259 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
260 "cc" => [user.follower_address],
261 "context" => Pleroma.Web.ActivityPub.Utils.generate_context_id(),
262 "closed" => DateTime.utc_now() |> DateTime.add(86_400) |> DateTime.to_iso8601(),
263 "oneOf" => [
264 %{
265 "type" => "Note",
266 "name" => "chocolate",
267 "replies" => %{"totalItems" => 0, "type" => "Collection"}
268 },
269 %{
270 "type" => "Note",
271 "name" => "vanilla",
272 "replies" => %{"totalItems" => 0, "type" => "Collection"}
273 }
274 ]
275 }
276
277 %Pleroma.Object{
278 data: merge_attributes(data, Map.get(attrs, :data, %{}))
279 }
280 end
281
282 def direct_note_activity_factory do
283 dm = insert(:direct_note)
284
285 data = %{
286 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
287 "type" => "Create",
288 "actor" => dm.data["actor"],
289 "to" => dm.data["to"],
290 "object" => dm.data,
291 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
292 "context" => dm.data["context"]
293 }
294
295 %Pleroma.Activity{
296 data: data,
297 actor: data["actor"],
298 recipients: data["to"]
299 }
300 end
301
302 def add_activity_factory(attrs \\ %{}) do
303 featured_collection_activity(attrs, "Add")
304 end
305
306 def remove_activity_factor(attrs \\ %{}) do
307 featured_collection_activity(attrs, "Remove")
308 end
309
310 defp featured_collection_activity(attrs, type) do
311 user = attrs[:user] || insert(:user)
312 note = attrs[:note] || insert(:note, user: user)
313
314 data_attrs =
315 attrs
316 |> Map.get(:data_attrs, %{})
317 |> Map.put(:type, type)
318
319 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
320
321 data =
322 %{
323 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
324 "target" => user.featured_address,
325 "object" => note.data["object"],
326 "actor" => note.data["actor"],
327 "type" => "Add",
328 "to" => [Pleroma.Constants.as_public()],
329 "cc" => [user.follower_address]
330 }
331 |> Map.merge(data_attrs)
332
333 %Pleroma.Activity{
334 data: data,
335 actor: data["actor"],
336 recipients: data["to"]
337 }
338 |> Map.merge(attrs)
339 end
340
341 def followers_only_note_activity_factory(attrs \\ %{}) do
342 user = attrs[:user] || insert(:user)
343 note = insert(:followers_only_note, user: user)
344
345 data_attrs = attrs[:data_attrs] || %{}
346 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
347
348 data =
349 %{
350 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
351 "type" => "Create",
352 "actor" => note.data["actor"],
353 "to" => note.data["to"],
354 "object" => note.data,
355 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
356 "context" => note.data["context"]
357 }
358 |> Map.merge(data_attrs)
359
360 %Pleroma.Activity{
361 data: data,
362 actor: data["actor"],
363 recipients: data["to"]
364 }
365 |> Map.merge(attrs)
366 end
367
368 def note_activity_factory(attrs \\ %{}) do
369 user = attrs[:user] || insert(:user)
370 note = attrs[:note] || insert(:note, user: user)
371
372 data_attrs = attrs[:data_attrs] || %{}
373 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
374
375 data =
376 %{
377 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
378 "type" => "Create",
379 "actor" => note.data["actor"],
380 "to" => note.data["to"],
381 "object" => note.data["id"],
382 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
383 "context" => note.data["context"]
384 }
385 |> Map.merge(data_attrs)
386
387 %Pleroma.Activity{
388 data: data,
389 actor: data["actor"],
390 recipients: data["to"]
391 }
392 |> Map.merge(attrs)
393 end
394
395 def article_activity_factory do
396 article = insert(:article)
397
398 data = %{
399 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
400 "type" => "Create",
401 "actor" => article.data["actor"],
402 "to" => article.data["to"],
403 "object" => article.data,
404 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
405 "context" => article.data["context"]
406 }
407
408 %Pleroma.Activity{
409 data: data,
410 actor: data["actor"],
411 recipients: data["to"]
412 }
413 end
414
415 def announce_activity_factory(attrs \\ %{}) do
416 note_activity = attrs[:note_activity] || insert(:note_activity)
417 user = attrs[:user] || insert(:user)
418
419 data = %{
420 "type" => "Announce",
421 "actor" => note_activity.actor,
422 "object" => note_activity.data["id"],
423 "to" => [user.follower_address, note_activity.data["actor"]],
424 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
425 "context" => note_activity.data["context"]
426 }
427
428 %Pleroma.Activity{
429 data: data,
430 actor: user.ap_id,
431 recipients: data["to"]
432 }
433 end
434
435 def like_activity_factory(attrs \\ %{}) do
436 note_activity = attrs[:note_activity] || insert(:note_activity)
437 object = Object.normalize(note_activity, fetch: false)
438 user = insert(:user)
439
440 data =
441 %{
442 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
443 "actor" => user.ap_id,
444 "type" => "Like",
445 "object" => object.data["id"],
446 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
447 }
448 |> Map.merge(attrs[:data_attrs] || %{})
449
450 %Pleroma.Activity{
451 data: data
452 }
453 end
454
455 def follow_activity_factory(attrs \\ %{}) do
456 follower = attrs[:follower] || insert(:user)
457 followed = attrs[:followed] || insert(:user)
458
459 data = %{
460 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
461 "actor" => follower.ap_id,
462 "type" => "Follow",
463 "object" => followed.ap_id,
464 "state" => attrs[:state] || "pending",
465 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
466 }
467
468 %Pleroma.Activity{
469 data: data,
470 actor: follower.ap_id
471 }
472 |> Map.merge(attrs)
473 end
474
475 def report_activity_factory(attrs \\ %{}) do
476 user = attrs[:user] || insert(:user)
477 activity = attrs[:activity] || insert(:note_activity)
478 state = attrs[:state] || "open"
479
480 data = %{
481 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
482 "actor" => user.ap_id,
483 "type" => "Flag",
484 "object" => [activity.actor, activity.data["id"]],
485 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
486 "to" => [],
487 "cc" => [activity.actor],
488 "context" => activity.data["context"],
489 "state" => state
490 }
491
492 %Pleroma.Activity{
493 data: data,
494 actor: data["actor"],
495 recipients: data["to"] ++ data["cc"]
496 }
497 end
498
499 def question_activity_factory(attrs \\ %{}) do
500 user = attrs[:user] || insert(:user)
501 question = attrs[:question] || insert(:question, user: user)
502
503 data_attrs = attrs[:data_attrs] || %{}
504 attrs = Map.drop(attrs, [:user, :question, :data_attrs])
505
506 data =
507 %{
508 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
509 "type" => "Create",
510 "actor" => question.data["actor"],
511 "to" => question.data["to"],
512 "object" => question.data["id"],
513 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
514 "context" => question.data["context"]
515 }
516 |> Map.merge(data_attrs)
517
518 %Pleroma.Activity{
519 data: data,
520 actor: data["actor"],
521 recipients: data["to"]
522 }
523 |> Map.merge(attrs)
524 end
525
526 def delete_activity_factory(attrs \\ %{}) do
527 user = attrs[:user] || insert(:user)
528 note_activity = attrs[:note_activity] || insert(:note_activity, user: user)
529
530 data_attrs = attrs[:data_attrs] || %{}
531 attrs = Map.drop(attrs, [:user, :data_attrs])
532
533 data =
534 %{
535 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
536 "type" => "Delete",
537 "actor" => note_activity.data["actor"],
538 "to" => note_activity.data["to"],
539 "object" => note_activity.data["id"],
540 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
541 "context" => note_activity.data["context"]
542 }
543 |> Map.merge(data_attrs)
544
545 %Pleroma.Activity{
546 data: data,
547 actor: data["actor"],
548 recipients: data["to"]
549 }
550 |> Map.merge(attrs)
551 end
552
553 def oauth_app_factory do
554 %Pleroma.Web.OAuth.App{
555 client_name: sequence(:client_name, &"Some client #{&1}"),
556 redirect_uris: "https://example.com/callback",
557 scopes: ["read", "write", "follow", "push", "admin"],
558 website: "https://example.com",
559 client_id: Ecto.UUID.generate(),
560 client_secret: "aaa;/&bbb"
561 }
562 end
563
564 def oauth_token_factory(attrs \\ %{}) do
565 scopes = Map.get(attrs, :scopes, ["read"])
566 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
567 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
568
569 valid_until =
570 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
571
572 %Pleroma.Web.OAuth.Token{
573 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
574 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
575 scopes: scopes,
576 user: user,
577 app: oauth_app,
578 valid_until: valid_until
579 }
580 end
581
582 def oauth_admin_token_factory(attrs \\ %{}) do
583 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
584
585 scopes =
586 attrs
587 |> Map.get(:scopes, ["admin"])
588 |> Kernel.++(["admin"])
589 |> Enum.uniq()
590
591 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
592 oauth_token_factory(attrs)
593 end
594
595 def oauth_authorization_factory do
596 %Pleroma.Web.OAuth.Authorization{
597 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
598 scopes: ["read", "write", "follow", "push"],
599 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
600 user: build(:user),
601 app: build(:oauth_app)
602 }
603 end
604
605 def push_subscription_factory do
606 %Pleroma.Web.Push.Subscription{
607 user: build(:user),
608 token: build(:oauth_token),
609 endpoint: "https://example.com/example/1234",
610 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
611 key_p256dh:
612 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
613 data: %{}
614 }
615 end
616
617 def notification_factory do
618 %Pleroma.Notification{
619 user: build(:user)
620 }
621 end
622
623 def scheduled_activity_factory do
624 %Pleroma.ScheduledActivity{
625 user: build(:user),
626 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
627 params: build(:note) |> Map.from_struct() |> Map.get(:data)
628 }
629 end
630
631 def registration_factory do
632 user = insert(:user)
633
634 %Pleroma.Registration{
635 user: user,
636 provider: "twitter",
637 uid: "171799000",
638 info: %{
639 "name" => "John Doe",
640 "email" => "john@doe.com",
641 "nickname" => "johndoe",
642 "description" => "My bio"
643 }
644 }
645 end
646
647 def config_factory(attrs \\ %{}) do
648 %Pleroma.ConfigDB{
649 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
650 group: :pleroma,
651 value:
652 sequence(
653 :value,
654 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
655 )
656 }
657 |> merge_attributes(attrs)
658 end
659
660 def marker_factory do
661 %Pleroma.Marker{
662 user: build(:user),
663 timeline: "notifications",
664 lock_version: 0,
665 last_read_id: "1"
666 }
667 end
668
669 def mfa_token_factory do
670 %Pleroma.MFA.Token{
671 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
672 authorization: build(:oauth_authorization),
673 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
674 user: build(:user)
675 }
676 end
677
678 def filter_factory do
679 %Pleroma.Filter{
680 user: build(:user),
681 filter_id: sequence(:filter_id, & &1),
682 phrase: "cofe",
683 context: ["home"]
684 }
685 end
686
687 def announcement_factory(params \\ %{}) do
688 data = Map.get(params, :data, %{})
689
690 {_, params} = Map.pop(params, :data)
691
692 %Pleroma.Announcement{
693 data: Map.merge(%{"content" => "test announcement", "all_day" => false}, data)
694 }
695 |> Map.merge(params)
696 |> Pleroma.Announcement.add_rendered_properties()
697 end
698
699 def frontend_setting_profile_factory(params \\ %{}) do
700 %Pleroma.Akkoma.FrontendSettingsProfile{
701 user: build(:user),
702 frontend_name: "akkoma-fe",
703 profile_name: "default",
704 settings: %{"test" => "test"},
705 version: 1
706 }
707 |> Map.merge(params)
708 end
709
710 def delivery_factory(params \\ %{}) do
711 object = Map.get(params, :object, build(:note))
712 user = Map.get(params, :user, build(:user))
713
714 %Pleroma.Delivery{
715 object: object,
716 user: user
717 }
718 end
719 end