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