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