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}")
46 user = attrs[:user] || insert(:user)
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 user = attrs[:user] || insert(:user)
117 note = attrs[:note] || insert(:note, user: user)
120 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
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"]
131 actor: data["actor"],
132 recipients: data["to"]
136 def article_activity_factory do
137 article = insert(:article)
140 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
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"]
151 actor: data["actor"],
152 recipients: data["to"]
156 def announce_activity_factory(attrs \\ %{}) do
157 note_activity = attrs[:note_activity] || insert(:note_activity)
158 user = attrs[:user] || insert(:user)
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"]
172 recipients: data["to"]
176 def like_activity_factory do
177 note_activity = insert(:note_activity)
181 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
182 "actor" => user.ap_id,
184 "object" => note_activity.data["object"]["id"],
185 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
193 def follow_activity_factory do
194 follower = insert(:user)
195 followed = insert(:user)
198 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
199 "actor" => follower.ap_id,
201 "object" => followed.ap_id,
202 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
207 actor: follower.ap_id
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),
221 def websub_client_subscription_factory do
222 %Pleroma.Web.Websub.WebsubClientSubscription{
223 topic: "http://example.org",
224 secret: "here's a secret",
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"
242 def instance_factory do
243 %Pleroma.Instances.Instance{
245 unreachable_since: nil
249 def oauth_token_factory do
250 oauth_app = insert(:oauth_app)
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(),
256 app_id: oauth_app.id,
257 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
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),
267 app: build(:oauth_app)
271 def push_subscription_factory do
272 %Pleroma.Web.Push.Subscription{
274 token: build(:oauth_token),
275 endpoint: "https://example.com/example/1234",
276 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
278 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
283 def notification_factory do
284 %Pleroma.Notification{
289 def scheduled_activity_factory do
290 %Pleroma.ScheduledActivity{
292 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
293 params: build(:note) |> Map.from_struct() |> Map.get(:data)
297 def registration_factory do
300 %Pleroma.Registration{
305 "name" => "John Doe",
306 "email" => "john@doe.com",
307 "nickname" => "johndoe",
308 "description" => "My bio"