Skip to content

Commit

Permalink
Merge branch 'release/0.28-stable' of github.com:AjuntamentdeBarcelon…
Browse files Browse the repository at this point in the history
…a/decidim into release/0.28-stable-bcn
  • Loading branch information
fblupi committed Oct 31, 2024
2 parents 6e50c98 + 261ca28 commit bd07656
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 32 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 @@ -134,8 +134,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 @@ -121,6 +122,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 @@ -55,16 +55,16 @@
<%= 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 %>
<%= icon_link_to "printer-line",
decidim_initiatives.print_initiative_path(initiative),
t(".print"),
class: "action-icon--print",
target: "_blank",
data: { "external-link": false } %>
<% else %>
<span class="action-space icon"></span>
<% end %>
<% if allowed_to? :print, :initiative, initiative: initiative %>
<%= icon_link_to "printer-line",
decidim_initiatives.print_initiative_path(initiative),
t(".print"),
class: "action-icon--print",
target: "_blank",
data: { "external-link": false } %>
<% else %>
<span class="action-space icon"></span>
<% end %>

<% if allowed_to? :preview, :initiative, initiative: initiative %>
<%= icon_link_to "eye-line",
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, :published
it_behaves_like "checks initiative state", :unpublish, :published, :validating
it_behaves_like "checks initiative state", :discard, :validating, :published
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).not_to have_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).not_to have_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_link("Edit", href: edit_initiative_path)
within ".initiative__aside" do
click_on("Edit")
end

expect(page).to have_content "Edit Initiative"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ extra_admin_link(
<% end %>

<% if component_settings.geocoding_enabled? && @proposal.geocoded? %>
<div class="static-map__container">
<div class="static-map__container py-6">
<%= render partial: "decidim/shared/static_map", locals: { icon_name: "proposals", geolocalizable: @proposal } %>
</div>
<% end %>
Expand Down

0 comments on commit bd07656

Please sign in to comment.