1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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 user_factory(attrs \\ %{}) do
40 pem = Enum.random(@rsa_keys)
43 name: sequence(:name, &"Test テスト User #{&1}"),
44 email: sequence(:email, &"user#{&1}@example.com"),
45 nickname: sequence(:nickname, &"nick#{&1}"),
46 password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt("test"),
47 bio: sequence(:bio, &"Tester Number #{&1}"),
48 is_discoverable: true,
49 last_digest_emailed_at: NaiveDateTime.utc_now(),
50 last_refreshed_at: NaiveDateTime.utc_now(),
51 notification_settings: %Pleroma.User.NotificationSetting{},
52 multi_factor_authentication_settings: %Pleroma.MFA.Settings{},
58 if attrs[:local] == false do
59 base_domain = attrs[:domain] || Enum.random(["domain1.com", "domain2.com", "domain3.com"])
61 ap_id = "https://#{base_domain}/users/#{user.nickname}"
65 follower_address: ap_id <> "/followers",
66 following_address: ap_id <> "/following",
67 featured_address: ap_id <> "/collections/featured"
71 ap_id: User.ap_id(user),
72 follower_address: User.ap_followers(user),
73 following_address: User.ap_following(user),
74 featured_address: User.ap_featured_collection(user)
78 attrs = Map.delete(attrs, :domain)
81 |> Map.put(:raw_bio, user.bio)
83 |> merge_attributes(attrs)
86 def user_relationship_factory(attrs \\ %{}) do
87 source = attrs[:source] || insert(:user)
88 target = attrs[:target] || insert(:user)
89 relationship_type = attrs[:relationship_type] || :block
91 %Pleroma.UserRelationship{
94 relationship_type: relationship_type
98 def note_factory(attrs \\ %{}) do
99 text = sequence(:text, &"This is :moominmamma: note #{&1}")
101 user = attrs[:user] || insert(:user)
107 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
108 "actor" => user.ap_id,
109 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
110 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
117 "2hu" => "corndog.png"
122 data: merge_attributes(data, Map.get(attrs, :data, %{}))
126 def attachment_factory(attrs \\ %{}) do
127 user = attrs[:user] || insert(:user)
130 attachment_data(user.ap_id, nil)
131 |> Map.put("id", Pleroma.Web.ActivityPub.Utils.generate_object_id())
134 data: merge_attributes(data, Map.get(attrs, :data, %{}))
138 def attachment_note_factory(attrs \\ %{}) do
139 user = attrs[:user] || insert(:user)
140 {length, attrs} = Map.pop(attrs, :length, 1)
144 Stream.repeatedly(fn -> attachment_data(user.ap_id, attrs[:href]) end)
148 build(:note, Map.put(attrs, :data, data))
151 defp attachment_data(ap_id, href) do
152 href = href || sequence(:href, &"#{Pleroma.Web.Endpoint.url()}/media/#{&1}.jpg")
159 "mediaType" => "image/jpeg"
162 "name" => "some name",
163 "type" => "Document",
165 "mediaType" => "image/jpeg"
169 def followers_only_note_factory(attrs \\ %{}) do
170 %Pleroma.Object{data: data} = note_factory(attrs)
171 %Pleroma.Object{data: Map.merge(data, %{"to" => [data["actor"] <> "/followers"]})}
174 def audio_factory(attrs \\ %{}) do
175 text = sequence(:text, &"lain radio episode #{&1}")
177 user = attrs[:user] || insert(:user)
181 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
184 "album" => "lain radio",
185 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
186 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
187 "actor" => user.ap_id,
192 data: merge_attributes(data, Map.get(attrs, :data, %{}))
196 def listen_factory do
197 audio = insert(:audio)
200 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
202 "actor" => audio.data["actor"],
203 "to" => audio.data["to"],
204 "object" => audio.data,
205 "published" => audio.data["published"]
210 actor: data["actor"],
211 recipients: data["to"]
215 def direct_note_factory do
216 user2 = insert(:user)
218 %Pleroma.Object{data: data} = note_factory()
219 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
222 def article_factory do
223 %Pleroma.Object{data: data} = note_factory()
224 %Pleroma.Object{data: Map.merge(data, %{"type" => "Article"})}
227 def tombstone_factory do
229 "type" => "Tombstone",
230 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
231 "formerType" => "Note",
232 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
240 def question_factory(attrs \\ %{}) do
241 user = attrs[:user] || insert(:user)
244 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
245 "type" => "Question",
246 "actor" => user.ap_id,
247 "attributedTo" => user.ap_id,
249 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
250 "cc" => [user.follower_address],
251 "context" => Pleroma.Web.ActivityPub.Utils.generate_context_id(),
252 "closed" => DateTime.utc_now() |> DateTime.add(86_400) |> DateTime.to_iso8601(),
256 "name" => "chocolate",
257 "replies" => %{"totalItems" => 0, "type" => "Collection"}
262 "replies" => %{"totalItems" => 0, "type" => "Collection"}
268 data: merge_attributes(data, Map.get(attrs, :data, %{}))
272 def direct_note_activity_factory do
273 dm = insert(:direct_note)
276 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
278 "actor" => dm.data["actor"],
279 "to" => dm.data["to"],
281 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
282 "context" => dm.data["context"]
287 actor: data["actor"],
288 recipients: data["to"]
292 def add_activity_factory(attrs \\ %{}) do
293 featured_collection_activity(attrs, "Add")
296 def remove_activity_factor(attrs \\ %{}) do
297 featured_collection_activity(attrs, "Remove")
300 defp featured_collection_activity(attrs, type) do
301 user = attrs[:user] || insert(:user)
302 note = attrs[:note] || insert(:note, user: user)
306 |> Map.get(:data_attrs, %{})
307 |> Map.put(:type, type)
309 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
313 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
314 "target" => user.featured_address,
315 "object" => note.data["object"],
316 "actor" => note.data["actor"],
318 "to" => [Pleroma.Constants.as_public()],
319 "cc" => [user.follower_address]
321 |> Map.merge(data_attrs)
325 actor: data["actor"],
326 recipients: data["to"]
331 def followers_only_note_activity_factory(attrs \\ %{}) do
332 user = attrs[:user] || insert(:user)
333 note = insert(:followers_only_note, user: user)
335 data_attrs = attrs[:data_attrs] || %{}
336 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
340 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
342 "actor" => note.data["actor"],
343 "to" => note.data["to"],
344 "object" => note.data,
345 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
346 "context" => note.data["context"]
348 |> Map.merge(data_attrs)
352 actor: data["actor"],
353 recipients: data["to"]
358 def note_activity_factory(attrs \\ %{}) do
359 user = attrs[:user] || insert(:user)
360 note = attrs[:note] || insert(:note, user: user)
362 data_attrs = attrs[:data_attrs] || %{}
363 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
367 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
369 "actor" => note.data["actor"],
370 "to" => note.data["to"],
371 "object" => note.data["id"],
372 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
373 "context" => note.data["context"]
375 |> Map.merge(data_attrs)
379 actor: data["actor"],
380 recipients: data["to"]
385 def article_activity_factory do
386 article = insert(:article)
389 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
391 "actor" => article.data["actor"],
392 "to" => article.data["to"],
393 "object" => article.data,
394 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
395 "context" => article.data["context"]
400 actor: data["actor"],
401 recipients: data["to"]
405 def announce_activity_factory(attrs \\ %{}) do
406 note_activity = attrs[:note_activity] || insert(:note_activity)
407 user = attrs[:user] || insert(:user)
410 "type" => "Announce",
411 "actor" => note_activity.actor,
412 "object" => note_activity.data["id"],
413 "to" => [user.follower_address, note_activity.data["actor"]],
414 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
415 "context" => note_activity.data["context"]
421 recipients: data["to"]
425 def like_activity_factory(attrs \\ %{}) do
426 note_activity = attrs[:note_activity] || insert(:note_activity)
427 object = Object.normalize(note_activity, fetch: false)
432 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
433 "actor" => user.ap_id,
435 "object" => object.data["id"],
436 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
438 |> Map.merge(attrs[:data_attrs] || %{})
445 def follow_activity_factory do
446 follower = insert(:user)
447 followed = insert(:user)
450 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
451 "actor" => follower.ap_id,
453 "object" => followed.ap_id,
454 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
459 actor: follower.ap_id
463 def report_activity_factory(attrs \\ %{}) do
464 user = attrs[:user] || insert(:user)
465 activity = attrs[:activity] || insert(:note_activity)
466 state = attrs[:state] || "open"
469 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
470 "actor" => user.ap_id,
472 "object" => [activity.actor, activity.data["id"]],
473 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
475 "cc" => [activity.actor],
476 "context" => activity.data["context"],
482 actor: data["actor"],
483 recipients: data["to"] ++ data["cc"]
487 def question_activity_factory(attrs \\ %{}) do
488 user = attrs[:user] || insert(:user)
489 question = attrs[:question] || insert(:question, user: user)
491 data_attrs = attrs[:data_attrs] || %{}
492 attrs = Map.drop(attrs, [:user, :question, :data_attrs])
496 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
498 "actor" => question.data["actor"],
499 "to" => question.data["to"],
500 "object" => question.data["id"],
501 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
502 "context" => question.data["context"]
504 |> Map.merge(data_attrs)
508 actor: data["actor"],
509 recipients: data["to"]
514 def oauth_app_factory do
515 %Pleroma.Web.OAuth.App{
516 client_name: sequence(:client_name, &"Some client #{&1}"),
517 redirect_uris: "https://example.com/callback",
518 scopes: ["read", "write", "follow", "push", "admin"],
519 website: "https://example.com",
520 client_id: Ecto.UUID.generate(),
521 client_secret: "aaa;/&bbb"
525 def instance_factory do
526 %Pleroma.Instances.Instance{
528 unreachable_since: nil
532 def oauth_token_factory(attrs \\ %{}) do
533 scopes = Map.get(attrs, :scopes, ["read"])
534 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
535 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
538 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
540 %Pleroma.Web.OAuth.Token{
541 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
542 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
546 valid_until: valid_until
550 def oauth_admin_token_factory(attrs \\ %{}) do
551 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
555 |> Map.get(:scopes, ["admin"])
556 |> Kernel.++(["admin"])
559 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
560 oauth_token_factory(attrs)
563 def oauth_authorization_factory do
564 %Pleroma.Web.OAuth.Authorization{
565 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
566 scopes: ["read", "write", "follow", "push"],
567 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
569 app: build(:oauth_app)
573 def push_subscription_factory do
574 %Pleroma.Web.Push.Subscription{
576 token: build(:oauth_token),
577 endpoint: "https://example.com/example/1234",
578 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
580 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
585 def notification_factory do
586 %Pleroma.Notification{
591 def scheduled_activity_factory do
592 %Pleroma.ScheduledActivity{
594 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
595 params: build(:note) |> Map.from_struct() |> Map.get(:data)
599 def registration_factory do
602 %Pleroma.Registration{
607 "name" => "John Doe",
608 "email" => "john@doe.com",
609 "nickname" => "johndoe",
610 "description" => "My bio"
615 def config_factory(attrs \\ %{}) do
617 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
622 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
625 |> merge_attributes(attrs)
628 def marker_factory do
631 timeline: "notifications",
637 def mfa_token_factory do
639 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
640 authorization: build(:oauth_authorization),
641 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
646 def filter_factory do
649 filter_id: sequence(:filter_id, & &1),
655 def announcement_factory(params \\ %{}) do
656 data = Map.get(params, :data, %{})
658 {_, params} = Map.pop(params, :data)
660 %Pleroma.Announcement{
661 data: Map.merge(%{"content" => "test announcement", "all_day" => false}, data)
664 |> Pleroma.Announcement.add_rendered_properties()