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}"),
38 | ap_id: User.ap_id(user),
39 follower_address: User.ap_followers(user),
40 following: [User.ap_id(user)]
44 def note_factory(attrs \\ %{}) do
45 text = sequence(:text, &"This is :moominmamma: note #{&1}")
47 user = attrs[:user] || insert(:user)
52 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
53 "actor" => user.ap_id,
54 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
55 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
62 "2hu" => "corndog.png"
67 data: merge_attributes(data, Map.get(attrs, :data, %{}))
71 def direct_note_factory do
74 %Pleroma.Object{data: data} = note_factory()
75 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
78 def article_factory do
80 |> Map.put("type", "Article")
83 def tombstone_factory do
85 "type" => "Tombstone",
86 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
87 "formerType" => "Note",
88 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
96 def direct_note_activity_factory do
97 dm = insert(:direct_note)
100 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
102 "actor" => dm.data["actor"],
103 "to" => dm.data["to"],
105 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
106 "context" => dm.data["context"]
111 actor: data["actor"],
112 recipients: data["to"]
116 def note_activity_factory(attrs \\ %{}) do
117 user = attrs[:user] || insert(:user)
118 note = attrs[:note] || insert(:note, user: user)
121 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
123 "actor" => note.data["actor"],
124 "to" => note.data["to"],
125 "object" => note.data,
126 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
127 "context" => note.data["context"]
132 actor: data["actor"],
133 recipients: data["to"]
137 def article_activity_factory do
138 article = insert(:article)
141 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
143 "actor" => article.data["actor"],
144 "to" => article.data["to"],
145 "object" => article.data,
146 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
147 "context" => article.data["context"]
152 actor: data["actor"],
153 recipients: data["to"]
157 def announce_activity_factory(attrs \\ %{}) do
158 note_activity = attrs[:note_activity] || insert(:note_activity)
159 user = attrs[:user] || insert(:user)
162 "type" => "Announce",
163 "actor" => note_activity.actor,
164 "object" => note_activity.data["id"],
165 "to" => [user.follower_address, note_activity.data["actor"]],
166 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
167 "context" => note_activity.data["context"]
173 recipients: data["to"]
177 def like_activity_factory do
178 note_activity = insert(:note_activity)
182 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
183 "actor" => user.ap_id,
185 "object" => note_activity.data["object"]["id"],
186 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
194 def follow_activity_factory do
195 follower = insert(:user)
196 followed = insert(:user)
199 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
200 "actor" => follower.ap_id,
202 "object" => followed.ap_id,
203 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
208 actor: follower.ap_id
212 def websub_subscription_factory do
213 %Pleroma.Web.Websub.WebsubServerSubscription{
214 topic: "http://example.org",
215 callback: "http://example.org/callback",
216 secret: "here's a secret",
217 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 100),
222 def websub_client_subscription_factory do
223 %Pleroma.Web.Websub.WebsubClientSubscription{
224 topic: "http://example.org",
225 secret: "here's a secret",
232 def oauth_app_factory do
233 %Pleroma.Web.OAuth.App{
234 client_name: "Some client",
235 redirect_uris: "https://example.com/callback",
236 scopes: ["read", "write", "follow", "push"],
237 website: "https://example.com",
238 client_id: Ecto.UUID.generate(),
239 client_secret: "aaa;/&bbb"
243 def instance_factory do
244 %Pleroma.Instances.Instance{
246 unreachable_since: nil
250 def oauth_token_factory do
251 oauth_app = insert(:oauth_app)
253 %Pleroma.Web.OAuth.Token{
254 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
255 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
257 app_id: oauth_app.id,
258 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
262 def oauth_authorization_factory do
263 %Pleroma.Web.OAuth.Authorization{
264 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
265 scopes: ["read", "write", "follow", "push"],
266 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
268 app: build(:oauth_app)
272 def push_subscription_factory do
273 %Pleroma.Web.Push.Subscription{
275 token: build(:oauth_token),
276 endpoint: "https://example.com/example/1234",
277 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
279 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
284 def notification_factory do
285 %Pleroma.Notification{
290 def scheduled_activity_factory do
291 %Pleroma.ScheduledActivity{
293 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
294 params: build(:note) |> Map.from_struct() |> Map.get(:data)
298 def registration_factory do
301 %Pleroma.Registration{
306 "name" => "John Doe",
307 "email" => "john@doe.com",
308 "nickname" => "johndoe",
309 "description" => "My bio"
314 def config_factory do
315 %Pleroma.Web.AdminAPI.Config{
316 key: sequence(:key, &"some_key_#{&1}"),
322 :erlang.term_to_binary(%{another_key: "#{key}somevalue", another: "#{key}somevalue"})