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 name: sequence(:name, &"Test テスト User #{&1}"),
11 email: sequence(:email, &"user#{&1}@example.com"),
12 nickname: sequence(:nickname, &"nick#{&1}"),
13 password_hash: Comeonin.Pbkdf2.hashpwsalt("test"),
14 bio: sequence(:bio, &"Tester Number #{&1}"),
20 | ap_id: Pleroma.User.ap_id(user),
21 follower_address: Pleroma.User.ap_followers(user),
22 following: [Pleroma.User.ap_id(user)]
26 def scheduled_activity_factory do
27 %Pleroma.ScheduledActivity{
29 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
30 params: build(:note) |> Map.from_struct() |> Map.get(:data)
34 def note_factory(attrs \\ %{}) do
35 text = sequence(:text, &"This is :moominmamma: note #{&1}")
42 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
43 "actor" => user.ap_id,
44 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
45 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
52 "2hu" => "corndog.png"
57 data: merge_attributes(data, Map.get(attrs, :data, %{}))
61 def direct_note_factory do
64 %Pleroma.Object{data: data} = note_factory()
65 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
68 def article_factory do
70 |> Map.put("type", "Article")
73 def tombstone_factory do
75 "type" => "Tombstone",
76 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
77 "formerType" => "Note",
78 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
86 def direct_note_activity_factory do
87 dm = insert(:direct_note)
90 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
92 "actor" => dm.data["actor"],
93 "to" => dm.data["to"],
95 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
96 "context" => dm.data["context"]
101 actor: data["actor"],
102 recipients: data["to"]
106 def note_activity_factory(attrs \\ %{}) do
107 note = attrs[:note] || insert(:note)
110 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
112 "actor" => note.data["actor"],
113 "to" => note.data["to"],
114 "object" => note.data,
115 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
116 "context" => note.data["context"]
121 actor: data["actor"],
122 recipients: data["to"]
126 def article_activity_factory do
127 article = insert(:article)
130 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
132 "actor" => article.data["actor"],
133 "to" => article.data["to"],
134 "object" => article.data,
135 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
136 "context" => article.data["context"]
141 actor: data["actor"],
142 recipients: data["to"]
146 def announce_activity_factory(attrs \\ %{}) do
147 note_activity = attrs[:note_activity] || insert(:note_activity)
148 user = attrs[:user] || insert(:user)
151 "type" => "Announce",
152 "actor" => note_activity.actor,
153 "object" => note_activity.data["id"],
154 "to" => [user.follower_address, note_activity.data["actor"]],
155 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
156 "context" => note_activity.data["context"]
162 recipients: data["to"]
166 def like_activity_factory do
167 note_activity = insert(:note_activity)
171 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
172 "actor" => user.ap_id,
174 "object" => note_activity.data["object"]["id"],
175 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
183 def follow_activity_factory do
184 follower = insert(:user)
185 followed = insert(:user)
188 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
189 "actor" => follower.ap_id,
191 "object" => followed.ap_id,
192 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
197 actor: follower.ap_id
201 def websub_subscription_factory do
202 %Pleroma.Web.Websub.WebsubServerSubscription{
203 topic: "http://example.org",
204 callback: "http://example.org/callback",
205 secret: "here's a secret",
206 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 100),
211 def websub_client_subscription_factory do
212 %Pleroma.Web.Websub.WebsubClientSubscription{
213 topic: "http://example.org",
214 secret: "here's a secret",
221 def oauth_app_factory do
222 %Pleroma.Web.OAuth.App{
223 client_name: "Some client",
224 redirect_uris: "https://example.com/callback",
225 scopes: ["read", "write", "follow", "push"],
226 website: "https://example.com",
227 client_id: Ecto.UUID.generate(),
228 client_secret: "aaa;/&bbb"
232 def instance_factory do
233 %Pleroma.Instances.Instance{
235 unreachable_since: nil
239 def oauth_token_factory do
240 oauth_app = insert(:oauth_app)
242 %Pleroma.Web.OAuth.Token{
243 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
244 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
246 app_id: oauth_app.id,
247 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
251 def oauth_authorization_factory do
252 %Pleroma.Web.OAuth.Authorization{
253 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
254 scopes: ["read", "write", "follow", "push"],
255 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
257 app: build(:oauth_app)
261 def push_subscription_factory do
262 %Pleroma.Web.Push.Subscription{
264 token: build(:oauth_token),
265 endpoint: "https://example.com/example/1234",
266 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
268 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
273 def notification_factory do
274 %Pleroma.Notification{