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 direct_note_activity_factory do
205 dm = insert(:direct_note)
208 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
210 "actor" => dm.data["actor"],
211 "to" => dm.data["to"],
213 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
214 "context" => dm.data["context"]
219 actor: data["actor"],
220 recipients: data["to"]
224 def note_activity_factory(attrs \\ %{}) do
225 user = attrs[:user] || insert(:user)
226 note = attrs[:note] || insert(:note, user: user)
228 data_attrs = attrs[:data_attrs] || %{}
229 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
233 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
235 "actor" => note.data["actor"],
236 "to" => note.data["to"],
237 "object" => note.data["id"],
238 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
239 "context" => note.data["context"]
241 |> Map.merge(data_attrs)
245 actor: data["actor"],
246 recipients: data["to"]
251 def article_activity_factory do
252 article = insert(:article)
255 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
257 "actor" => article.data["actor"],
258 "to" => article.data["to"],
259 "object" => article.data,
260 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
261 "context" => article.data["context"]
266 actor: data["actor"],
267 recipients: data["to"]
271 def announce_activity_factory(attrs \\ %{}) do
272 note_activity = attrs[:note_activity] || insert(:note_activity)
273 user = attrs[:user] || insert(:user)
276 "type" => "Announce",
277 "actor" => note_activity.actor,
278 "object" => note_activity.data["id"],
279 "to" => [user.follower_address, note_activity.data["actor"]],
280 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
281 "context" => note_activity.data["context"]
287 recipients: data["to"]
291 def like_activity_factory(attrs \\ %{}) do
292 note_activity = attrs[:note_activity] || insert(:note_activity)
293 object = Object.normalize(note_activity, fetch: false)
298 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
299 "actor" => user.ap_id,
301 "object" => object.data["id"],
302 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
304 |> Map.merge(attrs[:data_attrs] || %{})
311 def follow_activity_factory do
312 follower = insert(:user)
313 followed = insert(:user)
316 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
317 "actor" => follower.ap_id,
319 "object" => followed.ap_id,
320 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
325 actor: follower.ap_id
329 def report_activity_factory(attrs \\ %{}) do
330 user = attrs[:user] || insert(:user)
331 activity = attrs[:activity] || insert(:note_activity)
332 state = attrs[:state] || "open"
335 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
336 "actor" => user.ap_id,
338 "object" => [activity.actor, activity.data["id"]],
339 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
341 "cc" => [activity.actor],
342 "context" => activity.data["context"],
348 actor: data["actor"],
349 recipients: data["to"] ++ data["cc"]
353 def oauth_app_factory do
354 %Pleroma.Web.OAuth.App{
355 client_name: sequence(:client_name, &"Some client #{&1}"),
356 redirect_uris: "https://example.com/callback",
357 scopes: ["read", "write", "follow", "push", "admin"],
358 website: "https://example.com",
359 client_id: Ecto.UUID.generate(),
360 client_secret: "aaa;/&bbb"
364 def instance_factory do
365 %Pleroma.Instances.Instance{
367 unreachable_since: nil
371 def oauth_token_factory(attrs \\ %{}) do
372 scopes = Map.get(attrs, :scopes, ["read"])
373 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
374 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
377 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
379 %Pleroma.Web.OAuth.Token{
380 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
381 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
385 valid_until: valid_until
389 def oauth_admin_token_factory(attrs \\ %{}) do
390 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
394 |> Map.get(:scopes, ["admin"])
395 |> Kernel.++(["admin"])
398 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
399 oauth_token_factory(attrs)
402 def oauth_authorization_factory do
403 %Pleroma.Web.OAuth.Authorization{
404 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
405 scopes: ["read", "write", "follow", "push"],
406 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
408 app: build(:oauth_app)
412 def push_subscription_factory do
413 %Pleroma.Web.Push.Subscription{
415 token: build(:oauth_token),
416 endpoint: "https://example.com/example/1234",
417 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
419 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
424 def notification_factory do
425 %Pleroma.Notification{
430 def scheduled_activity_factory do
431 %Pleroma.ScheduledActivity{
433 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
434 params: build(:note) |> Map.from_struct() |> Map.get(:data)
438 def registration_factory do
441 %Pleroma.Registration{
446 "name" => "John Doe",
447 "email" => "john@doe.com",
448 "nickname" => "johndoe",
449 "description" => "My bio"
454 def config_factory(attrs \\ %{}) do
456 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
461 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
464 |> merge_attributes(attrs)
467 def marker_factory do
470 timeline: "notifications",
476 def mfa_token_factory do
478 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
479 authorization: build(:oauth_authorization),
480 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
485 def filter_factory do
488 filter_id: sequence(:filter_id, & &1),