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 = attrs[: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)
63 attrs = Map.delete(attrs, :domain)
66 |> Map.put(:raw_bio, user.bio)
68 |> merge_attributes(attrs)
71 def user_relationship_factory(attrs \\ %{}) do
72 source = attrs[:source] || insert(:user)
73 target = attrs[:target] || insert(:user)
74 relationship_type = attrs[:relationship_type] || :block
76 %Pleroma.UserRelationship{
79 relationship_type: relationship_type
83 def note_factory(attrs \\ %{}) do
84 text = sequence(:text, &"This is :moominmamma: note #{&1}")
86 user = attrs[:user] || insert(:user)
92 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
93 "actor" => user.ap_id,
94 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
95 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
102 "2hu" => "corndog.png"
107 data: merge_attributes(data, Map.get(attrs, :data, %{}))
111 def attachment_note_factory(attrs \\ %{}) do
112 user = attrs[:user] || insert(:user)
113 {length, attrs} = Map.pop(attrs, :length, 1)
117 Stream.repeatedly(fn -> attachment_data(user.ap_id, attrs[:href]) end)
121 build(:note, Map.put(attrs, :data, data))
124 defp attachment_data(ap_id, href) do
125 href = href || sequence(:href, &"#{Pleroma.Web.Endpoint.url()}/media/#{&1}.jpg")
132 "mediaType" => "image/jpeg"
135 "name" => "some name",
136 "type" => "Document",
138 "mediaType" => "image/jpeg"
142 def audio_factory(attrs \\ %{}) do
143 text = sequence(:text, &"lain radio episode #{&1}")
145 user = attrs[:user] || insert(:user)
149 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
152 "album" => "lain radio",
153 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
154 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
155 "actor" => user.ap_id,
160 data: merge_attributes(data, Map.get(attrs, :data, %{}))
164 def listen_factory do
165 audio = insert(:audio)
168 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
170 "actor" => audio.data["actor"],
171 "to" => audio.data["to"],
172 "object" => audio.data,
173 "published" => audio.data["published"]
178 actor: data["actor"],
179 recipients: data["to"]
183 def direct_note_factory do
184 user2 = insert(:user)
186 %Pleroma.Object{data: data} = note_factory()
187 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
190 def article_factory do
192 |> Map.put("type", "Article")
195 def tombstone_factory do
197 "type" => "Tombstone",
198 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
199 "formerType" => "Note",
200 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
208 def direct_note_activity_factory do
209 dm = insert(:direct_note)
212 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
214 "actor" => dm.data["actor"],
215 "to" => dm.data["to"],
217 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
218 "context" => dm.data["context"]
223 actor: data["actor"],
224 recipients: data["to"]
228 def note_activity_factory(attrs \\ %{}) do
229 user = attrs[:user] || insert(:user)
230 note = attrs[:note] || insert(:note, user: user)
232 data_attrs = attrs[:data_attrs] || %{}
233 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
237 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
239 "actor" => note.data["actor"],
240 "to" => note.data["to"],
241 "object" => note.data["id"],
242 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
243 "context" => note.data["context"]
245 |> Map.merge(data_attrs)
249 actor: data["actor"],
250 recipients: data["to"]
255 def article_activity_factory do
256 article = insert(:article)
259 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
261 "actor" => article.data["actor"],
262 "to" => article.data["to"],
263 "object" => article.data,
264 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
265 "context" => article.data["context"]
270 actor: data["actor"],
271 recipients: data["to"]
275 def announce_activity_factory(attrs \\ %{}) do
276 note_activity = attrs[:note_activity] || insert(:note_activity)
277 user = attrs[:user] || insert(:user)
280 "type" => "Announce",
281 "actor" => note_activity.actor,
282 "object" => note_activity.data["id"],
283 "to" => [user.follower_address, note_activity.data["actor"]],
284 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
285 "context" => note_activity.data["context"]
291 recipients: data["to"]
295 def like_activity_factory(attrs \\ %{}) do
296 note_activity = attrs[:note_activity] || insert(:note_activity)
297 object = Object.normalize(note_activity, fetch: false)
302 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
303 "actor" => user.ap_id,
305 "object" => object.data["id"],
306 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
308 |> Map.merge(attrs[:data_attrs] || %{})
315 def follow_activity_factory do
316 follower = insert(:user)
317 followed = insert(:user)
320 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
321 "actor" => follower.ap_id,
323 "object" => followed.ap_id,
324 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
329 actor: follower.ap_id
333 def report_activity_factory(attrs \\ %{}) do
334 user = attrs[:user] || insert(:user)
335 activity = attrs[:activity] || insert(:note_activity)
336 state = attrs[:state] || "open"
339 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
340 "actor" => user.ap_id,
342 "object" => [activity.actor, activity.data["id"]],
343 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
345 "cc" => [activity.actor],
346 "context" => activity.data["context"],
352 actor: data["actor"],
353 recipients: data["to"] ++ data["cc"]
357 def oauth_app_factory do
358 %Pleroma.Web.OAuth.App{
359 client_name: sequence(:client_name, &"Some client #{&1}"),
360 redirect_uris: "https://example.com/callback",
361 scopes: ["read", "write", "follow", "push", "admin"],
362 website: "https://example.com",
363 client_id: Ecto.UUID.generate(),
364 client_secret: "aaa;/&bbb"
368 def instance_factory do
369 %Pleroma.Instances.Instance{
371 unreachable_since: nil
375 def oauth_token_factory(attrs \\ %{}) do
376 scopes = Map.get(attrs, :scopes, ["read"])
377 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
378 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
381 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
383 %Pleroma.Web.OAuth.Token{
384 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
385 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
389 valid_until: valid_until
393 def oauth_admin_token_factory(attrs \\ %{}) do
394 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
398 |> Map.get(:scopes, ["admin"])
399 |> Kernel.++(["admin"])
402 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
403 oauth_token_factory(attrs)
406 def oauth_authorization_factory do
407 %Pleroma.Web.OAuth.Authorization{
408 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
409 scopes: ["read", "write", "follow", "push"],
410 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
412 app: build(:oauth_app)
416 def push_subscription_factory do
417 %Pleroma.Web.Push.Subscription{
419 token: build(:oauth_token),
420 endpoint: "https://example.com/example/1234",
421 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
423 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
428 def notification_factory do
429 %Pleroma.Notification{
434 def scheduled_activity_factory do
435 %Pleroma.ScheduledActivity{
437 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
438 params: build(:note) |> Map.from_struct() |> Map.get(:data)
442 def registration_factory do
445 %Pleroma.Registration{
450 "name" => "John Doe",
451 "email" => "john@doe.com",
452 "nickname" => "johndoe",
453 "description" => "My bio"
458 def config_factory(attrs \\ %{}) do
460 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
465 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
468 |> merge_attributes(attrs)
471 def marker_factory do
474 timeline: "notifications",
480 def mfa_token_factory do
482 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
483 authorization: build(:oauth_authorization),
484 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
489 def filter_factory do
492 filter_id: sequence(:filter_id, & &1),