2019-09-27 06:35:45 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-02 05:08:45 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-09-27 06:35:45 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.ScheduledActivityControllerTest do
|
2019-11-20 07:25:00 +00:00
|
|
|
use Pleroma.Web.ConnCase
|
2019-09-27 06:35:45 +00:00
|
|
|
|
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.ScheduledActivity
|
|
|
|
|
|
|
|
import Pleroma.Factory
|
2019-12-03 18:30:10 +00:00
|
|
|
import Ecto.Query
|
2019-09-27 06:35:45 +00:00
|
|
|
|
2020-03-20 15:33:00 +00:00
|
|
|
setup do: clear_config([ScheduledActivity, :enabled])
|
2019-12-04 18:18:05 +00:00
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
test "shows scheduled activities" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:statuses"])
|
|
|
|
|
2019-09-27 06:35:45 +00:00
|
|
|
scheduled_activity_id1 = insert(:scheduled_activity, user: user).id |> to_string()
|
|
|
|
scheduled_activity_id2 = insert(:scheduled_activity, user: user).id |> to_string()
|
|
|
|
scheduled_activity_id3 = insert(:scheduled_activity, user: user).id |> to_string()
|
|
|
|
scheduled_activity_id4 = insert(:scheduled_activity, user: user).id |> to_string()
|
|
|
|
|
|
|
|
# min_id
|
2019-12-19 14:23:27 +00:00
|
|
|
conn_res = get(conn, "/api/v1/scheduled_statuses?limit=2&min_id=#{scheduled_activity_id1}")
|
2019-09-27 06:35:45 +00:00
|
|
|
|
2020-05-05 19:42:18 +00:00
|
|
|
result = json_response_and_validate_schema(conn_res, 200)
|
2019-09-27 06:35:45 +00:00
|
|
|
assert [%{"id" => ^scheduled_activity_id3}, %{"id" => ^scheduled_activity_id2}] = result
|
|
|
|
|
|
|
|
# since_id
|
2019-12-19 14:23:27 +00:00
|
|
|
conn_res = get(conn, "/api/v1/scheduled_statuses?limit=2&since_id=#{scheduled_activity_id1}")
|
2019-09-27 06:35:45 +00:00
|
|
|
|
2020-05-05 19:42:18 +00:00
|
|
|
result = json_response_and_validate_schema(conn_res, 200)
|
2019-09-27 06:35:45 +00:00
|
|
|
assert [%{"id" => ^scheduled_activity_id4}, %{"id" => ^scheduled_activity_id3}] = result
|
|
|
|
|
|
|
|
# max_id
|
2019-12-19 14:23:27 +00:00
|
|
|
conn_res = get(conn, "/api/v1/scheduled_statuses?limit=2&max_id=#{scheduled_activity_id4}")
|
2019-09-27 06:35:45 +00:00
|
|
|
|
2020-05-05 19:42:18 +00:00
|
|
|
result = json_response_and_validate_schema(conn_res, 200)
|
2019-09-27 06:35:45 +00:00
|
|
|
assert [%{"id" => ^scheduled_activity_id3}, %{"id" => ^scheduled_activity_id2}] = result
|
|
|
|
end
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
test "shows a scheduled activity" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:statuses"])
|
2019-09-27 06:35:45 +00:00
|
|
|
scheduled_activity = insert(:scheduled_activity, user: user)
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
res_conn = get(conn, "/api/v1/scheduled_statuses/#{scheduled_activity.id}")
|
2019-09-27 06:35:45 +00:00
|
|
|
|
2020-05-05 19:42:18 +00:00
|
|
|
assert %{"id" => scheduled_activity_id} = json_response_and_validate_schema(res_conn, 200)
|
2019-09-27 06:35:45 +00:00
|
|
|
assert scheduled_activity_id == scheduled_activity.id |> to_string()
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
res_conn = get(conn, "/api/v1/scheduled_statuses/404")
|
2019-09-27 06:35:45 +00:00
|
|
|
|
2020-05-05 19:42:18 +00:00
|
|
|
assert %{"error" => "Record not found"} = json_response_and_validate_schema(res_conn, 404)
|
2019-09-27 06:35:45 +00:00
|
|
|
end
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
test "updates a scheduled activity" do
|
2019-12-04 18:18:05 +00:00
|
|
|
Pleroma.Config.put([ScheduledActivity, :enabled], true)
|
2019-12-19 14:23:27 +00:00
|
|
|
%{user: user, conn: conn} = oauth_access(["write:statuses"])
|
2019-12-03 18:30:10 +00:00
|
|
|
|
2020-01-13 09:41:07 +00:00
|
|
|
scheduled_at = Timex.shift(NaiveDateTime.utc_now(), minutes: 60)
|
2019-12-03 18:30:10 +00:00
|
|
|
|
|
|
|
{:ok, scheduled_activity} =
|
|
|
|
ScheduledActivity.create(
|
|
|
|
user,
|
|
|
|
%{
|
|
|
|
scheduled_at: scheduled_at,
|
|
|
|
params: build(:note).data
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-01-13 09:41:07 +00:00
|
|
|
job = Repo.one(from(j in Oban.Job, where: j.queue == "scheduled_activities"))
|
2019-12-03 18:30:10 +00:00
|
|
|
|
2020-01-13 09:41:07 +00:00
|
|
|
assert job.args == %{"activity_id" => scheduled_activity.id}
|
|
|
|
assert DateTime.truncate(job.scheduled_at, :second) == to_datetime(scheduled_at)
|
2019-12-03 18:30:10 +00:00
|
|
|
|
2020-05-05 19:42:18 +00:00
|
|
|
new_scheduled_at =
|
|
|
|
NaiveDateTime.utc_now()
|
|
|
|
|> Timex.shift(minutes: 120)
|
|
|
|
|> Timex.format!("%Y-%m-%dT%H:%M:%S.%fZ", :strftime)
|
2019-09-27 06:35:45 +00:00
|
|
|
|
|
|
|
res_conn =
|
2020-05-05 19:42:18 +00:00
|
|
|
conn
|
|
|
|
|> put_req_header("content-type", "application/json")
|
|
|
|
|> put("/api/v1/scheduled_statuses/#{scheduled_activity.id}", %{
|
2019-09-27 06:35:45 +00:00
|
|
|
scheduled_at: new_scheduled_at
|
|
|
|
})
|
|
|
|
|
2020-05-05 19:42:18 +00:00
|
|
|
assert %{"scheduled_at" => expected_scheduled_at} =
|
|
|
|
json_response_and_validate_schema(res_conn, 200)
|
|
|
|
|
2019-09-27 06:35:45 +00:00
|
|
|
assert expected_scheduled_at == Pleroma.Web.CommonAPI.Utils.to_masto_date(new_scheduled_at)
|
2020-01-13 09:41:07 +00:00
|
|
|
job = refresh_record(job)
|
2019-12-03 18:30:10 +00:00
|
|
|
|
2020-01-13 09:41:07 +00:00
|
|
|
assert DateTime.truncate(job.scheduled_at, :second) == to_datetime(new_scheduled_at)
|
2019-09-27 06:35:45 +00:00
|
|
|
|
2020-05-05 19:42:18 +00:00
|
|
|
res_conn =
|
|
|
|
conn
|
|
|
|
|> put_req_header("content-type", "application/json")
|
|
|
|
|> put("/api/v1/scheduled_statuses/404", %{scheduled_at: new_scheduled_at})
|
2019-09-27 06:35:45 +00:00
|
|
|
|
2020-05-05 19:42:18 +00:00
|
|
|
assert %{"error" => "Record not found"} = json_response_and_validate_schema(res_conn, 404)
|
2019-09-27 06:35:45 +00:00
|
|
|
end
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
test "deletes a scheduled activity" do
|
2019-12-04 18:18:05 +00:00
|
|
|
Pleroma.Config.put([ScheduledActivity, :enabled], true)
|
2019-12-19 14:23:27 +00:00
|
|
|
%{user: user, conn: conn} = oauth_access(["write:statuses"])
|
2020-01-13 09:41:07 +00:00
|
|
|
scheduled_at = Timex.shift(NaiveDateTime.utc_now(), minutes: 60)
|
2019-12-03 18:30:10 +00:00
|
|
|
|
|
|
|
{:ok, scheduled_activity} =
|
|
|
|
ScheduledActivity.create(
|
|
|
|
user,
|
|
|
|
%{
|
2020-01-13 09:41:07 +00:00
|
|
|
scheduled_at: scheduled_at,
|
2019-12-03 18:30:10 +00:00
|
|
|
params: build(:note).data
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-01-13 09:41:07 +00:00
|
|
|
job = Repo.one(from(j in Oban.Job, where: j.queue == "scheduled_activities"))
|
2019-12-03 18:30:10 +00:00
|
|
|
|
2020-01-13 09:41:07 +00:00
|
|
|
assert job.args == %{"activity_id" => scheduled_activity.id}
|
2019-09-27 06:35:45 +00:00
|
|
|
|
|
|
|
res_conn =
|
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
|
|
|
|> delete("/api/v1/scheduled_statuses/#{scheduled_activity.id}")
|
|
|
|
|
2020-05-05 19:42:18 +00:00
|
|
|
assert %{} = json_response_and_validate_schema(res_conn, 200)
|
2019-12-03 18:30:10 +00:00
|
|
|
refute Repo.get(ScheduledActivity, scheduled_activity.id)
|
2020-01-13 09:41:07 +00:00
|
|
|
refute Repo.get(Oban.Job, job.id)
|
2019-09-27 06:35:45 +00:00
|
|
|
|
|
|
|
res_conn =
|
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
|
|
|
|> delete("/api/v1/scheduled_statuses/#{scheduled_activity.id}")
|
|
|
|
|
2020-05-05 19:42:18 +00:00
|
|
|
assert %{"error" => "Record not found"} = json_response_and_validate_schema(res_conn, 404)
|
2019-09-27 06:35:45 +00:00
|
|
|
end
|
|
|
|
end
|