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}"),
35 last_digest_emailed_at: NaiveDateTime.utc_now()
40 | ap_id: User.ap_id(user),
41 follower_address: User.ap_followers(user),
42 following_address: User.ap_following(user),
43 following: [User.ap_id(user)]
47 def note_factory(attrs \\ %{}) do
48 text = sequence(:text, &"This is :moominmamma: note #{&1}")
50 user = attrs[:user] || insert(:user)
55 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
56 "actor" => user.ap_id,
57 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
58 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
65 "2hu" => "corndog.png"
70 data: merge_attributes(data, Map.get(attrs, :data, %{}))
74 def direct_note_factory do
77 %Pleroma.Object{data: data} = note_factory()
78 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
81 def article_factory do
83 |> Map.put("type", "Article")
86 def tombstone_factory do
88 "type" => "Tombstone",
89 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
90 "formerType" => "Note",
91 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
99 def direct_note_activity_factory do
100 dm = insert(:direct_note)
103 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
105 "actor" => dm.data["actor"],
106 "to" => dm.data["to"],
108 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
109 "context" => dm.data["context"]
114 actor: data["actor"],
115 recipients: data["to"]
119 def note_activity_factory(attrs \\ %{}) do
120 user = attrs[:user] || insert(:user)
121 note = attrs[:note] || insert(:note, user: user)
123 data_attrs = attrs[:data_attrs] || %{}
124 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
128 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
130 "actor" => note.data["actor"],
131 "to" => note.data["to"],
132 "object" => note.data["id"],
133 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
134 "context" => note.data["context"]
136 |> Map.merge(data_attrs)
140 actor: data["actor"],
141 recipients: data["to"]
146 def article_activity_factory do
147 article = insert(:article)
150 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
152 "actor" => article.data["actor"],
153 "to" => article.data["to"],
154 "object" => article.data,
155 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
156 "context" => article.data["context"]
161 actor: data["actor"],
162 recipients: data["to"]
166 def announce_activity_factory(attrs \\ %{}) do
167 note_activity = attrs[:note_activity] || insert(:note_activity)
168 user = attrs[:user] || insert(:user)
171 "type" => "Announce",
172 "actor" => note_activity.actor,
173 "object" => note_activity.data["id"],
174 "to" => [user.follower_address, note_activity.data["actor"]],
175 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
176 "context" => note_activity.data["context"]
182 recipients: data["to"]
186 def like_activity_factory(attrs \\ %{}) do
187 note_activity = attrs[:note_activity] || insert(:note_activity)
188 object = Object.normalize(note_activity)
192 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
193 "actor" => user.ap_id,
195 "object" => object.data["id"],
196 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
204 def follow_activity_factory do
205 follower = insert(:user)
206 followed = insert(:user)
209 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
210 "actor" => follower.ap_id,
212 "object" => followed.ap_id,
213 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
218 actor: follower.ap_id
222 def websub_subscription_factory do
223 %Pleroma.Web.Websub.WebsubServerSubscription{
224 topic: "http://example.org",
225 callback: "http://example.org/callback",
226 secret: "here's a secret",
227 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 100),
232 def websub_client_subscription_factory do
233 %Pleroma.Web.Websub.WebsubClientSubscription{
234 topic: "http://example.org",
235 secret: "here's a secret",
242 def oauth_app_factory do
243 %Pleroma.Web.OAuth.App{
244 client_name: "Some client",
245 redirect_uris: "https://example.com/callback",
246 scopes: ["read", "write", "follow", "push"],
247 website: "https://example.com",
248 client_id: Ecto.UUID.generate(),
249 client_secret: "aaa;/&bbb"
253 def instance_factory do
254 %Pleroma.Instances.Instance{
256 unreachable_since: nil
260 def oauth_token_factory do
261 oauth_app = insert(:oauth_app)
263 %Pleroma.Web.OAuth.Token{
264 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
265 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
267 app_id: oauth_app.id,
268 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
272 def oauth_authorization_factory do
273 %Pleroma.Web.OAuth.Authorization{
274 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
275 scopes: ["read", "write", "follow", "push"],
276 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
278 app: build(:oauth_app)
282 def push_subscription_factory do
283 %Pleroma.Web.Push.Subscription{
285 token: build(:oauth_token),
286 endpoint: "https://example.com/example/1234",
287 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
289 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
294 def notification_factory do
295 %Pleroma.Notification{
300 def scheduled_activity_factory do
301 %Pleroma.ScheduledActivity{
303 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
304 params: build(:note) |> Map.from_struct() |> Map.get(:data)
308 def registration_factory do
311 %Pleroma.Registration{
316 "name" => "John Doe",
317 "email" => "john@doe.com",
318 "nickname" => "johndoe",
319 "description" => "My bio"
324 def config_factory do
325 %Pleroma.Web.AdminAPI.Config{
326 key: sequence(:key, &"some_key_#{&1}"),
332 :erlang.term_to_binary(%{another_key: "#{key}somevalue", another: "#{key}somevalue"})