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