+ assert response["error"] == "Record not found"
+ end
+
+ describe "updating a filter" do
+ setup do: oauth_access(["write:filters"])
+
+ test "common" do
+ %{conn: conn, user: user} = oauth_access(["write:filters"])
+
+ filter =
+ insert(:filter,
+ user: user,
+ hide: true,
+ whole_word: true
+ )
+
+ params = %{
+ phrase: "nii",
+ context: ["public"],
+ irreversible: false
+ }
+
+ response =
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> put("/api/v1/filters/#{filter.filter_id}", params)
+ |> json_response_and_validate_schema(200)
+
+ assert response["phrase"] == params.phrase
+ assert response["context"] == params.context
+ assert response["irreversible"] == false
+ assert response["whole_word"] == true
+ end
+
+ test "with adding expires_at", %{conn: conn, user: user} do
+ filter = insert(:filter, user: user)
+ in_seconds = 600
+
+ response =
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> put("/api/v1/filters/#{filter.filter_id}", %{
+ phrase: "nii",
+ context: ["public"],
+ expires_in: in_seconds,
+ irreversible: true
+ })
+ |> json_response_and_validate_schema(200)
+
+ assert response["irreversible"] == true
+
+ assert response["expires_at"] ==
+ NaiveDateTime.utc_now()
+ |> NaiveDateTime.add(in_seconds)
+ |> Pleroma.Web.CommonAPI.Utils.to_masto_date()
+
+ filter = Filter.get(response["id"], user)
+
+ id = filter.id
+
+ assert_enqueued(
+ worker: PurgeExpiredFilter,
+ args: %{filter_id: id}
+ )
+
+ assert {:ok, %{id: ^id}} =
+ perform_job(PurgeExpiredFilter, %{
+ filter_id: id
+ })
+
+ assert Repo.aggregate(Filter, :count, :id) == 0
+ end
+
+ test "with removing expires_at", %{conn: conn, user: user} do
+ response =
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/v1/filters", %{
+ phrase: "cofe",
+ context: ["home"],
+ expires_in: 600
+ })
+ |> json_response_and_validate_schema(200)
+
+ filter = Filter.get(response["id"], user)
+
+ assert_enqueued(
+ worker: PurgeExpiredFilter,
+ args: %{filter_id: filter.id}
+ )
+
+ response =
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> put("/api/v1/filters/#{filter.filter_id}", %{
+ phrase: "nii",
+ context: ["public"],
+ expires_in: nil,
+ whole_word: true
+ })
+ |> json_response_and_validate_schema(200)
+
+ refute_enqueued(
+ worker: PurgeExpiredFilter,
+ args: %{filter_id: filter.id}
+ )
+
+ assert response["irreversible"] == false
+ assert response["whole_word"] == true
+ assert response["expires_at"] == nil
+ end
+
+ test "expires_at is the same in create and update so job is in db", %{conn: conn, user: user} do
+ resp1 =
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/v1/filters", %{
+ phrase: "cofe",
+ context: ["home"],
+ expires_in: 600
+ })
+ |> json_response_and_validate_schema(200)
+
+ filter = Filter.get(resp1["id"], user)
+
+ assert_enqueued(
+ worker: PurgeExpiredFilter,
+ args: %{filter_id: filter.id}
+ )
+
+ job = PurgeExpiredFilter.get_expiration(filter.id)
+
+ resp2 =
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> put("/api/v1/filters/#{filter.filter_id}", %{
+ phrase: "nii",
+ context: ["public"]
+ })
+ |> json_response_and_validate_schema(200)
+
+ updated_filter = Filter.get(resp2["id"], user)
+
+ assert_enqueued(
+ worker: PurgeExpiredFilter,
+ args: %{filter_id: updated_filter.id}
+ )
+
+ after_update = PurgeExpiredFilter.get_expiration(updated_filter.id)
+
+ assert resp1["expires_at"] == resp2["expires_at"]
+
+ assert job.scheduled_at == after_update.scheduled_at
+ end
+
+ test "updating expires_at updates oban job too", %{conn: conn, user: user} do
+ resp1 =
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> post("/api/v1/filters", %{
+ phrase: "cofe",
+ context: ["home"],
+ expires_in: 600
+ })
+ |> json_response_and_validate_schema(200)
+
+ filter = Filter.get(resp1["id"], user)
+
+ assert_enqueued(
+ worker: PurgeExpiredFilter,
+ args: %{filter_id: filter.id}
+ )
+
+ job = PurgeExpiredFilter.get_expiration(filter.id)
+
+ resp2 =
+ conn
+ |> put_req_header("content-type", "application/json")
+ |> put("/api/v1/filters/#{filter.filter_id}", %{
+ phrase: "nii",
+ context: ["public"],
+ expires_in: 300
+ })
+ |> json_response_and_validate_schema(200)
+
+ updated_filter = Filter.get(resp2["id"], user)
+
+ assert_enqueued(
+ worker: PurgeExpiredFilter,
+ args: %{filter_id: updated_filter.id}
+ )
+
+ after_update = PurgeExpiredFilter.get_expiration(updated_filter.id)
+
+ refute resp1["expires_at"] == resp2["expires_at"]
+
+ refute job.scheduled_at == after_update.scheduled_at
+ end
+ end