logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

filter_test.exs (3728B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.FilterTest do
  5. use Pleroma.DataCase
  6. import Pleroma.Factory
  7. alias Pleroma.Filter
  8. alias Pleroma.Repo
  9. describe "creating filters" do
  10. test "creating one filter" do
  11. user = insert(:user)
  12. query = %Filter{
  13. user_id: user.id,
  14. filter_id: 42,
  15. phrase: "knights",
  16. context: ["home"]
  17. }
  18. {:ok, %Filter{} = filter} = Filter.create(query)
  19. result = Filter.get(filter.filter_id, user)
  20. assert query.phrase == result.phrase
  21. end
  22. test "creating one filter without a pre-defined filter_id" do
  23. user = insert(:user)
  24. query = %Filter{
  25. user_id: user.id,
  26. phrase: "knights",
  27. context: ["home"]
  28. }
  29. {:ok, %Filter{} = filter} = Filter.create(query)
  30. # Should start at 1
  31. assert filter.filter_id == 1
  32. end
  33. test "creating additional filters uses previous highest filter_id + 1" do
  34. user = insert(:user)
  35. query_one = %Filter{
  36. user_id: user.id,
  37. filter_id: 42,
  38. phrase: "knights",
  39. context: ["home"]
  40. }
  41. {:ok, %Filter{} = filter_one} = Filter.create(query_one)
  42. query_two = %Filter{
  43. user_id: user.id,
  44. # No filter_id
  45. phrase: "who",
  46. context: ["home"]
  47. }
  48. {:ok, %Filter{} = filter_two} = Filter.create(query_two)
  49. assert filter_two.filter_id == filter_one.filter_id + 1
  50. end
  51. test "filter_id is unique per user" do
  52. user_one = insert(:user)
  53. user_two = insert(:user)
  54. query_one = %Filter{
  55. user_id: user_one.id,
  56. phrase: "knights",
  57. context: ["home"]
  58. }
  59. {:ok, %Filter{} = filter_one} = Filter.create(query_one)
  60. query_two = %Filter{
  61. user_id: user_two.id,
  62. phrase: "who",
  63. context: ["home"]
  64. }
  65. {:ok, %Filter{} = filter_two} = Filter.create(query_two)
  66. assert filter_one.filter_id == 1
  67. assert filter_two.filter_id == 1
  68. result_one = Filter.get(filter_one.filter_id, user_one)
  69. assert result_one.phrase == filter_one.phrase
  70. result_two = Filter.get(filter_two.filter_id, user_two)
  71. assert result_two.phrase == filter_two.phrase
  72. end
  73. end
  74. test "deleting a filter" do
  75. user = insert(:user)
  76. query = %Filter{
  77. user_id: user.id,
  78. filter_id: 0,
  79. phrase: "knights",
  80. context: ["home"]
  81. }
  82. {:ok, _filter} = Filter.create(query)
  83. {:ok, filter} = Filter.delete(query)
  84. assert is_nil(Repo.get(Filter, filter.filter_id))
  85. end
  86. test "getting all filters by an user" do
  87. user = insert(:user)
  88. query_one = %Filter{
  89. user_id: user.id,
  90. filter_id: 1,
  91. phrase: "knights",
  92. context: ["home"]
  93. }
  94. query_two = %Filter{
  95. user_id: user.id,
  96. filter_id: 2,
  97. phrase: "who",
  98. context: ["home"]
  99. }
  100. {:ok, filter_one} = Filter.create(query_one)
  101. {:ok, filter_two} = Filter.create(query_two)
  102. filters = Filter.get_filters(user)
  103. assert filter_one in filters
  104. assert filter_two in filters
  105. end
  106. test "updating a filter" do
  107. user = insert(:user)
  108. query_one = %Filter{
  109. user_id: user.id,
  110. filter_id: 1,
  111. phrase: "knights",
  112. context: ["home"]
  113. }
  114. changes = %{
  115. phrase: "who",
  116. context: ["home", "timeline"]
  117. }
  118. {:ok, filter_one} = Filter.create(query_one)
  119. {:ok, filter_two} = Filter.update(filter_one, changes)
  120. assert filter_one != filter_two
  121. assert filter_two.phrase == changes.phrase
  122. assert filter_two.context == changes.context
  123. end
  124. end