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 def participation_factory do
15 conversation = insert(:conversation)
18 %Pleroma.Conversation.Participation{
19 conversation: conversation,
25 def conversation_factory do
26 %Pleroma.Conversation{
27 ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}")
31 def user_factory(attrs \\ %{}) do
32 {:ok, pem} = Keys.generate_rsa_pem()
35 name: sequence(:name, &"Test テスト User #{&1}"),
36 email: sequence(:email, &"user#{&1}@example.com"),
37 nickname: sequence(:nickname, &"nick#{&1}"),
38 password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt("test"),
39 bio: sequence(:bio, &"Tester Number #{&1}"),
40 is_discoverable: true,
41 last_digest_emailed_at: NaiveDateTime.utc_now(),
42 last_refreshed_at: NaiveDateTime.utc_now(),
43 notification_settings: %Pleroma.User.NotificationSetting{},
44 multi_factor_authentication_settings: %Pleroma.MFA.Settings{},
50 if attrs[:local] == false do
51 base_domain = attrs[:domain] || Enum.random(["domain1.com", "domain2.com", "domain3.com"])
53 ap_id = "https://#{base_domain}/users/#{user.nickname}"
57 follower_address: ap_id <> "/followers",
58 following_address: ap_id <> "/following",
59 featured_address: ap_id <> "/collections/featured"
63 ap_id: User.ap_id(user),
64 follower_address: User.ap_followers(user),
65 following_address: User.ap_following(user),
66 featured_address: User.ap_featured_collection(user)
70 attrs = Map.delete(attrs, :domain)
73 |> Map.put(:raw_bio, user.bio)
75 |> merge_attributes(attrs)
78 def user_relationship_factory(attrs \\ %{}) do
79 source = attrs[:source] || insert(:user)
80 target = attrs[:target] || insert(:user)
81 relationship_type = attrs[:relationship_type] || :block
83 %Pleroma.UserRelationship{
86 relationship_type: relationship_type
90 def note_factory(attrs \\ %{}) do
91 text = sequence(:text, &"This is :moominmamma: note #{&1}")
93 user = attrs[:user] || insert(:user)
99 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
100 "actor" => user.ap_id,
101 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
102 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
109 "2hu" => "corndog.png"
114 data: merge_attributes(data, Map.get(attrs, :data, %{}))
118 def attachment_factory(attrs \\ %{}) do
119 user = attrs[:user] || insert(:user)
122 attachment_data(user.ap_id, nil)
123 |> Map.put("id", Pleroma.Web.ActivityPub.Utils.generate_object_id())
126 data: merge_attributes(data, Map.get(attrs, :data, %{}))
130 def attachment_note_factory(attrs \\ %{}) do
131 user = attrs[:user] || insert(:user)
132 {length, attrs} = Map.pop(attrs, :length, 1)
136 Stream.repeatedly(fn -> attachment_data(user.ap_id, attrs[:href]) end)
140 build(:note, Map.put(attrs, :data, data))
143 defp attachment_data(ap_id, href) do
144 href = href || sequence(:href, &"#{Pleroma.Web.Endpoint.url()}/media/#{&1}.jpg")
151 "mediaType" => "image/jpeg"
154 "name" => "some name",
155 "type" => "Document",
157 "mediaType" => "image/jpeg"
161 def followers_only_note_factory(attrs \\ %{}) do
162 %Pleroma.Object{data: data} = note_factory(attrs)
163 %Pleroma.Object{data: Map.merge(data, %{"to" => [data["actor"] <> "/followers"]})}
166 def audio_factory(attrs \\ %{}) do
167 text = sequence(:text, &"lain radio episode #{&1}")
169 user = attrs[:user] || insert(:user)
173 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
176 "album" => "lain radio",
177 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
178 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
179 "actor" => user.ap_id,
184 data: merge_attributes(data, Map.get(attrs, :data, %{}))
188 def listen_factory do
189 audio = insert(:audio)
192 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
194 "actor" => audio.data["actor"],
195 "to" => audio.data["to"],
196 "object" => audio.data,
197 "published" => audio.data["published"]
202 actor: data["actor"],
203 recipients: data["to"]
207 def direct_note_factory do
208 user2 = insert(:user)
210 %Pleroma.Object{data: data} = note_factory()
211 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
214 def article_factory do
215 %Pleroma.Object{data: data} = note_factory()
216 %Pleroma.Object{data: Map.merge(data, %{"type" => "Article"})}
219 def tombstone_factory do
221 "type" => "Tombstone",
222 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
223 "formerType" => "Note",
224 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
232 def question_factory(attrs \\ %{}) do
233 user = attrs[:user] || insert(:user)
236 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
237 "type" => "Question",
238 "actor" => user.ap_id,
239 "attributedTo" => user.ap_id,
241 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
242 "cc" => [user.follower_address],
243 "context" => Pleroma.Web.ActivityPub.Utils.generate_context_id(),
244 "closed" => DateTime.utc_now() |> DateTime.add(86_400) |> DateTime.to_iso8601(),
248 "name" => "chocolate",
249 "replies" => %{"totalItems" => 0, "type" => "Collection"}
254 "replies" => %{"totalItems" => 0, "type" => "Collection"}
260 data: merge_attributes(data, Map.get(attrs, :data, %{}))
264 def direct_note_activity_factory do
265 dm = insert(:direct_note)
268 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
270 "actor" => dm.data["actor"],
271 "to" => dm.data["to"],
273 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
274 "context" => dm.data["context"]
279 actor: data["actor"],
280 recipients: data["to"]
284 def add_activity_factory(attrs \\ %{}) do
285 featured_collection_activity(attrs, "Add")
288 def remove_activity_factor(attrs \\ %{}) do
289 featured_collection_activity(attrs, "Remove")
292 defp featured_collection_activity(attrs, type) do
293 user = attrs[:user] || insert(:user)
294 note = attrs[:note] || insert(:note, user: user)
298 |> Map.get(:data_attrs, %{})
299 |> Map.put(:type, type)
301 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
305 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
306 "target" => user.featured_address,
307 "object" => note.data["object"],
308 "actor" => note.data["actor"],
310 "to" => [Pleroma.Constants.as_public()],
311 "cc" => [user.follower_address]
313 |> Map.merge(data_attrs)
317 actor: data["actor"],
318 recipients: data["to"]
323 def followers_only_note_activity_factory(attrs \\ %{}) do
324 user = attrs[:user] || insert(:user)
325 note = insert(:followers_only_note, user: user)
327 data_attrs = attrs[:data_attrs] || %{}
328 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
332 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
334 "actor" => note.data["actor"],
335 "to" => note.data["to"],
336 "object" => note.data,
337 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
338 "context" => note.data["context"]
340 |> Map.merge(data_attrs)
344 actor: data["actor"],
345 recipients: data["to"]
350 def note_activity_factory(attrs \\ %{}) do
351 user = attrs[:user] || insert(:user)
352 note = attrs[:note] || insert(:note, user: user)
354 data_attrs = attrs[:data_attrs] || %{}
355 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
359 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
361 "actor" => note.data["actor"],
362 "to" => note.data["to"],
363 "object" => note.data["id"],
364 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
365 "context" => note.data["context"]
367 |> Map.merge(data_attrs)
371 actor: data["actor"],
372 recipients: data["to"]
377 def article_activity_factory do
378 article = insert(:article)
381 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
383 "actor" => article.data["actor"],
384 "to" => article.data["to"],
385 "object" => article.data,
386 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
387 "context" => article.data["context"]
392 actor: data["actor"],
393 recipients: data["to"]
397 def announce_activity_factory(attrs \\ %{}) do
398 note_activity = attrs[:note_activity] || insert(:note_activity)
399 user = attrs[:user] || insert(:user)
402 "type" => "Announce",
403 "actor" => note_activity.actor,
404 "object" => note_activity.data["id"],
405 "to" => [user.follower_address, note_activity.data["actor"]],
406 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
407 "context" => note_activity.data["context"]
413 recipients: data["to"]
417 def like_activity_factory(attrs \\ %{}) do
418 note_activity = attrs[:note_activity] || insert(:note_activity)
419 object = Object.normalize(note_activity, fetch: false)
424 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
425 "actor" => user.ap_id,
427 "object" => object.data["id"],
428 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
430 |> Map.merge(attrs[:data_attrs] || %{})
437 def follow_activity_factory do
438 follower = insert(:user)
439 followed = insert(:user)
442 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
443 "actor" => follower.ap_id,
445 "object" => followed.ap_id,
446 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
451 actor: follower.ap_id
455 def report_activity_factory(attrs \\ %{}) do
456 user = attrs[:user] || insert(:user)
457 activity = attrs[:activity] || insert(:note_activity)
458 state = attrs[:state] || "open"
461 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
462 "actor" => user.ap_id,
464 "object" => [activity.actor, activity.data["id"]],
465 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
467 "cc" => [activity.actor],
468 "context" => activity.data["context"],
474 actor: data["actor"],
475 recipients: data["to"] ++ data["cc"]
479 def question_activity_factory(attrs \\ %{}) do
480 user = attrs[:user] || insert(:user)
481 question = attrs[:question] || insert(:question, user: user)
483 data_attrs = attrs[:data_attrs] || %{}
484 attrs = Map.drop(attrs, [:user, :question, :data_attrs])
488 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
490 "actor" => question.data["actor"],
491 "to" => question.data["to"],
492 "object" => question.data["id"],
493 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
494 "context" => question.data["context"]
496 |> Map.merge(data_attrs)
500 actor: data["actor"],
501 recipients: data["to"]
506 def oauth_app_factory do
507 %Pleroma.Web.OAuth.App{
508 client_name: sequence(:client_name, &"Some client #{&1}"),
509 redirect_uris: "https://example.com/callback",
510 scopes: ["read", "write", "follow", "push", "admin"],
511 website: "https://example.com",
512 client_id: Ecto.UUID.generate(),
513 client_secret: "aaa;/&bbb"
517 def instance_factory do
518 %Pleroma.Instances.Instance{
520 unreachable_since: nil
524 def oauth_token_factory(attrs \\ %{}) do
525 scopes = Map.get(attrs, :scopes, ["read"])
526 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
527 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
530 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
532 %Pleroma.Web.OAuth.Token{
533 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
534 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
538 valid_until: valid_until
542 def oauth_admin_token_factory(attrs \\ %{}) do
543 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
547 |> Map.get(:scopes, ["admin"])
548 |> Kernel.++(["admin"])
551 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
552 oauth_token_factory(attrs)
555 def oauth_authorization_factory do
556 %Pleroma.Web.OAuth.Authorization{
557 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
558 scopes: ["read", "write", "follow", "push"],
559 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
561 app: build(:oauth_app)
565 def push_subscription_factory do
566 %Pleroma.Web.Push.Subscription{
568 token: build(:oauth_token),
569 endpoint: "https://example.com/example/1234",
570 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
572 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
577 def notification_factory do
578 %Pleroma.Notification{
583 def scheduled_activity_factory do
584 %Pleroma.ScheduledActivity{
586 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
587 params: build(:note) |> Map.from_struct() |> Map.get(:data)
591 def registration_factory do
594 %Pleroma.Registration{
599 "name" => "John Doe",
600 "email" => "john@doe.com",
601 "nickname" => "johndoe",
602 "description" => "My bio"
607 def config_factory(attrs \\ %{}) do
609 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
614 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
617 |> merge_attributes(attrs)
620 def marker_factory do
623 timeline: "notifications",
629 def mfa_token_factory do
631 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
632 authorization: build(:oauth_authorization),
633 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
638 def filter_factory do
641 filter_id: sequence(:filter_id, & &1),
647 def announcement_factory(params \\ %{}) do
648 data = Map.get(params, :data, %{})
650 {_, params} = Map.pop(params, :data)
652 %Pleroma.Announcement{
653 data: Map.merge(%{"content" => "test announcement", "all_day" => false}, data)
656 |> Pleroma.Announcement.add_rendered_properties()