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()
39 | ap_id: User.ap_id(user),
40 follower_address: User.ap_followers(user),
41 following_address: User.ap_following(user)
45 def user_relationship_factory(attrs \\ %{}) do
46 source = attrs[:source] || insert(:user)
47 target = attrs[:target] || insert(:user)
48 relationship_type = attrs[:relationship_type] || :block
50 %Pleroma.UserRelationship{
53 relationship_type: relationship_type
57 def note_factory(attrs \\ %{}) do
58 text = sequence(:text, &"This is :moominmamma: note #{&1}")
60 user = attrs[:user] || insert(:user)
65 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
66 "actor" => user.ap_id,
67 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
68 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
75 "2hu" => "corndog.png"
80 data: merge_attributes(data, Map.get(attrs, :data, %{}))
84 def audio_factory(attrs \\ %{}) do
85 text = sequence(:text, &"lain radio episode #{&1}")
87 user = attrs[:user] || insert(:user)
91 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
94 "album" => "lain radio",
95 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
96 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
97 "actor" => user.ap_id,
102 data: merge_attributes(data, Map.get(attrs, :data, %{}))
106 def listen_factory do
107 audio = insert(:audio)
110 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
112 "actor" => audio.data["actor"],
113 "to" => audio.data["to"],
114 "object" => audio.data,
115 "published" => audio.data["published"]
120 actor: data["actor"],
121 recipients: data["to"]
125 def direct_note_factory do
126 user2 = insert(:user)
128 %Pleroma.Object{data: data} = note_factory()
129 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
132 def article_factory do
134 |> Map.put("type", "Article")
137 def tombstone_factory do
139 "type" => "Tombstone",
140 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
141 "formerType" => "Note",
142 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
150 def direct_note_activity_factory do
151 dm = insert(:direct_note)
154 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
156 "actor" => dm.data["actor"],
157 "to" => dm.data["to"],
159 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
160 "context" => dm.data["context"]
165 actor: data["actor"],
166 recipients: data["to"]
170 def note_activity_factory(attrs \\ %{}) do
171 user = attrs[:user] || insert(:user)
172 note = attrs[:note] || insert(:note, user: user)
174 data_attrs = attrs[:data_attrs] || %{}
175 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
179 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
181 "actor" => note.data["actor"],
182 "to" => note.data["to"],
183 "object" => note.data["id"],
184 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
185 "context" => note.data["context"]
187 |> Map.merge(data_attrs)
191 actor: data["actor"],
192 recipients: data["to"]
197 defp expiration_offset_by_minutes(attrs, minutes) do
199 NaiveDateTime.utc_now()
200 |> NaiveDateTime.add(:timer.minutes(minutes), :millisecond)
201 |> NaiveDateTime.truncate(:second)
203 %Pleroma.ActivityExpiration{}
205 |> Map.put(:scheduled_at, scheduled_at)
208 def expiration_in_the_past_factory(attrs \\ %{}) do
209 expiration_offset_by_minutes(attrs, -60)
212 def expiration_in_the_future_factory(attrs \\ %{}) do
213 expiration_offset_by_minutes(attrs, 61)
216 def article_activity_factory do
217 article = insert(:article)
220 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
222 "actor" => article.data["actor"],
223 "to" => article.data["to"],
224 "object" => article.data,
225 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
226 "context" => article.data["context"]
231 actor: data["actor"],
232 recipients: data["to"]
236 def announce_activity_factory(attrs \\ %{}) do
237 note_activity = attrs[:note_activity] || insert(:note_activity)
238 user = attrs[:user] || insert(:user)
241 "type" => "Announce",
242 "actor" => note_activity.actor,
243 "object" => note_activity.data["id"],
244 "to" => [user.follower_address, note_activity.data["actor"]],
245 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
246 "context" => note_activity.data["context"]
252 recipients: data["to"]
256 def like_activity_factory(attrs \\ %{}) do
257 note_activity = attrs[:note_activity] || insert(:note_activity)
258 object = Object.normalize(note_activity)
263 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
264 "actor" => user.ap_id,
266 "object" => object.data["id"],
267 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
269 |> Map.merge(attrs[:data_attrs] || %{})
276 def follow_activity_factory do
277 follower = insert(:user)
278 followed = insert(:user)
281 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
282 "actor" => follower.ap_id,
284 "object" => followed.ap_id,
285 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
290 actor: follower.ap_id
294 def oauth_app_factory do
295 %Pleroma.Web.OAuth.App{
296 client_name: "Some client",
297 redirect_uris: "https://example.com/callback",
298 scopes: ["read", "write", "follow", "push"],
299 website: "https://example.com",
300 client_id: Ecto.UUID.generate(),
301 client_secret: "aaa;/&bbb"
305 def instance_factory do
306 %Pleroma.Instances.Instance{
308 unreachable_since: nil
312 def oauth_token_factory do
313 oauth_app = insert(:oauth_app)
315 %Pleroma.Web.OAuth.Token{
316 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
318 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
320 app_id: oauth_app.id,
321 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
325 def oauth_authorization_factory do
326 %Pleroma.Web.OAuth.Authorization{
327 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
328 scopes: ["read", "write", "follow", "push"],
329 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
331 app: build(:oauth_app)
335 def push_subscription_factory do
336 %Pleroma.Web.Push.Subscription{
338 token: build(:oauth_token),
339 endpoint: "https://example.com/example/1234",
340 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
342 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
347 def notification_factory do
348 %Pleroma.Notification{
353 def scheduled_activity_factory do
354 %Pleroma.ScheduledActivity{
356 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
357 params: build(:note) |> Map.from_struct() |> Map.get(:data)
361 def registration_factory do
364 %Pleroma.Registration{
369 "name" => "John Doe",
370 "email" => "john@doe.com",
371 "nickname" => "johndoe",
372 "description" => "My bio"
377 def config_factory do
378 %Pleroma.Web.AdminAPI.Config{
379 key: sequence(:key, &"some_key_#{&1}"),
385 :erlang.term_to_binary(%{another_key: "#{key}somevalue", another: "#{key}somevalue"})
391 def marker_factory do
394 timeline: "notifications",