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}"),
34 last_digest_emailed_at: NaiveDateTime.utc_now(),
35 notification_settings: %Pleroma.User.NotificationSetting{}
40 | ap_id: User.ap_id(user),
41 follower_address: User.ap_followers(user),
42 following_address: User.ap_following(user)
46 def user_relationship_factory(attrs \\ %{}) do
47 source = attrs[:source] || insert(:user)
48 target = attrs[:target] || insert(:user)
49 relationship_type = attrs[:relationship_type] || :block
51 %Pleroma.UserRelationship{
54 relationship_type: relationship_type
58 def note_factory(attrs \\ %{}) do
59 text = sequence(:text, &"This is :moominmamma: note #{&1}")
61 user = attrs[:user] || insert(:user)
66 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
67 "actor" => user.ap_id,
68 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
69 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
76 "2hu" => "corndog.png"
81 data: merge_attributes(data, Map.get(attrs, :data, %{}))
85 def audio_factory(attrs \\ %{}) do
86 text = sequence(:text, &"lain radio episode #{&1}")
88 user = attrs[:user] || insert(:user)
92 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
95 "album" => "lain radio",
96 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
97 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
98 "actor" => user.ap_id,
103 data: merge_attributes(data, Map.get(attrs, :data, %{}))
107 def listen_factory do
108 audio = insert(:audio)
111 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
113 "actor" => audio.data["actor"],
114 "to" => audio.data["to"],
115 "object" => audio.data,
116 "published" => audio.data["published"]
121 actor: data["actor"],
122 recipients: data["to"]
126 def direct_note_factory do
127 user2 = insert(:user)
129 %Pleroma.Object{data: data} = note_factory()
130 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
133 def article_factory do
135 |> Map.put("type", "Article")
138 def tombstone_factory do
140 "type" => "Tombstone",
141 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
142 "formerType" => "Note",
143 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
151 def direct_note_activity_factory do
152 dm = insert(:direct_note)
155 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
157 "actor" => dm.data["actor"],
158 "to" => dm.data["to"],
160 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
161 "context" => dm.data["context"]
166 actor: data["actor"],
167 recipients: data["to"]
171 def note_activity_factory(attrs \\ %{}) do
172 user = attrs[:user] || insert(:user)
173 note = attrs[:note] || insert(:note, user: user)
175 data_attrs = attrs[:data_attrs] || %{}
176 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
180 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
182 "actor" => note.data["actor"],
183 "to" => note.data["to"],
184 "object" => note.data["id"],
185 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
186 "context" => note.data["context"]
188 |> Map.merge(data_attrs)
192 actor: data["actor"],
193 recipients: data["to"]
198 defp expiration_offset_by_minutes(attrs, minutes) do
200 NaiveDateTime.utc_now()
201 |> NaiveDateTime.add(:timer.minutes(minutes), :millisecond)
202 |> NaiveDateTime.truncate(:second)
204 %Pleroma.ActivityExpiration{}
206 |> Map.put(:scheduled_at, scheduled_at)
209 def expiration_in_the_past_factory(attrs \\ %{}) do
210 expiration_offset_by_minutes(attrs, -60)
213 def expiration_in_the_future_factory(attrs \\ %{}) do
214 expiration_offset_by_minutes(attrs, 61)
217 def article_activity_factory do
218 article = insert(:article)
221 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
223 "actor" => article.data["actor"],
224 "to" => article.data["to"],
225 "object" => article.data,
226 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
227 "context" => article.data["context"]
232 actor: data["actor"],
233 recipients: data["to"]
237 def announce_activity_factory(attrs \\ %{}) do
238 note_activity = attrs[:note_activity] || insert(:note_activity)
239 user = attrs[:user] || insert(:user)
242 "type" => "Announce",
243 "actor" => note_activity.actor,
244 "object" => note_activity.data["id"],
245 "to" => [user.follower_address, note_activity.data["actor"]],
246 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
247 "context" => note_activity.data["context"]
253 recipients: data["to"]
257 def like_activity_factory(attrs \\ %{}) do
258 note_activity = attrs[:note_activity] || insert(:note_activity)
259 object = Object.normalize(note_activity)
264 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
265 "actor" => user.ap_id,
267 "object" => object.data["id"],
268 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
270 |> Map.merge(attrs[:data_attrs] || %{})
277 def follow_activity_factory do
278 follower = insert(:user)
279 followed = insert(:user)
282 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
283 "actor" => follower.ap_id,
285 "object" => followed.ap_id,
286 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
291 actor: follower.ap_id
295 def oauth_app_factory do
296 %Pleroma.Web.OAuth.App{
297 client_name: "Some client",
298 redirect_uris: "https://example.com/callback",
299 scopes: ["read", "write", "follow", "push"],
300 website: "https://example.com",
301 client_id: Ecto.UUID.generate(),
302 client_secret: "aaa;/&bbb"
306 def instance_factory do
307 %Pleroma.Instances.Instance{
309 unreachable_since: nil
313 def oauth_token_factory do
314 oauth_app = insert(:oauth_app)
316 %Pleroma.Web.OAuth.Token{
317 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
319 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
321 app_id: oauth_app.id,
322 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
326 def oauth_authorization_factory do
327 %Pleroma.Web.OAuth.Authorization{
328 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
329 scopes: ["read", "write", "follow", "push"],
330 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
332 app: build(:oauth_app)
336 def push_subscription_factory do
337 %Pleroma.Web.Push.Subscription{
339 token: build(:oauth_token),
340 endpoint: "https://example.com/example/1234",
341 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
343 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
348 def notification_factory do
349 %Pleroma.Notification{
354 def scheduled_activity_factory do
355 %Pleroma.ScheduledActivity{
357 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
358 params: build(:note) |> Map.from_struct() |> Map.get(:data)
362 def registration_factory do
365 %Pleroma.Registration{
370 "name" => "John Doe",
371 "email" => "john@doe.com",
372 "nickname" => "johndoe",
373 "description" => "My bio"
378 def config_factory do
379 %Pleroma.Web.AdminAPI.Config{
380 key: sequence(:key, &"some_key_#{&1}"),
386 :erlang.term_to_binary(%{another_key: "#{key}somevalue", another: "#{key}somevalue"})
392 def marker_factory do
395 timeline: "notifications",