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