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
10 def participation_factory do
11 conversation = insert(:conversation)
14 %Pleroma.Conversation.Participation{
15 conversation: conversation,
21 def conversation_factory do
22 %Pleroma.Conversation{
23 ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}")
27 def user_factory(attrs \\ %{}) do
29 name: sequence(:name, &"Test テスト User #{&1}"),
30 email: sequence(:email, &"user#{&1}@example.com"),
31 nickname: sequence(:nickname, &"nick#{&1}"),
32 password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt("test"),
33 bio: sequence(:bio, &"Tester Number #{&1}"),
34 is_discoverable: true,
35 last_digest_emailed_at: NaiveDateTime.utc_now(),
36 last_refreshed_at: NaiveDateTime.utc_now(),
37 notification_settings: %Pleroma.User.NotificationSetting{},
38 multi_factor_authentication_settings: %Pleroma.MFA.Settings{},
43 if attrs[:local] == false do
44 base_domain = Enum.random(["domain1.com", "domain2.com", "domain3.com"])
46 ap_id = "https://#{base_domain}/users/#{user.nickname}"
50 follower_address: ap_id <> "/followers",
51 following_address: ap_id <> "/following"
55 ap_id: User.ap_id(user),
56 follower_address: User.ap_followers(user),
57 following_address: User.ap_following(user)
62 |> Map.put(:raw_bio, user.bio)
64 |> merge_attributes(attrs)
67 def user_relationship_factory(attrs \\ %{}) do
68 source = attrs[:source] || insert(:user)
69 target = attrs[:target] || insert(:user)
70 relationship_type = attrs[:relationship_type] || :block
72 %Pleroma.UserRelationship{
75 relationship_type: relationship_type
79 def note_factory(attrs \\ %{}) do
80 text = sequence(:text, &"This is :moominmamma: note #{&1}")
82 user = attrs[:user] || insert(:user)
88 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
89 "actor" => user.ap_id,
90 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
91 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
98 "2hu" => "corndog.png"
103 data: merge_attributes(data, Map.get(attrs, :data, %{}))
107 def attachment_note_factory(attrs \\ %{}) do
108 user = attrs[:user] || insert(:user)
109 {length, attrs} = Map.pop(attrs, :length, 1)
113 Stream.repeatedly(fn -> attachment_data(user.ap_id, attrs[:href]) end)
117 build(:note, Map.put(attrs, :data, data))
120 defp attachment_data(ap_id, href) do
121 href = href || sequence(:href, &"#{Pleroma.Web.Endpoint.url()}/media/#{&1}.jpg")
128 "mediaType" => "image/jpeg"
131 "name" => "some name",
132 "type" => "Document",
134 "mediaType" => "image/jpeg"
138 def audio_factory(attrs \\ %{}) do
139 text = sequence(:text, &"lain radio episode #{&1}")
141 user = attrs[:user] || insert(:user)
145 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
148 "album" => "lain radio",
149 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
150 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
151 "actor" => user.ap_id,
156 data: merge_attributes(data, Map.get(attrs, :data, %{}))
160 def listen_factory do
161 audio = insert(:audio)
164 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
166 "actor" => audio.data["actor"],
167 "to" => audio.data["to"],
168 "object" => audio.data,
169 "published" => audio.data["published"]
174 actor: data["actor"],
175 recipients: data["to"]
179 def direct_note_factory do
180 user2 = insert(:user)
182 %Pleroma.Object{data: data} = note_factory()
183 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
186 def article_factory do
188 |> Map.put("type", "Article")
191 def tombstone_factory do
193 "type" => "Tombstone",
194 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
195 "formerType" => "Note",
196 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
204 def question_factory(attrs \\ %{}) do
205 user = attrs[:user] || insert(:user)
208 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
209 "type" => "Question",
210 "actor" => user.ap_id,
211 "attributedTo" => user.ap_id,
213 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
214 "cc" => [user.follower_address],
215 "context" => Pleroma.Web.ActivityPub.Utils.generate_context_id(),
216 "closed" => DateTime.utc_now() |> DateTime.add(86_400) |> DateTime.to_iso8601(),
220 "name" => "chocolate",
221 "replies" => %{"totalItems" => 0, "type" => "Collection"}
226 "replies" => %{"totalItems" => 0, "type" => "Collection"}
232 data: merge_attributes(data, Map.get(attrs, :data, %{}))
236 def direct_note_activity_factory do
237 dm = insert(:direct_note)
240 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
242 "actor" => dm.data["actor"],
243 "to" => dm.data["to"],
245 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
246 "context" => dm.data["context"]
251 actor: data["actor"],
252 recipients: data["to"]
256 def note_activity_factory(attrs \\ %{}) do
257 user = attrs[:user] || insert(:user)
258 note = attrs[:note] || insert(:note, user: user)
260 data_attrs = attrs[:data_attrs] || %{}
261 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
265 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
267 "actor" => note.data["actor"],
268 "to" => note.data["to"],
269 "object" => note.data["id"],
270 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
271 "context" => note.data["context"]
273 |> Map.merge(data_attrs)
277 actor: data["actor"],
278 recipients: data["to"]
283 def article_activity_factory do
284 article = insert(:article)
287 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
289 "actor" => article.data["actor"],
290 "to" => article.data["to"],
291 "object" => article.data,
292 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
293 "context" => article.data["context"]
298 actor: data["actor"],
299 recipients: data["to"]
303 def announce_activity_factory(attrs \\ %{}) do
304 note_activity = attrs[:note_activity] || insert(:note_activity)
305 user = attrs[:user] || insert(:user)
308 "type" => "Announce",
309 "actor" => note_activity.actor,
310 "object" => note_activity.data["id"],
311 "to" => [user.follower_address, note_activity.data["actor"]],
312 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
313 "context" => note_activity.data["context"]
319 recipients: data["to"]
323 def like_activity_factory(attrs \\ %{}) do
324 note_activity = attrs[:note_activity] || insert(:note_activity)
325 object = Object.normalize(note_activity, fetch: false)
330 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
331 "actor" => user.ap_id,
333 "object" => object.data["id"],
334 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
336 |> Map.merge(attrs[:data_attrs] || %{})
343 def follow_activity_factory do
344 follower = insert(:user)
345 followed = insert(:user)
348 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
349 "actor" => follower.ap_id,
351 "object" => followed.ap_id,
352 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
357 actor: follower.ap_id
361 def report_activity_factory(attrs \\ %{}) do
362 user = attrs[:user] || insert(:user)
363 activity = attrs[:activity] || insert(:note_activity)
364 state = attrs[:state] || "open"
367 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
368 "actor" => user.ap_id,
370 "object" => [activity.actor, activity.data["id"]],
371 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
373 "cc" => [activity.actor],
374 "context" => activity.data["context"],
380 actor: data["actor"],
381 recipients: data["to"] ++ data["cc"]
385 def question_activity_factory(attrs \\ %{}) do
386 user = attrs[:user] || insert(:user)
387 question = attrs[:question] || insert(:question, user: user)
389 data_attrs = attrs[:data_attrs] || %{}
390 attrs = Map.drop(attrs, [:user, :question, :data_attrs])
394 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
396 "actor" => question.data["actor"],
397 "to" => question.data["to"],
398 "object" => question.data["id"],
399 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
400 "context" => question.data["context"]
402 |> Map.merge(data_attrs)
406 actor: data["actor"],
407 recipients: data["to"]
412 def oauth_app_factory do
413 %Pleroma.Web.OAuth.App{
414 client_name: sequence(:client_name, &"Some client #{&1}"),
415 redirect_uris: "https://example.com/callback",
416 scopes: ["read", "write", "follow", "push", "admin"],
417 website: "https://example.com",
418 client_id: Ecto.UUID.generate(),
419 client_secret: "aaa;/&bbb"
423 def instance_factory do
424 %Pleroma.Instances.Instance{
426 unreachable_since: nil
430 def oauth_token_factory(attrs \\ %{}) do
431 scopes = Map.get(attrs, :scopes, ["read"])
432 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
433 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
436 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
438 %Pleroma.Web.OAuth.Token{
439 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
440 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
444 valid_until: valid_until
448 def oauth_admin_token_factory(attrs \\ %{}) do
449 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
453 |> Map.get(:scopes, ["admin"])
454 |> Kernel.++(["admin"])
457 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
458 oauth_token_factory(attrs)
461 def oauth_authorization_factory do
462 %Pleroma.Web.OAuth.Authorization{
463 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
464 scopes: ["read", "write", "follow", "push"],
465 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
467 app: build(:oauth_app)
471 def push_subscription_factory do
472 %Pleroma.Web.Push.Subscription{
474 token: build(:oauth_token),
475 endpoint: "https://example.com/example/1234",
476 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
478 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
483 def notification_factory do
484 %Pleroma.Notification{
489 def scheduled_activity_factory do
490 %Pleroma.ScheduledActivity{
492 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
493 params: build(:note) |> Map.from_struct() |> Map.get(:data)
497 def registration_factory do
500 %Pleroma.Registration{
505 "name" => "John Doe",
506 "email" => "john@doe.com",
507 "nickname" => "johndoe",
508 "description" => "My bio"
513 def config_factory(attrs \\ %{}) do
515 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
520 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
523 |> merge_attributes(attrs)
526 def marker_factory do
529 timeline: "notifications",
535 def mfa_token_factory do
537 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
538 authorization: build(:oauth_authorization),
539 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
544 def filter_factory do
547 filter_id: sequence(:filter_id, & &1),