Skip to content

Commit

Permalink
Simplify tests requiring admin login
Browse files Browse the repository at this point in the history
We were repeating the same code over and over (with a few variants) to
setup tests which require an administrator. We can use a tag and
simplify the code.
  • Loading branch information
javierm committed Dec 2, 2020
1 parent 64205ab commit 3da4ee0
Show file tree
Hide file tree
Showing 82 changed files with 110 additions and 468 deletions.
6 changes: 1 addition & 5 deletions spec/shared/system/documentable.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
shared_examples "documentable" do |documentable_factory_name, documentable_path, documentable_path_arguments|
let(:administrator) { create(:user) }
let(:user) { create(:user) }
let(:arguments) { {} }
let(:documentable) { create(documentable_factory_name, author: user) }
let!(:document) { create(:document, documentable: documentable, user: documentable.author) }

before do
create(:administrator, user: administrator)

documentable_path_arguments.each do |argument_name, path_to_value|
arguments.merge!("#{argument_name}": documentable.send(path_to_value))
end
Expand Down Expand Up @@ -46,8 +43,7 @@
expect(page).to have_link("Delete document")
end

scenario "Administrators cannot destroy documentables they have not authored" do
login_as(administrator)
scenario "Administrators cannot destroy documentables they have not authored", :admin do
visit send(documentable_path, arguments)

expect(page).not_to have_link("Delete document")
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
Bullet.end_request
end

config.before(:each, :admin) do
login_as(create(:administrator).user)
end

config.before(:each, :delay_jobs) do
Delayed::Worker.delay_jobs = true
end
Expand Down
7 changes: 2 additions & 5 deletions spec/system/admin/admin_notifications_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
require "rails_helper"

describe "Admin Notifications" do
before do
create(:budget)
login_as(create(:administrator).user)
end
describe "Admin Notifications", :admin do
before { create(:budget) }

context "Show" do
scenario "Valid Admin Notification" do
Expand Down
6 changes: 1 addition & 5 deletions spec/system/admin/banners_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
require "rails_helper"

describe "Admin banners magement" do
before do
login_as(create(:administrator).user)
end

describe "Admin banners magement", :admin do
context "Index" do
before do
create(:banner, title: "Banner number one",
Expand Down
7 changes: 1 addition & 6 deletions spec/system/admin/budget_groups_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
require "rails_helper"

describe "Admin budget groups" do
describe "Admin budget groups", :admin do
let(:budget) { create(:budget, :drafting) }

before do
admin = create(:administrator)
login_as(admin.user)
end

context "Feature flag" do
before do
Setting["process.budgets"] = nil
Expand Down
7 changes: 1 addition & 6 deletions spec/system/admin/budget_headings_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
require "rails_helper"

describe "Admin budget headings" do
describe "Admin budget headings", :admin do
let(:budget) { create(:budget, :drafting) }
let(:group) { create(:budget_group, budget: budget) }

before do
admin = create(:administrator)
login_as(admin.user)
end

context "Feature flag" do
before do
Setting["process.budgets"] = nil
Expand Down
6 changes: 1 addition & 5 deletions spec/system/admin/budget_investments_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

describe "Admin budget investments" do
describe "Admin budget investments", :admin do
let(:budget) { create(:budget) }
let(:administrator) do
create(:administrator, user: create(:user, username: "Ana", email: "[email protected]"))
Expand All @@ -10,10 +10,6 @@
:budget_investment,
"admin_polymorphic_path"

before do
login_as(create(:administrator).user)
end

context "Feature flag" do
before do
Setting["process.budgets"] = nil
Expand Down
7 changes: 1 addition & 6 deletions spec/system/admin/budget_phases_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
describe "Admin budget phases" do
let(:budget) { create(:budget) }

context "Edit" do
before do
admin = create(:administrator)
login_as(admin.user)
end

context "Edit", :admin do
scenario "Update phase", :js do
visit edit_admin_budget_budget_phase_path(budget, budget.current_phase)

Expand Down
7 changes: 1 addition & 6 deletions spec/system/admin/budgets_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
require "rails_helper"

describe "Admin budgets" do
before do
admin = create(:administrator)
login_as(admin.user)
end

describe "Admin budgets", :admin do
context "Feature flag" do
before do
Setting["process.budgets"] = nil
Expand Down
7 changes: 1 addition & 6 deletions spec/system/admin/comments_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
require "rails_helper"

describe "Admin comments" do
before do
admin = create(:administrator)
login_as(admin.user)
end

describe "Admin comments", :admin do
scenario "Index" do
create(:comment, body: "Everything is awesome")

Expand Down
8 changes: 1 addition & 7 deletions spec/system/admin/dashboard/actions_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
require "rails_helper"

describe "Admin dashboard actions" do
let(:admin) { create :administrator }

before do
login_as(admin.user)
end

describe "Admin dashboard actions", :admin do
it_behaves_like "nested documentable",
"administrator",
"dashboard_action",
Expand Down
8 changes: 1 addition & 7 deletions spec/system/admin/dashboard/administrator_tasks_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
require "rails_helper"

describe "Admin administrator tasks" do
let(:admin) { create :administrator }

before do
login_as(admin.user)
end

describe "Admin administrator tasks", :admin do
context "when visiting index" do
context "and no pending tasks" do
before do
Expand Down
7 changes: 1 addition & 6 deletions spec/system/admin/debates_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
require "rails_helper"

describe "Admin debates" do
before do
admin = create(:administrator)
login_as(admin.user)
end

describe "Admin debates", :admin do
it_behaves_like "flaggable", :debate, admin: true

scenario "Index" do
Expand Down
3 changes: 1 addition & 2 deletions spec/system/admin/emails/newsletters_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
require "rails_helper"

describe "Admin newsletter emails" do
describe "Admin newsletter emails", :admin do
before do
create(:budget)
login_as(create(:administrator).user)
end

context "Show" do
Expand Down
3 changes: 1 addition & 2 deletions spec/system/admin/feature_flags_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
require "rails_helper"

describe "Admin feature flags" do
describe "Admin feature flags", :admin do
before do
Setting["process.budgets"] = true
login_as(create(:administrator).user)
end

scenario "Enabled features are listed on menu" do
Expand Down
6 changes: 1 addition & 5 deletions spec/system/admin/geozones_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
require "rails_helper"

describe "Admin geozones" do
before do
login_as(create(:administrator).user)
end

describe "Admin geozones", :admin do
scenario "Show list of geozones" do
chamberi = create(:geozone, name: "Chamberí")
retiro = create(:geozone, name: "Retiro")
Expand Down
7 changes: 1 addition & 6 deletions spec/system/admin/hidden_budget_investments_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
require "rails_helper"

describe "Admin hidden budget investments" do
describe "Admin hidden budget investments", :admin do
let(:budget) { create(:budget) }
let(:heading) { create(:budget_heading, budget: budget, price: 666666) }

before do
admin = create(:administrator)
login_as(admin.user)
end

scenario "Disabled with a feature flag" do
Setting["process.budgets"] = nil

Expand Down
7 changes: 1 addition & 6 deletions spec/system/admin/hidden_comments_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
require "rails_helper"

describe "Admin hidden comments" do
before do
admin = create(:administrator)
login_as(admin.user)
end

describe "Admin hidden comments", :admin do
scenario "Do not show comments from blocked users" do
comment = create(:comment, :hidden, body: "SPAM from SPAMMER")
proposal = create(:proposal, author: comment.author)
Expand Down
7 changes: 1 addition & 6 deletions spec/system/admin/hidden_debates_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
require "rails_helper"

describe "Admin hidden debates" do
before do
admin = create(:administrator)
login_as(admin.user)
end

describe "Admin hidden debates", :admin do
scenario "Disabled with a feature flag" do
Setting["process.debates"] = nil

Expand Down
9 changes: 1 addition & 8 deletions spec/system/admin/hidden_proposals_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
require "rails_helper"

describe "Admin hidden proposals" do
before do
admin = create(:administrator)
login_as(admin.user)
end

describe "Admin hidden proposals", :admin do
scenario "Disabled with a feature flag" do
Setting["process.proposals"] = nil
admin = create(:administrator)
login_as(admin.user)

expect { visit admin_hidden_proposals_path }.to raise_exception(FeatureFlags::FeatureDisabled)
end
Expand Down
7 changes: 1 addition & 6 deletions spec/system/admin/hidden_users_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
require "rails_helper"

describe "Admin hidden users" do
before do
admin = create(:administrator)
login_as(admin.user)
end

describe "Admin hidden users", :admin do
scenario "Show user activity" do
user = create(:user, :hidden)

Expand Down
5 changes: 1 addition & 4 deletions spec/system/admin/homepage/homepage_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
require "rails_helper"

describe "Homepage" do
describe "Homepage", :admin do
before do
Setting["homepage.widgets.feeds.proposals"] = false
Setting["homepage.widgets.feeds.debates"] = false
Setting["homepage.widgets.feeds.processes"] = false
Setting["feature.user.recommendations"] = false

admin = create(:administrator).user
login_as(admin)
end

let!(:proposals_feed) { create(:widget_feed, kind: "proposals") }
Expand Down
7 changes: 1 addition & 6 deletions spec/system/admin/legislation/draft_versions_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
require "rails_helper"

describe "Admin legislation draft versions" do
before do
admin = create(:administrator)
login_as(admin.user)
end

describe "Admin legislation draft versions", :admin do
context "Feature flag" do
scenario "Disabled with a feature flag" do
Setting["process.legislation"] = nil
Expand Down
7 changes: 1 addition & 6 deletions spec/system/admin/legislation/processes_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
require "rails_helper"

describe "Admin collaborative legislation" do
before do
admin = create(:administrator)
login_as(admin.user)
end

describe "Admin collaborative legislation", :admin do
it_behaves_like "admin_milestoneable",
:legislation_process,
"admin_legislation_process_milestones_path"
Expand Down
7 changes: 1 addition & 6 deletions spec/system/admin/legislation/proposals_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
require "rails_helper"

describe "Admin collaborative legislation" do
before do
admin = create(:administrator)
login_as(admin.user)
end

describe "Admin collaborative legislation", :admin do
context "Index" do
scenario "Displaying legislation proposals" do
proposal = create(:legislation_proposal, cached_votes_score: 10)
Expand Down
7 changes: 1 addition & 6 deletions spec/system/admin/legislation/questions_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
require "rails_helper"

describe "Admin legislation questions" do
before do
admin = create(:administrator)
login_as(admin.user)
end

describe "Admin legislation questions", :admin do
let!(:process) { create(:legislation_process, title: "An example legislation process") }

context "Feature flag" do
Expand Down
7 changes: 1 addition & 6 deletions spec/system/admin/local_census_records/imports_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
require "rails_helper"

describe "Imports" do
describe "Imports", :admin do
let(:base_files_path) { %w[spec fixtures files local_census_records import] }

before do
admin = create(:administrator)
login_as(admin.user)
end

describe "New" do
scenario "Should show import form" do
visit new_admin_local_census_records_import_path
Expand Down
6 changes: 1 addition & 5 deletions spec/system/admin/local_census_records_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
require "rails_helper"

describe "Admin local census records" do
before do
login_as(create(:administrator).user)
end

describe "Admin local census records", :admin do
context "Index" do
let!(:local_census_record) { create(:local_census_record) }

Expand Down
Loading

0 comments on commit 3da4ee0

Please sign in to comment.