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.

425 lines
11KB

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