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)
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{},
67 if attrs[:local] == false do
68 base_domain = attrs[:domain] || Enum.random(["domain1.com", "domain2.com", "domain3.com"])
70 ap_id = "https://#{base_domain}/users/#{user.nickname}"
74 follower_address: ap_id <> "/followers",
75 following_address: ap_id <> "/following",
76 featured_address: ap_id <> "/collections/featured"
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)
87 attrs = Map.delete(attrs, :domain)
90 |> Map.put(:raw_bio, user.bio)
92 |> merge_attributes(attrs)
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
100 %Pleroma.UserRelationship{
101 source_id: source.id,
102 target_id: target.id,
103 relationship_type: relationship_type
107 def note_factory(attrs \\ %{}) do
108 text = sequence(:text, &"This is :moominmamma: note #{&1}")
110 user = attrs[:user] || insert(:user)
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(),
126 "2hu" => "corndog.png"
131 data: merge_attributes(data, Map.get(attrs, :data, %{}))
135 def attachment_factory(attrs \\ %{}) do
136 user = attrs[:user] || insert(:user)
139 attachment_data(user.ap_id, nil)
140 |> Map.put("id", Pleroma.Web.ActivityPub.Utils.generate_object_id())
143 data: merge_attributes(data, Map.get(attrs, :data, %{}))
147 def attachment_note_factory(attrs \\ %{}) do
148 user = attrs[:user] || insert(:user)
149 {length, attrs} = Map.pop(attrs, :length, 1)
153 Stream.repeatedly(fn -> attachment_data(user.ap_id, attrs[:href]) end)
157 build(:note, Map.put(attrs, :data, data))
160 defp attachment_data(ap_id, href) do
161 href = href || sequence(:href, &"#{Pleroma.Web.Endpoint.url()}/media/#{&1}.jpg")
168 "mediaType" => "image/jpeg"
171 "name" => "some name",
172 "type" => "Document",
174 "mediaType" => "image/jpeg"
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"]})}
183 def audio_factory(attrs \\ %{}) do
184 text = sequence(:text, &"lain radio episode #{&1}")
186 user = attrs[:user] || insert(:user)
190 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
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,
201 data: merge_attributes(data, Map.get(attrs, :data, %{}))
205 def listen_factory do
206 audio = insert(:audio)
209 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
211 "actor" => audio.data["actor"],
212 "to" => audio.data["to"],
213 "object" => audio.data,
214 "published" => audio.data["published"]
219 actor: data["actor"],
220 recipients: data["to"]
224 def direct_note_factory do
225 user2 = insert(:user)
227 %Pleroma.Object{data: data} = note_factory()
228 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
231 def article_factory do
232 %Pleroma.Object{data: data} = note_factory()
233 %Pleroma.Object{data: Map.merge(data, %{"type" => "Article"})}
236 def tombstone_factory(attrs) do
238 "type" => "Tombstone",
239 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
240 "formerType" => "Note",
241 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
247 |> merge_attributes(attrs)
250 def question_factory(attrs \\ %{}) do
251 user = attrs[:user] || insert(:user)
254 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
255 "type" => "Question",
256 "actor" => user.ap_id,
257 "attributedTo" => user.ap_id,
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(),
266 "name" => "chocolate",
267 "replies" => %{"totalItems" => 0, "type" => "Collection"}
272 "replies" => %{"totalItems" => 0, "type" => "Collection"}
278 data: merge_attributes(data, Map.get(attrs, :data, %{}))
282 def direct_note_activity_factory do
283 dm = insert(:direct_note)
286 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
288 "actor" => dm.data["actor"],
289 "to" => dm.data["to"],
291 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
292 "context" => dm.data["context"]
297 actor: data["actor"],
298 recipients: data["to"]
302 def add_activity_factory(attrs \\ %{}) do
303 featured_collection_activity(attrs, "Add")
306 def remove_activity_factor(attrs \\ %{}) do
307 featured_collection_activity(attrs, "Remove")
310 defp featured_collection_activity(attrs, type) do
311 user = attrs[:user] || insert(:user)
312 note = attrs[:note] || insert(:note, user: user)
316 |> Map.get(:data_attrs, %{})
317 |> Map.put(:type, type)
319 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
323 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
324 "target" => user.featured_address,
325 "object" => note.data["object"],
326 "actor" => note.data["actor"],
328 "to" => [Pleroma.Constants.as_public()],
329 "cc" => [user.follower_address]
331 |> Map.merge(data_attrs)
335 actor: data["actor"],
336 recipients: data["to"]
341 def followers_only_note_activity_factory(attrs \\ %{}) do
342 user = attrs[:user] || insert(:user)
343 note = insert(:followers_only_note, user: user)
345 data_attrs = attrs[:data_attrs] || %{}
346 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
350 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
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"]
358 |> Map.merge(data_attrs)
362 actor: data["actor"],
363 recipients: data["to"]
368 def note_activity_factory(attrs \\ %{}) do
369 user = attrs[:user] || insert(:user)
370 note = attrs[:note] || insert(:note, user: user)
372 data_attrs = attrs[:data_attrs] || %{}
373 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
377 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
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"]
385 |> Map.merge(data_attrs)
389 actor: data["actor"],
390 recipients: data["to"]
395 def article_activity_factory do
396 article = insert(:article)
399 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
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"]
410 actor: data["actor"],
411 recipients: data["to"]
415 def announce_activity_factory(attrs \\ %{}) do
416 note_activity = attrs[:note_activity] || insert(:note_activity)
417 user = attrs[:user] || insert(:user)
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"]
431 recipients: data["to"]
435 def like_activity_factory(attrs \\ %{}) do
436 note_activity = attrs[:note_activity] || insert(:note_activity)
437 object = Object.normalize(note_activity, fetch: false)
442 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
443 "actor" => user.ap_id,
445 "object" => object.data["id"],
446 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
448 |> Map.merge(attrs[:data_attrs] || %{})
455 def follow_activity_factory(attrs \\ %{}) do
456 follower = attrs[:follower] || insert(:user)
457 followed = attrs[:followed] || insert(:user)
460 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
461 "actor" => follower.ap_id,
463 "object" => followed.ap_id,
464 "state" => attrs[:state] || "pending",
465 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
470 actor: follower.ap_id
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"
480 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
481 "actor" => user.ap_id,
483 "object" => [activity.actor, activity.data["id"]],
484 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
486 "cc" => [activity.actor],
487 "context" => activity.data["context"],
493 actor: data["actor"],
494 recipients: data["to"] ++ data["cc"]
498 def question_activity_factory(attrs \\ %{}) do
499 user = attrs[:user] || insert(:user)
500 question = attrs[:question] || insert(:question, user: user)
502 data_attrs = attrs[:data_attrs] || %{}
503 attrs = Map.drop(attrs, [:user, :question, :data_attrs])
507 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
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"]
515 |> Map.merge(data_attrs)
519 actor: data["actor"],
520 recipients: data["to"]
525 def delete_activity_factory(attrs \\ %{}) do
526 user = attrs[:user] || insert(:user)
527 note_activity = attrs[:note_activity] || insert(:note_activity, user: user)
529 data_attrs = attrs[:data_attrs] || %{}
530 attrs = Map.drop(attrs, [:user, :data_attrs])
534 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
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"]
542 |> Map.merge(data_attrs)
546 actor: data["actor"],
547 recipients: data["to"]
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"
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)
569 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
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(),
577 valid_until: valid_until
581 def oauth_admin_token_factory(attrs \\ %{}) do
582 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
586 |> Map.get(:scopes, ["admin"])
587 |> Kernel.++(["admin"])
590 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
591 oauth_token_factory(attrs)
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),
600 app: build(:oauth_app)
604 def push_subscription_factory do
605 %Pleroma.Web.Push.Subscription{
607 token: build(:oauth_token),
608 endpoint: "https://example.com/example/1234",
609 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
611 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
616 def notification_factory do
617 %Pleroma.Notification{
622 def scheduled_activity_factory do
623 %Pleroma.ScheduledActivity{
625 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
626 params: build(:note) |> Map.from_struct() |> Map.get(:data)
630 def registration_factory do
633 %Pleroma.Registration{
638 "name" => "John Doe",
639 "email" => "john@doe.com",
640 "nickname" => "johndoe",
641 "description" => "My bio"
646 def config_factory(attrs \\ %{}) do
648 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
653 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
656 |> merge_attributes(attrs)
659 def marker_factory do
662 timeline: "notifications",
668 def mfa_token_factory do
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),
677 def filter_factory do
680 filter_id: sequence(:filter_id, & &1),
686 def announcement_factory(params \\ %{}) do
687 data = Map.get(params, :data, %{})
689 {_, params} = Map.pop(params, :data)
691 %Pleroma.Announcement{
692 data: Map.merge(%{"content" => "test announcement", "all_day" => false}, data)
695 |> Pleroma.Announcement.add_rendered_properties()
698 def frontend_setting_profile_factory(params \\ %{}) do
699 %Pleroma.Akkoma.FrontendSettingsProfile{
701 frontend_name: "akkoma-fe",
702 profile_name: "default",
703 settings: %{"test" => "test"},
709 def delivery_factory(params \\ %{}) do
710 object = Map.get(params, :object, build(:note))
711 user = Map.get(params, :user, build(:user))