Skip to content

Commit

Permalink
Rspec modernization
Browse files Browse the repository at this point in the history
Use devise helpers for login
Use new rails type:system for capybara tests
  • Loading branch information
jparr committed Apr 18, 2024
1 parent 739b27f commit 4911c0c
Show file tree
Hide file tree
Showing 19 changed files with 30 additions and 62 deletions.
8 changes: 2 additions & 6 deletions spec/controllers/action_page_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
require "rails_helper"

RSpec.describe ActionPageController, type: :controller do
include Devise::Test::ControllerHelpers

let(:action_page) { FactoryBot.create :action_page }
let(:admin) { login_as_admin }
let(:collaborator) { login_as_collaborator }

describe "GET #index" do
render_views
Expand Down Expand Up @@ -78,13 +74,13 @@
end

it "notifies admin users that a page is unpublished" do
admin
sign_in FactoryBot.create(:admin_user)
get :show, params: { id: unpublished_action_page }
expect(flash[:notice]).to include("not published")
end

it "notifies collaborator users that a page is unpublished" do
collaborator
sign_in FactoryBot.create(:collaborator_user)
get :show, params: { id: unpublished_action_page }
expect(flash[:notice]).to include("not published")
end
Expand Down
4 changes: 0 additions & 4 deletions spec/controllers/admin/institutions_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require "rails_helper"

RSpec.describe Admin::InstitutionsController, type: :controller do
include Devise::Test::ControllerHelpers

# This should return the minimal set of attributes required to create a valid
# Admin::Institution. As you add validations to Admin::InstitutionSet, be sure to
# adjust the attributes here as well.
Expand All @@ -11,8 +9,6 @@
end

before(:each) do
# Admin login
@request.env["devise.mapping"] = Devise.mappings[:admin]
sign_in FactoryBot.create(:admin_user)

# Set parent action
Expand Down
3 changes: 0 additions & 3 deletions spec/controllers/admin/petitions_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
require "rails_helper"

RSpec.describe Admin::PetitionsController, type: :controller do
include Devise::Test::ControllerHelpers

before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:admin]
sign_in FactoryBot.create(:admin_user)
end

Expand Down
4 changes: 1 addition & 3 deletions spec/controllers/admin/users_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
require "rails_helper"

RSpec.describe Admin::UsersController, type: :controller do
include Devise::Test::ControllerHelpers

before { login_as_admin }
before { sign_in FactoryBot.create(:admin_user) }

describe "GET #index" do
it "returns http success" do
Expand Down
2 changes: 1 addition & 1 deletion spec/features/action_pages/call_action_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

RSpec.feature "Call actions", type: :feature, js: true do
RSpec.describe "Call actions", type: :system, js: true do
let!(:action) do
FactoryBot.create(:call_campaign).action_page
end
Expand Down
2 changes: 1 addition & 1 deletion spec/features/action_pages/congress_action_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

RSpec.feature "Congress actions", type: :feature, js: true do
RSpec.describe "Congress actions", type: :system, js: true do
let!(:action) do
FactoryBot.create(:action_page_with_congress_message)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/features/action_pages/custom_email_action_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

RSpec.feature "Custom email actions", type: :feature, js: true do
RSpec.describe "Custom email actions", type: :system, js: true do
let!(:custom_action) do
FactoryBot.create(:email_campaign, :custom_email).action_page
end
Expand Down
2 changes: 1 addition & 1 deletion spec/features/action_pages/petition_action_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

RSpec.feature "Petition actions", type: :feature, js: true do
RSpec.describe "Petition actions", type: :system, js: true do
let!(:action) do
FactoryBot.create(:petition).action_page
end
Expand Down
2 changes: 1 addition & 1 deletion spec/features/action_pages/state_leg_email_action_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

RSpec.feature "State legislator email actions", type: :feature, js: true do
RSpec.describe "State legislator email actions", type: :system, js: true do
let!(:state_action) do
FactoryBot.create(:email_campaign, :state_leg).action_page
end
Expand Down
2 changes: 1 addition & 1 deletion spec/features/action_pages/tweet_action_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

RSpec.feature "Tweet actions", type: :feature, js: true do
RSpec.describe "Tweet actions", type: :system, js: true do
let!(:tweet_action) do
FactoryBot.create(:tweet, message: "Default message").action_page
end
Expand Down
2 changes: 1 addition & 1 deletion spec/features/congress_message.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

RSpec.feature "Submit congress message", type: :feature do
RSpec.describe "Submit congress message", type: :system do
let(:action_page) { FactoryBot.create(:action_page_with_congress_message, :with_partner) }

let(:partner) { action_page.partners.first }
Expand Down
27 changes: 13 additions & 14 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@

RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::IntegrationHelpers, type: :system
config.include Devise::Test::IntegrationHelpers, type: :request
config.include Warden::Test::Helpers, type: :request
config.include Warden::Test::Helpers, type: :feature
config.include Warden::Test::Helpers, type: :system

# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = Rails.root.join("spec/fixtures")
Expand Down Expand Up @@ -64,24 +66,21 @@
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!

config.before(:each, type: :feature) do
config.before(:each) do
# disable call tool by default; it will be stubbed for tests that need it
disable_call_tool
allow(CallTool).to receive(:enabled?).and_return(false)
end

config.before(:each, type: :system) do
driven_by :rack_test
end

config.before(:each, type: :system, js: true) do
driven_by :selenium_chrome_headless
end

FileUtils.mkdir_p(Rails.root.join("tmp/cache"))
config.before(:each) do
Rails.cache.clear
end
end

# for request tests
def login(user)
login_path = "/login"
post login_path, params: {
user: {
email: user.email,
password: "strong passwords defeat lobsters covering wealth"
}
}
end
2 changes: 1 addition & 1 deletion spec/requests/admin/action_pages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
describe "Admins" do
before(:each) do
@admin = FactoryBot.create(:admin_user)
login @admin
sign_in @admin
end

it "should allow them creating action pages with valid attributes" do
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/admin/events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe "Admin Action Page Analytics", type: :request do
let(:action_page) { FactoryBot.create(:action_page_with_views) }
before { login FactoryBot.create(:admin_user) }
before { sign_in FactoryBot.create(:admin_user) }

describe "#index" do
context "with type param" do
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/admin/petition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
describe "admins" do
before(:each) do
@admin = FactoryBot.create(:admin_user)
login @admin
sign_in @admin
end

it "should let admins download the CSV" do
Expand Down
9 changes: 3 additions & 6 deletions spec/requests/admin/s3_uploads_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,20 @@
end

it "should allow admins" do
@admin = FactoryBot.create(:admin_user)
login @admin
sign_in FactoryBot.create(:admin_user)

expect do
post "/admin/source_files", params: valid_attributes
end.to change { SourceFile.count }.by(1)
end

it "should have valid response" do
@admin = FactoryBot.create(:admin_user)
login @admin
sign_in FactoryBot.create(:admin_user)

post "/admin/source_files", params: valid_attributes

expect(response.parsed_body).to include(
"id" => 1,
"delete_url" => "/admin/source_files/1.json",
"delete_url" => a_string_matching(%r{/admin/source_files/[0-9]+.json}),
"full_url" => a_string_matching(%r{/uploads/3be325f2b4e64d9d92a89405577280a4/img.png}),
"image" => true,
"name" => "img.png",
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/admin/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe "Admin Users", type: :request do
before(:each) do
admin = FactoryBot.create(:admin_user)
login admin
sign_in admin
end

describe "#index" do
Expand Down
11 changes: 0 additions & 11 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,6 @@
config.timestamp_enabled = false
end

# for controller tests
def login_as_admin
@request.env["devise.mapping"] = Devise.mappings[:admin]
sign_in FactoryBot.create(:admin_user)
end

def login_as_collaborator
@request.env["devise.mapping"] = Devise.mappings[:admin]
sign_in FactoryBot.create(:collaborator_user)
end

def set_weak_password(user)
weak_password = "12345678"
user.password = weak_password
Expand Down
4 changes: 0 additions & 4 deletions spec/support/feature_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ def sign_out_user(_user)
find("input[value='Logout']", visible: :all, match: :first).click
end

def disable_call_tool
allow(CallTool).to receive(:enabled?).and_return(false)
end

def fill_in_editor(locator, with:)
within_frame find(locator, visible: :all).sibling("div").find("iframe") do
within_frame find("#epiceditor-editor-frame") do
Expand Down

0 comments on commit 4911c0c

Please sign in to comment.