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