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 audio_factory(attrs \\ %{}) do
108 text = sequence(:text, &"lain radio episode #{&1}")
110 user = attrs[:user] || insert(:user)
114 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
117 "album" => "lain radio",
118 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
119 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
120 "actor" => user.ap_id,
125 data: merge_attributes(data, Map.get(attrs, :data, %{}))
129 def listen_factory do
130 audio = insert(:audio)
133 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
135 "actor" => audio.data["actor"],
136 "to" => audio.data["to"],
137 "object" => audio.data,
138 "published" => audio.data["published"]
143 actor: data["actor"],
144 recipients: data["to"]
148 def direct_note_factory do
149 user2 = insert(:user)
151 %Pleroma.Object{data: data} = note_factory()
152 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
155 def article_factory do
157 |> Map.put("type", "Article")
160 def tombstone_factory do
162 "type" => "Tombstone",
163 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
164 "formerType" => "Note",
165 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
173 def direct_note_activity_factory do
174 dm = insert(:direct_note)
177 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
179 "actor" => dm.data["actor"],
180 "to" => dm.data["to"],
182 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
183 "context" => dm.data["context"]
188 actor: data["actor"],
189 recipients: data["to"]
193 def note_activity_factory(attrs \\ %{}) do
194 user = attrs[:user] || insert(:user)
195 note = attrs[:note] || insert(:note, user: user)
197 data_attrs = attrs[:data_attrs] || %{}
198 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
202 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
204 "actor" => note.data["actor"],
205 "to" => note.data["to"],
206 "object" => note.data["id"],
207 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
208 "context" => note.data["context"]
210 |> Map.merge(data_attrs)
214 actor: data["actor"],
215 recipients: data["to"]
220 def article_activity_factory do
221 article = insert(:article)
224 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
226 "actor" => article.data["actor"],
227 "to" => article.data["to"],
228 "object" => article.data,
229 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
230 "context" => article.data["context"]
235 actor: data["actor"],
236 recipients: data["to"]
240 def announce_activity_factory(attrs \\ %{}) do
241 note_activity = attrs[:note_activity] || insert(:note_activity)
242 user = attrs[:user] || insert(:user)
245 "type" => "Announce",
246 "actor" => note_activity.actor,
247 "object" => note_activity.data["id"],
248 "to" => [user.follower_address, note_activity.data["actor"]],
249 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
250 "context" => note_activity.data["context"]
256 recipients: data["to"]
260 def like_activity_factory(attrs \\ %{}) do
261 note_activity = attrs[:note_activity] || insert(:note_activity)
262 object = Object.normalize(note_activity, fetch: false)
267 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
268 "actor" => user.ap_id,
270 "object" => object.data["id"],
271 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
273 |> Map.merge(attrs[:data_attrs] || %{})
280 def follow_activity_factory do
281 follower = insert(:user)
282 followed = insert(:user)
285 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
286 "actor" => follower.ap_id,
288 "object" => followed.ap_id,
289 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
294 actor: follower.ap_id
298 def report_activity_factory(attrs \\ %{}) do
299 user = attrs[:user] || insert(:user)
300 activity = attrs[:activity] || insert(:note_activity)
301 state = attrs[:state] || "open"
304 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
305 "actor" => user.ap_id,
307 "object" => [activity.actor, activity.data["id"]],
308 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
310 "cc" => [activity.actor],
311 "context" => activity.data["context"],
317 actor: data["actor"],
318 recipients: data["to"] ++ data["cc"]
322 def oauth_app_factory do
323 %Pleroma.Web.OAuth.App{
324 client_name: sequence(:client_name, &"Some client #{&1}"),
325 redirect_uris: "https://example.com/callback",
326 scopes: ["read", "write", "follow", "push", "admin"],
327 website: "https://example.com",
328 client_id: Ecto.UUID.generate(),
329 client_secret: "aaa;/&bbb"
333 def instance_factory do
334 %Pleroma.Instances.Instance{
336 unreachable_since: nil
340 def oauth_token_factory(attrs \\ %{}) do
341 scopes = Map.get(attrs, :scopes, ["read"])
342 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
343 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
346 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
348 %Pleroma.Web.OAuth.Token{
349 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
350 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
354 valid_until: valid_until
358 def oauth_admin_token_factory(attrs \\ %{}) do
359 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
363 |> Map.get(:scopes, ["admin"])
364 |> Kernel.++(["admin"])
367 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
368 oauth_token_factory(attrs)
371 def oauth_authorization_factory do
372 %Pleroma.Web.OAuth.Authorization{
373 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
374 scopes: ["read", "write", "follow", "push"],
375 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
377 app: build(:oauth_app)
381 def push_subscription_factory do
382 %Pleroma.Web.Push.Subscription{
384 token: build(:oauth_token),
385 endpoint: "https://example.com/example/1234",
386 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
388 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
393 def notification_factory do
394 %Pleroma.Notification{
399 def scheduled_activity_factory do
400 %Pleroma.ScheduledActivity{
402 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
403 params: build(:note) |> Map.from_struct() |> Map.get(:data)
407 def registration_factory do
410 %Pleroma.Registration{
415 "name" => "John Doe",
416 "email" => "john@doe.com",
417 "nickname" => "johndoe",
418 "description" => "My bio"
423 def config_factory(attrs \\ %{}) do
425 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
430 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
433 |> merge_attributes(attrs)
436 def marker_factory do
439 timeline: "notifications",
445 def mfa_token_factory do
447 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
448 authorization: build(:oauth_authorization),
449 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
454 def filter_factory do
457 filter_id: sequence(:filter_id, & &1),