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)
49 def user_relationship_factory(attrs \\ %{}) do
50 source = attrs[:source] || insert(:user)
51 target = attrs[:target] || insert(:user)
52 relationship_type = attrs[:relationship_type] || :block
54 %Pleroma.UserRelationship{
57 relationship_type: relationship_type
61 def note_factory(attrs \\ %{}) do
62 text = sequence(:text, &"This is :moominmamma: note #{&1}")
64 user = attrs[:user] || insert(:user)
69 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
70 "actor" => user.ap_id,
71 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
72 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
79 "2hu" => "corndog.png"
84 data: merge_attributes(data, Map.get(attrs, :data, %{}))
88 def audio_factory(attrs \\ %{}) do
89 text = sequence(:text, &"lain radio episode #{&1}")
91 user = attrs[:user] || insert(:user)
95 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
98 "album" => "lain radio",
99 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
100 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
101 "actor" => user.ap_id,
106 data: merge_attributes(data, Map.get(attrs, :data, %{}))
110 def listen_factory do
111 audio = insert(:audio)
114 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
116 "actor" => audio.data["actor"],
117 "to" => audio.data["to"],
118 "object" => audio.data,
119 "published" => audio.data["published"]
124 actor: data["actor"],
125 recipients: data["to"]
129 def direct_note_factory do
130 user2 = insert(:user)
132 %Pleroma.Object{data: data} = note_factory()
133 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
136 def article_factory do
138 |> Map.put("type", "Article")
141 def tombstone_factory do
143 "type" => "Tombstone",
144 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
145 "formerType" => "Note",
146 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
154 def direct_note_activity_factory do
155 dm = insert(:direct_note)
158 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
160 "actor" => dm.data["actor"],
161 "to" => dm.data["to"],
163 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
164 "context" => dm.data["context"]
169 actor: data["actor"],
170 recipients: data["to"]
174 def note_activity_factory(attrs \\ %{}) do
175 user = attrs[:user] || insert(:user)
176 note = attrs[:note] || insert(:note, user: user)
178 data_attrs = attrs[:data_attrs] || %{}
179 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
183 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
185 "actor" => note.data["actor"],
186 "to" => note.data["to"],
187 "object" => note.data["id"],
188 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
189 "context" => note.data["context"]
191 |> Map.merge(data_attrs)
195 actor: data["actor"],
196 recipients: data["to"]
201 defp expiration_offset_by_minutes(attrs, minutes) do
203 NaiveDateTime.utc_now()
204 |> NaiveDateTime.add(:timer.minutes(minutes), :millisecond)
205 |> NaiveDateTime.truncate(:second)
207 %Pleroma.ActivityExpiration{}
209 |> Map.put(:scheduled_at, scheduled_at)
212 def expiration_in_the_past_factory(attrs \\ %{}) do
213 expiration_offset_by_minutes(attrs, -60)
216 def expiration_in_the_future_factory(attrs \\ %{}) do
217 expiration_offset_by_minutes(attrs, 61)
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)
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 oauth_app_factory do
299 %Pleroma.Web.OAuth.App{
300 client_name: sequence(:client_name, &"Some client #{&1}"),
301 redirect_uris: "https://example.com/callback",
302 scopes: ["read", "write", "follow", "push", "admin"],
303 website: "https://example.com",
304 client_id: Ecto.UUID.generate(),
305 client_secret: "aaa;/&bbb"
309 def instance_factory do
310 %Pleroma.Instances.Instance{
312 unreachable_since: nil
316 def oauth_token_factory(attrs \\ %{}) do
317 scopes = Map.get(attrs, :scopes, ["read"])
318 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
319 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
322 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
324 %Pleroma.Web.OAuth.Token{
325 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
326 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
330 valid_until: valid_until
334 def oauth_admin_token_factory(attrs \\ %{}) do
335 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
339 |> Map.get(:scopes, ["admin"])
340 |> Kernel.++(["admin"])
343 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
344 oauth_token_factory(attrs)
347 def oauth_authorization_factory do
348 %Pleroma.Web.OAuth.Authorization{
349 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
350 scopes: ["read", "write", "follow", "push"],
351 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
353 app: build(:oauth_app)
357 def push_subscription_factory do
358 %Pleroma.Web.Push.Subscription{
360 token: build(:oauth_token),
361 endpoint: "https://example.com/example/1234",
362 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
364 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
369 def notification_factory do
370 %Pleroma.Notification{
375 def scheduled_activity_factory do
376 %Pleroma.ScheduledActivity{
378 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
379 params: build(:note) |> Map.from_struct() |> Map.get(:data)
383 def registration_factory do
386 %Pleroma.Registration{
391 "name" => "John Doe",
392 "email" => "john@doe.com",
393 "nickname" => "johndoe",
394 "description" => "My bio"
399 def config_factory do
402 sequence(:key, fn key ->
403 # Atom dynamic registration hack in tests
413 :erlang.term_to_binary(%{another_key: "#{key}somevalue", another: "#{key}somevalue"})
419 def marker_factory do
422 timeline: "notifications",
428 def mfa_token_factory do
430 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
431 authorization: build(:oauth_authorization),
432 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),