1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Factory do
6 use ExMachina.Ecto, repo: Pleroma.Repo
8 require Pleroma.Constants
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"
20 |> Enum.map(&File.read!/1)
22 def participation_factory do
23 conversation = insert(:conversation)
26 %Pleroma.Conversation.Participation{
27 conversation: conversation,
33 def conversation_factory do
34 %Pleroma.Conversation{
35 ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}")
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
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
53 "$argon2id$v=19$m=65536,t=8,p=2$FEAarFuiOsROO24NHIHMYw$oxdaz2fTPpuU+dYCl60FsqE65T1Tjy6lGikKfmql4xo"
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{},
71 if attrs[:local] == false do
72 base_domain = attrs[:domain] || Enum.random(["domain1.com", "domain2.com", "domain3.com"])
74 ap_id = "https://#{base_domain}/users/#{user.nickname}"
78 follower_address: ap_id <> "/followers",
79 following_address: ap_id <> "/following",
80 featured_address: ap_id <> "/collections/featured"
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)
91 attrs = Map.delete(attrs, :domain)
94 |> Map.put(:raw_bio, user.bio)
96 |> merge_attributes(attrs)
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
104 %Pleroma.UserRelationship{
105 source_id: source.id,
106 target_id: target.id,
107 relationship_type: relationship_type
111 def note_factory(attrs \\ %{}) do
112 text = sequence(:text, &"This is :moominmamma: note #{&1}")
114 user = attrs[:user] || insert(:user)
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(),
130 "2hu" => "corndog.png"
135 data: merge_attributes(data, Map.get(attrs, :data, %{}))
139 def attachment_factory(attrs \\ %{}) do
140 user = attrs[:user] || insert(:user)
143 attachment_data(user.ap_id, nil)
144 |> Map.put("id", Pleroma.Web.ActivityPub.Utils.generate_object_id())
147 data: merge_attributes(data, Map.get(attrs, :data, %{}))
151 def attachment_note_factory(attrs \\ %{}) do
152 user = attrs[:user] || insert(:user)
153 {length, attrs} = Map.pop(attrs, :length, 1)
157 Stream.repeatedly(fn -> attachment_data(user.ap_id, attrs[:href]) end)
161 build(:note, Map.put(attrs, :data, data))
164 defp attachment_data(ap_id, href) do
165 href = href || sequence(:href, &"#{Pleroma.Web.Endpoint.url()}/media/#{&1}.jpg")
172 "mediaType" => "image/jpeg"
175 "name" => "some name",
176 "type" => "Document",
178 "mediaType" => "image/jpeg"
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"]})}
187 def audio_factory(attrs \\ %{}) do
188 text = sequence(:text, &"lain radio episode #{&1}")
190 user = attrs[:user] || insert(:user)
194 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
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,
205 data: merge_attributes(data, Map.get(attrs, :data, %{}))
209 def listen_factory do
210 audio = insert(:audio)
213 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
215 "actor" => audio.data["actor"],
216 "to" => audio.data["to"],
217 "object" => audio.data,
218 "published" => audio.data["published"]
223 actor: data["actor"],
224 recipients: data["to"]
228 def direct_note_factory do
229 user2 = insert(:user)
231 %Pleroma.Object{data: data} = note_factory()
232 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
235 def article_factory do
236 %Pleroma.Object{data: data} = note_factory()
237 %Pleroma.Object{data: Map.merge(data, %{"type" => "Article"})}
240 def tombstone_factory(attrs) do
242 "type" => "Tombstone",
243 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
244 "formerType" => "Note",
245 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
251 |> merge_attributes(attrs)
254 def question_factory(attrs \\ %{}) do
255 user = attrs[:user] || insert(:user)
258 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
259 "type" => "Question",
260 "actor" => user.ap_id,
261 "attributedTo" => user.ap_id,
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(),
270 "name" => "chocolate",
271 "replies" => %{"totalItems" => 0, "type" => "Collection"}
276 "replies" => %{"totalItems" => 0, "type" => "Collection"}
282 data: merge_attributes(data, Map.get(attrs, :data, %{}))
286 def direct_note_activity_factory do
287 dm = insert(:direct_note)
290 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
292 "actor" => dm.data["actor"],
293 "to" => dm.data["to"],
295 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
296 "context" => dm.data["context"]
301 actor: data["actor"],
302 recipients: data["to"]
306 def add_activity_factory(attrs \\ %{}) do
307 featured_collection_activity(attrs, "Add")
310 def remove_activity_factor(attrs \\ %{}) do
311 featured_collection_activity(attrs, "Remove")
314 defp featured_collection_activity(attrs, type) do
315 user = attrs[:user] || insert(:user)
316 note = attrs[:note] || insert(:note, user: user)
320 |> Map.get(:data_attrs, %{})
321 |> Map.put(:type, type)
323 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
327 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
328 "target" => user.featured_address,
329 "object" => note.data["object"],
330 "actor" => note.data["actor"],
332 "to" => [Pleroma.Constants.as_public()],
333 "cc" => [user.follower_address]
335 |> Map.merge(data_attrs)
339 actor: data["actor"],
340 recipients: data["to"]
345 def followers_only_note_activity_factory(attrs \\ %{}) do
346 user = attrs[:user] || insert(:user)
347 note = insert(:followers_only_note, user: user)
349 data_attrs = attrs[:data_attrs] || %{}
350 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
354 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
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"]
362 |> Map.merge(data_attrs)
366 actor: data["actor"],
367 recipients: data["to"]
372 def note_activity_factory(attrs \\ %{}) do
373 user = attrs[:user] || insert(:user)
374 note = attrs[:note] || insert(:note, user: user)
376 data_attrs = attrs[:data_attrs] || %{}
377 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
381 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
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"]
389 |> Map.merge(data_attrs)
393 actor: data["actor"],
394 recipients: data["to"]
399 def article_activity_factory do
400 article = insert(:article)
403 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
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"]
414 actor: data["actor"],
415 recipients: data["to"]
419 def announce_activity_factory(attrs \\ %{}) do
420 note_activity = attrs[:note_activity] || insert(:note_activity)
421 user = attrs[:user] || insert(:user)
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"]
435 recipients: data["to"]
439 def like_activity_factory(attrs \\ %{}) do
440 note_activity = attrs[:note_activity] || insert(:note_activity)
441 object = Object.normalize(note_activity, fetch: false)
446 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
447 "actor" => user.ap_id,
449 "object" => object.data["id"],
450 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
452 |> Map.merge(attrs[:data_attrs] || %{})
459 def follow_activity_factory(attrs \\ %{}) do
460 follower = attrs[:follower] || insert(:user)
461 followed = attrs[:followed] || insert(:user)
464 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
465 "actor" => follower.ap_id,
467 "object" => followed.ap_id,
468 "state" => attrs[:state] || "pending",
469 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
474 actor: follower.ap_id
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"
485 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
486 "actor" => user.ap_id,
488 "object" => [activity.actor, activity.data["id"]],
489 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
491 "cc" => [activity.actor],
492 "context" => activity.data["context"],
498 actor: data["actor"],
499 recipients: data["to"] ++ data["cc"]
503 def question_activity_factory(attrs \\ %{}) do
504 user = attrs[:user] || insert(:user)
505 question = attrs[:question] || insert(:question, user: user)
507 data_attrs = attrs[:data_attrs] || %{}
508 attrs = Map.drop(attrs, [:user, :question, :data_attrs])
512 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
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"]
520 |> Map.merge(data_attrs)
524 actor: data["actor"],
525 recipients: data["to"]
530 def delete_activity_factory(attrs \\ %{}) do
531 user = attrs[:user] || insert(:user)
532 note_activity = attrs[:note_activity] || insert(:note_activity, user: user)
534 data_attrs = attrs[:data_attrs] || %{}
535 attrs = Map.drop(attrs, [:user, :data_attrs])
539 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
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"]
547 |> Map.merge(data_attrs)
551 actor: data["actor"],
552 recipients: data["to"]
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"
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)
574 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
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(),
582 valid_until: valid_until
586 def oauth_admin_token_factory(attrs \\ %{}) do
587 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
591 |> Map.get(:scopes, ["admin"])
592 |> Kernel.++(["admin"])
595 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
596 oauth_token_factory(attrs)
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),
605 app: build(:oauth_app)
609 def push_subscription_factory do
610 %Pleroma.Web.Push.Subscription{
612 token: build(:oauth_token),
613 endpoint: "https://example.com/example/1234",
614 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
616 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
621 def notification_factory do
622 %Pleroma.Notification{
627 def scheduled_activity_factory do
628 %Pleroma.ScheduledActivity{
630 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
631 params: build(:note) |> Map.from_struct() |> Map.get(:data)
635 def registration_factory do
638 %Pleroma.Registration{
643 "name" => "John Doe",
644 "email" => "john@doe.com",
645 "nickname" => "johndoe",
646 "description" => "My bio"
651 def config_factory(attrs \\ %{}) do
653 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
658 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
661 |> merge_attributes(attrs)
664 def marker_factory do
667 timeline: "notifications",
673 def mfa_token_factory do
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),
682 def filter_factory do
685 filter_id: sequence(:filter_id, & &1),
691 def announcement_factory(params \\ %{}) do
692 data = Map.get(params, :data, %{})
694 {_, params} = Map.pop(params, :data)
696 %Pleroma.Announcement{
697 data: Map.merge(%{"content" => "test announcement", "all_day" => false}, data)
700 |> Pleroma.Announcement.add_rendered_properties()
703 def frontend_setting_profile_factory(params \\ %{}) do
704 %Pleroma.Akkoma.FrontendSettingsProfile{
706 frontend_name: "akkoma-fe",
707 profile_name: "default",
708 settings: %{"test" => "test"},
714 def delivery_factory(params \\ %{}) do
715 object = Map.get(params, :object, build(:note))
716 user = Map.get(params, :user, build(:user))
724 def hashtag_factory(params \\ %{}) do
726 name: "test #{sequence(:hashtag_name, & &1)}"