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