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{},
37 multi_factor_authentication_settings: %Pleroma.MFA.Settings{}
42 | ap_id: User.ap_id(user),
43 follower_address: User.ap_followers(user),
44 following_address: User.ap_following(user)
48 def user_relationship_factory(attrs \\ %{}) do
49 source = attrs[:source] || insert(:user)
50 target = attrs[:target] || insert(:user)
51 relationship_type = attrs[:relationship_type] || :block
53 %Pleroma.UserRelationship{
56 relationship_type: relationship_type
60 def note_factory(attrs \\ %{}) do
61 text = sequence(:text, &"This is :moominmamma: note #{&1}")
63 user = attrs[:user] || insert(:user)
68 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
69 "actor" => user.ap_id,
70 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
71 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
78 "2hu" => "corndog.png"
83 data: merge_attributes(data, Map.get(attrs, :data, %{}))
87 def audio_factory(attrs \\ %{}) do
88 text = sequence(:text, &"lain radio episode #{&1}")
90 user = attrs[:user] || insert(:user)
94 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
97 "album" => "lain radio",
98 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
99 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
100 "actor" => user.ap_id,
105 data: merge_attributes(data, Map.get(attrs, :data, %{}))
109 def listen_factory do
110 audio = insert(:audio)
113 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
115 "actor" => audio.data["actor"],
116 "to" => audio.data["to"],
117 "object" => audio.data,
118 "published" => audio.data["published"]
123 actor: data["actor"],
124 recipients: data["to"]
128 def direct_note_factory do
129 user2 = insert(:user)
131 %Pleroma.Object{data: data} = note_factory()
132 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
135 def article_factory do
137 |> Map.put("type", "Article")
140 def tombstone_factory do
142 "type" => "Tombstone",
143 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
144 "formerType" => "Note",
145 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
153 def direct_note_activity_factory do
154 dm = insert(:direct_note)
157 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
159 "actor" => dm.data["actor"],
160 "to" => dm.data["to"],
162 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
163 "context" => dm.data["context"]
168 actor: data["actor"],
169 recipients: data["to"]
173 def note_activity_factory(attrs \\ %{}) do
174 user = attrs[:user] || insert(:user)
175 note = attrs[:note] || insert(:note, user: user)
177 data_attrs = attrs[:data_attrs] || %{}
178 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
182 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
184 "actor" => note.data["actor"],
185 "to" => note.data["to"],
186 "object" => note.data["id"],
187 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
188 "context" => note.data["context"]
190 |> Map.merge(data_attrs)
194 actor: data["actor"],
195 recipients: data["to"]
200 defp expiration_offset_by_minutes(attrs, minutes) do
202 NaiveDateTime.utc_now()
203 |> NaiveDateTime.add(:timer.minutes(minutes), :millisecond)
204 |> NaiveDateTime.truncate(:second)
206 %Pleroma.ActivityExpiration{}
208 |> Map.put(:scheduled_at, scheduled_at)
211 def expiration_in_the_past_factory(attrs \\ %{}) do
212 expiration_offset_by_minutes(attrs, -60)
215 def expiration_in_the_future_factory(attrs \\ %{}) do
216 expiration_offset_by_minutes(attrs, 61)
219 def article_activity_factory do
220 article = insert(:article)
223 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
225 "actor" => article.data["actor"],
226 "to" => article.data["to"],
227 "object" => article.data,
228 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
229 "context" => article.data["context"]
234 actor: data["actor"],
235 recipients: data["to"]
239 def announce_activity_factory(attrs \\ %{}) do
240 note_activity = attrs[:note_activity] || insert(:note_activity)
241 user = attrs[:user] || insert(:user)
244 "type" => "Announce",
245 "actor" => note_activity.actor,
246 "object" => note_activity.data["id"],
247 "to" => [user.follower_address, note_activity.data["actor"]],
248 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
249 "context" => note_activity.data["context"]
255 recipients: data["to"]
259 def like_activity_factory(attrs \\ %{}) do
260 note_activity = attrs[:note_activity] || insert(:note_activity)
261 object = Object.normalize(note_activity)
266 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
267 "actor" => user.ap_id,
269 "object" => object.data["id"],
270 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
272 |> Map.merge(attrs[:data_attrs] || %{})
279 def follow_activity_factory do
280 follower = insert(:user)
281 followed = insert(:user)
284 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
285 "actor" => follower.ap_id,
287 "object" => followed.ap_id,
288 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
293 actor: follower.ap_id
297 def oauth_app_factory do
298 %Pleroma.Web.OAuth.App{
299 client_name: sequence(:client_name, &"Some client #{&1}"),
300 redirect_uris: "https://example.com/callback",
301 scopes: ["read", "write", "follow", "push", "admin"],
302 website: "https://example.com",
303 client_id: Ecto.UUID.generate(),
304 client_secret: "aaa;/&bbb"
308 def instance_factory do
309 %Pleroma.Instances.Instance{
311 unreachable_since: nil
315 def oauth_token_factory(attrs \\ %{}) do
316 scopes = Map.get(attrs, :scopes, ["read"])
317 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
318 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
321 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
323 %Pleroma.Web.OAuth.Token{
324 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
325 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
329 valid_until: valid_until
333 def oauth_admin_token_factory(attrs \\ %{}) do
334 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
338 |> Map.get(:scopes, ["admin"])
339 |> Kernel.++(["admin"])
342 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
343 oauth_token_factory(attrs)
346 def oauth_authorization_factory do
347 %Pleroma.Web.OAuth.Authorization{
348 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
349 scopes: ["read", "write", "follow", "push"],
350 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
352 app: build(:oauth_app)
356 def push_subscription_factory do
357 %Pleroma.Web.Push.Subscription{
359 token: build(:oauth_token),
360 endpoint: "https://example.com/example/1234",
361 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
363 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
368 def notification_factory do
369 %Pleroma.Notification{
374 def scheduled_activity_factory do
375 %Pleroma.ScheduledActivity{
377 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
378 params: build(:note) |> Map.from_struct() |> Map.get(:data)
382 def registration_factory do
385 %Pleroma.Registration{
390 "name" => "John Doe",
391 "email" => "john@doe.com",
392 "nickname" => "johndoe",
393 "description" => "My bio"
398 def config_factory do
401 sequence(:key, fn key ->
402 # Atom dynamic registration hack in tests
412 :erlang.term_to_binary(%{another_key: "#{key}somevalue", another: "#{key}somevalue"})
418 def marker_factory do
421 timeline: "notifications",
427 def mfa_token_factory do
429 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
430 authorization: build(:oauth_authorization),
431 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),