User: generate private keys on user creation
[akkoma] / test / support / factory.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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
8 require Pleroma.Constants
9
10 alias Pleroma.Keys
11 alias Pleroma.Object
12 alias Pleroma.User
13
14 def participation_factory do
15 conversation = insert(:conversation)
16 user = insert(:user)
17
18 %Pleroma.Conversation.Participation{
19 conversation: conversation,
20 user: user,
21 read: false
22 }
23 end
24
25 def conversation_factory do
26 %Pleroma.Conversation{
27 ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}")
28 }
29 end
30
31 def user_factory(attrs \\ %{}) do
32 {:ok, pem} = Keys.generate_rsa_pem()
33
34 user = %User{
35 name: sequence(:name, &"Test テスト User #{&1}"),
36 email: sequence(:email, &"user#{&1}@example.com"),
37 nickname: sequence(:nickname, &"nick#{&1}"),
38 password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt("test"),
39 bio: sequence(:bio, &"Tester Number #{&1}"),
40 is_discoverable: true,
41 last_digest_emailed_at: NaiveDateTime.utc_now(),
42 last_refreshed_at: NaiveDateTime.utc_now(),
43 notification_settings: %Pleroma.User.NotificationSetting{},
44 multi_factor_authentication_settings: %Pleroma.MFA.Settings{},
45 ap_enabled: true,
46 keys: pem
47 }
48
49 urls =
50 if attrs[:local] == false do
51 base_domain = attrs[:domain] || Enum.random(["domain1.com", "domain2.com", "domain3.com"])
52
53 ap_id = "https://#{base_domain}/users/#{user.nickname}"
54
55 %{
56 ap_id: ap_id,
57 follower_address: ap_id <> "/followers",
58 following_address: ap_id <> "/following",
59 featured_address: ap_id <> "/collections/featured"
60 }
61 else
62 %{
63 ap_id: User.ap_id(user),
64 follower_address: User.ap_followers(user),
65 following_address: User.ap_following(user),
66 featured_address: User.ap_featured_collection(user)
67 }
68 end
69
70 attrs = Map.delete(attrs, :domain)
71
72 user
73 |> Map.put(:raw_bio, user.bio)
74 |> Map.merge(urls)
75 |> merge_attributes(attrs)
76 end
77
78 def user_relationship_factory(attrs \\ %{}) do
79 source = attrs[:source] || insert(:user)
80 target = attrs[:target] || insert(:user)
81 relationship_type = attrs[:relationship_type] || :block
82
83 %Pleroma.UserRelationship{
84 source_id: source.id,
85 target_id: target.id,
86 relationship_type: relationship_type
87 }
88 end
89
90 def note_factory(attrs \\ %{}) do
91 text = sequence(:text, &"This is :moominmamma: note #{&1}")
92
93 user = attrs[:user] || insert(:user)
94
95 data = %{
96 "type" => "Note",
97 "content" => text,
98 "source" => text,
99 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
100 "actor" => user.ap_id,
101 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
102 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
103 "likes" => [],
104 "like_count" => 0,
105 "context" => "2hu",
106 "summary" => "2hu",
107 "tag" => ["2hu"],
108 "emoji" => %{
109 "2hu" => "corndog.png"
110 }
111 }
112
113 %Pleroma.Object{
114 data: merge_attributes(data, Map.get(attrs, :data, %{}))
115 }
116 end
117
118 def attachment_factory(attrs \\ %{}) do
119 user = attrs[:user] || insert(:user)
120
121 data =
122 attachment_data(user.ap_id, nil)
123 |> Map.put("id", Pleroma.Web.ActivityPub.Utils.generate_object_id())
124
125 %Pleroma.Object{
126 data: merge_attributes(data, Map.get(attrs, :data, %{}))
127 }
128 end
129
130 def attachment_note_factory(attrs \\ %{}) do
131 user = attrs[:user] || insert(:user)
132 {length, attrs} = Map.pop(attrs, :length, 1)
133
134 data = %{
135 "attachment" =>
136 Stream.repeatedly(fn -> attachment_data(user.ap_id, attrs[:href]) end)
137 |> Enum.take(length)
138 }
139
140 build(:note, Map.put(attrs, :data, data))
141 end
142
143 defp attachment_data(ap_id, href) do
144 href = href || sequence(:href, &"#{Pleroma.Web.Endpoint.url()}/media/#{&1}.jpg")
145
146 %{
147 "url" => [
148 %{
149 "href" => href,
150 "type" => "Link",
151 "mediaType" => "image/jpeg"
152 }
153 ],
154 "name" => "some name",
155 "type" => "Document",
156 "actor" => ap_id,
157 "mediaType" => "image/jpeg"
158 }
159 end
160
161 def followers_only_note_factory(attrs \\ %{}) do
162 %Pleroma.Object{data: data} = note_factory(attrs)
163 %Pleroma.Object{data: Map.merge(data, %{"to" => [data["actor"] <> "/followers"]})}
164 end
165
166 def audio_factory(attrs \\ %{}) do
167 text = sequence(:text, &"lain radio episode #{&1}")
168
169 user = attrs[:user] || insert(:user)
170
171 data = %{
172 "type" => "Audio",
173 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
174 "artist" => "lain",
175 "title" => text,
176 "album" => "lain radio",
177 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
178 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
179 "actor" => user.ap_id,
180 "length" => 180_000
181 }
182
183 %Pleroma.Object{
184 data: merge_attributes(data, Map.get(attrs, :data, %{}))
185 }
186 end
187
188 def listen_factory do
189 audio = insert(:audio)
190
191 data = %{
192 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
193 "type" => "Listen",
194 "actor" => audio.data["actor"],
195 "to" => audio.data["to"],
196 "object" => audio.data,
197 "published" => audio.data["published"]
198 }
199
200 %Pleroma.Activity{
201 data: data,
202 actor: data["actor"],
203 recipients: data["to"]
204 }
205 end
206
207 def direct_note_factory do
208 user2 = insert(:user)
209
210 %Pleroma.Object{data: data} = note_factory()
211 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
212 end
213
214 def article_factory do
215 %Pleroma.Object{data: data} = note_factory()
216 %Pleroma.Object{data: Map.merge(data, %{"type" => "Article"})}
217 end
218
219 def tombstone_factory do
220 data = %{
221 "type" => "Tombstone",
222 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
223 "formerType" => "Note",
224 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
225 }
226
227 %Pleroma.Object{
228 data: data
229 }
230 end
231
232 def question_factory(attrs \\ %{}) do
233 user = attrs[:user] || insert(:user)
234
235 data = %{
236 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
237 "type" => "Question",
238 "actor" => user.ap_id,
239 "attributedTo" => user.ap_id,
240 "attachment" => [],
241 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
242 "cc" => [user.follower_address],
243 "context" => Pleroma.Web.ActivityPub.Utils.generate_context_id(),
244 "closed" => DateTime.utc_now() |> DateTime.add(86_400) |> DateTime.to_iso8601(),
245 "oneOf" => [
246 %{
247 "type" => "Note",
248 "name" => "chocolate",
249 "replies" => %{"totalItems" => 0, "type" => "Collection"}
250 },
251 %{
252 "type" => "Note",
253 "name" => "vanilla",
254 "replies" => %{"totalItems" => 0, "type" => "Collection"}
255 }
256 ]
257 }
258
259 %Pleroma.Object{
260 data: merge_attributes(data, Map.get(attrs, :data, %{}))
261 }
262 end
263
264 def direct_note_activity_factory do
265 dm = insert(:direct_note)
266
267 data = %{
268 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
269 "type" => "Create",
270 "actor" => dm.data["actor"],
271 "to" => dm.data["to"],
272 "object" => dm.data,
273 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
274 "context" => dm.data["context"]
275 }
276
277 %Pleroma.Activity{
278 data: data,
279 actor: data["actor"],
280 recipients: data["to"]
281 }
282 end
283
284 def add_activity_factory(attrs \\ %{}) do
285 featured_collection_activity(attrs, "Add")
286 end
287
288 def remove_activity_factor(attrs \\ %{}) do
289 featured_collection_activity(attrs, "Remove")
290 end
291
292 defp featured_collection_activity(attrs, type) do
293 user = attrs[:user] || insert(:user)
294 note = attrs[:note] || insert(:note, user: user)
295
296 data_attrs =
297 attrs
298 |> Map.get(:data_attrs, %{})
299 |> Map.put(:type, type)
300
301 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
302
303 data =
304 %{
305 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
306 "target" => user.featured_address,
307 "object" => note.data["object"],
308 "actor" => note.data["actor"],
309 "type" => "Add",
310 "to" => [Pleroma.Constants.as_public()],
311 "cc" => [user.follower_address]
312 }
313 |> Map.merge(data_attrs)
314
315 %Pleroma.Activity{
316 data: data,
317 actor: data["actor"],
318 recipients: data["to"]
319 }
320 |> Map.merge(attrs)
321 end
322
323 def followers_only_note_activity_factory(attrs \\ %{}) do
324 user = attrs[:user] || insert(:user)
325 note = insert(:followers_only_note, user: user)
326
327 data_attrs = attrs[:data_attrs] || %{}
328 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
329
330 data =
331 %{
332 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
333 "type" => "Create",
334 "actor" => note.data["actor"],
335 "to" => note.data["to"],
336 "object" => note.data,
337 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
338 "context" => note.data["context"]
339 }
340 |> Map.merge(data_attrs)
341
342 %Pleroma.Activity{
343 data: data,
344 actor: data["actor"],
345 recipients: data["to"]
346 }
347 |> Map.merge(attrs)
348 end
349
350 def note_activity_factory(attrs \\ %{}) do
351 user = attrs[:user] || insert(:user)
352 note = attrs[:note] || insert(:note, user: user)
353
354 data_attrs = attrs[:data_attrs] || %{}
355 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
356
357 data =
358 %{
359 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
360 "type" => "Create",
361 "actor" => note.data["actor"],
362 "to" => note.data["to"],
363 "object" => note.data["id"],
364 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
365 "context" => note.data["context"]
366 }
367 |> Map.merge(data_attrs)
368
369 %Pleroma.Activity{
370 data: data,
371 actor: data["actor"],
372 recipients: data["to"]
373 }
374 |> Map.merge(attrs)
375 end
376
377 def article_activity_factory do
378 article = insert(:article)
379
380 data = %{
381 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
382 "type" => "Create",
383 "actor" => article.data["actor"],
384 "to" => article.data["to"],
385 "object" => article.data,
386 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
387 "context" => article.data["context"]
388 }
389
390 %Pleroma.Activity{
391 data: data,
392 actor: data["actor"],
393 recipients: data["to"]
394 }
395 end
396
397 def announce_activity_factory(attrs \\ %{}) do
398 note_activity = attrs[:note_activity] || insert(:note_activity)
399 user = attrs[:user] || insert(:user)
400
401 data = %{
402 "type" => "Announce",
403 "actor" => note_activity.actor,
404 "object" => note_activity.data["id"],
405 "to" => [user.follower_address, note_activity.data["actor"]],
406 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
407 "context" => note_activity.data["context"]
408 }
409
410 %Pleroma.Activity{
411 data: data,
412 actor: user.ap_id,
413 recipients: data["to"]
414 }
415 end
416
417 def like_activity_factory(attrs \\ %{}) do
418 note_activity = attrs[:note_activity] || insert(:note_activity)
419 object = Object.normalize(note_activity, fetch: false)
420 user = insert(:user)
421
422 data =
423 %{
424 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
425 "actor" => user.ap_id,
426 "type" => "Like",
427 "object" => object.data["id"],
428 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
429 }
430 |> Map.merge(attrs[:data_attrs] || %{})
431
432 %Pleroma.Activity{
433 data: data
434 }
435 end
436
437 def follow_activity_factory do
438 follower = insert(:user)
439 followed = insert(:user)
440
441 data = %{
442 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
443 "actor" => follower.ap_id,
444 "type" => "Follow",
445 "object" => followed.ap_id,
446 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
447 }
448
449 %Pleroma.Activity{
450 data: data,
451 actor: follower.ap_id
452 }
453 end
454
455 def report_activity_factory(attrs \\ %{}) do
456 user = attrs[:user] || insert(:user)
457 activity = attrs[:activity] || insert(:note_activity)
458 state = attrs[:state] || "open"
459
460 data = %{
461 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
462 "actor" => user.ap_id,
463 "type" => "Flag",
464 "object" => [activity.actor, activity.data["id"]],
465 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
466 "to" => [],
467 "cc" => [activity.actor],
468 "context" => activity.data["context"],
469 "state" => state
470 }
471
472 %Pleroma.Activity{
473 data: data,
474 actor: data["actor"],
475 recipients: data["to"] ++ data["cc"]
476 }
477 end
478
479 def question_activity_factory(attrs \\ %{}) do
480 user = attrs[:user] || insert(:user)
481 question = attrs[:question] || insert(:question, user: user)
482
483 data_attrs = attrs[:data_attrs] || %{}
484 attrs = Map.drop(attrs, [:user, :question, :data_attrs])
485
486 data =
487 %{
488 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
489 "type" => "Create",
490 "actor" => question.data["actor"],
491 "to" => question.data["to"],
492 "object" => question.data["id"],
493 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
494 "context" => question.data["context"]
495 }
496 |> Map.merge(data_attrs)
497
498 %Pleroma.Activity{
499 data: data,
500 actor: data["actor"],
501 recipients: data["to"]
502 }
503 |> Map.merge(attrs)
504 end
505
506 def oauth_app_factory do
507 %Pleroma.Web.OAuth.App{
508 client_name: sequence(:client_name, &"Some client #{&1}"),
509 redirect_uris: "https://example.com/callback",
510 scopes: ["read", "write", "follow", "push", "admin"],
511 website: "https://example.com",
512 client_id: Ecto.UUID.generate(),
513 client_secret: "aaa;/&bbb"
514 }
515 end
516
517 def instance_factory do
518 %Pleroma.Instances.Instance{
519 host: "domain.com",
520 unreachable_since: nil
521 }
522 end
523
524 def oauth_token_factory(attrs \\ %{}) do
525 scopes = Map.get(attrs, :scopes, ["read"])
526 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
527 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
528
529 valid_until =
530 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
531
532 %Pleroma.Web.OAuth.Token{
533 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
534 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
535 scopes: scopes,
536 user: user,
537 app: oauth_app,
538 valid_until: valid_until
539 }
540 end
541
542 def oauth_admin_token_factory(attrs \\ %{}) do
543 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
544
545 scopes =
546 attrs
547 |> Map.get(:scopes, ["admin"])
548 |> Kernel.++(["admin"])
549 |> Enum.uniq()
550
551 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
552 oauth_token_factory(attrs)
553 end
554
555 def oauth_authorization_factory do
556 %Pleroma.Web.OAuth.Authorization{
557 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
558 scopes: ["read", "write", "follow", "push"],
559 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
560 user: build(:user),
561 app: build(:oauth_app)
562 }
563 end
564
565 def push_subscription_factory do
566 %Pleroma.Web.Push.Subscription{
567 user: build(:user),
568 token: build(:oauth_token),
569 endpoint: "https://example.com/example/1234",
570 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
571 key_p256dh:
572 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
573 data: %{}
574 }
575 end
576
577 def notification_factory do
578 %Pleroma.Notification{
579 user: build(:user)
580 }
581 end
582
583 def scheduled_activity_factory do
584 %Pleroma.ScheduledActivity{
585 user: build(:user),
586 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
587 params: build(:note) |> Map.from_struct() |> Map.get(:data)
588 }
589 end
590
591 def registration_factory do
592 user = insert(:user)
593
594 %Pleroma.Registration{
595 user: user,
596 provider: "twitter",
597 uid: "171799000",
598 info: %{
599 "name" => "John Doe",
600 "email" => "john@doe.com",
601 "nickname" => "johndoe",
602 "description" => "My bio"
603 }
604 }
605 end
606
607 def config_factory(attrs \\ %{}) do
608 %Pleroma.ConfigDB{
609 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
610 group: :pleroma,
611 value:
612 sequence(
613 :value,
614 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
615 )
616 }
617 |> merge_attributes(attrs)
618 end
619
620 def marker_factory do
621 %Pleroma.Marker{
622 user: build(:user),
623 timeline: "notifications",
624 lock_version: 0,
625 last_read_id: "1"
626 }
627 end
628
629 def mfa_token_factory do
630 %Pleroma.MFA.Token{
631 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
632 authorization: build(:oauth_authorization),
633 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
634 user: build(:user)
635 }
636 end
637
638 def filter_factory do
639 %Pleroma.Filter{
640 user: build(:user),
641 filter_id: sequence(:filter_id, & &1),
642 phrase: "cofe",
643 context: ["home"]
644 }
645 end
646
647 def announcement_factory(params \\ %{}) do
648 data = Map.get(params, :data, %{})
649
650 {_, params} = Map.pop(params, :data)
651
652 %Pleroma.Announcement{
653 data: Map.merge(%{"content" => "test announcement", "all_day" => false}, data)
654 }
655 |> Map.merge(params)
656 |> Pleroma.Announcement.add_rendered_properties()
657 end
658 end