Skip to content

Commit

Permalink
renamed check_posts_permissions to get_accessible_posts to match its …
Browse files Browse the repository at this point in the history
…function & added unit test for it
  • Loading branch information
Oaphi committed Jan 12, 2025
1 parent 91c0902 commit 71dc1af
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
8 changes: 4 additions & 4 deletions app/helpers/search_helper.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module SearchHelper
def check_posts_permissions
(current_user&.is_moderator || current_user&.is_admin ? Post : Post.undeleted)
# @param user [User] user to check
def get_accessible_posts(user)
(user&.is_moderator || user&.is_admin ? Post : Post.undeleted)
.qa_only.list_includes
end

def search_posts
posts = check_posts_permissions

posts = get_accessible_posts(current_user)
qualifiers = params_to_qualifiers
search_string = params[:search]

Expand Down
18 changes: 18 additions & 0 deletions test/helpers/search_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,22 @@ class SearchHelperTest < ActionView::TestCase
assert_equal expect, date_value_sql(input)
end
end

test 'get_accessible_posts should correctly check access' do
admin_user = users(:admin)
mod_user = users(:moderator)
standard_user = users(:standard_user)

admin_posts = get_accessible_posts(admin_user)
mod_posts = get_accessible_posts(mod_user)
user_posts = get_accessible_posts(standard_user)

can_admin_get_deleted_posts = admin_posts.any?(&:deleted)
can_mod_get_deleted_posts = mod_posts.any?(&:deleted)
can_user_get_deleted_posts = user_posts.any?(&:deleted)

assert_equal can_admin_get_deleted_posts, true
assert_equal can_mod_get_deleted_posts, true
assert_equal can_user_get_deleted_posts, false
end
end

0 comments on commit 71dc1af

Please sign in to comment.