1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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}"),
35 last_digest_emailed_at: NaiveDateTime.utc_now()
40 | ap_id: User.ap_id(user),
41 follower_address: User.ap_followers(user),
42 following_address: User.ap_following(user)
46 def note_factory(attrs \\ %{}) do
47 text = sequence(:text, &"This is :moominmamma: note #{&1}")
49 user = attrs[:user] || insert(:user)
54 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
55 "actor" => user.ap_id,
56 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
57 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
64 "2hu" => "corndog.png"
69 data: merge_attributes(data, Map.get(attrs, :data, %{}))
73 def audio_factory(attrs \\ %{}) do
74 text = sequence(:text, &"lain radio episode #{&1}")
76 user = attrs[:user] || insert(:user)
80 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
83 "album" => "lain radio",
84 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
85 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
86 "actor" => user.ap_id,
91 data: merge_attributes(data, Map.get(attrs, :data, %{}))
96 audio = insert(:audio)
99 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
101 "actor" => audio.data["actor"],
102 "to" => audio.data["to"],
103 "object" => audio.data,
104 "published" => audio.data["published"]
109 actor: data["actor"],
110 recipients: data["to"]
114 def direct_note_factory do
115 user2 = insert(:user)
117 %Pleroma.Object{data: data} = note_factory()
118 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
121 def article_factory do
123 |> Map.put("type", "Article")
126 def tombstone_factory do
128 "type" => "Tombstone",
129 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
130 "formerType" => "Note",
131 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
139 def direct_note_activity_factory do
140 dm = insert(:direct_note)
143 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
145 "actor" => dm.data["actor"],
146 "to" => dm.data["to"],
148 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
149 "context" => dm.data["context"]
154 actor: data["actor"],
155 recipients: data["to"]
159 def note_activity_factory(attrs \\ %{}) do
160 user = attrs[:user] || insert(:user)
161 note = attrs[:note] || insert(:note, user: user)
163 data_attrs = attrs[:data_attrs] || %{}
164 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
168 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
170 "actor" => note.data["actor"],
171 "to" => note.data["to"],
172 "object" => note.data["id"],
173 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
174 "context" => note.data["context"]
176 |> Map.merge(data_attrs)
180 actor: data["actor"],
181 recipients: data["to"]
186 defp expiration_offset_by_minutes(attrs, minutes) do
188 NaiveDateTime.utc_now()
189 |> NaiveDateTime.add(:timer.minutes(minutes), :millisecond)
190 |> NaiveDateTime.truncate(:second)
192 %Pleroma.ActivityExpiration{}
194 |> Map.put(:scheduled_at, scheduled_at)
197 def expiration_in_the_past_factory(attrs \\ %{}) do
198 expiration_offset_by_minutes(attrs, -60)
201 def expiration_in_the_future_factory(attrs \\ %{}) do
202 expiration_offset_by_minutes(attrs, 61)
205 def article_activity_factory do
206 article = insert(:article)
209 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
211 "actor" => article.data["actor"],
212 "to" => article.data["to"],
213 "object" => article.data,
214 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
215 "context" => article.data["context"]
220 actor: data["actor"],
221 recipients: data["to"]
225 def announce_activity_factory(attrs \\ %{}) do
226 note_activity = attrs[:note_activity] || insert(:note_activity)
227 user = attrs[:user] || insert(:user)
230 "type" => "Announce",
231 "actor" => note_activity.actor,
232 "object" => note_activity.data["id"],
233 "to" => [user.follower_address, note_activity.data["actor"]],
234 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
235 "context" => note_activity.data["context"]
241 recipients: data["to"]
245 def like_activity_factory(attrs \\ %{}) do
246 note_activity = attrs[:note_activity] || insert(:note_activity)
247 object = Object.normalize(note_activity)
252 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
253 "actor" => user.ap_id,
255 "object" => object.data["id"],
256 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
258 |> Map.merge(attrs[:data_attrs] || %{})
265 def follow_activity_factory do
266 follower = insert(:user)
267 followed = insert(:user)
270 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
271 "actor" => follower.ap_id,
273 "object" => followed.ap_id,
274 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
279 actor: follower.ap_id
283 def websub_subscription_factory do
284 %Pleroma.Web.Websub.WebsubServerSubscription{
285 topic: "http://example.org",
286 callback: "http://example.org/callback",
287 secret: "here's a secret",
288 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 100),
293 def websub_client_subscription_factory do
294 %Pleroma.Web.Websub.WebsubClientSubscription{
295 topic: "http://example.org",
296 secret: "here's a secret",
303 def oauth_app_factory do
304 %Pleroma.Web.OAuth.App{
305 client_name: "Some client",
306 redirect_uris: "https://example.com/callback",
307 scopes: ["read", "write", "follow", "push"],
308 website: "https://example.com",
309 client_id: Ecto.UUID.generate(),
310 client_secret: "aaa;/&bbb"
314 def instance_factory do
315 %Pleroma.Instances.Instance{
317 unreachable_since: nil
321 def oauth_token_factory do
322 oauth_app = insert(:oauth_app)
324 %Pleroma.Web.OAuth.Token{
325 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
327 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
329 app_id: oauth_app.id,
330 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
334 def oauth_authorization_factory do
335 %Pleroma.Web.OAuth.Authorization{
336 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
337 scopes: ["read", "write", "follow", "push"],
338 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
340 app: build(:oauth_app)
344 def push_subscription_factory do
345 %Pleroma.Web.Push.Subscription{
347 token: build(:oauth_token),
348 endpoint: "https://example.com/example/1234",
349 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
351 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
356 def notification_factory do
357 %Pleroma.Notification{
362 def scheduled_activity_factory do
363 %Pleroma.ScheduledActivity{
365 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
366 params: build(:note) |> Map.from_struct() |> Map.get(:data)
370 def registration_factory do
373 %Pleroma.Registration{
378 "name" => "John Doe",
379 "email" => "john@doe.com",
380 "nickname" => "johndoe",
381 "description" => "My bio"
386 def config_factory do
387 %Pleroma.Web.AdminAPI.Config{
388 key: sequence(:key, &"some_key_#{&1}"),
394 :erlang.term_to_binary(%{another_key: "#{key}somevalue", another: "#{key}somevalue"})