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