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),
43 following: [User.ap_id(user)]
47 def note_factory(attrs \\ %{}) do
48 text = sequence(:text, &"This is :moominmamma: note #{&1}")
50 user = attrs[:user] || insert(:user)
55 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
56 "actor" => user.ap_id,
57 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
58 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
65 "2hu" => "corndog.png"
70 data: merge_attributes(data, Map.get(attrs, :data, %{}))
74 def audio_factory(attrs \\ %{}) do
75 text = sequence(:text, &"lain radio episode #{&1}")
77 user = attrs[:user] || insert(:user)
81 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
84 "album" => "lain radio",
85 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
86 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
87 "actor" => user.ap_id,
92 data: merge_attributes(data, Map.get(attrs, :data, %{}))
97 audio = insert(:audio)
100 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
102 "actor" => audio.data["actor"],
103 "to" => audio.data["to"],
104 "object" => audio.data,
105 "published" => audio.data["published"]
110 actor: data["actor"],
111 recipients: data["to"]
115 def direct_note_factory do
116 user2 = insert(:user)
118 %Pleroma.Object{data: data} = note_factory()
119 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
122 def article_factory do
124 |> Map.put("type", "Article")
127 def tombstone_factory do
129 "type" => "Tombstone",
130 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
131 "formerType" => "Note",
132 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
140 def direct_note_activity_factory do
141 dm = insert(:direct_note)
144 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
146 "actor" => dm.data["actor"],
147 "to" => dm.data["to"],
149 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
150 "context" => dm.data["context"]
155 actor: data["actor"],
156 recipients: data["to"]
160 def note_activity_factory(attrs \\ %{}) do
161 user = attrs[:user] || insert(:user)
162 note = attrs[:note] || insert(:note, user: user)
164 data_attrs = attrs[:data_attrs] || %{}
165 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
169 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
171 "actor" => note.data["actor"],
172 "to" => note.data["to"],
173 "object" => note.data["id"],
174 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
175 "context" => note.data["context"]
177 |> Map.merge(data_attrs)
181 actor: data["actor"],
182 recipients: data["to"]
187 defp expiration_offset_by_minutes(attrs, minutes) do
189 NaiveDateTime.utc_now()
190 |> NaiveDateTime.add(:timer.minutes(minutes), :millisecond)
191 |> NaiveDateTime.truncate(:second)
193 %Pleroma.ActivityExpiration{}
195 |> Map.put(:scheduled_at, scheduled_at)
198 def expiration_in_the_past_factory(attrs \\ %{}) do
199 expiration_offset_by_minutes(attrs, -60)
202 def expiration_in_the_future_factory(attrs \\ %{}) do
203 expiration_offset_by_minutes(attrs, 61)
206 def article_activity_factory do
207 article = insert(:article)
210 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
212 "actor" => article.data["actor"],
213 "to" => article.data["to"],
214 "object" => article.data,
215 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
216 "context" => article.data["context"]
221 actor: data["actor"],
222 recipients: data["to"]
226 def announce_activity_factory(attrs \\ %{}) do
227 note_activity = attrs[:note_activity] || insert(:note_activity)
228 user = attrs[:user] || insert(:user)
231 "type" => "Announce",
232 "actor" => note_activity.actor,
233 "object" => note_activity.data["id"],
234 "to" => [user.follower_address, note_activity.data["actor"]],
235 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
236 "context" => note_activity.data["context"]
242 recipients: data["to"]
246 def like_activity_factory(attrs \\ %{}) do
247 note_activity = attrs[:note_activity] || insert(:note_activity)
248 object = Object.normalize(note_activity)
253 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
254 "actor" => user.ap_id,
256 "object" => object.data["id"],
257 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
259 |> Map.merge(attrs[:data_attrs] || %{})
266 def follow_activity_factory do
267 follower = insert(:user)
268 followed = insert(:user)
271 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
272 "actor" => follower.ap_id,
274 "object" => followed.ap_id,
275 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
280 actor: follower.ap_id
284 def oauth_app_factory do
285 %Pleroma.Web.OAuth.App{
286 client_name: "Some client",
287 redirect_uris: "https://example.com/callback",
288 scopes: ["read", "write", "follow", "push"],
289 website: "https://example.com",
290 client_id: Ecto.UUID.generate(),
291 client_secret: "aaa;/&bbb"
295 def instance_factory do
296 %Pleroma.Instances.Instance{
298 unreachable_since: nil
302 def oauth_token_factory do
303 oauth_app = insert(:oauth_app)
305 %Pleroma.Web.OAuth.Token{
306 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
308 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
310 app_id: oauth_app.id,
311 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
315 def oauth_authorization_factory do
316 %Pleroma.Web.OAuth.Authorization{
317 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
318 scopes: ["read", "write", "follow", "push"],
319 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
321 app: build(:oauth_app)
325 def push_subscription_factory do
326 %Pleroma.Web.Push.Subscription{
328 token: build(:oauth_token),
329 endpoint: "https://example.com/example/1234",
330 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
332 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
337 def notification_factory do
338 %Pleroma.Notification{
343 def scheduled_activity_factory do
344 %Pleroma.ScheduledActivity{
346 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
347 params: build(:note) |> Map.from_struct() |> Map.get(:data)
351 def registration_factory do
354 %Pleroma.Registration{
359 "name" => "John Doe",
360 "email" => "john@doe.com",
361 "nickname" => "johndoe",
362 "description" => "My bio"
367 def config_factory do
368 %Pleroma.Web.AdminAPI.Config{
369 key: sequence(:key, &"some_key_#{&1}"),
375 :erlang.term_to_binary(%{another_key: "#{key}somevalue", another: "#{key}somevalue"})