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