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
10 def participation_factory do
11 conversation = insert(:conversation)
14 %Pleroma.Conversation.Participation{
15 conversation: conversation,
21 def conversation_factory do
22 %Pleroma.Conversation{
23 ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}")
29 name: sequence(:name, &"Test テスト User #{&1}"),
30 email: sequence(:email, &"user#{&1}@example.com"),
31 nickname: sequence(:nickname, &"nick#{&1}"),
32 password_hash: Comeonin.Pbkdf2.hashpwsalt("test"),
33 bio: sequence(:bio, &"Tester Number #{&1}"),
39 | ap_id: User.ap_id(user),
40 follower_address: User.ap_followers(user),
41 following_address: User.ap_following(user),
42 following: [User.ap_id(user)]
46 def note_factory(attrs \\ %{}) do
47 text = sequence(:text, &"This is :moominmamma: note #{&1}")
49 user = attrs[:user] || insert(:user)
54 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
55 "actor" => user.ap_id,
56 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
57 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
64 "2hu" => "corndog.png"
69 data: merge_attributes(data, Map.get(attrs, :data, %{}))
73 def direct_note_factory do
76 %Pleroma.Object{data: data} = note_factory()
77 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
80 def article_factory do
82 |> Map.put("type", "Article")
85 def tombstone_factory do
87 "type" => "Tombstone",
88 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
89 "formerType" => "Note",
90 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
98 def direct_note_activity_factory do
99 dm = insert(:direct_note)
102 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
104 "actor" => dm.data["actor"],
105 "to" => dm.data["to"],
107 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
108 "context" => dm.data["context"]
113 actor: data["actor"],
114 recipients: data["to"]
118 def note_activity_factory(attrs \\ %{}) do
119 user = attrs[:user] || insert(:user)
120 note = attrs[:note] || insert(:note, user: user)
122 data_attrs = attrs[:data_attrs] || %{}
123 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
127 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
129 "actor" => note.data["actor"],
130 "to" => note.data["to"],
131 "object" => note.data["id"],
132 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
133 "context" => note.data["context"]
135 |> Map.merge(data_attrs)
139 actor: data["actor"],
140 recipients: data["to"]
145 def article_activity_factory do
146 article = insert(:article)
149 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
151 "actor" => article.data["actor"],
152 "to" => article.data["to"],
153 "object" => article.data,
154 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
155 "context" => article.data["context"]
160 actor: data["actor"],
161 recipients: data["to"]
165 def announce_activity_factory(attrs \\ %{}) do
166 note_activity = attrs[:note_activity] || insert(:note_activity)
167 user = attrs[:user] || insert(:user)
170 "type" => "Announce",
171 "actor" => note_activity.actor,
172 "object" => note_activity.data["id"],
173 "to" => [user.follower_address, note_activity.data["actor"]],
174 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
175 "context" => note_activity.data["context"]
181 recipients: data["to"]
185 def like_activity_factory do
186 note_activity = insert(:note_activity)
187 object = Object.normalize(note_activity)
191 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
192 "actor" => user.ap_id,
194 "object" => object.data["id"],
195 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
203 def follow_activity_factory do
204 follower = insert(:user)
205 followed = insert(:user)
208 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
209 "actor" => follower.ap_id,
211 "object" => followed.ap_id,
212 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
217 actor: follower.ap_id
221 def websub_subscription_factory do
222 %Pleroma.Web.Websub.WebsubServerSubscription{
223 topic: "http://example.org",
224 callback: "http://example.org/callback",
225 secret: "here's a secret",
226 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 100),
231 def websub_client_subscription_factory do
232 %Pleroma.Web.Websub.WebsubClientSubscription{
233 topic: "http://example.org",
234 secret: "here's a secret",
241 def oauth_app_factory do
242 %Pleroma.Web.OAuth.App{
243 client_name: "Some client",
244 redirect_uris: "https://example.com/callback",
245 scopes: ["read", "write", "follow", "push"],
246 website: "https://example.com",
247 client_id: Ecto.UUID.generate(),
248 client_secret: "aaa;/&bbb"
252 def instance_factory do
253 %Pleroma.Instances.Instance{
255 unreachable_since: nil
259 def oauth_token_factory do
260 oauth_app = insert(:oauth_app)
262 %Pleroma.Web.OAuth.Token{
263 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
264 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
266 app_id: oauth_app.id,
267 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
271 def oauth_authorization_factory do
272 %Pleroma.Web.OAuth.Authorization{
273 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
274 scopes: ["read", "write", "follow", "push"],
275 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
277 app: build(:oauth_app)
281 def push_subscription_factory do
282 %Pleroma.Web.Push.Subscription{
284 token: build(:oauth_token),
285 endpoint: "https://example.com/example/1234",
286 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
288 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
293 def notification_factory do
294 %Pleroma.Notification{
299 def scheduled_activity_factory do
300 %Pleroma.ScheduledActivity{
302 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
303 params: build(:note) |> Map.from_struct() |> Map.get(:data)
307 def registration_factory do
310 %Pleroma.Registration{
315 "name" => "John Doe",
316 "email" => "john@doe.com",
317 "nickname" => "johndoe",
318 "description" => "My bio"
323 def config_factory do
324 %Pleroma.Web.AdminAPI.Config{
325 key: sequence(:key, &"some_key_#{&1}"),
331 :erlang.term_to_binary(%{another_key: "#{key}somevalue", another: "#{key}somevalue"})