logo

pleroma

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

scheduled_activity_controller_test.exs (4964B)


  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.MastodonAPI.ScheduledActivityControllerTest do
  5. use Pleroma.Web.ConnCase
  6. alias Pleroma.Repo
  7. alias Pleroma.ScheduledActivity
  8. import Pleroma.Factory
  9. import Ecto.Query
  10. setup do: clear_config([ScheduledActivity, :enabled])
  11. test "shows scheduled activities" do
  12. %{user: user, conn: conn} = oauth_access(["read:statuses"])
  13. scheduled_activity_id1 = insert(:scheduled_activity, user: user).id |> to_string()
  14. scheduled_activity_id2 = insert(:scheduled_activity, user: user).id |> to_string()
  15. scheduled_activity_id3 = insert(:scheduled_activity, user: user).id |> to_string()
  16. scheduled_activity_id4 = insert(:scheduled_activity, user: user).id |> to_string()
  17. # min_id
  18. conn_res = get(conn, "/api/v1/scheduled_statuses?limit=2&min_id=#{scheduled_activity_id1}")
  19. result = json_response_and_validate_schema(conn_res, 200)
  20. assert [%{"id" => ^scheduled_activity_id3}, %{"id" => ^scheduled_activity_id2}] = result
  21. # since_id
  22. conn_res = get(conn, "/api/v1/scheduled_statuses?limit=2&since_id=#{scheduled_activity_id1}")
  23. result = json_response_and_validate_schema(conn_res, 200)
  24. assert [%{"id" => ^scheduled_activity_id4}, %{"id" => ^scheduled_activity_id3}] = result
  25. # max_id
  26. conn_res = get(conn, "/api/v1/scheduled_statuses?limit=2&max_id=#{scheduled_activity_id4}")
  27. result = json_response_and_validate_schema(conn_res, 200)
  28. assert [%{"id" => ^scheduled_activity_id3}, %{"id" => ^scheduled_activity_id2}] = result
  29. end
  30. test "shows a scheduled activity" do
  31. %{user: user, conn: conn} = oauth_access(["read:statuses"])
  32. scheduled_activity = insert(:scheduled_activity, user: user)
  33. res_conn = get(conn, "/api/v1/scheduled_statuses/#{scheduled_activity.id}")
  34. assert %{"id" => scheduled_activity_id} = json_response_and_validate_schema(res_conn, 200)
  35. assert scheduled_activity_id == scheduled_activity.id |> to_string()
  36. res_conn = get(conn, "/api/v1/scheduled_statuses/404")
  37. assert %{"error" => "Record not found"} = json_response_and_validate_schema(res_conn, 404)
  38. end
  39. test "updates a scheduled activity" do
  40. Pleroma.Config.put([ScheduledActivity, :enabled], true)
  41. %{user: user, conn: conn} = oauth_access(["write:statuses"])
  42. scheduled_at = Timex.shift(NaiveDateTime.utc_now(), minutes: 60)
  43. {:ok, scheduled_activity} =
  44. ScheduledActivity.create(
  45. user,
  46. %{
  47. scheduled_at: scheduled_at,
  48. params: build(:note).data
  49. }
  50. )
  51. job = Repo.one(from(j in Oban.Job, where: j.queue == "scheduled_activities"))
  52. assert job.args == %{"activity_id" => scheduled_activity.id}
  53. assert DateTime.truncate(job.scheduled_at, :second) == to_datetime(scheduled_at)
  54. new_scheduled_at =
  55. NaiveDateTime.utc_now()
  56. |> Timex.shift(minutes: 120)
  57. |> Timex.format!("%Y-%m-%dT%H:%M:%S.%fZ", :strftime)
  58. res_conn =
  59. conn
  60. |> put_req_header("content-type", "application/json")
  61. |> put("/api/v1/scheduled_statuses/#{scheduled_activity.id}", %{
  62. scheduled_at: new_scheduled_at
  63. })
  64. assert %{"scheduled_at" => expected_scheduled_at} =
  65. json_response_and_validate_schema(res_conn, 200)
  66. assert expected_scheduled_at == Pleroma.Web.CommonAPI.Utils.to_masto_date(new_scheduled_at)
  67. job = refresh_record(job)
  68. assert DateTime.truncate(job.scheduled_at, :second) == to_datetime(new_scheduled_at)
  69. res_conn =
  70. conn
  71. |> put_req_header("content-type", "application/json")
  72. |> put("/api/v1/scheduled_statuses/404", %{scheduled_at: new_scheduled_at})
  73. assert %{"error" => "Record not found"} = json_response_and_validate_schema(res_conn, 404)
  74. end
  75. test "deletes a scheduled activity" do
  76. Pleroma.Config.put([ScheduledActivity, :enabled], true)
  77. %{user: user, conn: conn} = oauth_access(["write:statuses"])
  78. scheduled_at = Timex.shift(NaiveDateTime.utc_now(), minutes: 60)
  79. {:ok, scheduled_activity} =
  80. ScheduledActivity.create(
  81. user,
  82. %{
  83. scheduled_at: scheduled_at,
  84. params: build(:note).data
  85. }
  86. )
  87. job = Repo.one(from(j in Oban.Job, where: j.queue == "scheduled_activities"))
  88. assert job.args == %{"activity_id" => scheduled_activity.id}
  89. res_conn =
  90. conn
  91. |> assign(:user, user)
  92. |> delete("/api/v1/scheduled_statuses/#{scheduled_activity.id}")
  93. assert %{} = json_response_and_validate_schema(res_conn, 200)
  94. refute Repo.get(ScheduledActivity, scheduled_activity.id)
  95. refute Repo.get(Oban.Job, job.id)
  96. res_conn =
  97. conn
  98. |> assign(:user, user)
  99. |> delete("/api/v1/scheduled_statuses/#{scheduled_activity.id}")
  100. assert %{"error" => "Record not found"} = json_response_and_validate_schema(res_conn, 404)
  101. end
  102. end