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)
70 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
71 "actor" => user.ap_id,
72 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
73 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
80 "2hu" => "corndog.png"
85 data: merge_attributes(data, Map.get(attrs, :data, %{}))
89 def audio_factory(attrs \\ %{}) do
90 text = sequence(:text, &"lain radio episode #{&1}")
92 user = attrs[:user] || insert(:user)
96 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
99 "album" => "lain radio",
100 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
101 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
102 "actor" => user.ap_id,
107 data: merge_attributes(data, Map.get(attrs, :data, %{}))
111 def listen_factory do
112 audio = insert(:audio)
115 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
117 "actor" => audio.data["actor"],
118 "to" => audio.data["to"],
119 "object" => audio.data,
120 "published" => audio.data["published"]
125 actor: data["actor"],
126 recipients: data["to"]
130 def direct_note_factory do
131 user2 = insert(:user)
133 %Pleroma.Object{data: data} = note_factory()
134 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
137 def article_factory do
139 |> Map.put("type", "Article")
142 def tombstone_factory do
144 "type" => "Tombstone",
145 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
146 "formerType" => "Note",
147 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
155 def direct_note_activity_factory do
156 dm = insert(:direct_note)
159 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
161 "actor" => dm.data["actor"],
162 "to" => dm.data["to"],
164 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
165 "context" => dm.data["context"]
170 actor: data["actor"],
171 recipients: data["to"]
175 def note_activity_factory(attrs \\ %{}) do
176 user = attrs[:user] || insert(:user)
177 note = attrs[:note] || insert(:note, user: user)
179 data_attrs = attrs[:data_attrs] || %{}
180 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
184 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
186 "actor" => note.data["actor"],
187 "to" => note.data["to"],
188 "object" => note.data["id"],
189 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
190 "context" => note.data["context"]
192 |> Map.merge(data_attrs)
196 actor: data["actor"],
197 recipients: data["to"]
202 defp expiration_offset_by_minutes(attrs, minutes) do
204 NaiveDateTime.utc_now()
205 |> NaiveDateTime.add(:timer.minutes(minutes), :millisecond)
206 |> NaiveDateTime.truncate(:second)
208 %Pleroma.ActivityExpiration{}
210 |> Map.put(:scheduled_at, scheduled_at)
213 def expiration_in_the_past_factory(attrs \\ %{}) do
214 expiration_offset_by_minutes(attrs, -60)
217 def expiration_in_the_future_factory(attrs \\ %{}) do
218 expiration_offset_by_minutes(attrs, 61)
221 def article_activity_factory do
222 article = insert(:article)
225 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
227 "actor" => article.data["actor"],
228 "to" => article.data["to"],
229 "object" => article.data,
230 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
231 "context" => article.data["context"]
236 actor: data["actor"],
237 recipients: data["to"]
241 def announce_activity_factory(attrs \\ %{}) do
242 note_activity = attrs[:note_activity] || insert(:note_activity)
243 user = attrs[:user] || insert(:user)
246 "type" => "Announce",
247 "actor" => note_activity.actor,
248 "object" => note_activity.data["id"],
249 "to" => [user.follower_address, note_activity.data["actor"]],
250 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
251 "context" => note_activity.data["context"]
257 recipients: data["to"]
261 def like_activity_factory(attrs \\ %{}) do
262 note_activity = attrs[:note_activity] || insert(:note_activity)
263 object = Object.normalize(note_activity)
268 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
269 "actor" => user.ap_id,
271 "object" => object.data["id"],
272 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
274 |> Map.merge(attrs[:data_attrs] || %{})
281 def follow_activity_factory do
282 follower = insert(:user)
283 followed = insert(:user)
286 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
287 "actor" => follower.ap_id,
289 "object" => followed.ap_id,
290 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
295 actor: follower.ap_id
299 def oauth_app_factory do
300 %Pleroma.Web.OAuth.App{
301 client_name: sequence(:client_name, &"Some client #{&1}"),
302 redirect_uris: "https://example.com/callback",
303 scopes: ["read", "write", "follow", "push", "admin"],
304 website: "https://example.com",
305 client_id: Ecto.UUID.generate(),
306 client_secret: "aaa;/&bbb"
310 def instance_factory do
311 %Pleroma.Instances.Instance{
313 unreachable_since: nil
317 def oauth_token_factory(attrs \\ %{}) do
318 scopes = Map.get(attrs, :scopes, ["read"])
319 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
320 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
323 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
325 %Pleroma.Web.OAuth.Token{
326 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
327 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
331 valid_until: valid_until
335 def oauth_admin_token_factory(attrs \\ %{}) do
336 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
340 |> Map.get(:scopes, ["admin"])
341 |> Kernel.++(["admin"])
344 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
345 oauth_token_factory(attrs)
348 def oauth_authorization_factory do
349 %Pleroma.Web.OAuth.Authorization{
350 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
351 scopes: ["read", "write", "follow", "push"],
352 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
354 app: build(:oauth_app)
358 def push_subscription_factory do
359 %Pleroma.Web.Push.Subscription{
361 token: build(:oauth_token),
362 endpoint: "https://example.com/example/1234",
363 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
365 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
370 def notification_factory do
371 %Pleroma.Notification{
376 def scheduled_activity_factory do
377 %Pleroma.ScheduledActivity{
379 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
380 params: build(:note) |> Map.from_struct() |> Map.get(:data)
384 def registration_factory do
387 %Pleroma.Registration{
392 "name" => "John Doe",
393 "email" => "john@doe.com",
394 "nickname" => "johndoe",
395 "description" => "My bio"
400 def config_factory(attrs \\ %{}) do
402 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
407 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
410 |> merge_attributes(attrs)
413 def marker_factory do
416 timeline: "notifications",
422 def mfa_token_factory do
424 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
425 authorization: build(:oauth_authorization),
426 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),