logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

twitter_api_test.exs (12382B)


  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.Web.TwitterAPI.TwitterAPITest do
  5. use Pleroma.DataCase
  6. import Pleroma.Factory
  7. alias Pleroma.Repo
  8. alias Pleroma.Tests.ObanHelpers
  9. alias Pleroma.User
  10. alias Pleroma.UserInviteToken
  11. alias Pleroma.Web.TwitterAPI.TwitterAPI
  12. setup_all do
  13. Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
  14. :ok
  15. end
  16. test "it registers a new user and returns the user." do
  17. data = %{
  18. :username => "lain",
  19. :email => "lain@wired.jp",
  20. :fullname => "lain iwakura",
  21. :password => "bear",
  22. :confirm => "bear"
  23. }
  24. {:ok, user} = TwitterAPI.register_user(data)
  25. assert user == User.get_cached_by_nickname("lain")
  26. end
  27. test "it registers a new user with empty string in bio and returns the user" do
  28. data = %{
  29. :username => "lain",
  30. :email => "lain@wired.jp",
  31. :fullname => "lain iwakura",
  32. :bio => "",
  33. :password => "bear",
  34. :confirm => "bear"
  35. }
  36. {:ok, user} = TwitterAPI.register_user(data)
  37. assert user == User.get_cached_by_nickname("lain")
  38. end
  39. test "it sends confirmation email if :account_activation_required is specified in instance config" do
  40. setting = Pleroma.Config.get([:instance, :account_activation_required])
  41. unless setting do
  42. Pleroma.Config.put([:instance, :account_activation_required], true)
  43. on_exit(fn -> Pleroma.Config.put([:instance, :account_activation_required], setting) end)
  44. end
  45. data = %{
  46. :username => "lain",
  47. :email => "lain@wired.jp",
  48. :fullname => "lain iwakura",
  49. :bio => "",
  50. :password => "bear",
  51. :confirm => "bear"
  52. }
  53. {:ok, user} = TwitterAPI.register_user(data)
  54. ObanHelpers.perform_all()
  55. assert user.confirmation_pending
  56. email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
  57. notify_email = Pleroma.Config.get([:instance, :notify_email])
  58. instance_name = Pleroma.Config.get([:instance, :name])
  59. Swoosh.TestAssertions.assert_email_sent(
  60. from: {instance_name, notify_email},
  61. to: {user.name, user.email},
  62. html_body: email.html_body
  63. )
  64. end
  65. test "it sends an admin email if :account_approval_required is specified in instance config" do
  66. admin = insert(:user, is_admin: true)
  67. setting = Pleroma.Config.get([:instance, :account_approval_required])
  68. unless setting do
  69. Pleroma.Config.put([:instance, :account_approval_required], true)
  70. on_exit(fn -> Pleroma.Config.put([:instance, :account_approval_required], setting) end)
  71. end
  72. data = %{
  73. :username => "lain",
  74. :email => "lain@wired.jp",
  75. :fullname => "lain iwakura",
  76. :bio => "",
  77. :password => "bear",
  78. :confirm => "bear",
  79. :reason => "I love anime"
  80. }
  81. {:ok, user} = TwitterAPI.register_user(data)
  82. ObanHelpers.perform_all()
  83. assert user.approval_pending
  84. email = Pleroma.Emails.AdminEmail.new_unapproved_registration(admin, user)
  85. notify_email = Pleroma.Config.get([:instance, :notify_email])
  86. instance_name = Pleroma.Config.get([:instance, :name])
  87. Swoosh.TestAssertions.assert_email_sent(
  88. from: {instance_name, notify_email},
  89. to: {admin.name, admin.email},
  90. html_body: email.html_body
  91. )
  92. end
  93. test "it registers a new user and parses mentions in the bio" do
  94. data1 = %{
  95. :username => "john",
  96. :email => "john@gmail.com",
  97. :fullname => "John Doe",
  98. :bio => "test",
  99. :password => "bear",
  100. :confirm => "bear"
  101. }
  102. {:ok, user1} = TwitterAPI.register_user(data1)
  103. data2 = %{
  104. :username => "lain",
  105. :email => "lain@wired.jp",
  106. :fullname => "lain iwakura",
  107. :bio => "@john test",
  108. :password => "bear",
  109. :confirm => "bear"
  110. }
  111. {:ok, user2} = TwitterAPI.register_user(data2)
  112. expected_text =
  113. ~s(<span class="h-card"><a class="u-url mention" data-user="#{user1.id}" href="#{
  114. user1.ap_id
  115. }" rel="ugc">@<span>john</span></a></span> test)
  116. assert user2.bio == expected_text
  117. end
  118. describe "register with one time token" do
  119. setup do: clear_config([:instance, :registrations_open], false)
  120. test "returns user on success" do
  121. {:ok, invite} = UserInviteToken.create_invite()
  122. data = %{
  123. :username => "vinny",
  124. :email => "pasta@pizza.vs",
  125. :fullname => "Vinny Vinesauce",
  126. :bio => "streamer",
  127. :password => "hiptofbees",
  128. :confirm => "hiptofbees",
  129. :token => invite.token
  130. }
  131. {:ok, user} = TwitterAPI.register_user(data)
  132. assert user == User.get_cached_by_nickname("vinny")
  133. invite = Repo.get_by(UserInviteToken, token: invite.token)
  134. assert invite.used == true
  135. end
  136. test "returns error on invalid token" do
  137. data = %{
  138. :username => "GrimReaper",
  139. :email => "death@reapers.afterlife",
  140. :fullname => "Reaper Grim",
  141. :bio => "Your time has come",
  142. :password => "scythe",
  143. :confirm => "scythe",
  144. :token => "DudeLetMeInImAFairy"
  145. }
  146. {:error, msg} = TwitterAPI.register_user(data)
  147. assert msg == "Invalid token"
  148. refute User.get_cached_by_nickname("GrimReaper")
  149. end
  150. test "returns error on expired token" do
  151. {:ok, invite} = UserInviteToken.create_invite()
  152. UserInviteToken.update_invite!(invite, used: true)
  153. data = %{
  154. :username => "GrimReaper",
  155. :email => "death@reapers.afterlife",
  156. :fullname => "Reaper Grim",
  157. :bio => "Your time has come",
  158. :password => "scythe",
  159. :confirm => "scythe",
  160. :token => invite.token
  161. }
  162. {:error, msg} = TwitterAPI.register_user(data)
  163. assert msg == "Expired token"
  164. refute User.get_cached_by_nickname("GrimReaper")
  165. end
  166. end
  167. describe "registers with date limited token" do
  168. setup do: clear_config([:instance, :registrations_open], false)
  169. setup do
  170. data = %{
  171. :username => "vinny",
  172. :email => "pasta@pizza.vs",
  173. :fullname => "Vinny Vinesauce",
  174. :bio => "streamer",
  175. :password => "hiptofbees",
  176. :confirm => "hiptofbees"
  177. }
  178. check_fn = fn invite ->
  179. data = Map.put(data, :token, invite.token)
  180. {:ok, user} = TwitterAPI.register_user(data)
  181. assert user == User.get_cached_by_nickname("vinny")
  182. end
  183. {:ok, data: data, check_fn: check_fn}
  184. end
  185. test "returns user on success", %{check_fn: check_fn} do
  186. {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today()})
  187. check_fn.(invite)
  188. invite = Repo.get_by(UserInviteToken, token: invite.token)
  189. refute invite.used
  190. end
  191. test "returns user on token which expired tomorrow", %{check_fn: check_fn} do
  192. {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), 1)})
  193. check_fn.(invite)
  194. invite = Repo.get_by(UserInviteToken, token: invite.token)
  195. refute invite.used
  196. end
  197. test "returns an error on overdue date", %{data: data} do
  198. {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1)})
  199. data = Map.put(data, "token", invite.token)
  200. {:error, msg} = TwitterAPI.register_user(data)
  201. assert msg == "Expired token"
  202. refute User.get_cached_by_nickname("vinny")
  203. invite = Repo.get_by(UserInviteToken, token: invite.token)
  204. refute invite.used
  205. end
  206. end
  207. describe "registers with reusable token" do
  208. setup do: clear_config([:instance, :registrations_open], false)
  209. test "returns user on success, after him registration fails" do
  210. {:ok, invite} = UserInviteToken.create_invite(%{max_use: 100})
  211. UserInviteToken.update_invite!(invite, uses: 99)
  212. data = %{
  213. :username => "vinny",
  214. :email => "pasta@pizza.vs",
  215. :fullname => "Vinny Vinesauce",
  216. :bio => "streamer",
  217. :password => "hiptofbees",
  218. :confirm => "hiptofbees",
  219. :token => invite.token
  220. }
  221. {:ok, user} = TwitterAPI.register_user(data)
  222. assert user == User.get_cached_by_nickname("vinny")
  223. invite = Repo.get_by(UserInviteToken, token: invite.token)
  224. assert invite.used == true
  225. data = %{
  226. :username => "GrimReaper",
  227. :email => "death@reapers.afterlife",
  228. :fullname => "Reaper Grim",
  229. :bio => "Your time has come",
  230. :password => "scythe",
  231. :confirm => "scythe",
  232. :token => invite.token
  233. }
  234. {:error, msg} = TwitterAPI.register_user(data)
  235. assert msg == "Expired token"
  236. refute User.get_cached_by_nickname("GrimReaper")
  237. end
  238. end
  239. describe "registers with reusable date limited token" do
  240. setup do: clear_config([:instance, :registrations_open], false)
  241. test "returns user on success" do
  242. {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
  243. data = %{
  244. :username => "vinny",
  245. :email => "pasta@pizza.vs",
  246. :fullname => "Vinny Vinesauce",
  247. :bio => "streamer",
  248. :password => "hiptofbees",
  249. :confirm => "hiptofbees",
  250. :token => invite.token
  251. }
  252. {:ok, user} = TwitterAPI.register_user(data)
  253. assert user == User.get_cached_by_nickname("vinny")
  254. invite = Repo.get_by(UserInviteToken, token: invite.token)
  255. refute invite.used
  256. end
  257. test "error after max uses" do
  258. {:ok, invite} = UserInviteToken.create_invite(%{expires_at: Date.utc_today(), max_use: 100})
  259. UserInviteToken.update_invite!(invite, uses: 99)
  260. data = %{
  261. :username => "vinny",
  262. :email => "pasta@pizza.vs",
  263. :fullname => "Vinny Vinesauce",
  264. :bio => "streamer",
  265. :password => "hiptofbees",
  266. :confirm => "hiptofbees",
  267. :token => invite.token
  268. }
  269. {:ok, user} = TwitterAPI.register_user(data)
  270. assert user == User.get_cached_by_nickname("vinny")
  271. invite = Repo.get_by(UserInviteToken, token: invite.token)
  272. assert invite.used == true
  273. data = %{
  274. :username => "GrimReaper",
  275. :email => "death@reapers.afterlife",
  276. :fullname => "Reaper Grim",
  277. :bio => "Your time has come",
  278. :password => "scythe",
  279. :confirm => "scythe",
  280. :token => invite.token
  281. }
  282. {:error, msg} = TwitterAPI.register_user(data)
  283. assert msg == "Expired token"
  284. refute User.get_cached_by_nickname("GrimReaper")
  285. end
  286. test "returns error on overdue date" do
  287. {:ok, invite} =
  288. UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
  289. data = %{
  290. :username => "GrimReaper",
  291. :email => "death@reapers.afterlife",
  292. :fullname => "Reaper Grim",
  293. :bio => "Your time has come",
  294. :password => "scythe",
  295. :confirm => "scythe",
  296. :token => invite.token
  297. }
  298. {:error, msg} = TwitterAPI.register_user(data)
  299. assert msg == "Expired token"
  300. refute User.get_cached_by_nickname("GrimReaper")
  301. end
  302. test "returns error on with overdue date and after max" do
  303. {:ok, invite} =
  304. UserInviteToken.create_invite(%{expires_at: Date.add(Date.utc_today(), -1), max_use: 100})
  305. UserInviteToken.update_invite!(invite, uses: 100)
  306. data = %{
  307. :username => "GrimReaper",
  308. :email => "death@reapers.afterlife",
  309. :fullname => "Reaper Grim",
  310. :bio => "Your time has come",
  311. :password => "scythe",
  312. :confirm => "scythe",
  313. :token => invite.token
  314. }
  315. {:error, msg} = TwitterAPI.register_user(data)
  316. assert msg == "Expired token"
  317. refute User.get_cached_by_nickname("GrimReaper")
  318. end
  319. end
  320. test "it returns the error on registration problems" do
  321. data = %{
  322. :username => "lain",
  323. :email => "lain@wired.jp",
  324. :fullname => "lain iwakura",
  325. :bio => "close the world."
  326. }
  327. {:error, error} = TwitterAPI.register_user(data)
  328. assert is_binary(error)
  329. refute User.get_cached_by_nickname("lain")
  330. end
  331. setup do
  332. Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
  333. Supervisor.restart_child(Pleroma.Supervisor, Cachex)
  334. :ok
  335. end
  336. end