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}"),
35 last_digest_emailed_at: NaiveDateTime.utc_now(),
36 last_refreshed_at: NaiveDateTime.utc_now(),
37 notification_settings: %Pleroma.User.NotificationSetting{},
38 multi_factor_authentication_settings: %Pleroma.MFA.Settings{},
44 | ap_id: User.ap_id(user),
45 follower_address: User.ap_followers(user),
46 following_address: User.ap_following(user),
51 def user_relationship_factory(attrs \\ %{}) do
52 source = attrs[:source] || insert(:user)
53 target = attrs[:target] || insert(:user)
54 relationship_type = attrs[:relationship_type] || :block
56 %Pleroma.UserRelationship{
59 relationship_type: relationship_type
63 def note_factory(attrs \\ %{}) do
64 text = sequence(:text, &"This is :moominmamma: note #{&1}")
66 user = attrs[:user] || insert(:user)
72 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
73 "actor" => user.ap_id,
74 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
75 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
82 "2hu" => "corndog.png"
87 data: merge_attributes(data, Map.get(attrs, :data, %{}))
91 def audio_factory(attrs \\ %{}) do
92 text = sequence(:text, &"lain radio episode #{&1}")
94 user = attrs[:user] || insert(:user)
98 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
101 "album" => "lain radio",
102 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
103 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
104 "actor" => user.ap_id,
109 data: merge_attributes(data, Map.get(attrs, :data, %{}))
113 def listen_factory do
114 audio = insert(:audio)
117 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
119 "actor" => audio.data["actor"],
120 "to" => audio.data["to"],
121 "object" => audio.data,
122 "published" => audio.data["published"]
127 actor: data["actor"],
128 recipients: data["to"]
132 def direct_note_factory do
133 user2 = insert(:user)
135 %Pleroma.Object{data: data} = note_factory()
136 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
139 def article_factory do
141 |> Map.put("type", "Article")
144 def tombstone_factory do
146 "type" => "Tombstone",
147 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
148 "formerType" => "Note",
149 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
157 def direct_note_activity_factory do
158 dm = insert(:direct_note)
161 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
163 "actor" => dm.data["actor"],
164 "to" => dm.data["to"],
166 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
167 "context" => dm.data["context"]
172 actor: data["actor"],
173 recipients: data["to"]
177 def note_activity_factory(attrs \\ %{}) do
178 user = attrs[:user] || insert(:user)
179 note = attrs[:note] || insert(:note, user: user)
181 data_attrs = attrs[:data_attrs] || %{}
182 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
186 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
188 "actor" => note.data["actor"],
189 "to" => note.data["to"],
190 "object" => note.data["id"],
191 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
192 "context" => note.data["context"]
194 |> Map.merge(data_attrs)
198 actor: data["actor"],
199 recipients: data["to"]
204 def article_activity_factory do
205 article = insert(:article)
208 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
210 "actor" => article.data["actor"],
211 "to" => article.data["to"],
212 "object" => article.data,
213 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
214 "context" => article.data["context"]
219 actor: data["actor"],
220 recipients: data["to"]
224 def announce_activity_factory(attrs \\ %{}) do
225 note_activity = attrs[:note_activity] || insert(:note_activity)
226 user = attrs[:user] || insert(:user)
229 "type" => "Announce",
230 "actor" => note_activity.actor,
231 "object" => note_activity.data["id"],
232 "to" => [user.follower_address, note_activity.data["actor"]],
233 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
234 "context" => note_activity.data["context"]
240 recipients: data["to"]
244 def like_activity_factory(attrs \\ %{}) do
245 note_activity = attrs[:note_activity] || insert(:note_activity)
246 object = Object.normalize(note_activity)
251 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
252 "actor" => user.ap_id,
254 "object" => object.data["id"],
255 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
257 |> Map.merge(attrs[:data_attrs] || %{})
264 def follow_activity_factory do
265 follower = insert(:user)
266 followed = insert(:user)
269 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
270 "actor" => follower.ap_id,
272 "object" => followed.ap_id,
273 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
278 actor: follower.ap_id
282 def report_activity_factory(attrs \\ %{}) do
283 user = attrs[:user] || insert(:user)
284 activity = attrs[:activity] || insert(:note_activity)
285 state = attrs[:state] || "open"
288 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
289 "actor" => user.ap_id,
291 "object" => [activity.actor, activity.data["id"]],
292 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
294 "cc" => [activity.actor],
295 "context" => activity.data["context"],
301 actor: data["actor"],
302 recipients: data["to"] ++ data["cc"]
306 def oauth_app_factory do
307 %Pleroma.Web.OAuth.App{
308 client_name: sequence(:client_name, &"Some client #{&1}"),
309 redirect_uris: "https://example.com/callback",
310 scopes: ["read", "write", "follow", "push", "admin"],
311 website: "https://example.com",
312 client_id: Ecto.UUID.generate(),
313 client_secret: "aaa;/&bbb"
317 def instance_factory do
318 %Pleroma.Instances.Instance{
320 unreachable_since: nil
324 def oauth_token_factory(attrs \\ %{}) do
325 scopes = Map.get(attrs, :scopes, ["read"])
326 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
327 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
330 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
332 %Pleroma.Web.OAuth.Token{
333 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
334 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
338 valid_until: valid_until
342 def oauth_admin_token_factory(attrs \\ %{}) do
343 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
347 |> Map.get(:scopes, ["admin"])
348 |> Kernel.++(["admin"])
351 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
352 oauth_token_factory(attrs)
355 def oauth_authorization_factory do
356 %Pleroma.Web.OAuth.Authorization{
357 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
358 scopes: ["read", "write", "follow", "push"],
359 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
361 app: build(:oauth_app)
365 def push_subscription_factory do
366 %Pleroma.Web.Push.Subscription{
368 token: build(:oauth_token),
369 endpoint: "https://example.com/example/1234",
370 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
372 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
377 def notification_factory do
378 %Pleroma.Notification{
383 def scheduled_activity_factory do
384 %Pleroma.ScheduledActivity{
386 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
387 params: build(:note) |> Map.from_struct() |> Map.get(:data)
391 def registration_factory do
394 %Pleroma.Registration{
399 "name" => "John Doe",
400 "email" => "john@doe.com",
401 "nickname" => "johndoe",
402 "description" => "My bio"
407 def config_factory(attrs \\ %{}) do
409 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
414 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
417 |> merge_attributes(attrs)
420 def marker_factory do
423 timeline: "notifications",
429 def mfa_token_factory do
431 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
432 authorization: build(:oauth_authorization),
433 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
438 def filter_factory do
441 filter_id: sequence(:filter_id, & &1),