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 do
456 follower = insert(:user)
457 followed = insert(:user)
460 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
461 "actor" => follower.ap_id,
463 "object" => followed.ap_id,
464 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
469 actor: follower.ap_id
473 def report_activity_factory(attrs \\ %{}) do
474 user = attrs[:user] || insert(:user)
475 activity = attrs[:activity] || insert(:note_activity)
476 state = attrs[:state] || "open"
479 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
480 "actor" => user.ap_id,
482 "object" => [activity.actor, activity.data["id"]],
483 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
485 "cc" => [activity.actor],
486 "context" => activity.data["context"],
492 actor: data["actor"],
493 recipients: data["to"] ++ data["cc"]
497 def question_activity_factory(attrs \\ %{}) do
498 user = attrs[:user] || insert(:user)
499 question = attrs[:question] || insert(:question, user: user)
501 data_attrs = attrs[:data_attrs] || %{}
502 attrs = Map.drop(attrs, [:user, :question, :data_attrs])
506 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
508 "actor" => question.data["actor"],
509 "to" => question.data["to"],
510 "object" => question.data["id"],
511 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
512 "context" => question.data["context"]
514 |> Map.merge(data_attrs)
518 actor: data["actor"],
519 recipients: data["to"]
524 def delete_activity_factory(attrs \\ %{}) do
525 user = attrs[:user] || insert(:user)
526 note_activity = attrs[:note_activity] || insert(:note_activity, user: user)
528 data_attrs = attrs[:data_attrs] || %{}
529 attrs = Map.drop(attrs, [:user, :data_attrs])
533 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
535 "actor" => note_activity.data["actor"],
536 "to" => note_activity.data["to"],
537 "object" => note_activity.data["id"],
538 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
539 "context" => note_activity.data["context"]
541 |> Map.merge(data_attrs)
545 actor: data["actor"],
546 recipients: data["to"]
551 def oauth_app_factory do
552 %Pleroma.Web.OAuth.App{
553 client_name: sequence(:client_name, &"Some client #{&1}"),
554 redirect_uris: "https://example.com/callback",
555 scopes: ["read", "write", "follow", "push", "admin"],
556 website: "https://example.com",
557 client_id: Ecto.UUID.generate(),
558 client_secret: "aaa;/&bbb"
562 def oauth_token_factory(attrs \\ %{}) do
563 scopes = Map.get(attrs, :scopes, ["read"])
564 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
565 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
568 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
570 %Pleroma.Web.OAuth.Token{
571 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
572 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
576 valid_until: valid_until
580 def oauth_admin_token_factory(attrs \\ %{}) do
581 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
585 |> Map.get(:scopes, ["admin"])
586 |> Kernel.++(["admin"])
589 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
590 oauth_token_factory(attrs)
593 def oauth_authorization_factory do
594 %Pleroma.Web.OAuth.Authorization{
595 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
596 scopes: ["read", "write", "follow", "push"],
597 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
599 app: build(:oauth_app)
603 def push_subscription_factory do
604 %Pleroma.Web.Push.Subscription{
606 token: build(:oauth_token),
607 endpoint: "https://example.com/example/1234",
608 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
610 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
615 def notification_factory do
616 %Pleroma.Notification{
621 def scheduled_activity_factory do
622 %Pleroma.ScheduledActivity{
624 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
625 params: build(:note) |> Map.from_struct() |> Map.get(:data)
629 def registration_factory do
632 %Pleroma.Registration{
637 "name" => "John Doe",
638 "email" => "john@doe.com",
639 "nickname" => "johndoe",
640 "description" => "My bio"
645 def config_factory(attrs \\ %{}) do
647 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
652 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
655 |> merge_attributes(attrs)
658 def marker_factory do
661 timeline: "notifications",
667 def mfa_token_factory do
669 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
670 authorization: build(:oauth_authorization),
671 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
676 def filter_factory do
679 filter_id: sequence(:filter_id, & &1),
685 def announcement_factory(params \\ %{}) do
686 data = Map.get(params, :data, %{})
688 {_, params} = Map.pop(params, :data)
690 %Pleroma.Announcement{
691 data: Map.merge(%{"content" => "test announcement", "all_day" => false}, data)
694 |> Pleroma.Announcement.add_rendered_properties()
697 def frontend_setting_profile_factory(params \\ %{}) do
698 %Pleroma.Akkoma.FrontendSettingsProfile{
700 frontend_name: "akkoma-fe",
701 profile_name: "default",
702 settings: %{"test" => "test"},
708 def delivery_factory(params \\ %{}) do
709 object = Map.get(params, :object, build(:note))
710 user = Map.get(params, :user, build(:user))