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