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",
52 featured_address: ap_id <> "/collections/featured"
56 ap_id: User.ap_id(user),
57 follower_address: User.ap_followers(user),
58 following_address: User.ap_following(user),
59 featured_address: User.ap_featured_collection(user)
64 |> Map.put(:raw_bio, user.bio)
66 |> merge_attributes(attrs)
69 def user_relationship_factory(attrs \\ %{}) do
70 source = attrs[:source] || insert(:user)
71 target = attrs[:target] || insert(:user)
72 relationship_type = attrs[:relationship_type] || :block
74 %Pleroma.UserRelationship{
77 relationship_type: relationship_type
81 def note_factory(attrs \\ %{}) do
82 text = sequence(:text, &"This is :moominmamma: note #{&1}")
84 user = attrs[:user] || insert(:user)
90 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
91 "actor" => user.ap_id,
92 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
93 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
100 "2hu" => "corndog.png"
105 data: merge_attributes(data, Map.get(attrs, :data, %{}))
109 def attachment_note_factory(attrs \\ %{}) do
110 user = attrs[:user] || insert(:user)
111 {length, attrs} = Map.pop(attrs, :length, 1)
115 Stream.repeatedly(fn -> attachment_data(user.ap_id, attrs[:href]) end)
119 build(:note, Map.put(attrs, :data, data))
122 defp attachment_data(ap_id, href) do
123 href = href || sequence(:href, &"#{Pleroma.Web.Endpoint.url()}/media/#{&1}.jpg")
130 "mediaType" => "image/jpeg"
133 "name" => "some name",
134 "type" => "Document",
136 "mediaType" => "image/jpeg"
140 def audio_factory(attrs \\ %{}) do
141 text = sequence(:text, &"lain radio episode #{&1}")
143 user = attrs[:user] || insert(:user)
147 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
150 "album" => "lain radio",
151 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
152 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
153 "actor" => user.ap_id,
158 data: merge_attributes(data, Map.get(attrs, :data, %{}))
162 def listen_factory do
163 audio = insert(:audio)
166 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
168 "actor" => audio.data["actor"],
169 "to" => audio.data["to"],
170 "object" => audio.data,
171 "published" => audio.data["published"]
176 actor: data["actor"],
177 recipients: data["to"]
181 def direct_note_factory do
182 user2 = insert(:user)
184 %Pleroma.Object{data: data} = note_factory()
185 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
188 def article_factory do
190 |> Map.put("type", "Article")
193 def tombstone_factory do
195 "type" => "Tombstone",
196 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
197 "formerType" => "Note",
198 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
206 def direct_note_activity_factory do
207 dm = insert(:direct_note)
210 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
212 "actor" => dm.data["actor"],
213 "to" => dm.data["to"],
215 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
216 "context" => dm.data["context"]
221 actor: data["actor"],
222 recipients: data["to"]
226 def note_activity_factory(attrs \\ %{}) do
227 user = attrs[:user] || insert(:user)
228 note = attrs[:note] || insert(:note, user: user)
230 data_attrs = attrs[:data_attrs] || %{}
231 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
235 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
237 "actor" => note.data["actor"],
238 "to" => note.data["to"],
239 "object" => note.data["id"],
240 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
241 "context" => note.data["context"]
243 |> Map.merge(data_attrs)
247 actor: data["actor"],
248 recipients: data["to"]
253 def article_activity_factory do
254 article = insert(:article)
257 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
259 "actor" => article.data["actor"],
260 "to" => article.data["to"],
261 "object" => article.data,
262 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
263 "context" => article.data["context"]
268 actor: data["actor"],
269 recipients: data["to"]
273 def announce_activity_factory(attrs \\ %{}) do
274 note_activity = attrs[:note_activity] || insert(:note_activity)
275 user = attrs[:user] || insert(:user)
278 "type" => "Announce",
279 "actor" => note_activity.actor,
280 "object" => note_activity.data["id"],
281 "to" => [user.follower_address, note_activity.data["actor"]],
282 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
283 "context" => note_activity.data["context"]
289 recipients: data["to"]
293 def like_activity_factory(attrs \\ %{}) do
294 note_activity = attrs[:note_activity] || insert(:note_activity)
295 object = Object.normalize(note_activity, fetch: false)
300 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
301 "actor" => user.ap_id,
303 "object" => object.data["id"],
304 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
306 |> Map.merge(attrs[:data_attrs] || %{})
313 def follow_activity_factory do
314 follower = insert(:user)
315 followed = insert(:user)
318 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
319 "actor" => follower.ap_id,
321 "object" => followed.ap_id,
322 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
327 actor: follower.ap_id
331 def report_activity_factory(attrs \\ %{}) do
332 user = attrs[:user] || insert(:user)
333 activity = attrs[:activity] || insert(:note_activity)
334 state = attrs[:state] || "open"
337 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
338 "actor" => user.ap_id,
340 "object" => [activity.actor, activity.data["id"]],
341 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
343 "cc" => [activity.actor],
344 "context" => activity.data["context"],
350 actor: data["actor"],
351 recipients: data["to"] ++ data["cc"]
355 def oauth_app_factory do
356 %Pleroma.Web.OAuth.App{
357 client_name: sequence(:client_name, &"Some client #{&1}"),
358 redirect_uris: "https://example.com/callback",
359 scopes: ["read", "write", "follow", "push", "admin"],
360 website: "https://example.com",
361 client_id: Ecto.UUID.generate(),
362 client_secret: "aaa;/&bbb"
366 def instance_factory do
367 %Pleroma.Instances.Instance{
369 unreachable_since: nil
373 def oauth_token_factory(attrs \\ %{}) do
374 scopes = Map.get(attrs, :scopes, ["read"])
375 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
376 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
379 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
381 %Pleroma.Web.OAuth.Token{
382 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
383 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
387 valid_until: valid_until
391 def oauth_admin_token_factory(attrs \\ %{}) do
392 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
396 |> Map.get(:scopes, ["admin"])
397 |> Kernel.++(["admin"])
400 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
401 oauth_token_factory(attrs)
404 def oauth_authorization_factory do
405 %Pleroma.Web.OAuth.Authorization{
406 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
407 scopes: ["read", "write", "follow", "push"],
408 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
410 app: build(:oauth_app)
414 def push_subscription_factory do
415 %Pleroma.Web.Push.Subscription{
417 token: build(:oauth_token),
418 endpoint: "https://example.com/example/1234",
419 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
421 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
426 def notification_factory do
427 %Pleroma.Notification{
432 def scheduled_activity_factory do
433 %Pleroma.ScheduledActivity{
435 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
436 params: build(:note) |> Map.from_struct() |> Map.get(:data)
440 def registration_factory do
443 %Pleroma.Registration{
448 "name" => "John Doe",
449 "email" => "john@doe.com",
450 "nickname" => "johndoe",
451 "description" => "My bio"
456 def config_factory(attrs \\ %{}) do
458 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
463 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
466 |> merge_attributes(attrs)
469 def marker_factory do
472 timeline: "notifications",
478 def mfa_token_factory do
480 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
481 authorization: build(:oauth_authorization),
482 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
487 def filter_factory do
490 filter_id: sequence(:filter_id, & &1),