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