2018-12-23 21:11:29 +01:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 23:46:45 +01:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 21:11:29 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-08-14 04:27:28 +02:00
|
|
|
defmodule Pleroma.FilterTest do
|
|
|
|
use Pleroma.DataCase
|
|
|
|
|
|
|
|
import Pleroma.Factory
|
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
alias Pleroma.Filter
|
|
|
|
alias Pleroma.Repo
|
|
|
|
|
2018-10-12 07:19:43 +02:00
|
|
|
describe "creating filters" do
|
|
|
|
test "creating one filter" do
|
|
|
|
user = insert(:user)
|
2018-08-14 04:27:28 +02:00
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
query = %Filter{
|
2018-10-12 07:19:43 +02:00
|
|
|
user_id: user.id,
|
|
|
|
filter_id: 42,
|
|
|
|
phrase: "knights",
|
|
|
|
context: ["home"]
|
|
|
|
}
|
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
{:ok, %Filter{} = filter} = Filter.create(query)
|
|
|
|
result = Filter.get(filter.filter_id, user)
|
2018-10-12 07:19:43 +02:00
|
|
|
assert query.phrase == result.phrase
|
|
|
|
end
|
|
|
|
|
|
|
|
test "creating one filter without a pre-defined filter_id" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
query = %Filter{
|
2018-10-12 07:19:43 +02:00
|
|
|
user_id: user.id,
|
|
|
|
phrase: "knights",
|
|
|
|
context: ["home"]
|
|
|
|
}
|
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
{:ok, %Filter{} = filter} = Filter.create(query)
|
2018-10-12 07:19:43 +02:00
|
|
|
# Should start at 1
|
|
|
|
assert filter.filter_id == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
test "creating additional filters uses previous highest filter_id + 1" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
query_one = %Filter{
|
2018-10-12 07:19:43 +02:00
|
|
|
user_id: user.id,
|
|
|
|
filter_id: 42,
|
|
|
|
phrase: "knights",
|
|
|
|
context: ["home"]
|
|
|
|
}
|
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
{:ok, %Filter{} = filter_one} = Filter.create(query_one)
|
2018-10-12 07:19:43 +02:00
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
query_two = %Filter{
|
2018-10-12 07:19:43 +02:00
|
|
|
user_id: user.id,
|
|
|
|
# No filter_id
|
|
|
|
phrase: "who",
|
|
|
|
context: ["home"]
|
|
|
|
}
|
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
{:ok, %Filter{} = filter_two} = Filter.create(query_two)
|
2018-10-12 07:19:43 +02:00
|
|
|
assert filter_two.filter_id == filter_one.filter_id + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
test "filter_id is unique per user" do
|
|
|
|
user_one = insert(:user)
|
|
|
|
user_two = insert(:user)
|
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
query_one = %Filter{
|
2018-10-12 07:19:43 +02:00
|
|
|
user_id: user_one.id,
|
|
|
|
phrase: "knights",
|
|
|
|
context: ["home"]
|
|
|
|
}
|
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
{:ok, %Filter{} = filter_one} = Filter.create(query_one)
|
2018-10-12 07:19:43 +02:00
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
query_two = %Filter{
|
2018-10-12 07:19:43 +02:00
|
|
|
user_id: user_two.id,
|
|
|
|
phrase: "who",
|
|
|
|
context: ["home"]
|
|
|
|
}
|
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
{:ok, %Filter{} = filter_two} = Filter.create(query_two)
|
2018-10-12 07:19:43 +02:00
|
|
|
|
|
|
|
assert filter_one.filter_id == 1
|
|
|
|
assert filter_two.filter_id == 1
|
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
result_one = Filter.get(filter_one.filter_id, user_one)
|
2018-10-12 07:19:43 +02:00
|
|
|
assert result_one.phrase == filter_one.phrase
|
2018-08-14 04:27:28 +02:00
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
result_two = Filter.get(filter_two.filter_id, user_two)
|
2018-10-12 07:19:43 +02:00
|
|
|
assert result_two.phrase == filter_two.phrase
|
|
|
|
end
|
2018-08-14 04:27:28 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
test "deleting a filter" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
query = %Filter{
|
2018-08-14 04:27:28 +02:00
|
|
|
user_id: user.id,
|
|
|
|
filter_id: 0,
|
|
|
|
phrase: "knights",
|
|
|
|
context: ["home"]
|
|
|
|
}
|
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
{:ok, _filter} = Filter.create(query)
|
|
|
|
{:ok, filter} = Filter.delete(query)
|
|
|
|
assert is_nil(Repo.get(Filter, filter.filter_id))
|
2018-08-14 04:27:28 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
test "getting all filters by an user" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
query_one = %Filter{
|
2018-08-14 04:27:28 +02:00
|
|
|
user_id: user.id,
|
|
|
|
filter_id: 1,
|
|
|
|
phrase: "knights",
|
|
|
|
context: ["home"]
|
|
|
|
}
|
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
query_two = %Filter{
|
2018-08-14 04:27:28 +02:00
|
|
|
user_id: user.id,
|
|
|
|
filter_id: 2,
|
|
|
|
phrase: "who",
|
|
|
|
context: ["home"]
|
|
|
|
}
|
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
{:ok, filter_one} = Filter.create(query_one)
|
|
|
|
{:ok, filter_two} = Filter.create(query_two)
|
|
|
|
filters = Filter.get_filters(user)
|
2018-08-14 04:27:28 +02:00
|
|
|
assert filter_one in filters
|
|
|
|
assert filter_two in filters
|
|
|
|
end
|
|
|
|
|
|
|
|
test "updating a filter" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
query_one = %Filter{
|
2018-08-14 04:27:28 +02:00
|
|
|
user_id: user.id,
|
|
|
|
filter_id: 1,
|
|
|
|
phrase: "knights",
|
|
|
|
context: ["home"]
|
|
|
|
}
|
|
|
|
|
2020-04-14 16:36:32 +02:00
|
|
|
changes = %{
|
2018-08-14 04:27:28 +02:00
|
|
|
phrase: "who",
|
|
|
|
context: ["home", "timeline"]
|
|
|
|
}
|
|
|
|
|
2020-02-26 15:50:56 +01:00
|
|
|
{:ok, filter_one} = Filter.create(query_one)
|
|
|
|
{:ok, filter_two} = Filter.update(filter_one, changes)
|
|
|
|
|
2018-08-14 04:27:28 +02:00
|
|
|
assert filter_one != filter_two
|
2020-04-14 16:36:32 +02:00
|
|
|
assert filter_two.phrase == changes.phrase
|
|
|
|
assert filter_two.context == changes.context
|
2018-08-14 04:27:28 +02:00
|
|
|
end
|
|
|
|
end
|