logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

filter_controller_test.exs (11242B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.MastodonAPI.FilterControllerTest do
  5. use Pleroma.Web.ConnCase, async: false
  6. use Oban.Testing, repo: Pleroma.Repo
  7. import Mock
  8. import Pleroma.Factory
  9. alias Pleroma.Filter
  10. alias Pleroma.Repo
  11. alias Pleroma.Workers.PurgeExpiredFilter
  12. test "non authenticated creation request", %{conn: conn} do
  13. response =
  14. conn
  15. |> put_req_header("content-type", "application/json")
  16. |> post("/api/v1/filters", %{"phrase" => "knights", context: ["home"]})
  17. |> json_response(403)
  18. assert response["error"] == "Invalid credentials."
  19. end
  20. describe "creating" do
  21. setup do: oauth_access(["write:filters"])
  22. test "a common filter", %{conn: conn, user: user} do
  23. params = %{
  24. phrase: "knights",
  25. context: ["home"],
  26. irreversible: true
  27. }
  28. response =
  29. conn
  30. |> put_req_header("content-type", "application/json")
  31. |> post("/api/v1/filters", params)
  32. |> json_response_and_validate_schema(200)
  33. assert response["phrase"] == params.phrase
  34. assert response["context"] == params.context
  35. assert response["irreversible"] == true
  36. assert response["id"] != nil
  37. assert response["id"] != ""
  38. assert response["expires_at"] == nil
  39. filter = Filter.get(response["id"], user)
  40. assert filter.hide == true
  41. end
  42. test "a filter with expires_in", %{conn: conn, user: user} do
  43. in_seconds = 600
  44. response =
  45. with_mock NaiveDateTime, [:passthrough], utc_now: fn -> ~N[2017-03-17 17:09:58] end do
  46. conn
  47. |> put_req_header("content-type", "application/json")
  48. |> post("/api/v1/filters", %{
  49. "phrase" => "knights",
  50. context: ["home"],
  51. expires_in: in_seconds
  52. })
  53. |> json_response_and_validate_schema(200)
  54. end
  55. assert response["irreversible"] == false
  56. assert response["expires_at"] == "2017-03-17T17:19:58.000Z"
  57. filter = Filter.get(response["id"], user)
  58. id = filter.id
  59. assert_enqueued(
  60. worker: PurgeExpiredFilter,
  61. args: %{filter_id: filter.id}
  62. )
  63. assert {:ok, %{id: ^id}} =
  64. perform_job(PurgeExpiredFilter, %{
  65. filter_id: filter.id
  66. })
  67. assert Repo.aggregate(Filter, :count, :id) == 0
  68. end
  69. end
  70. test "fetching a list of filters" do
  71. %{user: user, conn: conn} = oauth_access(["read:filters"])
  72. %{filter_id: id1} = insert(:filter, user: user)
  73. %{filter_id: id2} = insert(:filter, user: user)
  74. id1 = to_string(id1)
  75. id2 = to_string(id2)
  76. assert [%{"id" => ^id2}, %{"id" => ^id1}] =
  77. conn
  78. |> get("/api/v1/filters")
  79. |> json_response_and_validate_schema(200)
  80. end
  81. test "fetching a list of filters without token", %{conn: conn} do
  82. insert(:filter)
  83. response =
  84. conn
  85. |> get("/api/v1/filters")
  86. |> json_response(403)
  87. assert response["error"] == "Invalid credentials."
  88. end
  89. test "get a filter" do
  90. %{user: user, conn: conn} = oauth_access(["read:filters"])
  91. # check whole_word false
  92. filter = insert(:filter, user: user, whole_word: false)
  93. resp1 =
  94. conn |> get("/api/v1/filters/#{filter.filter_id}") |> json_response_and_validate_schema(200)
  95. assert resp1["whole_word"] == false
  96. # check whole_word true
  97. filter = insert(:filter, user: user, whole_word: true)
  98. resp2 =
  99. conn |> get("/api/v1/filters/#{filter.filter_id}") |> json_response_and_validate_schema(200)
  100. assert resp2["whole_word"] == true
  101. end
  102. test "get a filter not_found error" do
  103. filter = insert(:filter)
  104. %{conn: conn} = oauth_access(["read:filters"])
  105. response =
  106. conn |> get("/api/v1/filters/#{filter.filter_id}") |> json_response_and_validate_schema(404)
  107. assert response["error"] == "Record not found"
  108. end
  109. describe "updating a filter" do
  110. setup do: oauth_access(["write:filters"])
  111. test "common" do
  112. %{conn: conn, user: user} = oauth_access(["write:filters"])
  113. filter =
  114. insert(:filter,
  115. user: user,
  116. hide: true,
  117. whole_word: true
  118. )
  119. params = %{
  120. phrase: "nii",
  121. context: ["public"],
  122. irreversible: false
  123. }
  124. response =
  125. conn
  126. |> put_req_header("content-type", "application/json")
  127. |> put("/api/v1/filters/#{filter.filter_id}", params)
  128. |> json_response_and_validate_schema(200)
  129. assert response["phrase"] == params.phrase
  130. assert response["context"] == params.context
  131. assert response["irreversible"] == false
  132. assert response["whole_word"] == true
  133. end
  134. test "with adding expires_at", %{conn: conn, user: user} do
  135. filter = insert(:filter, user: user)
  136. in_seconds = 600
  137. response =
  138. with_mock NaiveDateTime, [:passthrough], utc_now: fn -> ~N[2017-03-17 17:09:58] end do
  139. conn
  140. |> put_req_header("content-type", "application/json")
  141. |> put("/api/v1/filters/#{filter.filter_id}", %{
  142. phrase: "nii",
  143. context: ["public"],
  144. expires_in: in_seconds,
  145. irreversible: true
  146. })
  147. |> json_response_and_validate_schema(200)
  148. end
  149. assert response["irreversible"] == true
  150. assert response["expires_at"] == "2017-03-17T17:19:58.000Z"
  151. filter = Filter.get(response["id"], user)
  152. id = filter.id
  153. assert_enqueued(
  154. worker: PurgeExpiredFilter,
  155. args: %{filter_id: id}
  156. )
  157. assert {:ok, %{id: ^id}} =
  158. perform_job(PurgeExpiredFilter, %{
  159. filter_id: id
  160. })
  161. assert Repo.aggregate(Filter, :count, :id) == 0
  162. end
  163. test "with removing expires_at", %{conn: conn, user: user} do
  164. response =
  165. conn
  166. |> put_req_header("content-type", "application/json")
  167. |> post("/api/v1/filters", %{
  168. phrase: "cofe",
  169. context: ["home"],
  170. expires_in: 600
  171. })
  172. |> json_response_and_validate_schema(200)
  173. filter = Filter.get(response["id"], user)
  174. assert_enqueued(
  175. worker: PurgeExpiredFilter,
  176. args: %{filter_id: filter.id}
  177. )
  178. response =
  179. conn
  180. |> put_req_header("content-type", "application/json")
  181. |> put("/api/v1/filters/#{filter.filter_id}", %{
  182. phrase: "nii",
  183. context: ["public"],
  184. expires_in: nil,
  185. whole_word: true
  186. })
  187. |> json_response_and_validate_schema(200)
  188. refute_enqueued(
  189. worker: PurgeExpiredFilter,
  190. args: %{filter_id: filter.id}
  191. )
  192. assert response["irreversible"] == false
  193. assert response["whole_word"] == true
  194. assert response["expires_at"] == nil
  195. end
  196. test "expires_at is the same in create and update so job is in db", %{conn: conn, user: user} do
  197. resp1 =
  198. conn
  199. |> put_req_header("content-type", "application/json")
  200. |> post("/api/v1/filters", %{
  201. phrase: "cofe",
  202. context: ["home"],
  203. expires_in: 600
  204. })
  205. |> json_response_and_validate_schema(200)
  206. filter = Filter.get(resp1["id"], user)
  207. assert_enqueued(
  208. worker: PurgeExpiredFilter,
  209. args: %{filter_id: filter.id}
  210. )
  211. job = PurgeExpiredFilter.get_expiration(filter.id)
  212. resp2 =
  213. conn
  214. |> put_req_header("content-type", "application/json")
  215. |> put("/api/v1/filters/#{filter.filter_id}", %{
  216. phrase: "nii",
  217. context: ["public"]
  218. })
  219. |> json_response_and_validate_schema(200)
  220. updated_filter = Filter.get(resp2["id"], user)
  221. assert_enqueued(
  222. worker: PurgeExpiredFilter,
  223. args: %{filter_id: updated_filter.id}
  224. )
  225. after_update = PurgeExpiredFilter.get_expiration(updated_filter.id)
  226. assert resp1["expires_at"] == resp2["expires_at"]
  227. assert job.scheduled_at == after_update.scheduled_at
  228. end
  229. test "updating expires_at updates oban job too", %{conn: conn, user: user} do
  230. resp1 =
  231. conn
  232. |> put_req_header("content-type", "application/json")
  233. |> post("/api/v1/filters", %{
  234. phrase: "cofe",
  235. context: ["home"],
  236. expires_in: 600
  237. })
  238. |> json_response_and_validate_schema(200)
  239. filter = Filter.get(resp1["id"], user)
  240. assert_enqueued(
  241. worker: PurgeExpiredFilter,
  242. args: %{filter_id: filter.id}
  243. )
  244. job = PurgeExpiredFilter.get_expiration(filter.id)
  245. resp2 =
  246. conn
  247. |> put_req_header("content-type", "application/json")
  248. |> put("/api/v1/filters/#{filter.filter_id}", %{
  249. phrase: "nii",
  250. context: ["public"],
  251. expires_in: 300
  252. })
  253. |> json_response_and_validate_schema(200)
  254. updated_filter = Filter.get(resp2["id"], user)
  255. assert_enqueued(
  256. worker: PurgeExpiredFilter,
  257. args: %{filter_id: updated_filter.id}
  258. )
  259. after_update = PurgeExpiredFilter.get_expiration(updated_filter.id)
  260. refute resp1["expires_at"] == resp2["expires_at"]
  261. refute job.scheduled_at == after_update.scheduled_at
  262. end
  263. end
  264. test "update filter without token", %{conn: conn} do
  265. filter = insert(:filter)
  266. response =
  267. conn
  268. |> put_req_header("content-type", "application/json")
  269. |> put("/api/v1/filters/#{filter.filter_id}", %{
  270. phrase: "nii",
  271. context: ["public"]
  272. })
  273. |> json_response(403)
  274. assert response["error"] == "Invalid credentials."
  275. end
  276. describe "delete a filter" do
  277. setup do: oauth_access(["write:filters"])
  278. test "common", %{conn: conn, user: user} do
  279. filter = insert(:filter, user: user)
  280. assert conn
  281. |> delete("/api/v1/filters/#{filter.filter_id}")
  282. |> json_response_and_validate_schema(200) == %{}
  283. assert Repo.all(Filter) == []
  284. end
  285. test "with expires_at", %{conn: conn, user: user} do
  286. response =
  287. conn
  288. |> put_req_header("content-type", "application/json")
  289. |> post("/api/v1/filters", %{
  290. phrase: "cofe",
  291. context: ["home"],
  292. expires_in: 600
  293. })
  294. |> json_response_and_validate_schema(200)
  295. filter = Filter.get(response["id"], user)
  296. assert_enqueued(
  297. worker: PurgeExpiredFilter,
  298. args: %{filter_id: filter.id}
  299. )
  300. assert conn
  301. |> delete("/api/v1/filters/#{filter.filter_id}")
  302. |> json_response_and_validate_schema(200) == %{}
  303. refute_enqueued(
  304. worker: PurgeExpiredFilter,
  305. args: %{filter_id: filter.id}
  306. )
  307. assert Repo.all(Filter) == []
  308. assert Repo.all(Oban.Job) == []
  309. end
  310. end
  311. test "delete a filter without token", %{conn: conn} do
  312. filter = insert(:filter)
  313. response =
  314. conn
  315. |> delete("/api/v1/filters/#{filter.filter_id}")
  316. |> json_response(403)
  317. assert response["error"] == "Invalid credentials."
  318. end
  319. end