Skip to content

Commit

Permalink
Add new print permission in intiatives (decidim#13339)
Browse files Browse the repository at this point in the history
* Fix permissions in initiatives

* Fix specs

* Add specs
  • Loading branch information
alecslupu authored Oct 21, 2024
1 parent 55c7014 commit 606a187
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def update
end

def print
enforce_permission_to :read, :initiative, initiative: current_initiative
enforce_permission_to :print, :initiative, initiative: current_initiative
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ def initiative_admin_user_action?
return unless permission_action.subject == :initiative

case permission_action.action
when :read
toggle_allow(Decidim::Initiatives.print_enabled)
when :print
toggle_allow(Decidim::Initiatives.print_enabled && user.admin?)
when :publish, :discard
toggle_allow(initiative.validating?)
when :unpublish
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def permissions
create_initiative?
edit_public_initiative?
update_public_initiative?
print_initiative?

vote_initiative?
sign_initiative?
Expand Down Expand Up @@ -122,6 +123,17 @@ def access_request_membership?
)
end

def print_initiative?
return unless permission_action.action == :print &&
permission_action.subject == :initiative

toggle_allow(Decidim::Initiatives.print_enabled && (authorship_or_admin? || committee_member?))
end

def committee_member?
InitiativesPromoted.by(user).exists?(id: initiative.id)
end

def vote_initiative?
return unless permission_action.action == :vote &&
permission_action.subject == :initiative
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<%= icon "chat-1-line", scope: "decidim.admin", class: "action-icon action-icon--disabled", role: "img", "aria-hidden": true %>
<% end %>
<% if allowed_to? :read, :initiative, initiative: initiative %>
<% if allowed_to? :print, :initiative, initiative: initiative %>
<%= icon_link_to "printer-line",
decidim_initiatives.print_initiative_path(initiative),
t(".print"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,8 @@
context "when managing initiatives" do
let(:action_subject) { :initiative }

context "when reading" do
let(:action_name) { :read }
context "when printing" do
let(:action_name) { :print }

before do
allow(Decidim::Initiatives).to receive(:print_enabled).and_return(print_enabled)
Expand All @@ -404,6 +404,16 @@
end
end

context "when reading" do
let(:action_name) { :read }

context "when print is enabled" do
let(:print_enabled) { true }

it { is_expected.to be true }
end
end

it_behaves_like "checks initiative state", :publish, :validating, :open
it_behaves_like "checks initiative state", :unpublish, :open, :validating
it_behaves_like "checks initiative state", :discard, :validating, :open
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,43 @@
context "when managing an initiative" do
let(:action_subject) { :initiative }

context "when printing" do
let(:action_name) { :print }
let(:action) do
{ scope: :public, action: :print, subject: :initiative }
end
let(:context) do
{ initiative: }
end

before do
allow(Decidim::Initiatives).to receive(:print_enabled).and_return(true)
end

context "when user is a committee member" do
let(:initiative) { create(:initiative, :created, organization:) }

before do
create(:initiatives_committee_member, initiative:, user:)
end

it { is_expected.to be true }
end

context "when user is not an initiative author" do
let(:initiative) { create(:initiative, :created, organization:) }

it { is_expected.to be false }
end

context "when user is admin" do
let(:user) { create(:user, :admin, organization:) }
let(:initiative) { create(:initiative, :created, author: user, organization:) }

it { is_expected.to be true }
end
end

context "when editing" do
let(:action_name) { :edit }
let(:action) do
Expand Down
153 changes: 138 additions & 15 deletions decidim-initiatives/spec/system/admin/print_initiative_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,158 @@

describe "User prints the initiative" do
context "when initiative print" do
include_context "when admins initiative"
context "when user is unauthenticated" do
include_context "when admins initiative"

before do
allow(Decidim::Initiatives).to receive(:print_enabled).and_return(print_enabled)
before do
allow(Decidim::Initiatives).to receive(:print_enabled).and_return(print_enabled)
switch_to_host(organization.host)
visit decidim_initiatives.print_initiative_path(initiative)
end

context "when the setting is enabled" do
let(:print_enabled) { true }

switch_to_host(organization.host)
login_as user, scope: :user
visit decidim_admin_initiatives.initiatives_path
it "redirects to the login page" do
expect(page).to have_current_path("/users/sign_in")
expect(page).to have_content("You are not authorized to perform this action.")
end
end

context "when the setting is disabled" do
let(:print_enabled) { false }

it "does not show the print link" do
expect(page).to have_current_path("/users/sign_in")
end
end
end

context "when the setting is enabled" do
let(:print_enabled) { true }
context "when is regular user" do
include_context "when admins initiative"
let(:user) { create(:user, :confirmed, organization:) }

before do
allow(Decidim::Initiatives).to receive(:print_enabled).and_return(print_enabled)
switch_to_host(organization.host)
login_as user, scope: :user
visit decidim_initiatives.print_initiative_path(initiative)
end

context "when the setting is enabled" do
let(:print_enabled) { true }

it "redirects to the home page" do
expect(page).to have_current_path(decidim.root_path)
expect(page).to have_content("You are not authorized to perform this action.")
end
end

it "shows a printable form with all available data about the initiative" do
new_window = window_opened_by { page.find(".action-icon--print").click }
context "when the setting is disabled" do
let(:print_enabled) { false }

page.within_window(new_window) do
it "does not show the print link" do
expect(page).to have_current_path(decidim.root_path)
end
end
end

context "when user is the author" do
include_context "when admins initiative"
let(:user) { author }

before do
allow(Decidim::Initiatives).to receive(:print_enabled).and_return(print_enabled)

switch_to_host(organization.host)
login_as user, scope: :user
visit decidim_initiatives.print_initiative_path(initiative)
end

context "when the setting is enabled" do
let(:print_enabled) { true }

it "shows a printable form with all available data about the initiative" do
expect(page).to have_current_path(decidim_initiatives.print_initiative_path(initiative))
expect(page).to have_content(translated(initiative.title, locale: :en))
expect(page).to have_content(translated(initiative.type.title, locale: :en))
expect(page).to have_content(ActionView::Base.full_sanitizer.sanitize(translated(initiative.description, locale: :en), tags: []))
end
end

context "when the setting is disabled" do
let(:print_enabled) { false }

it "does not show the print link" do
expect(page).to have_current_path(decidim.root_path)
end
end
end

context "when the setting is disabled" do
let(:print_enabled) { false }
context "when user is the committee" do
include_context "when admins initiative"
let(:user) { create(:user, :confirmed, organization:) }
let!(:initiatives_committee_member) { create(:initiatives_committee_member, initiative:, user:) }

it "does not show the print link" do
expect(page).to have_no_css(".action-icon--print")
before do
allow(Decidim::Initiatives).to receive(:print_enabled).and_return(print_enabled)

switch_to_host(organization.host)
login_as user, scope: :user
visit decidim_initiatives.print_initiative_path(initiative)
end

context "when the setting is enabled" do
let(:print_enabled) { true }

it "shows a printable form with all available data about the initiative" do
expect(page).to have_current_path(decidim_initiatives.print_initiative_path(initiative))
expect(page).to have_content(translated(initiative.title, locale: :en))
expect(page).to have_content(translated(initiative.type.title, locale: :en))
expect(page).to have_content(ActionView::Base.full_sanitizer.sanitize(translated(initiative.description, locale: :en), tags: []))
end
end

context "when the setting is disabled" do
let(:print_enabled) { false }

it "does not show the print link" do
expect(page).to have_current_path(decidim.root_path)
end
end
end

context "when user is admin" do
include_context "when admins initiative"

before do
allow(Decidim::Initiatives).to receive(:print_enabled).and_return(print_enabled)

switch_to_host(organization.host)
login_as user, scope: :user
visit decidim_admin_initiatives.initiatives_path
end

context "when the setting is enabled" do
let(:print_enabled) { true }

it "shows a printable form with all available data about the initiative" do
new_window = window_opened_by { page.find(".action-icon--print").click }

page.within_window(new_window) do
expect(page).to have_content(translated(initiative.title, locale: :en))
expect(page).to have_content(translated(initiative.type.title, locale: :en))
expect(page).to have_content(ActionView::Base.full_sanitizer.sanitize(translated(initiative.description, locale: :en), tags: []))
end
end
end

context "when the setting is disabled" do
let(:print_enabled) { false }

it "does not show the print link" do
expect(page).to have_no_css(".action-icon--print")
end
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion decidim-initiatives/spec/system/edit_initiative_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
it "can be updated" do
visit initiative_path

click_on("Edit")
within ".initiative__aside" do
click_on("Edit")
end

expect(page).to have_content "Edit Initiative"

Expand Down

0 comments on commit 606a187

Please sign in to comment.