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{},
57 if attrs[:local] == false do
58 base_domain = attrs[:domain] || Enum.random(["domain1.com", "domain2.com", "domain3.com"])
60 ap_id = "https://#{base_domain}/users/#{user.nickname}"
64 follower_address: ap_id <> "/followers",
65 following_address: ap_id <> "/following",
66 featured_address: ap_id <> "/collections/featured"
70 ap_id: User.ap_id(user),
71 follower_address: User.ap_followers(user),
72 following_address: User.ap_following(user),
73 featured_address: User.ap_featured_collection(user)
77 attrs = Map.delete(attrs, :domain)
80 |> Map.put(:raw_bio, user.bio)
82 |> merge_attributes(attrs)
85 def user_relationship_factory(attrs \\ %{}) do
86 source = attrs[:source] || insert(:user)
87 target = attrs[:target] || insert(:user)
88 relationship_type = attrs[:relationship_type] || :block
90 %Pleroma.UserRelationship{
93 relationship_type: relationship_type
97 def note_factory(attrs \\ %{}) do
98 text = sequence(:text, &"This is :moominmamma: note #{&1}")
100 user = attrs[:user] || insert(:user)
106 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
107 "actor" => user.ap_id,
108 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
109 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
116 "2hu" => "corndog.png"
121 data: merge_attributes(data, Map.get(attrs, :data, %{}))
125 def attachment_factory(attrs \\ %{}) do
126 user = attrs[:user] || insert(:user)
129 attachment_data(user.ap_id, nil)
130 |> Map.put("id", Pleroma.Web.ActivityPub.Utils.generate_object_id())
133 data: merge_attributes(data, Map.get(attrs, :data, %{}))
137 def attachment_note_factory(attrs \\ %{}) do
138 user = attrs[:user] || insert(:user)
139 {length, attrs} = Map.pop(attrs, :length, 1)
143 Stream.repeatedly(fn -> attachment_data(user.ap_id, attrs[:href]) end)
147 build(:note, Map.put(attrs, :data, data))
150 defp attachment_data(ap_id, href) do
151 href = href || sequence(:href, &"#{Pleroma.Web.Endpoint.url()}/media/#{&1}.jpg")
158 "mediaType" => "image/jpeg"
161 "name" => "some name",
162 "type" => "Document",
164 "mediaType" => "image/jpeg"
168 def followers_only_note_factory(attrs \\ %{}) do
169 %Pleroma.Object{data: data} = note_factory(attrs)
170 %Pleroma.Object{data: Map.merge(data, %{"to" => [data["actor"] <> "/followers"]})}
173 def audio_factory(attrs \\ %{}) do
174 text = sequence(:text, &"lain radio episode #{&1}")
176 user = attrs[:user] || insert(:user)
180 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
183 "album" => "lain radio",
184 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
185 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
186 "actor" => user.ap_id,
191 data: merge_attributes(data, Map.get(attrs, :data, %{}))
195 def listen_factory do
196 audio = insert(:audio)
199 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
201 "actor" => audio.data["actor"],
202 "to" => audio.data["to"],
203 "object" => audio.data,
204 "published" => audio.data["published"]
209 actor: data["actor"],
210 recipients: data["to"]
214 def direct_note_factory do
215 user2 = insert(:user, local: false, inbox: "http://example.com/inbox")
217 %Pleroma.Object{data: data} = note_factory()
218 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
221 def article_factory do
222 %Pleroma.Object{data: data} = note_factory()
223 %Pleroma.Object{data: Map.merge(data, %{"type" => "Article"})}
226 def tombstone_factory do
228 "type" => "Tombstone",
229 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
230 "formerType" => "Note",
231 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
239 def question_factory(attrs \\ %{}) do
240 user = attrs[:user] || insert(:user)
243 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
244 "type" => "Question",
245 "actor" => user.ap_id,
246 "attributedTo" => user.ap_id,
248 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
249 "cc" => [user.follower_address],
250 "context" => Pleroma.Web.ActivityPub.Utils.generate_context_id(),
251 "closed" => DateTime.utc_now() |> DateTime.add(86_400) |> DateTime.to_iso8601(),
255 "name" => "chocolate",
256 "replies" => %{"totalItems" => 0, "type" => "Collection"}
261 "replies" => %{"totalItems" => 0, "type" => "Collection"}
267 data: merge_attributes(data, Map.get(attrs, :data, %{}))
271 def direct_note_activity_factory do
272 dm = insert(:direct_note)
275 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
277 "actor" => dm.data["actor"],
278 "to" => dm.data["to"],
280 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
281 "context" => dm.data["context"]
286 actor: data["actor"],
287 recipients: data["to"]
291 def add_activity_factory(attrs \\ %{}) do
292 featured_collection_activity(attrs, "Add")
295 def remove_activity_factor(attrs \\ %{}) do
296 featured_collection_activity(attrs, "Remove")
299 defp featured_collection_activity(attrs, type) do
300 user = attrs[:user] || insert(:user)
301 note = attrs[:note] || insert(:note, user: user)
305 |> Map.get(:data_attrs, %{})
306 |> Map.put(:type, type)
308 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
312 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
313 "target" => user.featured_address,
314 "object" => note.data["object"],
315 "actor" => note.data["actor"],
317 "to" => [Pleroma.Constants.as_public()],
318 "cc" => [user.follower_address]
320 |> Map.merge(data_attrs)
324 actor: data["actor"],
325 recipients: data["to"]
330 def followers_only_note_activity_factory(attrs \\ %{}) do
331 user = attrs[:user] || insert(:user)
332 note = insert(:followers_only_note, user: user)
334 data_attrs = attrs[:data_attrs] || %{}
335 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
339 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
341 "actor" => note.data["actor"],
342 "to" => note.data["to"],
343 "object" => note.data,
344 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
345 "context" => note.data["context"]
347 |> Map.merge(data_attrs)
351 actor: data["actor"],
352 recipients: data["to"]
357 def note_activity_factory(attrs \\ %{}) do
358 user = attrs[:user] || insert(:user)
359 note = attrs[:note] || insert(:note, user: user)
361 data_attrs = attrs[:data_attrs] || %{}
362 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
366 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
368 "actor" => note.data["actor"],
369 "to" => note.data["to"],
370 "object" => note.data["id"],
371 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
372 "context" => note.data["context"]
374 |> Map.merge(data_attrs)
378 actor: data["actor"],
379 recipients: data["to"]
384 def article_activity_factory do
385 article = insert(:article)
388 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
390 "actor" => article.data["actor"],
391 "to" => article.data["to"],
392 "object" => article.data,
393 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
394 "context" => article.data["context"]
399 actor: data["actor"],
400 recipients: data["to"]
404 def announce_activity_factory(attrs \\ %{}) do
405 note_activity = attrs[:note_activity] || insert(:note_activity)
406 user = attrs[:user] || insert(:user)
409 "type" => "Announce",
410 "actor" => note_activity.actor,
411 "object" => note_activity.data["id"],
412 "to" => [user.follower_address, note_activity.data["actor"]],
413 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
414 "context" => note_activity.data["context"]
420 recipients: data["to"]
424 def like_activity_factory(attrs \\ %{}) do
425 note_activity = attrs[:note_activity] || insert(:note_activity)
426 object = Object.normalize(note_activity, fetch: false)
431 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
432 "actor" => user.ap_id,
434 "object" => object.data["id"],
435 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
437 |> Map.merge(attrs[:data_attrs] || %{})
444 def follow_activity_factory do
445 follower = insert(:user)
446 followed = insert(:user)
449 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
450 "actor" => follower.ap_id,
452 "object" => followed.ap_id,
453 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
458 actor: follower.ap_id
462 def report_activity_factory(attrs \\ %{}) do
463 user = attrs[:user] || insert(:user)
464 activity = attrs[:activity] || insert(:note_activity)
465 state = attrs[:state] || "open"
468 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
469 "actor" => user.ap_id,
471 "object" => [activity.actor, activity.data["id"]],
472 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
474 "cc" => [activity.actor],
475 "context" => activity.data["context"],
481 actor: data["actor"],
482 recipients: data["to"] ++ data["cc"]
486 def question_activity_factory(attrs \\ %{}) do
487 user = attrs[:user] || insert(:user)
488 question = attrs[:question] || insert(:question, user: user)
490 data_attrs = attrs[:data_attrs] || %{}
491 attrs = Map.drop(attrs, [:user, :question, :data_attrs])
495 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
497 "actor" => question.data["actor"],
498 "to" => question.data["to"],
499 "object" => question.data["id"],
500 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
501 "context" => question.data["context"]
503 |> Map.merge(data_attrs)
507 actor: data["actor"],
508 recipients: data["to"]
513 def oauth_app_factory do
514 %Pleroma.Web.OAuth.App{
515 client_name: sequence(:client_name, &"Some client #{&1}"),
516 redirect_uris: "https://example.com/callback",
517 scopes: ["read", "write", "follow", "push", "admin"],
518 website: "https://example.com",
519 client_id: Ecto.UUID.generate(),
520 client_secret: "aaa;/&bbb"
524 def instance_factory do
525 %Pleroma.Instances.Instance{
527 unreachable_since: nil
531 def oauth_token_factory(attrs \\ %{}) do
532 scopes = Map.get(attrs, :scopes, ["read"])
533 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
534 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
537 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
539 %Pleroma.Web.OAuth.Token{
540 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
541 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
545 valid_until: valid_until
549 def oauth_admin_token_factory(attrs \\ %{}) do
550 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
554 |> Map.get(:scopes, ["admin"])
555 |> Kernel.++(["admin"])
558 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
559 oauth_token_factory(attrs)
562 def oauth_authorization_factory do
563 %Pleroma.Web.OAuth.Authorization{
564 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
565 scopes: ["read", "write", "follow", "push"],
566 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
568 app: build(:oauth_app)
572 def push_subscription_factory do
573 %Pleroma.Web.Push.Subscription{
575 token: build(:oauth_token),
576 endpoint: "https://example.com/example/1234",
577 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
579 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
584 def notification_factory do
585 %Pleroma.Notification{
590 def scheduled_activity_factory do
591 %Pleroma.ScheduledActivity{
593 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
594 params: build(:note) |> Map.from_struct() |> Map.get(:data)
598 def registration_factory do
601 %Pleroma.Registration{
606 "name" => "John Doe",
607 "email" => "john@doe.com",
608 "nickname" => "johndoe",
609 "description" => "My bio"
614 def config_factory(attrs \\ %{}) do
616 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
621 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
624 |> merge_attributes(attrs)
627 def marker_factory do
630 timeline: "notifications",
636 def mfa_token_factory do
638 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
639 authorization: build(:oauth_authorization),
640 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
645 def filter_factory do
648 filter_id: sequence(:filter_id, & &1),
654 def announcement_factory(params \\ %{}) do
655 data = Map.get(params, :data, %{})
657 {_, params} = Map.pop(params, :data)
659 %Pleroma.Announcement{
660 data: Map.merge(%{"content" => "test announcement", "all_day" => false}, data)
663 |> Pleroma.Announcement.add_rendered_properties()