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 def article_activity_factory do
204 article = insert(:article)
207 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
209 "actor" => article.data["actor"],
210 "to" => article.data["to"],
211 "object" => article.data,
212 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
213 "context" => article.data["context"]
218 actor: data["actor"],
219 recipients: data["to"]
223 def announce_activity_factory(attrs \\ %{}) do
224 note_activity = attrs[:note_activity] || insert(:note_activity)
225 user = attrs[:user] || insert(:user)
228 "type" => "Announce",
229 "actor" => note_activity.actor,
230 "object" => note_activity.data["id"],
231 "to" => [user.follower_address, note_activity.data["actor"]],
232 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
233 "context" => note_activity.data["context"]
239 recipients: data["to"]
243 def like_activity_factory(attrs \\ %{}) do
244 note_activity = attrs[:note_activity] || insert(:note_activity)
245 object = Object.normalize(note_activity)
250 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
251 "actor" => user.ap_id,
253 "object" => object.data["id"],
254 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
256 |> Map.merge(attrs[:data_attrs] || %{})
263 def follow_activity_factory do
264 follower = insert(:user)
265 followed = insert(:user)
268 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
269 "actor" => follower.ap_id,
271 "object" => followed.ap_id,
272 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
277 actor: follower.ap_id
281 def report_activity_factory(attrs \\ %{}) do
282 user = attrs[:user] || insert(:user)
283 activity = attrs[:activity] || insert(:note_activity)
284 state = attrs[:state] || "open"
287 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
288 "actor" => user.ap_id,
290 "object" => [activity.actor, activity.data["id"]],
291 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
293 "cc" => [activity.actor],
294 "context" => activity.data["context"],
300 actor: data["actor"],
301 recipients: data["to"] ++ data["cc"]
305 def oauth_app_factory do
306 %Pleroma.Web.OAuth.App{
307 client_name: sequence(:client_name, &"Some client #{&1}"),
308 redirect_uris: "https://example.com/callback",
309 scopes: ["read", "write", "follow", "push", "admin"],
310 website: "https://example.com",
311 client_id: Ecto.UUID.generate(),
312 client_secret: "aaa;/&bbb"
316 def instance_factory do
317 %Pleroma.Instances.Instance{
319 unreachable_since: nil
323 def oauth_token_factory(attrs \\ %{}) do
324 scopes = Map.get(attrs, :scopes, ["read"])
325 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
326 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
329 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
331 %Pleroma.Web.OAuth.Token{
332 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
333 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
337 valid_until: valid_until
341 def oauth_admin_token_factory(attrs \\ %{}) do
342 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
346 |> Map.get(:scopes, ["admin"])
347 |> Kernel.++(["admin"])
350 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
351 oauth_token_factory(attrs)
354 def oauth_authorization_factory do
355 %Pleroma.Web.OAuth.Authorization{
356 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
357 scopes: ["read", "write", "follow", "push"],
358 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
360 app: build(:oauth_app)
364 def push_subscription_factory do
365 %Pleroma.Web.Push.Subscription{
367 token: build(:oauth_token),
368 endpoint: "https://example.com/example/1234",
369 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
371 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
376 def notification_factory do
377 %Pleroma.Notification{
382 def scheduled_activity_factory do
383 %Pleroma.ScheduledActivity{
385 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
386 params: build(:note) |> Map.from_struct() |> Map.get(:data)
390 def registration_factory do
393 %Pleroma.Registration{
398 "name" => "John Doe",
399 "email" => "john@doe.com",
400 "nickname" => "johndoe",
401 "description" => "My bio"
406 def config_factory(attrs \\ %{}) do
408 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
413 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
416 |> merge_attributes(attrs)
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),
437 def filter_factory do
440 filter_id: sequence(:filter_id, & &1),