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