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