Move exception tests to controller specs

System tests are used to test the application from the user's point of
view. To test for specific exceptions, particularly regarding
authorization permissions, controller tests fit better.

Another option would be to test the page displayed shows a certain text,
like "Internal server error". I'm choosing controller tests because
they're faster and we're basically testing the same scenario many times
and we've already got a test checking what happens when users access a
page raising an exception.
This commit is contained in:
Javi Martín
2021-03-30 13:28:49 +02:00
parent abd6a1971f
commit 02981324ab
53 changed files with 509 additions and 379 deletions

View File

@@ -0,0 +1,41 @@
require "rails_helper"
describe Admin::BudgetGroupsController, :admin do
describe "GET index" do
it "raises an exception when the feature is disabled" do
Setting["process.budgets"] = false
expect do
get :index, params: { budget_id: create(:budget).id }
end.to raise_exception(FeatureFlags::FeatureDisabled)
end
end
describe "GET edit" do
let(:group) { create(:budget_group) }
it "raises an error if budget slug is not found" do
expect do
get :edit, params: { budget_id: "wrong_budget", id: group.id }
end.to raise_error ActiveRecord::RecordNotFound
end
it "raises an error if budget id is not found" do
expect do
get :edit, params: { budget_id: 0, id: group.id }
end.to raise_error ActiveRecord::RecordNotFound
end
it "raises an error if group slug is not found" do
expect do
get :edit, params: { budget_id: group.budget.id, id: "wrong_group" }
end.to raise_error ActiveRecord::RecordNotFound
end
it "raises an error if group id is not found" do
expect do
get :edit, params: { budget_id: 0, id: "wrong_group" }
end.to raise_error ActiveRecord::RecordNotFound
end
end
end

View File

@@ -0,0 +1,54 @@
require "rails_helper"
describe Admin::BudgetHeadingsController, :admin do
context "processes disabled" do
it "raises feature disabled for budgets" do
Setting["process.budgets"] = nil
group = create(:budget_group)
expect do
get :index, params: { budget_id: group.budget.id, group_id: group.id }
end.to raise_exception(FeatureFlags::FeatureDisabled)
end
end
describe "GET edit" do
let(:heading) { create(:budget_heading) }
it "raises an error if budget slug is not found" do
expect do
get :edit, params: { budget_id: "wrong_budget", group_id: heading.group.id, id: heading.id }
end.to raise_error ActiveRecord::RecordNotFound
end
it "raises an error if budget id is not found" do
expect do
get :edit, params: { budget_id: 0, group_id: heading.group.id, id: heading.id }
end.to raise_error ActiveRecord::RecordNotFound
end
it "raises an error if group slug is not found" do
expect do
get :edit, params: { budget_id: heading.budget.id, group_id: "wrong group", id: heading.id }
end.to raise_error ActiveRecord::RecordNotFound
end
it "raises an error if group id is not found" do
expect do
get :edit, params: { budget_id: heading.budget.id, group_id: 0, id: heading.id }
end.to raise_error ActiveRecord::RecordNotFound
end
it "raises an error if heading slug is not found" do
expect do
get :edit, params: { budget_id: heading.budget.id, group_id: heading.group.id, id: "wrong heading" }
end.to raise_error ActiveRecord::RecordNotFound
end
it "raises an error if heading id is not found" do
expect do
get :edit, params: { budget_id: heading.budget.id, group_id: heading.group.id, id: 0 }
end.to raise_error ActiveRecord::RecordNotFound
end
end
end

View File

@@ -1,6 +1,20 @@
require "rails_helper"
describe Admin::BudgetInvestmentsController, :admin do
describe "GET index" do
it "raises an error if budget slug is not found" do
expect do
get :index, params: { budget_id: "wrong_budget" }
end.to raise_error ActiveRecord::RecordNotFound
end
it "raises an error if budget id is not found" do
expect do
get :index, params: { budget_id: 0 }
end.to raise_error ActiveRecord::RecordNotFound
end
end
describe "PATCH update" do
it "does not redirect on AJAX requests" do
investment = create(:budget_investment)

View File

@@ -0,0 +1,25 @@
require "rails_helper"
describe Admin::BudgetsController, :admin do
describe "GET index" do
it "raises an exception when the feature is disabled" do
Setting["process.budgets"] = false
expect { get :index }.to raise_exception(FeatureFlags::FeatureDisabled)
end
end
describe "GET edit" do
it "raises an error if budget slug is not found" do
expect do
get :edit, params: { id: "wrong_budget" }
end.to raise_error ActiveRecord::RecordNotFound
end
it "raises an error if budget id is not found" do
expect do
get :edit, params: { id: 0 }
end.to raise_error ActiveRecord::RecordNotFound
end
end
end

View File

@@ -0,0 +1,11 @@
require "rails_helper"
describe Admin::HiddenBudgetInvestmentsController, :admin do
describe "GET index" do
it "raises an exception when the feature is disabled" do
Setting["process.budgets"] = nil
expect { get :index }.to raise_exception(FeatureFlags::FeatureDisabled)
end
end
end

View File

@@ -1,6 +1,14 @@
require "rails_helper"
describe Admin::HiddenDebatesController, :admin do
describe "GET index" do
it "raises an exception when the feature is disabled" do
Setting["process.debates"] = false
expect { get :index }.to raise_exception(FeatureFlags::FeatureDisabled)
end
end
describe "PUT confirm_hide" do
it "keeps query parameters while using protected redirects" do
debate = create(:debate, :hidden)

View File

@@ -0,0 +1,11 @@
require "rails_helper"
describe Admin::HiddenProposalsController, :admin do
describe "GET index" do
it "raises an exception when the feature is disabled" do
Setting["process.proposals"] = false
expect { get :index }.to raise_exception(FeatureFlags::FeatureDisabled)
end
end
end

View File

@@ -0,0 +1,13 @@
require "rails_helper"
describe Admin::Legislation::DraftVersionsController, :admin do
describe "GET index" do
it "raises an exception when the feature is disabled" do
Setting["process.legislation"] = false
expect do
get :index, params: { process_id: create(:legislation_process).id }
end.to raise_exception(FeatureFlags::FeatureDisabled)
end
end
end

View File

@@ -0,0 +1,11 @@
require "rails_helper"
describe Admin::Legislation::ProcessesController, :admin do
describe "GET index" do
it "raises an exception when the feature is disabled" do
Setting["process.legislation"] = false
expect { get :index }.to raise_exception(FeatureFlags::FeatureDisabled)
end
end
end

View File

@@ -0,0 +1,13 @@
require "rails_helper"
describe Admin::Legislation::QuestionsController, :admin do
describe "GET index" do
it "raises an exception when the feature is disabled" do
Setting["process.legislation"] = nil
expect do
get :index, params: { process_id: create(:legislation_process).id }
end.to raise_exception(FeatureFlags::FeatureDisabled)
end
end
end

View File

@@ -0,0 +1,11 @@
require "rails_helper"
describe Admin::Poll::PollsController, :admin do
describe "GET index" do
it "raises an exception when the feature is disabled" do
Setting["process.polls"] = false
expect { get :index }.to raise_exception(FeatureFlags::FeatureDisabled)
end
end
end