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