Merge branch 'docs/storing-remote-media' into 'develop'
[akkoma] / test / support / factory.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Factory do
6 use ExMachina.Ecto, repo: Pleroma.Repo
7 alias Pleroma.Object
8 alias Pleroma.User
9
10 def participation_factory do
11 conversation = insert(:conversation)
12 user = insert(:user)
13
14 %Pleroma.Conversation.Participation{
15 conversation: conversation,
16 user: user,
17 read: false
18 }
19 end
20
21 def conversation_factory do
22 %Pleroma.Conversation{
23 ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}")
24 }
25 end
26
27 def user_factory do
28 user = %User{
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}"),
34 last_digest_emailed_at: NaiveDateTime.utc_now(),
35 last_refreshed_at: NaiveDateTime.utc_now(),
36 notification_settings: %Pleroma.User.NotificationSetting{},
37 multi_factor_authentication_settings: %Pleroma.MFA.Settings{}
38 }
39
40 %{
41 user
42 | ap_id: User.ap_id(user),
43 follower_address: User.ap_followers(user),
44 following_address: User.ap_following(user)
45 }
46 end
47
48 def user_relationship_factory(attrs \\ %{}) do
49 source = attrs[:source] || insert(:user)
50 target = attrs[:target] || insert(:user)
51 relationship_type = attrs[:relationship_type] || :block
52
53 %Pleroma.UserRelationship{
54 source_id: source.id,
55 target_id: target.id,
56 relationship_type: relationship_type
57 }
58 end
59
60 def note_factory(attrs \\ %{}) do
61 text = sequence(:text, &"This is :moominmamma: note #{&1}")
62
63 user = attrs[:user] || insert(:user)
64
65 data = %{
66 "type" => "Note",
67 "content" => text,
68 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
69 "actor" => user.ap_id,
70 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
71 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
72 "likes" => [],
73 "like_count" => 0,
74 "context" => "2hu",
75 "summary" => "2hu",
76 "tag" => ["2hu"],
77 "emoji" => %{
78 "2hu" => "corndog.png"
79 }
80 }
81
82 %Pleroma.Object{
83 data: merge_attributes(data, Map.get(attrs, :data, %{}))
84 }
85 end
86
87 def audio_factory(attrs \\ %{}) do
88 text = sequence(:text, &"lain radio episode #{&1}")
89
90 user = attrs[:user] || insert(:user)
91
92 data = %{
93 "type" => "Audio",
94 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
95 "artist" => "lain",
96 "title" => text,
97 "album" => "lain radio",
98 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
99 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
100 "actor" => user.ap_id,
101 "length" => 180_000
102 }
103
104 %Pleroma.Object{
105 data: merge_attributes(data, Map.get(attrs, :data, %{}))
106 }
107 end
108
109 def listen_factory do
110 audio = insert(:audio)
111
112 data = %{
113 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
114 "type" => "Listen",
115 "actor" => audio.data["actor"],
116 "to" => audio.data["to"],
117 "object" => audio.data,
118 "published" => audio.data["published"]
119 }
120
121 %Pleroma.Activity{
122 data: data,
123 actor: data["actor"],
124 recipients: data["to"]
125 }
126 end
127
128 def direct_note_factory do
129 user2 = insert(:user)
130
131 %Pleroma.Object{data: data} = note_factory()
132 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
133 end
134
135 def article_factory do
136 note_factory()
137 |> Map.put("type", "Article")
138 end
139
140 def tombstone_factory do
141 data = %{
142 "type" => "Tombstone",
143 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
144 "formerType" => "Note",
145 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
146 }
147
148 %Pleroma.Object{
149 data: data
150 }
151 end
152
153 def direct_note_activity_factory do
154 dm = insert(:direct_note)
155
156 data = %{
157 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
158 "type" => "Create",
159 "actor" => dm.data["actor"],
160 "to" => dm.data["to"],
161 "object" => dm.data,
162 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
163 "context" => dm.data["context"]
164 }
165
166 %Pleroma.Activity{
167 data: data,
168 actor: data["actor"],
169 recipients: data["to"]
170 }
171 end
172
173 def note_activity_factory(attrs \\ %{}) do
174 user = attrs[:user] || insert(:user)
175 note = attrs[:note] || insert(:note, user: user)
176
177 data_attrs = attrs[:data_attrs] || %{}
178 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
179
180 data =
181 %{
182 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
183 "type" => "Create",
184 "actor" => note.data["actor"],
185 "to" => note.data["to"],
186 "object" => note.data["id"],
187 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
188 "context" => note.data["context"]
189 }
190 |> Map.merge(data_attrs)
191
192 %Pleroma.Activity{
193 data: data,
194 actor: data["actor"],
195 recipients: data["to"]
196 }
197 |> Map.merge(attrs)
198 end
199
200 defp expiration_offset_by_minutes(attrs, minutes) do
201 scheduled_at =
202 NaiveDateTime.utc_now()
203 |> NaiveDateTime.add(:timer.minutes(minutes), :millisecond)
204 |> NaiveDateTime.truncate(:second)
205
206 %Pleroma.ActivityExpiration{}
207 |> Map.merge(attrs)
208 |> Map.put(:scheduled_at, scheduled_at)
209 end
210
211 def expiration_in_the_past_factory(attrs \\ %{}) do
212 expiration_offset_by_minutes(attrs, -60)
213 end
214
215 def expiration_in_the_future_factory(attrs \\ %{}) do
216 expiration_offset_by_minutes(attrs, 61)
217 end
218
219 def article_activity_factory do
220 article = insert(:article)
221
222 data = %{
223 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
224 "type" => "Create",
225 "actor" => article.data["actor"],
226 "to" => article.data["to"],
227 "object" => article.data,
228 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
229 "context" => article.data["context"]
230 }
231
232 %Pleroma.Activity{
233 data: data,
234 actor: data["actor"],
235 recipients: data["to"]
236 }
237 end
238
239 def announce_activity_factory(attrs \\ %{}) do
240 note_activity = attrs[:note_activity] || insert(:note_activity)
241 user = attrs[:user] || insert(:user)
242
243 data = %{
244 "type" => "Announce",
245 "actor" => note_activity.actor,
246 "object" => note_activity.data["id"],
247 "to" => [user.follower_address, note_activity.data["actor"]],
248 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
249 "context" => note_activity.data["context"]
250 }
251
252 %Pleroma.Activity{
253 data: data,
254 actor: user.ap_id,
255 recipients: data["to"]
256 }
257 end
258
259 def like_activity_factory(attrs \\ %{}) do
260 note_activity = attrs[:note_activity] || insert(:note_activity)
261 object = Object.normalize(note_activity)
262 user = insert(:user)
263
264 data =
265 %{
266 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
267 "actor" => user.ap_id,
268 "type" => "Like",
269 "object" => object.data["id"],
270 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
271 }
272 |> Map.merge(attrs[:data_attrs] || %{})
273
274 %Pleroma.Activity{
275 data: data
276 }
277 end
278
279 def follow_activity_factory do
280 follower = insert(:user)
281 followed = insert(:user)
282
283 data = %{
284 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
285 "actor" => follower.ap_id,
286 "type" => "Follow",
287 "object" => followed.ap_id,
288 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
289 }
290
291 %Pleroma.Activity{
292 data: data,
293 actor: follower.ap_id
294 }
295 end
296
297 def oauth_app_factory do
298 %Pleroma.Web.OAuth.App{
299 client_name: sequence(:client_name, &"Some client #{&1}"),
300 redirect_uris: "https://example.com/callback",
301 scopes: ["read", "write", "follow", "push", "admin"],
302 website: "https://example.com",
303 client_id: Ecto.UUID.generate(),
304 client_secret: "aaa;/&bbb"
305 }
306 end
307
308 def instance_factory do
309 %Pleroma.Instances.Instance{
310 host: "domain.com",
311 unreachable_since: nil
312 }
313 end
314
315 def oauth_token_factory(attrs \\ %{}) do
316 scopes = Map.get(attrs, :scopes, ["read"])
317 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
318 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
319
320 valid_until =
321 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
322
323 %Pleroma.Web.OAuth.Token{
324 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
325 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
326 scopes: scopes,
327 user: user,
328 app: oauth_app,
329 valid_until: valid_until
330 }
331 end
332
333 def oauth_admin_token_factory(attrs \\ %{}) do
334 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
335
336 scopes =
337 attrs
338 |> Map.get(:scopes, ["admin"])
339 |> Kernel.++(["admin"])
340 |> Enum.uniq()
341
342 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
343 oauth_token_factory(attrs)
344 end
345
346 def oauth_authorization_factory do
347 %Pleroma.Web.OAuth.Authorization{
348 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
349 scopes: ["read", "write", "follow", "push"],
350 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
351 user: build(:user),
352 app: build(:oauth_app)
353 }
354 end
355
356 def push_subscription_factory do
357 %Pleroma.Web.Push.Subscription{
358 user: build(:user),
359 token: build(:oauth_token),
360 endpoint: "https://example.com/example/1234",
361 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
362 key_p256dh:
363 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
364 data: %{}
365 }
366 end
367
368 def notification_factory do
369 %Pleroma.Notification{
370 user: build(:user)
371 }
372 end
373
374 def scheduled_activity_factory do
375 %Pleroma.ScheduledActivity{
376 user: build(:user),
377 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
378 params: build(:note) |> Map.from_struct() |> Map.get(:data)
379 }
380 end
381
382 def registration_factory do
383 user = insert(:user)
384
385 %Pleroma.Registration{
386 user: user,
387 provider: "twitter",
388 uid: "171799000",
389 info: %{
390 "name" => "John Doe",
391 "email" => "john@doe.com",
392 "nickname" => "johndoe",
393 "description" => "My bio"
394 }
395 }
396 end
397
398 def config_factory do
399 %Pleroma.ConfigDB{
400 key:
401 sequence(:key, fn key ->
402 # Atom dynamic registration hack in tests
403 "some_key_#{key}"
404 |> String.to_atom()
405 |> inspect()
406 end),
407 group: ":pleroma",
408 value:
409 sequence(
410 :value,
411 fn key ->
412 :erlang.term_to_binary(%{another_key: "#{key}somevalue", another: "#{key}somevalue"})
413 end
414 )
415 }
416 end
417
418 def marker_factory do
419 %Pleroma.Marker{
420 user: build(:user),
421 timeline: "notifications",
422 lock_version: 0,
423 last_read_id: "1"
424 }
425 end
426
427 def mfa_token_factory do
428 %Pleroma.MFA.Token{
429 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
430 authorization: build(:oauth_authorization),
431 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
432 user: build(:user)
433 }
434 end
435 end