Merge branch 'develop' into match-file-name
[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 attrs = Map.drop(attrs, [:user, :note])
122
123 data = %{
124 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
125 "type" => "Create",
126 "actor" => note.data["actor"],
127 "to" => note.data["to"],
128 "object" => note.data["id"],
129 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
130 "context" => note.data["context"]
131 }
132
133 %Pleroma.Activity{
134 data: data,
135 actor: data["actor"],
136 recipients: data["to"]
137 }
138 |> Map.merge(attrs)
139 end
140
141 def article_activity_factory do
142 article = insert(:article)
143
144 data = %{
145 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
146 "type" => "Create",
147 "actor" => article.data["actor"],
148 "to" => article.data["to"],
149 "object" => article.data,
150 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
151 "context" => article.data["context"]
152 }
153
154 %Pleroma.Activity{
155 data: data,
156 actor: data["actor"],
157 recipients: data["to"]
158 }
159 end
160
161 def announce_activity_factory(attrs \\ %{}) do
162 note_activity = attrs[:note_activity] || insert(:note_activity)
163 user = attrs[:user] || insert(:user)
164
165 data = %{
166 "type" => "Announce",
167 "actor" => note_activity.actor,
168 "object" => note_activity.data["id"],
169 "to" => [user.follower_address, note_activity.data["actor"]],
170 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
171 "context" => note_activity.data["context"]
172 }
173
174 %Pleroma.Activity{
175 data: data,
176 actor: user.ap_id,
177 recipients: data["to"]
178 }
179 end
180
181 def like_activity_factory do
182 note_activity = insert(:note_activity)
183 object = Object.normalize(note_activity)
184 user = insert(:user)
185
186 data = %{
187 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
188 "actor" => user.ap_id,
189 "type" => "Like",
190 "object" => object.data["id"],
191 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
192 }
193
194 %Pleroma.Activity{
195 data: data
196 }
197 end
198
199 def follow_activity_factory do
200 follower = insert(:user)
201 followed = insert(:user)
202
203 data = %{
204 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
205 "actor" => follower.ap_id,
206 "type" => "Follow",
207 "object" => followed.ap_id,
208 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
209 }
210
211 %Pleroma.Activity{
212 data: data,
213 actor: follower.ap_id
214 }
215 end
216
217 def websub_subscription_factory do
218 %Pleroma.Web.Websub.WebsubServerSubscription{
219 topic: "http://example.org",
220 callback: "http://example.org/callback",
221 secret: "here's a secret",
222 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 100),
223 state: "requested"
224 }
225 end
226
227 def websub_client_subscription_factory do
228 %Pleroma.Web.Websub.WebsubClientSubscription{
229 topic: "http://example.org",
230 secret: "here's a secret",
231 valid_until: nil,
232 state: "requested",
233 subscribers: []
234 }
235 end
236
237 def oauth_app_factory do
238 %Pleroma.Web.OAuth.App{
239 client_name: "Some client",
240 redirect_uris: "https://example.com/callback",
241 scopes: ["read", "write", "follow", "push"],
242 website: "https://example.com",
243 client_id: Ecto.UUID.generate(),
244 client_secret: "aaa;/&bbb"
245 }
246 end
247
248 def instance_factory do
249 %Pleroma.Instances.Instance{
250 host: "domain.com",
251 unreachable_since: nil
252 }
253 end
254
255 def oauth_token_factory do
256 oauth_app = insert(:oauth_app)
257
258 %Pleroma.Web.OAuth.Token{
259 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
260 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
261 user: build(:user),
262 app_id: oauth_app.id,
263 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
264 }
265 end
266
267 def oauth_authorization_factory do
268 %Pleroma.Web.OAuth.Authorization{
269 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
270 scopes: ["read", "write", "follow", "push"],
271 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
272 user: build(:user),
273 app: build(:oauth_app)
274 }
275 end
276
277 def push_subscription_factory do
278 %Pleroma.Web.Push.Subscription{
279 user: build(:user),
280 token: build(:oauth_token),
281 endpoint: "https://example.com/example/1234",
282 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
283 key_p256dh:
284 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
285 data: %{}
286 }
287 end
288
289 def notification_factory do
290 %Pleroma.Notification{
291 user: build(:user)
292 }
293 end
294
295 def scheduled_activity_factory do
296 %Pleroma.ScheduledActivity{
297 user: build(:user),
298 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
299 params: build(:note) |> Map.from_struct() |> Map.get(:data)
300 }
301 end
302
303 def registration_factory do
304 user = insert(:user)
305
306 %Pleroma.Registration{
307 user: user,
308 provider: "twitter",
309 uid: "171799000",
310 info: %{
311 "name" => "John Doe",
312 "email" => "john@doe.com",
313 "nickname" => "johndoe",
314 "description" => "My bio"
315 }
316 }
317 end
318
319 def config_factory do
320 %Pleroma.Web.AdminAPI.Config{
321 key: sequence(:key, &"some_key_#{&1}"),
322 group: "pleroma",
323 value:
324 sequence(
325 :value,
326 fn key ->
327 :erlang.term_to_binary(%{another_key: "#{key}somevalue", another: "#{key}somevalue"})
328 end
329 )
330 }
331 end
332 end