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