Skip to content

Commit

Permalink
Create received blocks resource
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKhorev committed Dec 24, 2024
1 parent 7a4115f commit 2df389c
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 121 deletions.
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ FactoryBot/ExcessiveCreateList:
- 'test/controllers/traces_controller_test.rb'
- 'test/controllers/user_blocks_controller_test.rb'
- 'test/controllers/users/issued_blocks_controller_test.rb'
- 'test/controllers/users/received_blocks_controller_test.rb'
- 'test/system/users_test.rb'

# Offense count: 635
Expand Down
2 changes: 1 addition & 1 deletion app/abilities/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def initialize(user)
can [:create, :destroy], :session
can [:read, :data, :georss], Trace
can [:read, :terms, :create, :save, :suspended, :auth_success, :auth_failure], User
can [:read, :blocks_on], UserBlock
can :read, UserBlock
end

if user&.active?
Expand Down
19 changes: 2 additions & 17 deletions app/controllers/user_blocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class UserBlocksController < ApplicationController

authorize_resource

before_action :lookup_user, :only => [:new, :create, :revoke_all, :blocks_on]
before_action :lookup_user, :only => [:new, :create, :revoke_all]
before_action :lookup_user_block, :only => [:show, :edit, :update]
before_action :require_valid_params, :only => [:create, :update]
before_action :check_database_readable
Expand Down Expand Up @@ -111,25 +111,10 @@ def revoke_all
if request.post? && params[:confirm]
@user.blocks.active.each { |block| block.revoke!(current_user) }
flash[:notice] = t ".flash"
redirect_to user_blocks_on_path(@user)
redirect_to user_received_blocks_path(@user)
end
end

##
# shows a list of all the blocks on the given user
def blocks_on
@params = params.permit(:display_name)

user_blocks = UserBlock.where(:user => @user)

@user_blocks, @newer_user_blocks_id, @older_user_blocks_id = get_page_items(user_blocks, :includes => [:user, :creator, :revoker])

@show_user_name = false
@show_creator_name = true

render :partial => "page" if turbo_frame_request_id == "pagination"
end

private

##
Expand Down
31 changes: 31 additions & 0 deletions app/controllers/users/received_blocks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Users
class ReceivedBlocksController < ApplicationController
include UserMethods
include PaginationMethods

layout "site"

before_action :authorize_web
before_action :set_locale

authorize_resource :class => UserBlock

before_action :lookup_user
before_action :check_database_readable

##
# shows a list of all the blocks on the given user
def show
@params = params.permit(:user_display_name)

user_blocks = UserBlock.where(:user => @user)

@user_blocks, @newer_user_blocks_id, @older_user_blocks_id = get_page_items(user_blocks, :includes => [:user, :creator, :revoker])

@show_user_name = false
@show_creator_name = true

render :partial => "user_blocks/page" if turbo_frame_request_id == "pagination"
end
end
end
8 changes: 4 additions & 4 deletions app/views/user_blocks/_navigation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
<% if current_user&.blocks&.exists? %>
<li class="nav-item">
<%= link_to t(".blocks_on_me"),
user_blocks_on_path(current_user),
:class => ["nav-link", { :active => action_name == "blocks_on" && current_user == @user }] %>
user_received_blocks_path(current_user),
:class => ["nav-link", { :active => controller_name == "received_blocks" && current_user == @user }] %>
</li>
<% end %>
<% on_user = @user || @user_block&.user %>
<% if on_user != current_user && on_user&.blocks&.exists? %>
<li class="nav-item">
<%= link_to t(".blocks_on_user_html", :user => tag.span(on_user.display_name, :class => "username text-truncate d-inline-block align-bottom", :dir => "auto")),
user_blocks_on_path(on_user),
:class => ["nav-link", { :active => action_name == "blocks_on" }] %>
user_received_blocks_path(on_user),
:class => ["nav-link", { :active => controller_name == "received_blocks" }] %>
</li>
<% end %>
<% if current_user&.blocks_created&.exists? %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<% content_for :heading_class, "pb-0" %>
<% content_for :heading do %>
<h1><%= t(".heading_html", :name => link_to(@user.display_name, @user)) %></h1>
<%= render :partial => "navigation" %>
<%= render :partial => "user_blocks/navigation" %>
<% end %>

<% unless @user_blocks.empty? %>
<%= render :partial => "page" %>
<%= render :partial => "user_blocks/page" %>
<% else %>
<p><%= t ".empty", :name => @user.display_name %></p>
<% end %>
4 changes: 2 additions & 2 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

<% if current_user.blocks.exists? %>
<li>
<%= link_to t(".blocks on me"), user_blocks_on_path(current_user) %>
<%= link_to t(".blocks on me"), user_received_blocks_path(current_user) %>
<span class='badge count-number'><%= number_with_delimiter(current_user.blocks.active.size) %></span>
</li>
<% end %>
Expand Down Expand Up @@ -93,7 +93,7 @@

<% if @user.blocks.exists? %>
<li>
<%= link_to t(".block_history"), user_blocks_on_path(@user) %>
<%= link_to t(".block_history"), user_received_blocks_path(@user) %>
<span class='badge count-number'><%= number_with_delimiter(@user.blocks.active.size) %></span>
</li>
<% end %>
Expand Down
9 changes: 5 additions & 4 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2851,6 +2851,11 @@ en:
title: "Blocks by %{name}"
heading_html: "List of Blocks by %{name}"
empty: "%{name} has not made any blocks yet."
received_blocks:
show:
title: "Blocks on %{name}"
heading_html: "List of Blocks on %{name}"
empty: "%{name} has not been blocked yet."
lists:
show:
title: Users
Expand Down Expand Up @@ -2967,10 +2972,6 @@ en:
read_html: "read at %{time}"
time_in_future_title: "%{time_absolute}; in %{time_relative}"
time_in_past_title: "%{time_absolute}; %{time_relative}"
blocks_on:
title: "Blocks on %{name}"
heading_html: "List of Blocks on %{name}"
empty: "%{name} has not been blocked yet."
show:
title: "%{block_on} blocked by %{block_by}"
heading_html: "%{block_on} blocked by %{block_by}"
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@
resource :role, :controller => "user_roles", :path => "roles/:role", :only => [:create, :destroy]
scope :module => :users do
resource :issued_blocks, :path => "blocks_by", :only => :show
resource :received_blocks, :path => "blocks", :only => :show
end
end
get "/user/:display_name/account", :to => redirect(:path => "/account/edit")
Expand Down Expand Up @@ -333,7 +334,6 @@
resources :user_mutes, :only => [:index]

# banning pages
get "/user/:display_name/blocks" => "user_blocks#blocks_on", :as => "user_blocks_on"
resources :user_blocks, :path_names => { :new => "new/:display_name" }
match "/user/:display_name/blocks/revoke_all" => "user_blocks#revoke_all", :via => [:get, :post], :as => "revoke_all_user_blocks"

Expand Down
92 changes: 2 additions & 90 deletions test/controllers/user_blocks_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ def test_routes
{ :controller => "user_blocks", :action => "destroy", :id => "1" }
)

assert_routing(
{ :path => "/user/username/blocks", :method => :get },
{ :controller => "user_blocks", :action => "blocks_on", :display_name => "username" }
)
assert_routing(
{ :path => "/user/username/blocks/revoke_all", :method => :get },
{ :controller => "user_blocks", :action => "revoke_all", :display_name => "username" }
Expand Down Expand Up @@ -609,7 +605,7 @@ def test_revoke_all_page
create(:user_block, :user => blocked_user)

# Asking for the revoke all blocks page with a bogus user name should fail
get user_blocks_on_path("non_existent_user")
get user_received_blocks_path("non_existent_user")
assert_response :not_found

# Check that the revoke all blocks page requires us to login
Expand Down Expand Up @@ -668,7 +664,7 @@ def test_revoke_all_action

# Check that revoking blocks works using POST
post revoke_all_user_blocks_path(blocked_user, :confirm => true)
assert_redirected_to user_blocks_on_path(blocked_user)
assert_redirected_to user_received_blocks_path(blocked_user)

blocks.each(&:reload)
assert_not_predicate active_block1, :active?
Expand Down Expand Up @@ -807,90 +803,6 @@ def test_update_legacy_deactivates_at
end
end

##
# test the blocks_on action
def test_blocks_on
blocked_user = create(:user)
unblocked_user = create(:user)
normal_user = create(:user)
active_block = create(:user_block, :user => blocked_user)
revoked_block = create(:user_block, :revoked, :user => blocked_user)
expired_block = create(:user_block, :expired, :user => unblocked_user)

# Asking for a list of blocks with a bogus user name should fail
get user_blocks_on_path("non_existent_user")
assert_response :not_found
assert_template "users/no_such_user"
assert_select "h1", "The user non_existent_user does not exist"

# Check the list of blocks for a user that has never been blocked
get user_blocks_on_path(normal_user)
assert_response :success
assert_select "table#block_list", false
assert_select "p", "#{normal_user.display_name} has not been blocked yet."

# Check the list of blocks for a user that is currently blocked
get user_blocks_on_path(blocked_user)
assert_response :success
assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
assert_select "a.active[href='#{user_blocks_on_path blocked_user}']"
assert_select "table#block_list tbody", :count => 1 do
assert_select "tr", 2
assert_select "a[href='#{user_block_path(active_block)}']", 1
assert_select "a[href='#{user_block_path(revoked_block)}']", 1
end

# Check the list of blocks for a user that has previously been blocked
get user_blocks_on_path(unblocked_user)
assert_response :success
assert_select "h1 a[href='#{user_path unblocked_user}']", :text => unblocked_user.display_name
assert_select "a.active[href='#{user_blocks_on_path unblocked_user}']"
assert_select "table#block_list tbody", :count => 1 do
assert_select "tr", 1
assert_select "a[href='#{user_block_path(expired_block)}']", 1
end
end

##
# test the blocks_on action with multiple pages
def test_blocks_on_paged
user = create(:user)
user_blocks = create_list(:user_block, 50, :user => user).reverse
next_path = user_blocks_on_path(user)

get next_path
assert_response :success
check_user_blocks_table user_blocks[0...20]
check_no_page_link "Newer Blocks"
next_path = check_page_link "Older Blocks"

get next_path
assert_response :success
check_user_blocks_table user_blocks[20...40]
check_page_link "Newer Blocks"
next_path = check_page_link "Older Blocks"

get next_path
assert_response :success
check_user_blocks_table user_blocks[40...50]
check_page_link "Newer Blocks"
check_no_page_link "Older Blocks"
end

##
# test the blocks_on action with invalid pages
def test_blocks_on_invalid_paged
user = create(:user)

%w[-1 0 fred].each do |id|
get user_blocks_on_path(user, :before => id)
assert_redirected_to :controller => :errors, :action => :bad_request

get user_blocks_on_path(user, :after => id)
assert_redirected_to :controller => :errors, :action => :bad_request
end
end

private

def check_block_buttons(block, edit: 0)
Expand Down
95 changes: 95 additions & 0 deletions test/controllers/users/received_blocks_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
require "test_helper"
require_relative "../user_blocks/table_test_helper"

module Users
class ReceivedBlocksControllerTest < ActionDispatch::IntegrationTest
include UserBlocks::TableTestHelper

##
# test all routes which lead to this controller
def test_routes
assert_routing(
{ :path => "/user/username/blocks", :method => :get },
{ :controller => "users/received_blocks", :action => "show", :user_display_name => "username" }
)
end

def test_show
blocked_user = create(:user)
unblocked_user = create(:user)
normal_user = create(:user)
active_block = create(:user_block, :user => blocked_user)
revoked_block = create(:user_block, :revoked, :user => blocked_user)
expired_block = create(:user_block, :expired, :user => unblocked_user)

# Asking for a list of blocks with a bogus user name should fail
get user_received_blocks_path("non_existent_user")
assert_response :not_found
assert_template "users/no_such_user"
assert_select "h1", "The user non_existent_user does not exist"

# Check the list of blocks for a user that has never been blocked
get user_received_blocks_path(normal_user)
assert_response :success
assert_select "table#block_list", false
assert_select "p", "#{normal_user.display_name} has not been blocked yet."

# Check the list of blocks for a user that is currently blocked
get user_received_blocks_path(blocked_user)
assert_response :success
assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
assert_select "a.active[href='#{user_received_blocks_path blocked_user}']"
assert_select "table#block_list tbody", :count => 1 do
assert_select "tr", 2
assert_select "a[href='#{user_block_path(active_block)}']", 1
assert_select "a[href='#{user_block_path(revoked_block)}']", 1
end

# Check the list of blocks for a user that has previously been blocked
get user_received_blocks_path(unblocked_user)
assert_response :success
assert_select "h1 a[href='#{user_path unblocked_user}']", :text => unblocked_user.display_name
assert_select "a.active[href='#{user_received_blocks_path unblocked_user}']"
assert_select "table#block_list tbody", :count => 1 do
assert_select "tr", 1
assert_select "a[href='#{user_block_path(expired_block)}']", 1
end
end

def test_show_paged
user = create(:user)
user_blocks = create_list(:user_block, 50, :user => user).reverse
next_path = user_received_blocks_path(user)

get next_path
assert_response :success
check_user_blocks_table user_blocks[0...20]
check_no_page_link "Newer Blocks"
next_path = check_page_link "Older Blocks"

get next_path
assert_response :success
check_user_blocks_table user_blocks[20...40]
check_page_link "Newer Blocks"
next_path = check_page_link "Older Blocks"

get next_path
assert_response :success
check_user_blocks_table user_blocks[40...50]
check_page_link "Newer Blocks"
check_no_page_link "Older Blocks"
end

def test_show_invalid_paged
user = create(:user)

%w[-1 0 fred].each do |id|
get user_received_blocks_path(user, :before => id)
assert_redirected_to :controller => "/errors", :action => :bad_request

get user_received_blocks_path(user, :after => id)
assert_redirected_to :controller => "/errors", :action => :bad_request
end
end
end
end

0 comments on commit 2df389c

Please sign in to comment.