1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Factory do
6 use ExMachina.Ecto, repo: Pleroma.Repo
8 def participation_factory do
9 conversation = insert(:conversation)
12 %Pleroma.Conversation.Participation{
13 conversation: conversation,
19 def conversation_factory do
20 %Pleroma.Conversation{
21 ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}")
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}"),
37 | ap_id: Pleroma.User.ap_id(user),
38 follower_address: Pleroma.User.ap_followers(user),
39 following: [Pleroma.User.ap_id(user)]
43 def note_factory(attrs \\ %{}) do
44 text = sequence(:text, &"This is :moominmamma: note #{&1}")
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(),
61 "2hu" => "corndog.png"
66 data: merge_attributes(data, Map.get(attrs, :data, %{}))
70 def direct_note_factory do
73 %Pleroma.Object{data: data} = note_factory()
74 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
77 def article_factory do
79 |> Map.put("type", "Article")
82 def tombstone_factory do
84 "type" => "Tombstone",
85 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
86 "formerType" => "Note",
87 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
95 def direct_note_activity_factory do
96 dm = insert(:direct_note)
99 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
101 "actor" => dm.data["actor"],
102 "to" => dm.data["to"],
104 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
105 "context" => dm.data["context"]
110 actor: data["actor"],
111 recipients: data["to"]
115 def note_activity_factory(attrs \\ %{}) do
116 note = attrs[:note] || insert(:note)
119 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
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"]
130 actor: data["actor"],
131 recipients: data["to"]
135 def article_activity_factory do
136 article = insert(:article)
139 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
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"]
150 actor: data["actor"],
151 recipients: data["to"]
155 def announce_activity_factory(attrs \\ %{}) do
156 note_activity = attrs[:note_activity] || insert(:note_activity)
157 user = attrs[:user] || insert(:user)
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"]
171 recipients: data["to"]
175 def like_activity_factory do
176 note_activity = insert(:note_activity)
180 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
181 "actor" => user.ap_id,
183 "object" => note_activity.data["object"]["id"],
184 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
192 def follow_activity_factory do
193 follower = insert(:user)
194 followed = insert(:user)
197 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
198 "actor" => follower.ap_id,
200 "object" => followed.ap_id,
201 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
206 actor: follower.ap_id
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),
220 def websub_client_subscription_factory do
221 %Pleroma.Web.Websub.WebsubClientSubscription{
222 topic: "http://example.org",
223 secret: "here's a secret",
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"
241 def instance_factory do
242 %Pleroma.Instances.Instance{
244 unreachable_since: nil
248 def oauth_token_factory do
249 oauth_app = insert(:oauth_app)
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(),
255 app_id: oauth_app.id,
256 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
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),
266 app: build(:oauth_app)
270 def push_subscription_factory do
271 %Pleroma.Web.Push.Subscription{
273 token: build(:oauth_token),
274 endpoint: "https://example.com/example/1234",
275 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
277 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
282 def notification_factory do
283 %Pleroma.Notification{
288 def scheduled_activity_factory do
289 %Pleroma.ScheduledActivity{
291 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
292 params: build(:note) |> Map.from_struct() |> Map.get(:data)
296 def registration_factory do
299 %Pleroma.Registration{
304 "name" => "John Doe",
305 "email" => "john@doe.com",
306 "nickname" => "johndoe",
307 "description" => "My bio"