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
13 def participation_factory do
14 conversation = insert(:conversation)
17 %Pleroma.Conversation.Participation{
18 conversation: conversation,
24 def conversation_factory do
25 %Pleroma.Conversation{
26 ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}")
30 def user_factory(attrs \\ %{}) do
32 name: sequence(:name, &"Test テスト User #{&1}"),
33 email: sequence(:email, &"user#{&1}@example.com"),
34 nickname: sequence(:nickname, &"nick#{&1}"),
35 password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt("test"),
36 bio: sequence(:bio, &"Tester Number #{&1}"),
37 is_discoverable: true,
38 last_digest_emailed_at: NaiveDateTime.utc_now(),
39 last_refreshed_at: NaiveDateTime.utc_now(),
40 notification_settings: %Pleroma.User.NotificationSetting{},
41 multi_factor_authentication_settings: %Pleroma.MFA.Settings{},
46 if attrs[:local] == false do
47 base_domain = attrs[:domain] || Enum.random(["domain1.com", "domain2.com", "domain3.com"])
49 ap_id = "https://#{base_domain}/users/#{user.nickname}"
53 follower_address: ap_id <> "/followers",
54 following_address: ap_id <> "/following",
55 featured_address: ap_id <> "/collections/featured"
59 ap_id: User.ap_id(user),
60 follower_address: User.ap_followers(user),
61 following_address: User.ap_following(user),
62 featured_address: User.ap_featured_collection(user)
66 attrs = Map.delete(attrs, :domain)
69 |> Map.put(:raw_bio, user.bio)
71 |> merge_attributes(attrs)
74 def user_relationship_factory(attrs \\ %{}) do
75 source = attrs[:source] || insert(:user)
76 target = attrs[:target] || insert(:user)
77 relationship_type = attrs[:relationship_type] || :block
79 %Pleroma.UserRelationship{
82 relationship_type: relationship_type
86 def note_factory(attrs \\ %{}) do
87 text = sequence(:text, &"This is :moominmamma: note #{&1}")
89 user = attrs[:user] || insert(:user)
95 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
96 "actor" => user.ap_id,
97 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
98 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
105 "2hu" => "corndog.png"
110 data: merge_attributes(data, Map.get(attrs, :data, %{}))
114 def attachment_factory(attrs \\ %{}) do
115 user = attrs[:user] || insert(:user)
118 attachment_data(user.ap_id, nil)
119 |> Map.put("id", Pleroma.Web.ActivityPub.Utils.generate_object_id())
122 data: merge_attributes(data, Map.get(attrs, :data, %{}))
126 def attachment_note_factory(attrs \\ %{}) do
127 user = attrs[:user] || insert(:user)
128 {length, attrs} = Map.pop(attrs, :length, 1)
132 Stream.repeatedly(fn -> attachment_data(user.ap_id, attrs[:href]) end)
136 build(:note, Map.put(attrs, :data, data))
139 defp attachment_data(ap_id, href) do
140 href = href || sequence(:href, &"#{Pleroma.Web.Endpoint.url()}/media/#{&1}.jpg")
147 "mediaType" => "image/jpeg"
150 "name" => "some name",
151 "type" => "Document",
153 "mediaType" => "image/jpeg"
157 def followers_only_note_factory(attrs \\ %{}) do
158 %Pleroma.Object{data: data} = note_factory(attrs)
159 %Pleroma.Object{data: Map.merge(data, %{"to" => [data["actor"] <> "/followers"]})}
162 def audio_factory(attrs \\ %{}) do
163 text = sequence(:text, &"lain radio episode #{&1}")
165 user = attrs[:user] || insert(:user)
169 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
172 "album" => "lain radio",
173 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
174 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
175 "actor" => user.ap_id,
180 data: merge_attributes(data, Map.get(attrs, :data, %{}))
184 def listen_factory do
185 audio = insert(:audio)
188 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
190 "actor" => audio.data["actor"],
191 "to" => audio.data["to"],
192 "object" => audio.data,
193 "published" => audio.data["published"]
198 actor: data["actor"],
199 recipients: data["to"]
203 def direct_note_factory do
204 user2 = insert(:user)
206 %Pleroma.Object{data: data} = note_factory()
207 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
210 def article_factory do
211 %Pleroma.Object{data: data} = note_factory()
212 %Pleroma.Object{data: Map.merge(data, %{"type" => "Article"})}
215 def tombstone_factory do
217 "type" => "Tombstone",
218 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
219 "formerType" => "Note",
220 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
228 def question_factory(attrs \\ %{}) do
229 user = attrs[:user] || insert(:user)
232 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
233 "type" => "Question",
234 "actor" => user.ap_id,
235 "attributedTo" => user.ap_id,
237 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
238 "cc" => [user.follower_address],
239 "context" => Pleroma.Web.ActivityPub.Utils.generate_context_id(),
240 "closed" => DateTime.utc_now() |> DateTime.add(86_400) |> DateTime.to_iso8601(),
244 "name" => "chocolate",
245 "replies" => %{"totalItems" => 0, "type" => "Collection"}
250 "replies" => %{"totalItems" => 0, "type" => "Collection"}
256 data: merge_attributes(data, Map.get(attrs, :data, %{}))
260 def direct_note_activity_factory do
261 dm = insert(:direct_note)
264 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
266 "actor" => dm.data["actor"],
267 "to" => dm.data["to"],
269 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
270 "context" => dm.data["context"]
275 actor: data["actor"],
276 recipients: data["to"]
280 def add_activity_factory(attrs \\ %{}) do
281 featured_collection_activity(attrs, "Add")
284 def remove_activity_factor(attrs \\ %{}) do
285 featured_collection_activity(attrs, "Remove")
288 defp featured_collection_activity(attrs, type) do
289 user = attrs[:user] || insert(:user)
290 note = attrs[:note] || insert(:note, user: user)
294 |> Map.get(:data_attrs, %{})
295 |> Map.put(:type, type)
297 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
301 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
302 "target" => user.featured_address,
303 "object" => note.data["object"],
304 "actor" => note.data["actor"],
306 "to" => [Pleroma.Constants.as_public()],
307 "cc" => [user.follower_address]
309 |> Map.merge(data_attrs)
313 actor: data["actor"],
314 recipients: data["to"]
319 def followers_only_note_activity_factory(attrs \\ %{}) do
320 user = attrs[:user] || insert(:user)
321 note = insert(:followers_only_note, user: user)
323 data_attrs = attrs[:data_attrs] || %{}
324 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
328 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
330 "actor" => note.data["actor"],
331 "to" => note.data["to"],
332 "object" => note.data,
333 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
334 "context" => note.data["context"]
336 |> Map.merge(data_attrs)
340 actor: data["actor"],
341 recipients: data["to"]
346 def note_activity_factory(attrs \\ %{}) do
347 user = attrs[:user] || insert(:user)
348 note = attrs[:note] || insert(:note, user: user)
350 data_attrs = attrs[:data_attrs] || %{}
351 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
355 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
357 "actor" => note.data["actor"],
358 "to" => note.data["to"],
359 "object" => note.data["id"],
360 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
361 "context" => note.data["context"]
363 |> Map.merge(data_attrs)
367 actor: data["actor"],
368 recipients: data["to"]
373 def article_activity_factory do
374 article = insert(:article)
377 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
379 "actor" => article.data["actor"],
380 "to" => article.data["to"],
381 "object" => article.data,
382 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
383 "context" => article.data["context"]
388 actor: data["actor"],
389 recipients: data["to"]
393 def announce_activity_factory(attrs \\ %{}) do
394 note_activity = attrs[:note_activity] || insert(:note_activity)
395 user = attrs[:user] || insert(:user)
398 "type" => "Announce",
399 "actor" => note_activity.actor,
400 "object" => note_activity.data["id"],
401 "to" => [user.follower_address, note_activity.data["actor"]],
402 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
403 "context" => note_activity.data["context"]
409 recipients: data["to"]
413 def like_activity_factory(attrs \\ %{}) do
414 note_activity = attrs[:note_activity] || insert(:note_activity)
415 object = Object.normalize(note_activity, fetch: false)
420 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
421 "actor" => user.ap_id,
423 "object" => object.data["id"],
424 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
426 |> Map.merge(attrs[:data_attrs] || %{})
433 def follow_activity_factory do
434 follower = insert(:user)
435 followed = insert(:user)
438 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
439 "actor" => follower.ap_id,
441 "object" => followed.ap_id,
442 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
447 actor: follower.ap_id
451 def report_activity_factory(attrs \\ %{}) do
452 user = attrs[:user] || insert(:user)
453 activity = attrs[:activity] || insert(:note_activity)
454 state = attrs[:state] || "open"
457 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
458 "actor" => user.ap_id,
460 "object" => [activity.actor, activity.data["id"]],
461 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
463 "cc" => [activity.actor],
464 "context" => activity.data["context"],
470 actor: data["actor"],
471 recipients: data["to"] ++ data["cc"]
475 def question_activity_factory(attrs \\ %{}) do
476 user = attrs[:user] || insert(:user)
477 question = attrs[:question] || insert(:question, user: user)
479 data_attrs = attrs[:data_attrs] || %{}
480 attrs = Map.drop(attrs, [:user, :question, :data_attrs])
484 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
486 "actor" => question.data["actor"],
487 "to" => question.data["to"],
488 "object" => question.data["id"],
489 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
490 "context" => question.data["context"]
492 |> Map.merge(data_attrs)
496 actor: data["actor"],
497 recipients: data["to"]
502 def oauth_app_factory do
503 %Pleroma.Web.OAuth.App{
504 client_name: sequence(:client_name, &"Some client #{&1}"),
505 redirect_uris: "https://example.com/callback",
506 scopes: ["read", "write", "follow", "push", "admin"],
507 website: "https://example.com",
508 client_id: Ecto.UUID.generate(),
509 client_secret: "aaa;/&bbb"
513 def instance_factory do
514 %Pleroma.Instances.Instance{
516 unreachable_since: nil
520 def oauth_token_factory(attrs \\ %{}) do
521 scopes = Map.get(attrs, :scopes, ["read"])
522 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
523 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
526 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
528 %Pleroma.Web.OAuth.Token{
529 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
530 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
534 valid_until: valid_until
538 def oauth_admin_token_factory(attrs \\ %{}) do
539 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
543 |> Map.get(:scopes, ["admin"])
544 |> Kernel.++(["admin"])
547 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
548 oauth_token_factory(attrs)
551 def oauth_authorization_factory do
552 %Pleroma.Web.OAuth.Authorization{
553 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
554 scopes: ["read", "write", "follow", "push"],
555 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
557 app: build(:oauth_app)
561 def push_subscription_factory do
562 %Pleroma.Web.Push.Subscription{
564 token: build(:oauth_token),
565 endpoint: "https://example.com/example/1234",
566 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
568 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
573 def notification_factory do
574 %Pleroma.Notification{
579 def scheduled_activity_factory do
580 %Pleroma.ScheduledActivity{
582 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
583 params: build(:note) |> Map.from_struct() |> Map.get(:data)
587 def registration_factory do
590 %Pleroma.Registration{
595 "name" => "John Doe",
596 "email" => "john@doe.com",
597 "nickname" => "johndoe",
598 "description" => "My bio"
603 def config_factory(attrs \\ %{}) do
605 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
610 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
613 |> merge_attributes(attrs)
616 def marker_factory do
619 timeline: "notifications",
625 def mfa_token_factory do
627 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
628 authorization: build(:oauth_authorization),
629 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
634 def filter_factory do
637 filter_id: sequence(:filter_id, & &1),
643 def announcement_factory(params \\ %{}) do
644 data = Map.get(params, :data, %{})
646 {_, params} = Map.pop(params, :data)
648 %Pleroma.Announcement{
649 data: Map.merge(%{"content" => "test announcement", "all_day" => false}, data)
652 |> Pleroma.Announcement.add_rendered_properties()