Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

440 lines
11KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Factory do
  5. use ExMachina.Ecto, repo: Pleroma.Repo
  6. alias Pleroma.Object
  7. alias Pleroma.User
  8. def participation_factory do
  9. conversation = insert(:conversation)
  10. user = insert(:user)
  11. %Pleroma.Conversation.Participation{
  12. conversation: conversation,
  13. user: user,
  14. read: false
  15. }
  16. end
  17. def conversation_factory do
  18. %Pleroma.Conversation{
  19. ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}")
  20. }
  21. end
  22. def user_factory do
  23. user = %User{
  24. name: sequence(:name, &"Test テスト User #{&1}"),
  25. email: sequence(:email, &"user#{&1}@example.com"),
  26. nickname: sequence(:nickname, &"nick#{&1}"),
  27. password_hash: Pbkdf2.hash_pwd_salt("test"),
  28. bio: sequence(:bio, &"Tester Number #{&1}"),
  29. last_digest_emailed_at: NaiveDateTime.utc_now(),
  30. last_refreshed_at: NaiveDateTime.utc_now(),
  31. notification_settings: %Pleroma.User.NotificationSetting{},
  32. multi_factor_authentication_settings: %Pleroma.MFA.Settings{},
  33. ap_enabled: true
  34. }
  35. %{
  36. user
  37. | ap_id: User.ap_id(user),
  38. follower_address: User.ap_followers(user),
  39. following_address: User.ap_following(user),
  40. raw_bio: user.bio
  41. }
  42. end
  43. def user_relationship_factory(attrs \\ %{}) do
  44. source = attrs[:source] || insert(:user)
  45. target = attrs[:target] || insert(:user)
  46. relationship_type = attrs[:relationship_type] || :block
  47. %Pleroma.UserRelationship{
  48. source_id: source.id,
  49. target_id: target.id,
  50. relationship_type: relationship_type
  51. }
  52. end
  53. def note_factory(attrs \\ %{}) do
  54. text = sequence(:text, &"This is :moominmamma: note #{&1}")
  55. user = attrs[:user] || insert(:user)
  56. data = %{
  57. "type" => "Note",
  58. "content" => text,
  59. "source" => text,
  60. "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
  61. "actor" => user.ap_id,
  62. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  63. "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
  64. "likes" => [],
  65. "like_count" => 0,
  66. "context" => "2hu",
  67. "summary" => "2hu",
  68. "tag" => ["2hu"],
  69. "emoji" => %{
  70. "2hu" => "corndog.png"
  71. }
  72. }
  73. %Pleroma.Object{
  74. data: merge_attributes(data, Map.get(attrs, :data, %{}))
  75. }
  76. end
  77. def audio_factory(attrs \\ %{}) do
  78. text = sequence(:text, &"lain radio episode #{&1}")
  79. user = attrs[:user] || insert(:user)
  80. data = %{
  81. "type" => "Audio",
  82. "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
  83. "artist" => "lain",
  84. "title" => text,
  85. "album" => "lain radio",
  86. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  87. "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
  88. "actor" => user.ap_id,
  89. "length" => 180_000
  90. }
  91. %Pleroma.Object{
  92. data: merge_attributes(data, Map.get(attrs, :data, %{}))
  93. }
  94. end
  95. def listen_factory do
  96. audio = insert(:audio)
  97. data = %{
  98. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  99. "type" => "Listen",
  100. "actor" => audio.data["actor"],
  101. "to" => audio.data["to"],
  102. "object" => audio.data,
  103. "published" => audio.data["published"]
  104. }
  105. %Pleroma.Activity{
  106. data: data,
  107. actor: data["actor"],
  108. recipients: data["to"]
  109. }
  110. end
  111. def direct_note_factory do
  112. user2 = insert(:user)
  113. %Pleroma.Object{data: data} = note_factory()
  114. %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
  115. end
  116. def article_factory do
  117. note_factory()
  118. |> Map.put("type", "Article")
  119. end
  120. def tombstone_factory do
  121. data = %{
  122. "type" => "Tombstone",
  123. "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
  124. "formerType" => "Note",
  125. "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
  126. }
  127. %Pleroma.Object{
  128. data: data
  129. }
  130. end
  131. def direct_note_activity_factory do
  132. dm = insert(:direct_note)
  133. data = %{
  134. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  135. "type" => "Create",
  136. "actor" => dm.data["actor"],
  137. "to" => dm.data["to"],
  138. "object" => dm.data,
  139. "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
  140. "context" => dm.data["context"]
  141. }
  142. %Pleroma.Activity{
  143. data: data,
  144. actor: data["actor"],
  145. recipients: data["to"]
  146. }
  147. end
  148. def note_activity_factory(attrs \\ %{}) do
  149. user = attrs[:user] || insert(:user)
  150. note = attrs[:note] || insert(:note, user: user)
  151. data_attrs = attrs[:data_attrs] || %{}
  152. attrs = Map.drop(attrs, [:user, :note, :data_attrs])
  153. data =
  154. %{
  155. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  156. "type" => "Create",
  157. "actor" => note.data["actor"],
  158. "to" => note.data["to"],
  159. "object" => note.data["id"],
  160. "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
  161. "context" => note.data["context"]
  162. }
  163. |> Map.merge(data_attrs)
  164. %Pleroma.Activity{
  165. data: data,
  166. actor: data["actor"],
  167. recipients: data["to"]
  168. }
  169. |> Map.merge(attrs)
  170. end
  171. defp expiration_offset_by_minutes(attrs, minutes) do
  172. scheduled_at =
  173. NaiveDateTime.utc_now()
  174. |> NaiveDateTime.add(:timer.minutes(minutes), :millisecond)
  175. |> NaiveDateTime.truncate(:second)
  176. %Pleroma.ActivityExpiration{}
  177. |> Map.merge(attrs)
  178. |> Map.put(:scheduled_at, scheduled_at)
  179. end
  180. def expiration_in_the_past_factory(attrs \\ %{}) do
  181. expiration_offset_by_minutes(attrs, -60)
  182. end
  183. def expiration_in_the_future_factory(attrs \\ %{}) do
  184. expiration_offset_by_minutes(attrs, 61)
  185. end
  186. def article_activity_factory do
  187. article = insert(:article)
  188. data = %{
  189. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  190. "type" => "Create",
  191. "actor" => article.data["actor"],
  192. "to" => article.data["to"],
  193. "object" => article.data,
  194. "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
  195. "context" => article.data["context"]
  196. }
  197. %Pleroma.Activity{
  198. data: data,
  199. actor: data["actor"],
  200. recipients: data["to"]
  201. }
  202. end
  203. def announce_activity_factory(attrs \\ %{}) do
  204. note_activity = attrs[:note_activity] || insert(:note_activity)
  205. user = attrs[:user] || insert(:user)
  206. data = %{
  207. "type" => "Announce",
  208. "actor" => note_activity.actor,
  209. "object" => note_activity.data["id"],
  210. "to" => [user.follower_address, note_activity.data["actor"]],
  211. "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
  212. "context" => note_activity.data["context"]
  213. }
  214. %Pleroma.Activity{
  215. data: data,
  216. actor: user.ap_id,
  217. recipients: data["to"]
  218. }
  219. end
  220. def like_activity_factory(attrs \\ %{}) do
  221. note_activity = attrs[:note_activity] || insert(:note_activity)
  222. object = Object.normalize(note_activity)
  223. user = insert(:user)
  224. data =
  225. %{
  226. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  227. "actor" => user.ap_id,
  228. "type" => "Like",
  229. "object" => object.data["id"],
  230. "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
  231. }
  232. |> Map.merge(attrs[:data_attrs] || %{})
  233. %Pleroma.Activity{
  234. data: data
  235. }
  236. end
  237. def follow_activity_factory do
  238. follower = insert(:user)
  239. followed = insert(:user)
  240. data = %{
  241. "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
  242. "actor" => follower.ap_id,
  243. "type" => "Follow",
  244. "object" => followed.ap_id,
  245. "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
  246. }
  247. %Pleroma.Activity{
  248. data: data,
  249. actor: follower.ap_id
  250. }
  251. end
  252. def oauth_app_factory do
  253. %Pleroma.Web.OAuth.App{
  254. client_name: sequence(:client_name, &"Some client #{&1}"),
  255. redirect_uris: "https://example.com/callback",
  256. scopes: ["read", "write", "follow", "push", "admin"],
  257. website: "https://example.com",
  258. client_id: Ecto.UUID.generate(),
  259. client_secret: "aaa;/&bbb"
  260. }
  261. end
  262. def instance_factory do
  263. %Pleroma.Instances.Instance{
  264. host: "domain.com",
  265. unreachable_since: nil
  266. }
  267. end
  268. def oauth_token_factory(attrs \\ %{}) do
  269. scopes = Map.get(attrs, :scopes, ["read"])
  270. oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
  271. user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
  272. valid_until =
  273. Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
  274. %Pleroma.Web.OAuth.Token{
  275. token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
  276. refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
  277. scopes: scopes,
  278. user: user,
  279. app: oauth_app,
  280. valid_until: valid_until
  281. }
  282. end
  283. def oauth_admin_token_factory(attrs \\ %{}) do
  284. user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
  285. scopes =
  286. attrs
  287. |> Map.get(:scopes, ["admin"])
  288. |> Kernel.++(["admin"])
  289. |> Enum.uniq()
  290. attrs = Map.merge(attrs, %{user: user, scopes: scopes})
  291. oauth_token_factory(attrs)
  292. end
  293. def oauth_authorization_factory do
  294. %Pleroma.Web.OAuth.Authorization{
  295. token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
  296. scopes: ["read", "write", "follow", "push"],
  297. valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
  298. user: build(:user),
  299. app: build(:oauth_app)
  300. }
  301. end
  302. def push_subscription_factory do
  303. %Pleroma.Web.Push.Subscription{
  304. user: build(:user),
  305. token: build(:oauth_token),
  306. endpoint: "https://example.com/example/1234",
  307. key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
  308. key_p256dh:
  309. "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
  310. data: %{}
  311. }
  312. end
  313. def notification_factory do
  314. %Pleroma.Notification{
  315. user: build(:user)
  316. }
  317. end
  318. def scheduled_activity_factory do
  319. %Pleroma.ScheduledActivity{
  320. user: build(:user),
  321. scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
  322. params: build(:note) |> Map.from_struct() |> Map.get(:data)
  323. }
  324. end
  325. def registration_factory do
  326. user = insert(:user)
  327. %Pleroma.Registration{
  328. user: user,
  329. provider: "twitter",
  330. uid: "171799000",
  331. info: %{
  332. "name" => "John Doe",
  333. "email" => "john@doe.com",
  334. "nickname" => "johndoe",
  335. "description" => "My bio"
  336. }
  337. }
  338. end
  339. def config_factory(attrs \\ %{}) do
  340. %Pleroma.ConfigDB{
  341. key: sequence(:key, &String.to_atom("some_key_#{&1}")),
  342. group: :pleroma,
  343. value:
  344. sequence(
  345. :value,
  346. &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
  347. )
  348. }
  349. |> merge_attributes(attrs)
  350. end
  351. def marker_factory do
  352. %Pleroma.Marker{
  353. user: build(:user),
  354. timeline: "notifications",
  355. lock_version: 0,
  356. last_read_id: "1"
  357. }
  358. end
  359. def mfa_token_factory do
  360. %Pleroma.MFA.Token{
  361. token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
  362. authorization: build(:oauth_authorization),
  363. valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
  364. user: build(:user)
  365. }
  366. end
  367. def filter_factory do
  368. %Pleroma.Filter{
  369. user: build(:user),
  370. filter_id: sequence(:filter_id, & &1),
  371. phrase: "cofe"
  372. }
  373. end
  374. end