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: Comeonin.Pbkdf2.hashpwsalt("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{}
41 | ap_id: User.ap_id(user),
42 follower_address: User.ap_followers(user),
43 following_address: User.ap_following(user)
47 def user_relationship_factory(attrs \\ %{}) do
48 source = attrs[:source] || insert(:user)
49 target = attrs[:target] || insert(:user)
50 relationship_type = attrs[:relationship_type] || :block
52 %Pleroma.UserRelationship{
55 relationship_type: relationship_type
59 def note_factory(attrs \\ %{}) do
60 text = sequence(:text, &"This is :moominmamma: note #{&1}")
62 user = attrs[:user] || insert(:user)
67 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
68 "actor" => user.ap_id,
69 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
70 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
77 "2hu" => "corndog.png"
82 data: merge_attributes(data, Map.get(attrs, :data, %{}))
86 def audio_factory(attrs \\ %{}) do
87 text = sequence(:text, &"lain radio episode #{&1}")
89 user = attrs[:user] || insert(:user)
93 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
96 "album" => "lain radio",
97 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
98 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
99 "actor" => user.ap_id,
104 data: merge_attributes(data, Map.get(attrs, :data, %{}))
108 def listen_factory do
109 audio = insert(:audio)
112 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
114 "actor" => audio.data["actor"],
115 "to" => audio.data["to"],
116 "object" => audio.data,
117 "published" => audio.data["published"]
122 actor: data["actor"],
123 recipients: data["to"]
127 def direct_note_factory do
128 user2 = insert(:user)
130 %Pleroma.Object{data: data} = note_factory()
131 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
134 def article_factory do
136 |> Map.put("type", "Article")
139 def tombstone_factory do
141 "type" => "Tombstone",
142 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
143 "formerType" => "Note",
144 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
152 def direct_note_activity_factory do
153 dm = insert(:direct_note)
156 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
158 "actor" => dm.data["actor"],
159 "to" => dm.data["to"],
161 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
162 "context" => dm.data["context"]
167 actor: data["actor"],
168 recipients: data["to"]
172 def note_activity_factory(attrs \\ %{}) do
173 user = attrs[:user] || insert(:user)
174 note = attrs[:note] || insert(:note, user: user)
176 data_attrs = attrs[:data_attrs] || %{}
177 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
181 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
183 "actor" => note.data["actor"],
184 "to" => note.data["to"],
185 "object" => note.data["id"],
186 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
187 "context" => note.data["context"]
189 |> Map.merge(data_attrs)
193 actor: data["actor"],
194 recipients: data["to"]
199 defp expiration_offset_by_minutes(attrs, minutes) do
201 NaiveDateTime.utc_now()
202 |> NaiveDateTime.add(:timer.minutes(minutes), :millisecond)
203 |> NaiveDateTime.truncate(:second)
205 %Pleroma.ActivityExpiration{}
207 |> Map.put(:scheduled_at, scheduled_at)
210 def expiration_in_the_past_factory(attrs \\ %{}) do
211 expiration_offset_by_minutes(attrs, -60)
214 def expiration_in_the_future_factory(attrs \\ %{}) do
215 expiration_offset_by_minutes(attrs, 61)
218 def article_activity_factory do
219 article = insert(:article)
222 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
224 "actor" => article.data["actor"],
225 "to" => article.data["to"],
226 "object" => article.data,
227 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
228 "context" => article.data["context"]
233 actor: data["actor"],
234 recipients: data["to"]
238 def announce_activity_factory(attrs \\ %{}) do
239 note_activity = attrs[:note_activity] || insert(:note_activity)
240 user = attrs[:user] || insert(:user)
243 "type" => "Announce",
244 "actor" => note_activity.actor,
245 "object" => note_activity.data["id"],
246 "to" => [user.follower_address, note_activity.data["actor"]],
247 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
248 "context" => note_activity.data["context"]
254 recipients: data["to"]
258 def like_activity_factory(attrs \\ %{}) do
259 note_activity = attrs[:note_activity] || insert(:note_activity)
260 object = Object.normalize(note_activity)
265 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
266 "actor" => user.ap_id,
268 "object" => object.data["id"],
269 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
271 |> Map.merge(attrs[:data_attrs] || %{})
278 def follow_activity_factory do
279 follower = insert(:user)
280 followed = insert(:user)
283 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
284 "actor" => follower.ap_id,
286 "object" => followed.ap_id,
287 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
292 actor: follower.ap_id
296 def oauth_app_factory do
297 %Pleroma.Web.OAuth.App{
298 client_name: sequence(:client_name, &"Some client #{&1}"),
299 redirect_uris: "https://example.com/callback",
300 scopes: ["read", "write", "follow", "push", "admin"],
301 website: "https://example.com",
302 client_id: Ecto.UUID.generate(),
303 client_secret: "aaa;/&bbb"
307 def instance_factory do
308 %Pleroma.Instances.Instance{
310 unreachable_since: nil
314 def oauth_token_factory(attrs \\ %{}) do
315 scopes = Map.get(attrs, :scopes, ["read"])
316 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
317 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
320 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
322 %Pleroma.Web.OAuth.Token{
323 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
324 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
328 valid_until: valid_until
332 def oauth_admin_token_factory(attrs \\ %{}) do
333 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
337 |> Map.get(:scopes, ["admin"])
338 |> Kernel.++(["admin"])
341 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
342 oauth_token_factory(attrs)
345 def oauth_authorization_factory do
346 %Pleroma.Web.OAuth.Authorization{
347 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
348 scopes: ["read", "write", "follow", "push"],
349 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
351 app: build(:oauth_app)
355 def push_subscription_factory do
356 %Pleroma.Web.Push.Subscription{
358 token: build(:oauth_token),
359 endpoint: "https://example.com/example/1234",
360 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
362 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
367 def notification_factory do
368 %Pleroma.Notification{
373 def scheduled_activity_factory do
374 %Pleroma.ScheduledActivity{
376 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
377 params: build(:note) |> Map.from_struct() |> Map.get(:data)
381 def registration_factory do
384 %Pleroma.Registration{
389 "name" => "John Doe",
390 "email" => "john@doe.com",
391 "nickname" => "johndoe",
392 "description" => "My bio"
397 def config_factory do
400 sequence(:key, fn key ->
401 # Atom dynamic registration hack in tests
411 :erlang.term_to_binary(%{another_key: "#{key}somevalue", another: "#{key}somevalue"})
417 def marker_factory do
420 timeline: "notifications",