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