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}"),
16 last_digest_emailed_at: NaiveDateTime.utc_now()
21 | ap_id: Pleroma.User.ap_id(user),
22 follower_address: Pleroma.User.ap_followers(user),
23 following: [Pleroma.User.ap_id(user)]
27 def note_factory(attrs \\ %{}) do
28 text = sequence(:text, &"This is :moominmamma: note #{&1}")
35 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
36 "actor" => user.ap_id,
37 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
38 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
45 "2hu" => "corndog.png"
50 data: merge_attributes(data, Map.get(attrs, :data, %{}))
54 def direct_note_factory do
57 %Pleroma.Object{data: data} = note_factory()
58 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
61 def article_factory do
63 |> Map.put("type", "Article")
66 def tombstone_factory do
68 "type" => "Tombstone",
69 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
70 "formerType" => "Note",
71 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
79 def direct_note_activity_factory do
80 dm = insert(:direct_note)
83 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
85 "actor" => dm.data["actor"],
86 "to" => dm.data["to"],
88 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
89 "context" => dm.data["context"]
95 recipients: data["to"]
99 def note_activity_factory(attrs \\ %{}) do
100 note = attrs[:note] || insert(:note)
103 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
105 "actor" => note.data["actor"],
106 "to" => note.data["to"],
107 "object" => note.data,
108 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
109 "context" => note.data["context"]
114 actor: data["actor"],
115 recipients: data["to"]
119 def article_activity_factory do
120 article = insert(:article)
123 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
125 "actor" => article.data["actor"],
126 "to" => article.data["to"],
127 "object" => article.data,
128 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
129 "context" => article.data["context"]
134 actor: data["actor"],
135 recipients: data["to"]
139 def announce_activity_factory(attrs \\ %{}) do
140 note_activity = attrs[:note_activity] || insert(:note_activity)
141 user = attrs[:user] || insert(:user)
144 "type" => "Announce",
145 "actor" => note_activity.actor,
146 "object" => note_activity.data["id"],
147 "to" => [user.follower_address, note_activity.data["actor"]],
148 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
149 "context" => note_activity.data["context"]
155 recipients: data["to"]
159 def like_activity_factory do
160 note_activity = insert(:note_activity)
164 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
165 "actor" => user.ap_id,
167 "object" => note_activity.data["object"]["id"],
168 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
176 def follow_activity_factory do
177 follower = insert(:user)
178 followed = insert(:user)
181 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
182 "actor" => follower.ap_id,
184 "object" => followed.ap_id,
185 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
190 actor: follower.ap_id
194 def websub_subscription_factory do
195 %Pleroma.Web.Websub.WebsubServerSubscription{
196 topic: "http://example.org",
197 callback: "http://example.org/callback",
198 secret: "here's a secret",
199 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 100),
204 def websub_client_subscription_factory do
205 %Pleroma.Web.Websub.WebsubClientSubscription{
206 topic: "http://example.org",
207 secret: "here's a secret",
214 def oauth_app_factory do
215 %Pleroma.Web.OAuth.App{
216 client_name: "Some client",
217 redirect_uris: "https://example.com/callback",
218 scopes: ["read", "write", "follow", "push"],
219 website: "https://example.com",
220 client_id: Ecto.UUID.generate(),
221 client_secret: "aaa;/&bbb"
225 def instance_factory do
226 %Pleroma.Instances.Instance{
228 unreachable_since: nil
232 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)
244 def oauth_authorization_factory do
245 %Pleroma.Web.OAuth.Authorization{
246 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
247 scopes: ["read", "write", "follow", "push"],
248 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
250 app: build(:oauth_app)
254 def push_subscription_factory do
255 %Pleroma.Web.Push.Subscription{
257 token: build(:oauth_token),
258 endpoint: "https://example.com/example/1234",
259 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
261 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
266 def notification_factory do
267 %Pleroma.Notification{
272 def scheduled_activity_factory do
273 %Pleroma.ScheduledActivity{
275 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
276 params: build(:note) |> Map.from_struct() |> Map.get(:data)
280 def registration_factory do
283 %Pleroma.Registration{
288 "name" => "John Doe",
289 "email" => "john@doe.com",
290 "nickname" => "johndoe",
291 "description" => "My bio"