d02bd9212ccb74f969bf1bcfd486c22f7d2fef51
[akkoma] / test / support / factory.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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 info: %{}
35 }
36
37 %{
38 user
39 | ap_id: User.ap_id(user),
40 follower_address: User.ap_followers(user),
41 following_address: User.ap_following(user),
42 following: [User.ap_id(user)]
43 }
44 end
45
46 def note_factory(attrs \\ %{}) do
47 text = sequence(:text, &"This is :moominmamma: note #{&1}")
48
49 user = attrs[:user] || insert(:user)
50
51 data = %{
52 "type" => "Note",
53 "content" => text,
54 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
55 "actor" => user.ap_id,
56 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
57 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
58 "likes" => [],
59 "like_count" => 0,
60 "context" => "2hu",
61 "summary" => "2hu",
62 "tag" => ["2hu"],
63 "emoji" => %{
64 "2hu" => "corndog.png"
65 }
66 }
67
68 %Pleroma.Object{
69 data: merge_attributes(data, Map.get(attrs, :data, %{}))
70 }
71 end
72
73 def direct_note_factory do
74 user2 = insert(:user)
75
76 %Pleroma.Object{data: data} = note_factory()
77 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
78 end
79
80 def article_factory do
81 note_factory()
82 |> Map.put("type", "Article")
83 end
84
85 def tombstone_factory do
86 data = %{
87 "type" => "Tombstone",
88 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
89 "formerType" => "Note",
90 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
91 }
92
93 %Pleroma.Object{
94 data: data
95 }
96 end
97
98 def direct_note_activity_factory do
99 dm = insert(:direct_note)
100
101 data = %{
102 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
103 "type" => "Create",
104 "actor" => dm.data["actor"],
105 "to" => dm.data["to"],
106 "object" => dm.data,
107 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
108 "context" => dm.data["context"]
109 }
110
111 %Pleroma.Activity{
112 data: data,
113 actor: data["actor"],
114 recipients: data["to"]
115 }
116 end
117
118 def note_activity_factory(attrs \\ %{}) do
119 user = attrs[:user] || insert(:user)
120 note = attrs[:note] || insert(:note, user: user)
121 published = attrs[:published] || DateTime.utc_now() |> DateTime.to_iso8601()
122 attrs = Map.drop(attrs, [:user, :note])
123
124 data = %{
125 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
126 "type" => "Create",
127 "actor" => note.data["actor"],
128 "to" => note.data["to"],
129 "object" => note.data["id"],
130 "published" => published,
131 "context" => note.data["context"]
132 }
133
134 %Pleroma.Activity{
135 data: data,
136 actor: data["actor"],
137 recipients: data["to"]
138 }
139 |> Map.merge(attrs)
140 end
141
142 def article_activity_factory do
143 article = insert(:article)
144
145 data = %{
146 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
147 "type" => "Create",
148 "actor" => article.data["actor"],
149 "to" => article.data["to"],
150 "object" => article.data,
151 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
152 "context" => article.data["context"]
153 }
154
155 %Pleroma.Activity{
156 data: data,
157 actor: data["actor"],
158 recipients: data["to"]
159 }
160 end
161
162 def announce_activity_factory(attrs \\ %{}) do
163 note_activity = attrs[:note_activity] || insert(:note_activity)
164 user = attrs[:user] || insert(:user)
165
166 data = %{
167 "type" => "Announce",
168 "actor" => note_activity.actor,
169 "object" => note_activity.data["id"],
170 "to" => [user.follower_address, note_activity.data["actor"]],
171 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
172 "context" => note_activity.data["context"]
173 }
174
175 %Pleroma.Activity{
176 data: data,
177 actor: user.ap_id,
178 recipients: data["to"]
179 }
180 end
181
182 def like_activity_factory do
183 note_activity = insert(:note_activity)
184 object = Object.normalize(note_activity)
185 user = insert(:user)
186
187 data = %{
188 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
189 "actor" => user.ap_id,
190 "type" => "Like",
191 "object" => object.data["id"],
192 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
193 }
194
195 %Pleroma.Activity{
196 data: data
197 }
198 end
199
200 def follow_activity_factory do
201 follower = insert(:user)
202 followed = insert(:user)
203
204 data = %{
205 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
206 "actor" => follower.ap_id,
207 "type" => "Follow",
208 "object" => followed.ap_id,
209 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
210 }
211
212 %Pleroma.Activity{
213 data: data,
214 actor: follower.ap_id
215 }
216 end
217
218 def websub_subscription_factory do
219 %Pleroma.Web.Websub.WebsubServerSubscription{
220 topic: "http://example.org",
221 callback: "http://example.org/callback",
222 secret: "here's a secret",
223 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 100),
224 state: "requested"
225 }
226 end
227
228 def websub_client_subscription_factory do
229 %Pleroma.Web.Websub.WebsubClientSubscription{
230 topic: "http://example.org",
231 secret: "here's a secret",
232 valid_until: nil,
233 state: "requested",
234 subscribers: []
235 }
236 end
237
238 def oauth_app_factory do
239 %Pleroma.Web.OAuth.App{
240 client_name: "Some client",
241 redirect_uris: "https://example.com/callback",
242 scopes: ["read", "write", "follow", "push"],
243 website: "https://example.com",
244 client_id: Ecto.UUID.generate(),
245 client_secret: "aaa;/&bbb"
246 }
247 end
248
249 def instance_factory do
250 %Pleroma.Instances.Instance{
251 host: "domain.com",
252 unreachable_since: nil
253 }
254 end
255
256 def oauth_token_factory do
257 oauth_app = insert(:oauth_app)
258
259 %Pleroma.Web.OAuth.Token{
260 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
261 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
262 user: build(:user),
263 app_id: oauth_app.id,
264 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
265 }
266 end
267
268 def oauth_authorization_factory do
269 %Pleroma.Web.OAuth.Authorization{
270 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
271 scopes: ["read", "write", "follow", "push"],
272 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
273 user: build(:user),
274 app: build(:oauth_app)
275 }
276 end
277
278 def push_subscription_factory do
279 %Pleroma.Web.Push.Subscription{
280 user: build(:user),
281 token: build(:oauth_token),
282 endpoint: "https://example.com/example/1234",
283 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
284 key_p256dh:
285 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
286 data: %{}
287 }
288 end
289
290 def notification_factory do
291 %Pleroma.Notification{
292 user: build(:user)
293 }
294 end
295
296 def scheduled_activity_factory do
297 %Pleroma.ScheduledActivity{
298 user: build(:user),
299 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
300 params: build(:note) |> Map.from_struct() |> Map.get(:data)
301 }
302 end
303
304 def registration_factory do
305 user = insert(:user)
306
307 %Pleroma.Registration{
308 user: user,
309 provider: "twitter",
310 uid: "171799000",
311 info: %{
312 "name" => "John Doe",
313 "email" => "john@doe.com",
314 "nickname" => "johndoe",
315 "description" => "My bio"
316 }
317 }
318 end
319
320 def config_factory do
321 %Pleroma.Web.AdminAPI.Config{
322 key: sequence(:key, &"some_key_#{&1}"),
323 group: "pleroma",
324 value:
325 sequence(
326 :value,
327 fn key ->
328 :erlang.term_to_binary(%{another_key: "#{key}somevalue", another: "#{key}somevalue"})
329 end
330 )
331 }
332 end
333 end