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
9 def participation_factory do
10 conversation = insert(:conversation)
13 %Pleroma.Conversation.Participation{
14 conversation: conversation,
20 def conversation_factory do
21 %Pleroma.Conversation{
22 ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}")
28 name: sequence(:name, &"Test テスト User #{&1}"),
29 email: sequence(:email, &"user#{&1}@example.com"),
30 nickname: sequence(:nickname, &"nick#{&1}"),
31 password_hash: Comeonin.Pbkdf2.hashpwsalt("test"),
32 bio: sequence(:bio, &"Tester Number #{&1}"),
34 last_digest_emailed_at: NaiveDateTime.utc_now()
39 | ap_id: User.ap_id(user),
40 follower_address: User.ap_followers(user),
41 following: [User.ap_id(user)]
45 def note_factory(attrs \\ %{}) do
46 text = sequence(:text, &"This is :moominmamma: note #{&1}")
48 user = attrs[:user] || insert(:user)
53 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
54 "actor" => user.ap_id,
55 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
56 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
63 "2hu" => "corndog.png"
68 data: merge_attributes(data, Map.get(attrs, :data, %{}))
72 def direct_note_factory do
75 %Pleroma.Object{data: data} = note_factory()
76 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
79 def article_factory do
81 |> Map.put("type", "Article")
84 def tombstone_factory do
86 "type" => "Tombstone",
87 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
88 "formerType" => "Note",
89 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
97 def direct_note_activity_factory do
98 dm = insert(:direct_note)
101 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
103 "actor" => dm.data["actor"],
104 "to" => dm.data["to"],
106 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
107 "context" => dm.data["context"]
112 actor: data["actor"],
113 recipients: data["to"]
117 def note_activity_factory(attrs \\ %{}) do
118 user = attrs[:user] || insert(:user)
119 note = attrs[:note] || insert(:note, user: user)
122 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
124 "actor" => note.data["actor"],
125 "to" => note.data["to"],
126 "object" => note.data,
127 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
128 "context" => note.data["context"]
133 actor: data["actor"],
134 recipients: data["to"]
138 def article_activity_factory do
139 article = insert(:article)
142 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
144 "actor" => article.data["actor"],
145 "to" => article.data["to"],
146 "object" => article.data,
147 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
148 "context" => article.data["context"]
153 actor: data["actor"],
154 recipients: data["to"]
158 def announce_activity_factory(attrs \\ %{}) do
159 note_activity = attrs[:note_activity] || insert(:note_activity)
160 user = attrs[:user] || insert(:user)
163 "type" => "Announce",
164 "actor" => note_activity.actor,
165 "object" => note_activity.data["id"],
166 "to" => [user.follower_address, note_activity.data["actor"]],
167 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
168 "context" => note_activity.data["context"]
174 recipients: data["to"]
178 def like_activity_factory do
179 note_activity = insert(:note_activity)
183 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
184 "actor" => user.ap_id,
186 "object" => note_activity.data["object"]["id"],
187 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
195 def follow_activity_factory do
196 follower = insert(:user)
197 followed = insert(:user)
200 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
201 "actor" => follower.ap_id,
203 "object" => followed.ap_id,
204 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
209 actor: follower.ap_id
213 def websub_subscription_factory do
214 %Pleroma.Web.Websub.WebsubServerSubscription{
215 topic: "http://example.org",
216 callback: "http://example.org/callback",
217 secret: "here's a secret",
218 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 100),
223 def websub_client_subscription_factory do
224 %Pleroma.Web.Websub.WebsubClientSubscription{
225 topic: "http://example.org",
226 secret: "here's a secret",
233 def oauth_app_factory do
234 %Pleroma.Web.OAuth.App{
235 client_name: "Some client",
236 redirect_uris: "https://example.com/callback",
237 scopes: ["read", "write", "follow", "push"],
238 website: "https://example.com",
239 client_id: Ecto.UUID.generate(),
240 client_secret: "aaa;/&bbb"
244 def instance_factory do
245 %Pleroma.Instances.Instance{
247 unreachable_since: nil
251 def oauth_token_factory do
252 oauth_app = insert(:oauth_app)
254 %Pleroma.Web.OAuth.Token{
255 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
256 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
258 app_id: oauth_app.id,
259 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
263 def oauth_authorization_factory do
264 %Pleroma.Web.OAuth.Authorization{
265 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
266 scopes: ["read", "write", "follow", "push"],
267 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
269 app: build(:oauth_app)
273 def push_subscription_factory do
274 %Pleroma.Web.Push.Subscription{
276 token: build(:oauth_token),
277 endpoint: "https://example.com/example/1234",
278 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
280 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
285 def notification_factory do
286 %Pleroma.Notification{
291 def scheduled_activity_factory do
292 %Pleroma.ScheduledActivity{
294 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
295 params: build(:note) |> Map.from_struct() |> Map.get(:data)
299 def registration_factory do
302 %Pleroma.Registration{
307 "name" => "John Doe",
308 "email" => "john@doe.com",
309 "nickname" => "johndoe",
310 "description" => "My bio"