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 do
238 "type" => "Tombstone",
239 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
240 "formerType" => "Note",
241 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
249 def question_factory(attrs \\ %{}) do
250 user = attrs[:user] || insert(:user)
253 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
254 "type" => "Question",
255 "actor" => user.ap_id,
256 "attributedTo" => user.ap_id,
258 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
259 "cc" => [user.follower_address],
260 "context" => Pleroma.Web.ActivityPub.Utils.generate_context_id(),
261 "closed" => DateTime.utc_now() |> DateTime.add(86_400) |> DateTime.to_iso8601(),
265 "name" => "chocolate",
266 "replies" => %{"totalItems" => 0, "type" => "Collection"}
271 "replies" => %{"totalItems" => 0, "type" => "Collection"}
277 data: merge_attributes(data, Map.get(attrs, :data, %{}))
281 def direct_note_activity_factory do
282 dm = insert(:direct_note)
285 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
287 "actor" => dm.data["actor"],
288 "to" => dm.data["to"],
290 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
291 "context" => dm.data["context"]
296 actor: data["actor"],
297 recipients: data["to"]
301 def add_activity_factory(attrs \\ %{}) do
302 featured_collection_activity(attrs, "Add")
305 def remove_activity_factor(attrs \\ %{}) do
306 featured_collection_activity(attrs, "Remove")
309 defp featured_collection_activity(attrs, type) do
310 user = attrs[:user] || insert(:user)
311 note = attrs[:note] || insert(:note, user: user)
315 |> Map.get(:data_attrs, %{})
316 |> Map.put(:type, type)
318 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
322 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
323 "target" => user.featured_address,
324 "object" => note.data["object"],
325 "actor" => note.data["actor"],
327 "to" => [Pleroma.Constants.as_public()],
328 "cc" => [user.follower_address]
330 |> Map.merge(data_attrs)
334 actor: data["actor"],
335 recipients: data["to"]
340 def followers_only_note_activity_factory(attrs \\ %{}) do
341 user = attrs[:user] || insert(:user)
342 note = insert(:followers_only_note, user: user)
344 data_attrs = attrs[:data_attrs] || %{}
345 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
349 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
351 "actor" => note.data["actor"],
352 "to" => note.data["to"],
353 "object" => note.data,
354 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
355 "context" => note.data["context"]
357 |> Map.merge(data_attrs)
361 actor: data["actor"],
362 recipients: data["to"]
367 def note_activity_factory(attrs \\ %{}) do
368 user = attrs[:user] || insert(:user)
369 note = attrs[:note] || insert(:note, user: user)
371 data_attrs = attrs[:data_attrs] || %{}
372 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
376 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
378 "actor" => note.data["actor"],
379 "to" => note.data["to"],
380 "object" => note.data["id"],
381 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
382 "context" => note.data["context"]
384 |> Map.merge(data_attrs)
388 actor: data["actor"],
389 recipients: data["to"]
394 def article_activity_factory do
395 article = insert(:article)
398 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
400 "actor" => article.data["actor"],
401 "to" => article.data["to"],
402 "object" => article.data,
403 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
404 "context" => article.data["context"]
409 actor: data["actor"],
410 recipients: data["to"]
414 def announce_activity_factory(attrs \\ %{}) do
415 note_activity = attrs[:note_activity] || insert(:note_activity)
416 user = attrs[:user] || insert(:user)
419 "type" => "Announce",
420 "actor" => note_activity.actor,
421 "object" => note_activity.data["id"],
422 "to" => [user.follower_address, note_activity.data["actor"]],
423 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
424 "context" => note_activity.data["context"]
430 recipients: data["to"]
434 def like_activity_factory(attrs \\ %{}) do
435 note_activity = attrs[:note_activity] || insert(:note_activity)
436 object = Object.normalize(note_activity, fetch: false)
441 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
442 "actor" => user.ap_id,
444 "object" => object.data["id"],
445 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
447 |> Map.merge(attrs[:data_attrs] || %{})
454 def follow_activity_factory do
455 follower = insert(:user)
456 followed = insert(:user)
459 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
460 "actor" => follower.ap_id,
462 "object" => followed.ap_id,
463 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
468 actor: follower.ap_id
472 def report_activity_factory(attrs \\ %{}) do
473 user = attrs[:user] || insert(:user)
474 activity = attrs[:activity] || insert(:note_activity)
475 state = attrs[:state] || "open"
478 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
479 "actor" => user.ap_id,
481 "object" => [activity.actor, activity.data["id"]],
482 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
484 "cc" => [activity.actor],
485 "context" => activity.data["context"],
491 actor: data["actor"],
492 recipients: data["to"] ++ data["cc"]
496 def question_activity_factory(attrs \\ %{}) do
497 user = attrs[:user] || insert(:user)
498 question = attrs[:question] || insert(:question, user: user)
500 data_attrs = attrs[:data_attrs] || %{}
501 attrs = Map.drop(attrs, [:user, :question, :data_attrs])
505 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
507 "actor" => question.data["actor"],
508 "to" => question.data["to"],
509 "object" => question.data["id"],
510 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
511 "context" => question.data["context"]
513 |> Map.merge(data_attrs)
517 actor: data["actor"],
518 recipients: data["to"]
523 def oauth_app_factory do
524 %Pleroma.Web.OAuth.App{
525 client_name: sequence(:client_name, &"Some client #{&1}"),
526 redirect_uris: "https://example.com/callback",
527 scopes: ["read", "write", "follow", "push", "admin"],
528 website: "https://example.com",
529 client_id: Ecto.UUID.generate(),
530 client_secret: "aaa;/&bbb"
534 def oauth_token_factory(attrs \\ %{}) do
535 scopes = Map.get(attrs, :scopes, ["read"])
536 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
537 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
540 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
542 %Pleroma.Web.OAuth.Token{
543 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
544 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
548 valid_until: valid_until
552 def oauth_admin_token_factory(attrs \\ %{}) do
553 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
557 |> Map.get(:scopes, ["admin"])
558 |> Kernel.++(["admin"])
561 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
562 oauth_token_factory(attrs)
565 def oauth_authorization_factory do
566 %Pleroma.Web.OAuth.Authorization{
567 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
568 scopes: ["read", "write", "follow", "push"],
569 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
571 app: build(:oauth_app)
575 def push_subscription_factory do
576 %Pleroma.Web.Push.Subscription{
578 token: build(:oauth_token),
579 endpoint: "https://example.com/example/1234",
580 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
582 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
587 def notification_factory do
588 %Pleroma.Notification{
593 def scheduled_activity_factory do
594 %Pleroma.ScheduledActivity{
596 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
597 params: build(:note) |> Map.from_struct() |> Map.get(:data)
601 def registration_factory do
604 %Pleroma.Registration{
609 "name" => "John Doe",
610 "email" => "john@doe.com",
611 "nickname" => "johndoe",
612 "description" => "My bio"
617 def config_factory(attrs \\ %{}) do
619 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
624 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
627 |> merge_attributes(attrs)
630 def marker_factory do
633 timeline: "notifications",
639 def mfa_token_factory do
641 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
642 authorization: build(:oauth_authorization),
643 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
648 def filter_factory do
651 filter_id: sequence(:filter_id, & &1),
657 def announcement_factory(params \\ %{}) do
658 data = Map.get(params, :data, %{})
660 {_, params} = Map.pop(params, :data)
662 %Pleroma.Announcement{
663 data: Map.merge(%{"content" => "test announcement", "all_day" => false}, data)
666 |> Pleroma.Announcement.add_rendered_properties()
669 def frontend_setting_profile_factory(params \\ %{}) do
670 %Pleroma.Akkoma.FrontendSettingsProfile{
672 frontend_name: "akkoma-fe",
673 profile_name: "default",
674 settings: %{"test" => "test"},