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: Ecto.UUID.generate(),
220 client_secret: "aaa;/&bbb"
224 def instance_factory do
225 %Pleroma.Instances.Instance{
227 unreachable_since: nil
231 def oauth_token_factory do
232 oauth_app = insert(:oauth_app)
234 %Pleroma.Web.OAuth.Token{
235 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
236 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
238 app_id: oauth_app.id,
239 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
243 def oauth_authorization_factory do
244 %Pleroma.Web.OAuth.Authorization{
245 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
246 scopes: ["read", "write", "follow", "push"],
247 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
249 app: build(:oauth_app)
253 def push_subscription_factory do
254 %Pleroma.Web.Push.Subscription{
256 token: build(:oauth_token),
257 endpoint: "https://example.com/example/1234",
258 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
260 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
265 def notification_factory do
266 %Pleroma.Notification{