Skip to content

Commit

Permalink
CSV & JSON export function fix
Browse files Browse the repository at this point in the history
* CSV/JSON export function fix

* Added change to permissions.rb/initiatives to allow export

* export_controller_spec.rb added for elections/voting and Initiatives
  • Loading branch information
greenwoodt authored and alecslupu committed Jul 14, 2023
1 parent 5c8852c commit 686c9d6
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 1 deletion.
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Decidim
module Votings
module Admin
# This controller allows exporting things.
# It is targeted for customizations for exporting things that lives under
# a participatory process.
class ExportsController < Decidim::Admin::ExportsController
include VotingAdmin
end
end
end
end
Expand Up @@ -132,7 +132,7 @@ def allowed_voting_action?
when :ballot_styles
toggle_allow(user.admin?) if permission_action.action == :read
when :component_data
toggle_allow(user.admin?) if permission_action.action == :import
toggle_allow(user.admin?) if [:import, :export].member? permission_action.action
end
end

Expand Down
@@ -0,0 +1,65 @@
# frozen_string_literal: true

require "spec_helper"

module Decidim
module Votings
module Admin
describe ExportsController, type: :controller do
routes { Decidim::Votings::AdminEngine.routes }

let!(:organization) { create(:organization) }
let!(:voting) { create(:voting, organization: organization) }
let!(:user) { create(:user, :admin, :confirmed, organization: organization) }
let!(:component) { create(:component, participatory_space: voting, manifest_name: "dummy") }

let(:params) do
{
id: "dummies",
component_id: component.id,
voting_slug: voting.slug
}
end

before do
request.env["decidim.current_organization"] = organization
sign_in user, scope: :user
end

describe "POST create" do
context "when a format is provided" do
it "enqueues a job with the provided format" do
params[:format] = "csv"

expect(ExportJob).to receive(:perform_later)
.with(user, component, "dummies", "csv", nil)

post(:create, params: params)
end
end

context "when a format is not provided" do
it "enqueues a job with the default format" do
expect(ExportJob).to receive(:perform_later)
.with(user, component, "dummies", "json", nil)

post(:create, params: params)
end
end
end

it "traces the action", versioning: true do
expect(Decidim.traceability)
.to receive(:perform_action!)
.with("export_component", component, user, { name: "dummies", format: "json" })
.and_call_original

expect { post(:create, params: params) }.to change(Decidim::ActionLog, :count)
action_log = Decidim::ActionLog.last
expect(action_log.action).to eq("export_component")
expect(action_log.version).to be_present
end
end
end
end
end
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Decidim
module Initiatives
module Admin
# This controller allows exporting things.
# It is targeted for customizations for exporting things that lives under
# a participatory process.
class ExportsController < Decidim::Admin::ExportsController
include InitiativeAdmin
end
end
end
end
Expand Up @@ -163,6 +163,7 @@ def initiative_admin_user_action?

def initiative_export_action?
allow! if permission_action.subject == :initiatives && permission_action.action == :export
allow! if permission_action.action == :export && permission_action.subject == :component_data
end

def moderator_action?
Expand Down
@@ -0,0 +1,65 @@
# frozen_string_literal: true

require "spec_helper"

module Decidim
module Initiatives
module Admin
describe ExportsController, type: :controller do
routes { Decidim::Initiatives::AdminEngine.routes }

let!(:organization) { create(:organization) }
let!(:initiative) { create(:initiative, organization: organization) }
let!(:user) { create(:user, :admin, :confirmed, organization: organization) }
let!(:component) { create(:component, participatory_space: initiative, manifest_name: "dummy") }

let(:params) do
{
id: "dummies",
component_id: component.id,
initiative_slug: initiative.slug
}
end

before do
request.env["decidim.current_organization"] = organization
sign_in user, scope: :user
end

describe "POST create" do
context "when a format is provided" do
it "enqueues a job with the provided format" do
params[:format] = "csv"

expect(ExportJob).to receive(:perform_later)
.with(user, component, "dummies", "csv", nil)

post(:create, params: params)
end
end

context "when a format is not provided" do
it "enqueues a job with the default format" do
expect(ExportJob).to receive(:perform_later)
.with(user, component, "dummies", "json", nil)

post(:create, params: params)
end
end
end

it "traces the action", versioning: true do
expect(Decidim.traceability)
.to receive(:perform_action!)
.with("export_component", component, user, { name: "dummies", format: "json" })
.and_call_original

expect { post(:create, params: params) }.to change(Decidim::ActionLog, :count)
action_log = Decidim::ActionLog.last
expect(action_log.action).to eq("export_component")
expect(action_log.version).to be_present
end
end
end
end
end

0 comments on commit 686c9d6

Please sign in to comment.