1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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}")
29 name: sequence(:name, &"Test テスト User #{&1}"),
30 email: sequence(:email, &"user#{&1}@example.com"),
31 nickname: sequence(:nickname, &"nick#{&1}"),
32 password_hash: Pbkdf2.hash_pwd_salt("test"),
33 bio: sequence(:bio, &"Tester Number #{&1}"),
34 last_digest_emailed_at: NaiveDateTime.utc_now(),
35 last_refreshed_at: NaiveDateTime.utc_now(),
36 notification_settings: %Pleroma.User.NotificationSetting{},
37 multi_factor_authentication_settings: %Pleroma.MFA.Settings{},
43 | ap_id: User.ap_id(user),
44 follower_address: User.ap_followers(user),
45 following_address: User.ap_following(user),
50 def user_relationship_factory(attrs \\ %{}) do
51 source = attrs[:source] || insert(:user)
52 target = attrs[:target] || insert(:user)
53 relationship_type = attrs[:relationship_type] || :block
55 %Pleroma.UserRelationship{
58 relationship_type: relationship_type
62 def note_factory(attrs \\ %{}) do
63 text = sequence(:text, &"This is :moominmamma: note #{&1}")
65 user = attrs[:user] || insert(:user)
71 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
72 "actor" => user.ap_id,
73 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
74 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
81 "2hu" => "corndog.png"
86 data: merge_attributes(data, Map.get(attrs, :data, %{}))
90 def audio_factory(attrs \\ %{}) do
91 text = sequence(:text, &"lain radio episode #{&1}")
93 user = attrs[:user] || insert(:user)
97 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
100 "album" => "lain radio",
101 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
102 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
103 "actor" => user.ap_id,
108 data: merge_attributes(data, Map.get(attrs, :data, %{}))
112 def listen_factory do
113 audio = insert(:audio)
116 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
118 "actor" => audio.data["actor"],
119 "to" => audio.data["to"],
120 "object" => audio.data,
121 "published" => audio.data["published"]
126 actor: data["actor"],
127 recipients: data["to"]
131 def direct_note_factory do
132 user2 = insert(:user)
134 %Pleroma.Object{data: data} = note_factory()
135 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
138 def article_factory do
140 |> Map.put("type", "Article")
143 def tombstone_factory do
145 "type" => "Tombstone",
146 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
147 "formerType" => "Note",
148 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
156 def direct_note_activity_factory do
157 dm = insert(:direct_note)
160 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
162 "actor" => dm.data["actor"],
163 "to" => dm.data["to"],
165 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
166 "context" => dm.data["context"]
171 actor: data["actor"],
172 recipients: data["to"]
176 def note_activity_factory(attrs \\ %{}) do
177 user = attrs[:user] || insert(:user)
178 note = attrs[:note] || insert(:note, user: user)
180 data_attrs = attrs[:data_attrs] || %{}
181 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
185 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
187 "actor" => note.data["actor"],
188 "to" => note.data["to"],
189 "object" => note.data["id"],
190 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
191 "context" => note.data["context"]
193 |> Map.merge(data_attrs)
197 actor: data["actor"],
198 recipients: data["to"]
203 defp expiration_offset_by_minutes(attrs, minutes) do
205 NaiveDateTime.utc_now()
206 |> NaiveDateTime.add(:timer.minutes(minutes), :millisecond)
207 |> NaiveDateTime.truncate(:second)
209 %Pleroma.ActivityExpiration{}
211 |> Map.put(:scheduled_at, scheduled_at)
214 def expiration_in_the_past_factory(attrs \\ %{}) do
215 expiration_offset_by_minutes(attrs, -60)
218 def expiration_in_the_future_factory(attrs \\ %{}) do
219 expiration_offset_by_minutes(attrs, 61)
222 def article_activity_factory do
223 article = insert(:article)
226 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
228 "actor" => article.data["actor"],
229 "to" => article.data["to"],
230 "object" => article.data,
231 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
232 "context" => article.data["context"]
237 actor: data["actor"],
238 recipients: data["to"]
242 def announce_activity_factory(attrs \\ %{}) do
243 note_activity = attrs[:note_activity] || insert(:note_activity)
244 user = attrs[:user] || insert(:user)
247 "type" => "Announce",
248 "actor" => note_activity.actor,
249 "object" => note_activity.data["id"],
250 "to" => [user.follower_address, note_activity.data["actor"]],
251 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
252 "context" => note_activity.data["context"]
258 recipients: data["to"]
262 def like_activity_factory(attrs \\ %{}) do
263 note_activity = attrs[:note_activity] || insert(:note_activity)
264 object = Object.normalize(note_activity)
269 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
270 "actor" => user.ap_id,
272 "object" => object.data["id"],
273 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
275 |> Map.merge(attrs[:data_attrs] || %{})
282 def follow_activity_factory do
283 follower = insert(:user)
284 followed = insert(:user)
287 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
288 "actor" => follower.ap_id,
290 "object" => followed.ap_id,
291 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
296 actor: follower.ap_id
300 def report_activity_factory(attrs \\ %{}) do
301 user = attrs[:user] || insert(:user)
302 activity = attrs[:activity] || insert(:note_activity)
303 state = attrs[:state] || "open"
306 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
307 "actor" => user.ap_id,
309 "object" => [activity.actor, activity.data["id"]],
310 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
312 "cc" => [activity.actor],
313 "context" => activity.data["context"],
319 actor: data["actor"],
320 recipients: data["to"] ++ data["cc"]
324 def oauth_app_factory do
325 %Pleroma.Web.OAuth.App{
326 client_name: sequence(:client_name, &"Some client #{&1}"),
327 redirect_uris: "https://example.com/callback",
328 scopes: ["read", "write", "follow", "push", "admin"],
329 website: "https://example.com",
330 client_id: Ecto.UUID.generate(),
331 client_secret: "aaa;/&bbb"
335 def instance_factory do
336 %Pleroma.Instances.Instance{
338 unreachable_since: nil
342 def oauth_token_factory(attrs \\ %{}) do
343 scopes = Map.get(attrs, :scopes, ["read"])
344 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
345 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
348 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
350 %Pleroma.Web.OAuth.Token{
351 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
352 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
356 valid_until: valid_until
360 def oauth_admin_token_factory(attrs \\ %{}) do
361 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
365 |> Map.get(:scopes, ["admin"])
366 |> Kernel.++(["admin"])
369 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
370 oauth_token_factory(attrs)
373 def oauth_authorization_factory do
374 %Pleroma.Web.OAuth.Authorization{
375 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
376 scopes: ["read", "write", "follow", "push"],
377 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
379 app: build(:oauth_app)
383 def push_subscription_factory do
384 %Pleroma.Web.Push.Subscription{
386 token: build(:oauth_token),
387 endpoint: "https://example.com/example/1234",
388 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
390 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
395 def notification_factory do
396 %Pleroma.Notification{
401 def scheduled_activity_factory do
402 %Pleroma.ScheduledActivity{
404 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
405 params: build(:note) |> Map.from_struct() |> Map.get(:data)
409 def registration_factory do
412 %Pleroma.Registration{
417 "name" => "John Doe",
418 "email" => "john@doe.com",
419 "nickname" => "johndoe",
420 "description" => "My bio"
425 def config_factory(attrs \\ %{}) do
427 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
432 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
435 |> merge_attributes(attrs)
438 def marker_factory do
441 timeline: "notifications",
447 def mfa_token_factory do
449 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
450 authorization: build(:oauth_authorization),
451 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
456 def filter_factory do
459 filter_id: sequence(:filter_id, & &1),