X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;ds=inline;f=test%2Fpleroma%2Ffilter_test.exs;h=19ad6b8c0f4c9b984cb603ae4229dc70d137d264;hb=ebcc28fef07236e6094998c230b10345abf8b307;hp=0a5c4426aaf69a2849816ef18cad8033a0044fd6;hpb=7bbc328d66aaac8d680f52521c6823aea42821af;p=akkoma
diff --git a/test/pleroma/filter_test.exs b/test/pleroma/filter_test.exs
index 0a5c4426a..19ad6b8c0 100644
--- a/test/pleroma/filter_test.exs
+++ b/test/pleroma/filter_test.exs
@@ -1,87 +1,126 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors
+# Copyright © 2017-2021 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.FilterTest do
- use Pleroma.DataCase
+ use Pleroma.DataCase, async: true
import Pleroma.Factory
+ alias Oban.Job
alias Pleroma.Filter
- alias Pleroma.Repo
+
+ setup do
+ [user: insert(:user)]
+ end
describe "creating filters" do
- test "creating one filter" do
- user = insert(:user)
+ test "creation validation error", %{user: user} do
+ attrs = %{
+ user_id: user.id,
+ expires_in: 60
+ }
+
+ {:error, _} = Filter.create(attrs)
+
+ assert Repo.all(Job) == []
+ end
- query = %Filter{
+ test "use passed expires_at instead expires_in", %{user: user} do
+ now = NaiveDateTime.utc_now()
+
+ attrs = %{
user_id: user.id,
- filter_id: 42,
+ expires_at: now,
phrase: "knights",
- context: ["home"]
+ context: ["home"],
+ expires_in: 600
}
- {:ok, %Filter{} = filter} = Filter.create(query)
+ {:ok, %Filter{} = filter} = Filter.create(attrs)
+
result = Filter.get(filter.filter_id, user)
- assert query.phrase == result.phrase
- end
+ assert result.expires_at == NaiveDateTime.truncate(now, :second)
- test "creating one filter without a pre-defined filter_id" do
- user = insert(:user)
+ [job] = Repo.all(Job)
- query = %Filter{
+ assert DateTime.truncate(job.scheduled_at, :second) ==
+ now |> NaiveDateTime.truncate(:second) |> DateTime.from_naive!("Etc/UTC")
+ end
+
+ test "creating one filter", %{user: user} do
+ attrs = %{
user_id: user.id,
+ filter_id: 42,
phrase: "knights",
context: ["home"]
}
- {:ok, %Filter{} = filter} = Filter.create(query)
- # Should start at 1
- assert filter.filter_id == 1
+ {:ok, %Filter{} = filter} = Filter.create(attrs)
+ result = Filter.get(filter.filter_id, user)
+ assert attrs.phrase == result.phrase
end
- test "creating additional filters uses previous highest filter_id + 1" do
- user = insert(:user)
-
- query_one = %Filter{
+ test "creating with expired_at", %{user: user} do
+ attrs = %{
user_id: user.id,
filter_id: 42,
phrase: "knights",
+ context: ["home"],
+ expires_in: 60
+ }
+
+ {:ok, %Filter{} = filter} = Filter.create(attrs)
+ result = Filter.get(filter.filter_id, user)
+ assert attrs.phrase == result.phrase
+
+ assert [_] = Repo.all(Job)
+ end
+
+ test "creating one filter without a pre-defined filter_id", %{user: user} do
+ attrs = %{
+ user_id: user.id,
+ phrase: "knights",
context: ["home"]
}
- {:ok, %Filter{} = filter_one} = Filter.create(query_one)
+ {:ok, %Filter{} = filter} = Filter.create(attrs)
+ # Should start at 1
+ assert filter.filter_id == 1
+ end
+
+ test "creating additional filters uses previous highest filter_id + 1", %{user: user} do
+ filter1 = insert(:filter, user: user)
- query_two = %Filter{
+ attrs = %{
user_id: user.id,
# No filter_id
phrase: "who",
context: ["home"]
}
- {:ok, %Filter{} = filter_two} = Filter.create(query_two)
- assert filter_two.filter_id == filter_one.filter_id + 1
+ {:ok, %Filter{} = filter2} = Filter.create(attrs)
+ assert filter2.filter_id == filter1.filter_id + 1
end
- test "filter_id is unique per user" do
- user_one = insert(:user)
+ test "filter_id is unique per user", %{user: user_one} do
user_two = insert(:user)
- query_one = %Filter{
+ attrs1 = %{
user_id: user_one.id,
phrase: "knights",
context: ["home"]
}
- {:ok, %Filter{} = filter_one} = Filter.create(query_one)
+ {:ok, %Filter{} = filter_one} = Filter.create(attrs1)
- query_two = %Filter{
+ attrs2 = %{
user_id: user_two.id,
phrase: "who",
context: ["home"]
}
- {:ok, %Filter{} = filter_two} = Filter.create(query_two)
+ {:ok, %Filter{} = filter_two} = Filter.create(attrs2)
assert filter_one.filter_id == 1
assert filter_two.filter_id == 1
@@ -94,65 +133,61 @@ defmodule Pleroma.FilterTest do
end
end
- test "deleting a filter" do
- user = insert(:user)
+ test "deleting a filter", %{user: user} do
+ filter = insert(:filter, user: user)
- query = %Filter{
- user_id: user.id,
- filter_id: 0,
- phrase: "knights",
- context: ["home"]
- }
-
- {:ok, _filter} = Filter.create(query)
- {:ok, filter} = Filter.delete(query)
- assert is_nil(Repo.get(Filter, filter.filter_id))
+ assert Repo.get(Filter, filter.id)
+ {:ok, filter} = Filter.delete(filter)
+ refute Repo.get(Filter, filter.id)
end
- test "getting all filters by an user" do
- user = insert(:user)
-
- query_one = %Filter{
+ test "deleting a filter with expires_at is removing Oban job too", %{user: user} do
+ attrs = %{
user_id: user.id,
- filter_id: 1,
- phrase: "knights",
- context: ["home"]
+ phrase: "cofe",
+ context: ["home"],
+ expires_in: 600
}
- query_two = %Filter{
- user_id: user.id,
- filter_id: 2,
- phrase: "who",
- context: ["home"]
- }
+ {:ok, filter} = Filter.create(attrs)
+ assert %Job{id: job_id} = Pleroma.Workers.PurgeExpiredFilter.get_expiration(filter.id)
+ {:ok, _} = Filter.delete(filter)
- {:ok, filter_one} = Filter.create(query_one)
- {:ok, filter_two} = Filter.create(query_two)
- filters = Filter.get_filters(user)
- assert filter_one in filters
- assert filter_two in filters
+ assert Repo.get(Job, job_id) == nil
end
- test "updating a filter" do
- user = insert(:user)
+ test "getting all filters by an user", %{user: user} do
+ filter1 = insert(:filter, user: user)
+ filter2 = insert(:filter, user: user)
- query_one = %Filter{
- user_id: user.id,
- filter_id: 1,
- phrase: "knights",
- context: ["home"]
- }
+ filter_ids = user |> Filter.get_filters() |> collect_ids()
+
+ assert filter1.id in filter_ids
+ assert filter2.id in filter_ids
+ end
+
+ test "updating a filter", %{user: user} do
+ filter = insert(:filter, user: user)
changes = %{
phrase: "who",
context: ["home", "timeline"]
}
- {:ok, filter_one} = Filter.create(query_one)
- {:ok, filter_two} = Filter.update(filter_one, changes)
+ {:ok, updated_filter} = Filter.update(filter, changes)
+
+ assert filter != updated_filter
+ assert updated_filter.phrase == changes.phrase
+ assert updated_filter.context == changes.context
+ end
+
+ test "updating with error", %{user: user} do
+ filter = insert(:filter, user: user)
+
+ changes = %{
+ phrase: nil
+ }
- assert filter_one != filter_two
- assert filter_two.phrase == changes.phrase
- assert filter_two.context == changes.context
+ {:error, _} = Filter.update(filter, changes)
end
end