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_note_factory(attrs \\ %{}) do
115 user = attrs[:user] || insert(:user)
116 {length, attrs} = Map.pop(attrs, :length, 1)
120 Stream.repeatedly(fn -> attachment_data(user.ap_id, attrs[:href]) end)
124 build(:note, Map.put(attrs, :data, data))
127 defp attachment_data(ap_id, href) do
128 href = href || sequence(:href, &"#{Pleroma.Web.Endpoint.url()}/media/#{&1}.jpg")
135 "mediaType" => "image/jpeg"
138 "name" => "some name",
139 "type" => "Document",
141 "mediaType" => "image/jpeg"
145 def followers_only_note_factory(attrs \\ %{}) do
146 %Pleroma.Object{data: data} = note_factory(attrs)
147 %Pleroma.Object{data: Map.merge(data, %{"to" => [data["actor"] <> "/followers"]})}
150 def audio_factory(attrs \\ %{}) do
151 text = sequence(:text, &"lain radio episode #{&1}")
153 user = attrs[:user] || insert(:user)
157 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
160 "album" => "lain radio",
161 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
162 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
163 "actor" => user.ap_id,
168 data: merge_attributes(data, Map.get(attrs, :data, %{}))
172 def listen_factory do
173 audio = insert(:audio)
176 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
178 "actor" => audio.data["actor"],
179 "to" => audio.data["to"],
180 "object" => audio.data,
181 "published" => audio.data["published"]
186 actor: data["actor"],
187 recipients: data["to"]
191 def direct_note_factory do
192 user2 = insert(:user)
194 %Pleroma.Object{data: data} = note_factory()
195 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
198 def article_factory do
199 %Pleroma.Object{data: data} = note_factory()
200 %Pleroma.Object{data: Map.merge(data, %{"type" => "Article"})}
203 def tombstone_factory do
205 "type" => "Tombstone",
206 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
207 "formerType" => "Note",
208 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
216 def question_factory(attrs \\ %{}) do
217 user = attrs[:user] || insert(:user)
220 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
221 "type" => "Question",
222 "actor" => user.ap_id,
223 "attributedTo" => user.ap_id,
225 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
226 "cc" => [user.follower_address],
227 "context" => Pleroma.Web.ActivityPub.Utils.generate_context_id(),
228 "closed" => DateTime.utc_now() |> DateTime.add(86_400) |> DateTime.to_iso8601(),
232 "name" => "chocolate",
233 "replies" => %{"totalItems" => 0, "type" => "Collection"}
238 "replies" => %{"totalItems" => 0, "type" => "Collection"}
244 data: merge_attributes(data, Map.get(attrs, :data, %{}))
248 def direct_note_activity_factory do
249 dm = insert(:direct_note)
252 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
254 "actor" => dm.data["actor"],
255 "to" => dm.data["to"],
257 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
258 "context" => dm.data["context"]
263 actor: data["actor"],
264 recipients: data["to"]
268 def add_activity_factory(attrs \\ %{}) do
269 featured_collection_activity(attrs, "Add")
272 def remove_activity_factor(attrs \\ %{}) do
273 featured_collection_activity(attrs, "Remove")
276 defp featured_collection_activity(attrs, type) do
277 user = attrs[:user] || insert(:user)
278 note = attrs[:note] || insert(:note, user: user)
282 |> Map.get(:data_attrs, %{})
283 |> Map.put(:type, type)
285 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
289 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
290 "target" => user.featured_address,
291 "object" => note.data["object"],
292 "actor" => note.data["actor"],
294 "to" => [Pleroma.Constants.as_public()],
295 "cc" => [user.follower_address]
297 |> Map.merge(data_attrs)
301 actor: data["actor"],
302 recipients: data["to"]
307 def followers_only_note_activity_factory(attrs \\ %{}) do
308 user = attrs[:user] || insert(:user)
309 note = insert(:followers_only_note, user: user)
311 data_attrs = attrs[:data_attrs] || %{}
312 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
316 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
318 "actor" => note.data["actor"],
319 "to" => note.data["to"],
320 "object" => note.data,
321 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
322 "context" => note.data["context"]
324 |> Map.merge(data_attrs)
328 actor: data["actor"],
329 recipients: data["to"]
334 def note_activity_factory(attrs \\ %{}) do
335 user = attrs[:user] || insert(:user)
336 note = attrs[:note] || insert(:note, user: user)
338 data_attrs = attrs[:data_attrs] || %{}
339 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
343 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
345 "actor" => note.data["actor"],
346 "to" => note.data["to"],
347 "object" => note.data["id"],
348 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
349 "context" => note.data["context"]
351 |> Map.merge(data_attrs)
355 actor: data["actor"],
356 recipients: data["to"]
361 def article_activity_factory do
362 article = insert(:article)
365 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
367 "actor" => article.data["actor"],
368 "to" => article.data["to"],
369 "object" => article.data,
370 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
371 "context" => article.data["context"]
376 actor: data["actor"],
377 recipients: data["to"]
381 def announce_activity_factory(attrs \\ %{}) do
382 note_activity = attrs[:note_activity] || insert(:note_activity)
383 user = attrs[:user] || insert(:user)
386 "type" => "Announce",
387 "actor" => note_activity.actor,
388 "object" => note_activity.data["id"],
389 "to" => [user.follower_address, note_activity.data["actor"]],
390 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
391 "context" => note_activity.data["context"]
397 recipients: data["to"]
401 def like_activity_factory(attrs \\ %{}) do
402 note_activity = attrs[:note_activity] || insert(:note_activity)
403 object = Object.normalize(note_activity, fetch: false)
408 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
409 "actor" => user.ap_id,
411 "object" => object.data["id"],
412 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
414 |> Map.merge(attrs[:data_attrs] || %{})
421 def follow_activity_factory do
422 follower = insert(:user)
423 followed = insert(:user)
426 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
427 "actor" => follower.ap_id,
429 "object" => followed.ap_id,
430 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
435 actor: follower.ap_id
439 def report_activity_factory(attrs \\ %{}) do
440 user = attrs[:user] || insert(:user)
441 activity = attrs[:activity] || insert(:note_activity)
442 state = attrs[:state] || "open"
445 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
446 "actor" => user.ap_id,
448 "object" => [activity.actor, activity.data["id"]],
449 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
451 "cc" => [activity.actor],
452 "context" => activity.data["context"],
458 actor: data["actor"],
459 recipients: data["to"] ++ data["cc"]
463 def question_activity_factory(attrs \\ %{}) do
464 user = attrs[:user] || insert(:user)
465 question = attrs[:question] || insert(:question, user: user)
467 data_attrs = attrs[:data_attrs] || %{}
468 attrs = Map.drop(attrs, [:user, :question, :data_attrs])
472 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
474 "actor" => question.data["actor"],
475 "to" => question.data["to"],
476 "object" => question.data["id"],
477 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
478 "context" => question.data["context"]
480 |> Map.merge(data_attrs)
484 actor: data["actor"],
485 recipients: data["to"]
490 def oauth_app_factory do
491 %Pleroma.Web.OAuth.App{
492 client_name: sequence(:client_name, &"Some client #{&1}"),
493 redirect_uris: "https://example.com/callback",
494 scopes: ["read", "write", "follow", "push", "admin"],
495 website: "https://example.com",
496 client_id: Ecto.UUID.generate(),
497 client_secret: "aaa;/&bbb"
501 def instance_factory do
502 %Pleroma.Instances.Instance{
504 unreachable_since: nil
508 def oauth_token_factory(attrs \\ %{}) do
509 scopes = Map.get(attrs, :scopes, ["read"])
510 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
511 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
514 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
516 %Pleroma.Web.OAuth.Token{
517 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
518 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
522 valid_until: valid_until
526 def oauth_admin_token_factory(attrs \\ %{}) do
527 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
531 |> Map.get(:scopes, ["admin"])
532 |> Kernel.++(["admin"])
535 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
536 oauth_token_factory(attrs)
539 def oauth_authorization_factory do
540 %Pleroma.Web.OAuth.Authorization{
541 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
542 scopes: ["read", "write", "follow", "push"],
543 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
545 app: build(:oauth_app)
549 def push_subscription_factory do
550 %Pleroma.Web.Push.Subscription{
552 token: build(:oauth_token),
553 endpoint: "https://example.com/example/1234",
554 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
556 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
561 def notification_factory do
562 %Pleroma.Notification{
567 def scheduled_activity_factory do
568 %Pleroma.ScheduledActivity{
570 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
571 params: build(:note) |> Map.from_struct() |> Map.get(:data)
575 def registration_factory do
578 %Pleroma.Registration{
583 "name" => "John Doe",
584 "email" => "john@doe.com",
585 "nickname" => "johndoe",
586 "description" => "My bio"
591 def config_factory(attrs \\ %{}) do
593 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
598 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
601 |> merge_attributes(attrs)
604 def marker_factory do
607 timeline: "notifications",
613 def mfa_token_factory do
615 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
616 authorization: build(:oauth_authorization),
617 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
622 def filter_factory do
625 filter_id: sequence(:filter_id, & &1),