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 note_factory(attrs \\ %{}) do
27 text = sequence(:text, &"This is :moominmamma: note #{&1}")
34 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
35 "actor" => user.ap_id,
36 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
37 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
44 "2hu" => "corndog.png"
49 data: merge_attributes(data, Map.get(attrs, :data, %{}))
53 def direct_note_factory do
56 %Pleroma.Object{data: data} = note_factory()
57 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
60 def article_factory do
62 |> Map.put("type", "Article")
65 def tombstone_factory do
67 "type" => "Tombstone",
68 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
69 "formerType" => "Note",
70 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
78 def direct_note_activity_factory do
79 dm = insert(:direct_note)
82 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
84 "actor" => dm.data["actor"],
85 "to" => dm.data["to"],
87 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
88 "context" => dm.data["context"]
94 recipients: data["to"]
98 def note_activity_factory(attrs \\ %{}) do
99 note = attrs[:note] || insert(:note)
102 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
104 "actor" => note.data["actor"],
105 "to" => note.data["to"],
106 "object" => note.data,
107 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
108 "context" => note.data["context"]
113 actor: data["actor"],
114 recipients: data["to"]
118 def article_activity_factory do
119 article = insert(:article)
122 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
124 "actor" => article.data["actor"],
125 "to" => article.data["to"],
126 "object" => article.data,
127 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
128 "context" => article.data["context"]
133 actor: data["actor"],
134 recipients: data["to"]
138 def announce_activity_factory(attrs \\ %{}) do
139 note_activity = attrs[:note_activity] || insert(:note_activity)
140 user = attrs[:user] || insert(:user)
143 "type" => "Announce",
144 "actor" => note_activity.actor,
145 "object" => note_activity.data["id"],
146 "to" => [user.follower_address, note_activity.data["actor"]],
147 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
148 "context" => note_activity.data["context"]
154 recipients: data["to"]
158 def like_activity_factory do
159 note_activity = insert(:note_activity)
163 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
164 "actor" => user.ap_id,
166 "object" => note_activity.data["object"]["id"],
167 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
175 def follow_activity_factory do
176 follower = insert(:user)
177 followed = insert(:user)
180 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
181 "actor" => follower.ap_id,
183 "object" => followed.ap_id,
184 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
189 actor: follower.ap_id
193 def websub_subscription_factory do
194 %Pleroma.Web.Websub.WebsubServerSubscription{
195 topic: "http://example.org",
196 callback: "http://example.org/callback",
197 secret: "here's a secret",
198 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 100),
203 def websub_client_subscription_factory do
204 %Pleroma.Web.Websub.WebsubClientSubscription{
205 topic: "http://example.org",
206 secret: "here's a secret",
213 def oauth_app_factory do
214 %Pleroma.Web.OAuth.App{
215 client_name: "Some client",
216 redirect_uris: "https://example.com/callback",
217 scopes: ["read", "write", "follow", "push"],
218 website: "https://example.com",
219 client_id: "aaabbb==",
220 client_secret: "aaa;/&bbb"
224 def instance_factory do
225 %Pleroma.Instances.Instance{
227 unreachable_since: nil
231 def oauth_token_factory do
233 oauth_app = insert(:oauth_app)
235 %Pleroma.Web.OAuth.Token{
236 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
237 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
239 app_id: oauth_app.id,
240 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)