904987aaf464d5ca64dd215ba41e183b535b2670
[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 do
456 follower = insert(:user)
457 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 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
465 }
466
467 %Pleroma.Activity{
468 data: data,
469 actor: follower.ap_id
470 }
471 end
472
473 def report_activity_factory(attrs \\ %{}) do
474 user = attrs[:user] || insert(:user)
475 activity = attrs[:activity] || insert(:note_activity)
476 state = attrs[:state] || "open"
477
478 data = %{
479 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
480 "actor" => user.ap_id,
481 "type" => "Flag",
482 "object" => [activity.actor, activity.data["id"]],
483 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
484 "to" => [],
485 "cc" => [activity.actor],
486 "context" => activity.data["context"],
487 "state" => state
488 }
489
490 %Pleroma.Activity{
491 data: data,
492 actor: data["actor"],
493 recipients: data["to"] ++ data["cc"]
494 }
495 end
496
497 def question_activity_factory(attrs \\ %{}) do
498 user = attrs[:user] || insert(:user)
499 question = attrs[:question] || insert(:question, user: user)
500
501 data_attrs = attrs[:data_attrs] || %{}
502 attrs = Map.drop(attrs, [:user, :question, :data_attrs])
503
504 data =
505 %{
506 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
507 "type" => "Create",
508 "actor" => question.data["actor"],
509 "to" => question.data["to"],
510 "object" => question.data["id"],
511 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
512 "context" => question.data["context"]
513 }
514 |> Map.merge(data_attrs)
515
516 %Pleroma.Activity{
517 data: data,
518 actor: data["actor"],
519 recipients: data["to"]
520 }
521 |> Map.merge(attrs)
522 end
523
524 def delete_activity_factory(attrs \\ %{}) do
525 user = attrs[:user] || insert(:user)
526 note_activity = attrs[:note_activity] || insert(:note_activity, user: user)
527
528 data_attrs = attrs[:data_attrs] || %{}
529 attrs = Map.drop(attrs, [:user, :data_attrs])
530
531 data =
532 %{
533 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
534 "type" => "Delete",
535 "actor" => note_activity.data["actor"],
536 "to" => note_activity.data["to"],
537 "object" => note_activity.data["id"],
538 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
539 "context" => note_activity.data["context"]
540 }
541 |> Map.merge(data_attrs)
542
543 %Pleroma.Activity{
544 data: data,
545 actor: data["actor"],
546 recipients: data["to"]
547 }
548 |> Map.merge(attrs)
549 end
550
551 def oauth_app_factory do
552 %Pleroma.Web.OAuth.App{
553 client_name: sequence(:client_name, &"Some client #{&1}"),
554 redirect_uris: "https://example.com/callback",
555 scopes: ["read", "write", "follow", "push", "admin"],
556 website: "https://example.com",
557 client_id: Ecto.UUID.generate(),
558 client_secret: "aaa;/&bbb"
559 }
560 end
561
562 def oauth_token_factory(attrs \\ %{}) do
563 scopes = Map.get(attrs, :scopes, ["read"])
564 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
565 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
566
567 valid_until =
568 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
569
570 %Pleroma.Web.OAuth.Token{
571 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
572 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
573 scopes: scopes,
574 user: user,
575 app: oauth_app,
576 valid_until: valid_until
577 }
578 end
579
580 def oauth_admin_token_factory(attrs \\ %{}) do
581 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
582
583 scopes =
584 attrs
585 |> Map.get(:scopes, ["admin"])
586 |> Kernel.++(["admin"])
587 |> Enum.uniq()
588
589 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
590 oauth_token_factory(attrs)
591 end
592
593 def oauth_authorization_factory do
594 %Pleroma.Web.OAuth.Authorization{
595 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
596 scopes: ["read", "write", "follow", "push"],
597 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
598 user: build(:user),
599 app: build(:oauth_app)
600 }
601 end
602
603 def push_subscription_factory do
604 %Pleroma.Web.Push.Subscription{
605 user: build(:user),
606 token: build(:oauth_token),
607 endpoint: "https://example.com/example/1234",
608 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
609 key_p256dh:
610 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
611 data: %{}
612 }
613 end
614
615 def notification_factory do
616 %Pleroma.Notification{
617 user: build(:user)
618 }
619 end
620
621 def scheduled_activity_factory do
622 %Pleroma.ScheduledActivity{
623 user: build(:user),
624 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
625 params: build(:note) |> Map.from_struct() |> Map.get(:data)
626 }
627 end
628
629 def registration_factory do
630 user = insert(:user)
631
632 %Pleroma.Registration{
633 user: user,
634 provider: "twitter",
635 uid: "171799000",
636 info: %{
637 "name" => "John Doe",
638 "email" => "john@doe.com",
639 "nickname" => "johndoe",
640 "description" => "My bio"
641 }
642 }
643 end
644
645 def config_factory(attrs \\ %{}) do
646 %Pleroma.ConfigDB{
647 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
648 group: :pleroma,
649 value:
650 sequence(
651 :value,
652 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
653 )
654 }
655 |> merge_attributes(attrs)
656 end
657
658 def marker_factory do
659 %Pleroma.Marker{
660 user: build(:user),
661 timeline: "notifications",
662 lock_version: 0,
663 last_read_id: "1"
664 }
665 end
666
667 def mfa_token_factory do
668 %Pleroma.MFA.Token{
669 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
670 authorization: build(:oauth_authorization),
671 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
672 user: build(:user)
673 }
674 end
675
676 def filter_factory do
677 %Pleroma.Filter{
678 user: build(:user),
679 filter_id: sequence(:filter_id, & &1),
680 phrase: "cofe",
681 context: ["home"]
682 }
683 end
684
685 def announcement_factory(params \\ %{}) do
686 data = Map.get(params, :data, %{})
687
688 {_, params} = Map.pop(params, :data)
689
690 %Pleroma.Announcement{
691 data: Map.merge(%{"content" => "test announcement", "all_day" => false}, data)
692 }
693 |> Map.merge(params)
694 |> Pleroma.Announcement.add_rendered_properties()
695 end
696
697 def frontend_setting_profile_factory(params \\ %{}) do
698 %Pleroma.Akkoma.FrontendSettingsProfile{
699 user: build(:user),
700 frontend_name: "akkoma-fe",
701 profile_name: "default",
702 settings: %{"test" => "test"},
703 version: 1
704 }
705 |> Map.merge(params)
706 end
707
708 def delivery_factory(params \\ %{}) do
709 object = Map.get(params, :object, build(:note))
710 user = Map.get(params, :user, build(:user))
711
712 %Pleroma.Delivery{
713 object: object,
714 user: user
715 }
716 end
717 end