Skip to content

Commit

Permalink
Fix showing announcement when comments are disabled (#12558)
Browse files Browse the repository at this point in the history
* Fix showing announcement when comments are disabled

* Wait for the comment thread loading before checking for the form

* Add a check for the verification announcement

* Extract comments blocked to a shared example

This is done mainly because some modules (such as Initiatives) don't
have the feature for blocking the comments in the steps (as they don't
have steps as a Participatory Process)

* Add missing spec for blogs module

* Fix specs

* Fix rubocop offenses

* Fix spec
  • Loading branch information
andreslucena committed Jun 21, 2024
1 parent 49427df commit 838a313
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 5 deletions.
7 changes: 7 additions & 0 deletions decidim-accountability/spec/system/comments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@
let(:resource_path) { resource_locator(commentable).path }

include_examples "comments"

context "with comments blocked" do
let!(:component) { create(:component, manifest_name: :accountability, participatory_space:, organization:) }
let(:participatory_space) { create(:participatory_process, :with_steps, organization:) }

include_examples "comments blocked"
end
end
19 changes: 19 additions & 0 deletions decidim-blogs/spec/system/comments_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require "spec_helper"

describe "Comments", perform_enqueued: true do
let!(:component) { create(:post_component, organization:) }
let!(:commentable) { create(:post, component:) }

let(:resource_path) { resource_locator(commentable).path }

include_examples "comments"

context "with comments blocked" do
let!(:component) { create(:post_component, participatory_space:, organization:) }
let(:participatory_space) { create(:participatory_process, :with_steps, organization:) }

include_examples "comments blocked"
end
end
7 changes: 7 additions & 0 deletions decidim-budgets/spec/system/comments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

include_examples "comments"

context "with comments blocked" do
let!(:component) { create(:budgets_component, participatory_space:, organization:) }
let(:participatory_space) { create(:participatory_process, :with_steps, organization:) }

include_examples "comments blocked"
end

context "when requesting the comments index with a non-XHR request" do
it "redirects the user to the correct commentable path" do
visit decidim_comments.comments_path(commentable_gid: commentable.to_signed_global_id.to_s)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def comments_loading

def blocked_comments_warning
return unless comments_blocked?
return unless user_comments_blocked?
return if user_comments_blocked?

render :blocked_comments_warning
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ module Decidim::Comments
before do
comment # Create the comment before disabling comments
allow(commentable).to receive(:accepts_new_comments?).and_return(false)
allow(commentable).to receive(:user_allowed_to_comment?).with(current_user).and_return(false)
allow(commentable).to receive(:user_allowed_to_comment?).with(current_user).and_return(true)
end

it "renders the comments blocked warning" do
Expand Down
3 changes: 1 addition & 2 deletions decidim-comments/spec/system/comments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

describe "Comments" do
let!(:component) { create(:component, manifest_name: :dummy, organization:) }
let!(:author) { create(:user, :confirmed, organization:) }
let!(:commentable) { create(:dummy_resource, component:, author:) }
let!(:commentable) { create(:dummy_resource, component:) }

let(:resource_path) { resource_locator(commentable).path }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
it "does not show form to add comments to user" do
visit resource_path
expect(page).to have_no_css(".add-comment form")
expect(page).to have_css(".comment-thread")
end
end

Expand All @@ -131,6 +132,32 @@
expect(page).to have_css(".add-comment form")
end

context "when user is not authorized to comment" do
let(:permissions) do
{
comment: {
authorization_handlers: {
"dummy_authorization_handler" => { "options" => {} }
}
}
}
end

before do
organization.available_authorizations = ["dummy_authorization_handler"]
organization.save!
commentable.create_resource_permission(permissions:)
allow(commentable).to receive(:user_allowed_to_comment?).with(user).and_return(false)
allow(commentable).to receive(:user_authorized_to_comment?).with(user).and_return(true)
end

it "shows a message indicating that comments are restricted" do
visit resource_path
expect(page).to have_no_content("Comments are disabled at this time")
expect(page).to have_content("You need to be verified to comment at this moment")
end
end

describe "when using emojis" do
before do
within_language_menu do
Expand Down Expand Up @@ -975,3 +1002,46 @@
end
end
end

shared_examples "comments blocked" do
context "when not authenticated" do
context "when comments are blocked" do
let(:active_step_id) { component.participatory_space.active_step.id }

before do
component.update!(step_settings: { active_step_id => { comments_blocked: true } })
end

it "shows a message indicating that comments are disabled" do
visit resource_path
expect(page).to have_content("Comments are disabled at this time")
expect(page).to have_no_content("You need to be verified to comment at this moment")
end
end
end

context "when authenticated" do
let!(:organization) { create(:organization) }
let!(:user) { create(:user, :confirmed, organization:) }
let!(:comments) { create_list(:comment, 3, commentable:) }

before do
login_as user, scope: :user
visit resource_path
end

context "when comments are blocked" do
let(:active_step_id) { component.participatory_space.active_step.id }

before do
component.update!(step_settings: { active_step_id => { comments_blocked: true } })
end

it "shows a message indicating that comments are disabled" do
visit resource_path
expect(page).to have_content("Comments are disabled at this time")
expect(page).to have_no_content("You need to be verified to comment at this moment")
end
end
end
end
7 changes: 7 additions & 0 deletions decidim-debates/spec/system/comments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@
let(:resource_path) { resource_locator(commentable).path }

include_examples "comments"

context "with comments blocked" do
let!(:component) { create(:debates_component, participatory_space:, organization:) }
let(:participatory_space) { create(:participatory_process, :with_steps, organization:) }

include_examples "comments blocked"
end
end
4 changes: 3 additions & 1 deletion decidim-dev/app/models/decidim/dev/dummy_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ def commentable?

# Public: Whether the object can have new comments or not.
def user_allowed_to_comment?(user)
component.can_participate_in_space?(user)
return unless component.can_participate_in_space?(user)

ActionAuthorizer.new(user, "comment", component, self).authorize.ok?
end

# Public: Whether the object can have new comment votes or not.
Expand Down
7 changes: 7 additions & 0 deletions decidim-meetings/spec/system/comments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@
let(:resource_path) { resource_locator(commentable).path }

include_examples "comments"

context "with comments blocked" do
let!(:component) { create(:component, manifest_name: :meetings, participatory_space:, organization:) }
let(:participatory_space) { create(:participatory_process, :with_steps, organization:) }

include_examples "comments blocked"
end
end
7 changes: 7 additions & 0 deletions decidim-proposals/spec/system/comments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@
end

include_examples "comments"

context "with comments blocked" do
let!(:component) { create(:proposal_component, participatory_space:, organization:) }
let(:participatory_space) { create(:participatory_process, :with_steps, organization:) }

include_examples "comments blocked"
end
end

0 comments on commit 838a313

Please sign in to comment.